<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: arham ahmed</title>
    <description>The latest articles on DEV Community by arham ahmed (@arham_ahmed_63699c0d1def9).</description>
    <link>https://dev.to/arham_ahmed_63699c0d1def9</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4099350%2F2a75a4c7-9ea8-45be-a576-2bc8b988d0a2.png</url>
      <title>DEV Community: arham ahmed</title>
      <link>https://dev.to/arham_ahmed_63699c0d1def9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arham_ahmed_63699c0d1def9"/>
    <language>en</language>
    <item>
      <title>Transformers: Understanding the Architecture Behind Modern AI</title>
      <dc:creator>arham ahmed</dc:creator>
      <pubDate>Fri, 28 Aug 2026 19:32:46 +0000</pubDate>
      <link>https://dev.to/arham_ahmed_63699c0d1def9/transformers-understanding-the-architecture-behind-modern-ai-537d</link>
      <guid>https://dev.to/arham_ahmed_63699c0d1def9/transformers-understanding-the-architecture-behind-modern-ai-537d</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Transformers are the heart of modern AI models. AI has seen a lot of breakthrough advancements from ChatGPT to AI, and now. After the development of Transformers, translations and question-answering tasks have seen a breakthrough.&lt;/p&gt;

&lt;p&gt;In this blog, I will try to explain Transformers from the basic concepts to how the complete architecture works.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Transformers?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's not that Transformers were the first architecture to exist. Various architectures existed before, like RNNs, which take the current input and also the previous input.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;I am a boy&lt;/p&gt;

&lt;p&gt;t₁ = I&lt;br&gt;
t₂ = I + am&lt;br&gt;
t₃ = I + am + a&lt;br&gt;
t₄ = I + am + a + boy&lt;/p&gt;

&lt;p&gt;But its popularity faded for long sequences as the effect of earlier inputs starts disappearing.&lt;/p&gt;

&lt;p&gt;Then the attention mechanism came, which focused not only on previous inputs but also on the relevant inputs to focus on. But their main limitation was that sentences with many words were not handled as effectively.&lt;/p&gt;

&lt;p&gt;To overcome the above limitations, Transformers using multi-head attention came into the picture.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Transformer: Encoder and Decoder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A Transformer is like a human that first encodes a sentence, understands it, and then decodes it according to the required task.&lt;/p&gt;

&lt;p&gt;The Transformer has two major parts:&lt;/p&gt;

&lt;p&gt;TRANSFORMER&lt;br&gt;
                /           \&lt;br&gt;
               /             \&lt;br&gt;
          ENCODER           DECODER&lt;br&gt;
             ↓                 ↓&lt;br&gt;
       Understands         Generates&lt;br&gt;
        the input           output&lt;/p&gt;

&lt;p&gt;The original Transformer architecture contains multiple encoder and decoder layers.&lt;/p&gt;

&lt;p&gt;The original Transformer uses 6 encoder layers and 6 decoder layers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Tokenization and Embeddings&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before reaching the main architecture, there are some prerequisites.&lt;/p&gt;

&lt;p&gt;First, the entire sentence is divided into tokens and then converted into embeddings.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;I am a boy&lt;br&gt;
      ↓&lt;br&gt;
[I, am, a, boy]&lt;br&gt;
      ↓&lt;br&gt;
Token IDs&lt;br&gt;
      ↓&lt;br&gt;
Embedding vectors&lt;/p&gt;

&lt;p&gt;Each single token is a vector of a particular dimension, for example (768, 1024) depending on the model.&lt;/p&gt;

&lt;p&gt;Think of it like what a particular word means in mathematics in a coordinate system.&lt;/p&gt;

&lt;p&gt;So instead of giving the Transformer raw words, we convert every word/token into a numerical vector.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Positional Encoding&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since we are working with long sequences, the position of each token is very important.&lt;/p&gt;

&lt;p&gt;Which token comes first or second can completely change the meaning of a sentence.&lt;/p&gt;

&lt;p&gt;Therefore, positional encodings are used to provide information about the position of each token.&lt;/p&gt;

&lt;p&gt;How do they work?&lt;/p&gt;

&lt;p&gt;The original Transformer uses sine and cosine functions.&lt;/p&gt;

&lt;p&gt;PE(pos, 2i) = sin(pos / 10000^(2i/d_model))&lt;/p&gt;

&lt;p&gt;PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))&lt;/p&gt;

&lt;p&gt;For example, let's take:&lt;/p&gt;

&lt;p&gt;Sentence = "I love AI"&lt;/p&gt;

&lt;p&gt;d_model = 4&lt;/p&gt;

&lt;p&gt;For position 0:&lt;/p&gt;

&lt;p&gt;PE(0) = [0, 1, 0, 1]&lt;/p&gt;

&lt;p&gt;For position 1:&lt;/p&gt;

&lt;p&gt;PE(1) ≈ [0.8415, 0.5403, 0.0100, 0.99995]&lt;/p&gt;

&lt;p&gt;For position 2:&lt;/p&gt;

&lt;p&gt;PE(2) ≈ [0.9093, -0.4161, 0.0200, 0.9998]&lt;/p&gt;

&lt;p&gt;Now suppose the embedding of "love" is:&lt;/p&gt;

&lt;p&gt;Embedding(love) = [0.4, 0.3, 0.8, 0.2]&lt;/p&gt;

&lt;p&gt;Since "love" is at position 1:&lt;/p&gt;

&lt;p&gt;Embedding = [0.4000, 0.3000, 0.8000, 0.2000]&lt;/p&gt;

&lt;p&gt;PE(1)     = [0.8415, 0.5403, 0.0100, 0.99995]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            ↓ ADD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Transformer input = [1.2415, 0.8403, 0.8100, 1.19995]&lt;/p&gt;

&lt;p&gt;So positional encoding basically tells the model:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is the token + Where is the token?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Self-Attention&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now comes the most important part: Attention.&lt;/p&gt;

&lt;p&gt;Consider the sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I am a boy&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each token is projected into the coordinate system as a vector.&lt;/p&gt;

&lt;p&gt;It is broken down into three parts by transformations:&lt;/p&gt;

&lt;p&gt;Query (Q)&lt;/p&gt;

&lt;p&gt;Key (K)&lt;/p&gt;

&lt;p&gt;Value (V)&lt;/p&gt;

&lt;p&gt;Think of them like this:&lt;/p&gt;

&lt;p&gt;Query&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What am I looking for?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Key&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"See, this is how I look in the coordinate system. Check how much I match with your Query."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Value&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This is what my value in the system contains."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What happens is that the Query vector for each word is multiplied with the Key vectors for each word using a dot product.&lt;/p&gt;

&lt;p&gt;The result is divided by √dₖ because for large dimensions the dot product can become large, pushing the probability toward one side.&lt;/p&gt;

&lt;p&gt;Then Softmax is applied.&lt;/p&gt;

&lt;p&gt;Attention(Q,K,V) = softmax(QKᵀ / √dₖ)V&lt;/p&gt;

&lt;p&gt;The Softmax gives the attention weights.&lt;/p&gt;

&lt;p&gt;Then these weights are multiplied with the corresponding Value vectors.&lt;/p&gt;

&lt;p&gt;This process is repeated multiple times to capture semantic similarity between tokens. This is known as multi-head attention.&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;Numerical Example of Self-Attention&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's actually calculate a small example.&lt;/p&gt;

&lt;p&gt;Consider:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I am a boy&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For simplicity, let:&lt;/p&gt;

&lt;p&gt;dₖ = 2&lt;/p&gt;

&lt;p&gt;Suppose the vectors are:&lt;/p&gt;

&lt;p&gt;Token   Q   K   V&lt;/p&gt;

&lt;p&gt;I   [1,0]   [1,1]   [1,0]&lt;br&gt;
am  [0,1]   [1,0]   [0,1]&lt;br&gt;
a   [1,1]   [1,1]   [1,1]&lt;br&gt;
boy [1,-1]  [0,1]   [0,2]&lt;/p&gt;

&lt;p&gt;Let's calculate attention for the last token "boy".&lt;/p&gt;

&lt;p&gt;Step 1: Calculate Q × Kᵀ&lt;/p&gt;

&lt;p&gt;For "boy":&lt;/p&gt;

&lt;p&gt;Q(boy) = [1,-1]&lt;/p&gt;

&lt;p&gt;Against every Key:&lt;/p&gt;

&lt;p&gt;[1,-1] · [1,1] = 0&lt;/p&gt;

&lt;p&gt;[1,-1] · [1,0] = 1&lt;/p&gt;

&lt;p&gt;[1,-1] · [1,1] = 0&lt;/p&gt;

&lt;p&gt;[1,-1] · [0,1] = -1&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;p&gt;QKᵀ = [0, 1, 0, -1]&lt;/p&gt;

&lt;p&gt;Step 2: Scale by √dₖ&lt;/p&gt;

&lt;p&gt;Since:&lt;/p&gt;

&lt;p&gt;dₖ = 2&lt;br&gt;
√dₖ = √2 ≈ 1.414&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;[0, 1, 0, -1] / 1.414&lt;/p&gt;

&lt;p&gt;≈ [0, 0.707, 0, -0.707]&lt;/p&gt;

&lt;p&gt;Step 3: Apply Softmax&lt;/p&gt;

&lt;p&gt;Softmax([0, 0.707, 0, -0.707])&lt;/p&gt;

&lt;p&gt;≈ [0.157, 0.256, 0.157, 0.430]&lt;/p&gt;

&lt;p&gt;These are the attention weights.&lt;/p&gt;

&lt;p&gt;So "boy" pays different amounts of attention to each token.&lt;/p&gt;

&lt;p&gt;Step 4: Multiply by V&lt;/p&gt;

&lt;p&gt;0.157 × [1,0]&lt;br&gt;
+&lt;br&gt;
0.256 × [0,1]&lt;br&gt;
+&lt;br&gt;
0.157 × [1,1]&lt;br&gt;
+&lt;br&gt;
0.430 × [0,2]&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;≈ [0.314, 1.017]&lt;/p&gt;

&lt;p&gt;So the output representation for "boy" becomes approximately:&lt;/p&gt;

&lt;p&gt;[0.314, 1.017]&lt;/p&gt;

&lt;p&gt;The important idea is not the particular numbers, but the process:&lt;/p&gt;

&lt;p&gt;Q&lt;br&gt;
↓&lt;br&gt;
QKᵀ&lt;br&gt;
↓&lt;br&gt;
Scale by √dₖ&lt;br&gt;
↓&lt;br&gt;
Softmax&lt;br&gt;
↓&lt;br&gt;
Attention weights&lt;br&gt;
↓&lt;br&gt;
Weighted sum of V&lt;br&gt;
↓&lt;br&gt;
Contextual representation&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Multi-Head Attention&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of one attention operation, we run multiple attention heads in parallel.&lt;/p&gt;

&lt;p&gt;Each head can learn different types of relationships.&lt;/p&gt;

&lt;p&gt;For example, one head may focus on:&lt;/p&gt;

&lt;p&gt;subject ↔ verb&lt;/p&gt;

&lt;p&gt;while another may focus on:&lt;/p&gt;

&lt;p&gt;verb ↔ object&lt;/p&gt;

&lt;p&gt;The outputs of all heads are then concatenated and passed through a linear transformation.&lt;/p&gt;

&lt;p&gt;Input&lt;br&gt;
                    │&lt;br&gt;
        ┌───────────┼───────────┐&lt;br&gt;
        ↓           ↓           ↓&lt;br&gt;
      Head 1      Head 2      Head 3 ... Head h&lt;br&gt;
        ↓           ↓           ↓&lt;br&gt;
        └───────────┼───────────┘&lt;br&gt;
                    ↓&lt;br&gt;
               Concatenate&lt;br&gt;
                    ↓&lt;br&gt;
              Linear Layer&lt;br&gt;
                    ↓&lt;br&gt;
                 Output&lt;/p&gt;

&lt;p&gt;The equation is:&lt;/p&gt;

&lt;p&gt;headᵢ = Attention(QWᵢQ, KWᵢK, VWᵢV)&lt;/p&gt;

&lt;p&gt;MultiHead(Q,K,V) =&lt;br&gt;
Concat(head₁, head₂, ..., headₕ)Wᴼ&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Residual Connection and Layer Normalization&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the attention operation, the output is combined with the original input using a residual connection.&lt;/p&gt;

&lt;p&gt;Then layer normalization is applied.&lt;/p&gt;

&lt;p&gt;The basic idea is:&lt;/p&gt;

&lt;p&gt;Input&lt;br&gt;
  ↓&lt;br&gt;
Attention&lt;br&gt;
  ↓&lt;br&gt;
Add original input&lt;br&gt;
  ↓&lt;br&gt;
Layer Normalization&lt;br&gt;
  ↓&lt;br&gt;
Output&lt;/p&gt;

&lt;p&gt;Residual connections help preserve information and allow gradients to flow through deeper networks.&lt;/p&gt;

&lt;p&gt;Layer normalization helps stabilize the network during training.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Feed-Forward Neural Network&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Further, a Feed-Forward Neural Network (FFN) is applied to each token.&lt;/p&gt;

&lt;p&gt;The equation is:&lt;/p&gt;

&lt;p&gt;FFN(x) = max(0, xW₁ + b₁)W₂ + b₂&lt;/p&gt;

&lt;p&gt;The FFN is applied independently to every token.&lt;/p&gt;

&lt;p&gt;Its purpose is to further transform the information received from the self-attention layer and introduce non-linearity.&lt;/p&gt;

&lt;p&gt;So a simple way to remember it is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Attention: Which tokens should interact?&lt;/p&gt;

&lt;p&gt;FFN: What transformation should be applied to the resulting representation?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After the FFN, residual connection and layer normalization are applied again.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Complete Encoder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The above entire process forms one encoder layer.&lt;/p&gt;

&lt;p&gt;Input Embedding&lt;br&gt;
      +&lt;br&gt;
Positional Encoding&lt;br&gt;
      ↓&lt;br&gt;
Multi-Head Self-Attention&lt;br&gt;
      ↓&lt;br&gt;
Add &amp;amp; Layer Normalization&lt;br&gt;
      ↓&lt;br&gt;
Feed-Forward Network&lt;br&gt;
      ↓&lt;br&gt;
Add &amp;amp; Layer Normalization&lt;br&gt;
      ↓&lt;br&gt;
Encoder Output&lt;/p&gt;

&lt;p&gt;The original Transformer has 6 such encoder layers stacked together.&lt;/p&gt;

&lt;p&gt;Encoder Layer 1&lt;br&gt;
      ↓&lt;br&gt;
Encoder Layer 2&lt;br&gt;
      ↓&lt;br&gt;
Encoder Layer 3&lt;br&gt;
      ↓&lt;br&gt;
Encoder Layer 4&lt;br&gt;
      ↓&lt;br&gt;
Encoder Layer 5&lt;br&gt;
      ↓&lt;br&gt;
Encoder Layer 6&lt;br&gt;
      ↓&lt;br&gt;
Final Encoder Output&lt;/p&gt;

&lt;p&gt;Each layer builds a richer contextual representation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decoder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the encoder finishes, the output embedding is passed to the decoder.&lt;/p&gt;

&lt;p&gt;After applying positional embeddings, the decoder undergoes masked multi-head attention.&lt;/p&gt;

&lt;p&gt;Unlike the encoder, the decoder takes the output token by token at a time.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;



&lt;p&gt;At the first step, the decoder predicts the first output token.&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt; → I&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt; I → am&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt; I am → a&lt;/p&gt;

&lt;p&gt;and so on.&lt;/p&gt;

&lt;p&gt;This is called autoregressive generation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Masked Attention?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The decoder must not see future tokens while generating the current token.&lt;/p&gt;

&lt;p&gt;For example, if we are predicting:&lt;/p&gt;

&lt;p&gt;I am a boy&lt;/p&gt;

&lt;p&gt;while predicting "a", the decoder should not already know "boy".&lt;/p&gt;

&lt;p&gt;Therefore, a mask is applied.&lt;/p&gt;

&lt;p&gt;I   am   a   boy&lt;/p&gt;

&lt;p&gt;I      ✓&lt;br&gt;
am     ✓    ✓&lt;br&gt;
a      ✓    ✓    ✓&lt;br&gt;
boy    ✓    ✓    ✓    ✓&lt;/p&gt;

&lt;p&gt;This is called causal/masked self-attention.&lt;/p&gt;

&lt;p&gt;The target sequence is also right-shifted, meaning the decoder receives previously generated/known tokens to predict the next token.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Cross-Attention: How Encoder Output Reaches the Decoder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is an important part of the Transformer.&lt;/p&gt;

&lt;p&gt;The final output of Encoder 6 is used by the cross-attention sublayer of the decoder layers.&lt;/p&gt;

&lt;p&gt;In cross-attention:&lt;/p&gt;

&lt;p&gt;Query (Q) → comes from decoder&lt;/p&gt;

&lt;p&gt;Key (K)   → comes from final encoder output&lt;/p&gt;

&lt;p&gt;Value (V) → comes from final encoder output&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;p&gt;Encoder&lt;br&gt;
                    ↓&lt;br&gt;
              Encoder Layer 6&lt;br&gt;
                    ↓&lt;br&gt;
          Final Encoder Output&lt;br&gt;
                    │&lt;br&gt;
        ┌───────────┼───────────┐&lt;br&gt;
        ↓           ↓           ↓&lt;br&gt;
      Decoder 1   Decoder 2   ... Decoder 6&lt;br&gt;
      Cross-Attn  Cross-Attn      Cross-Attn&lt;/p&gt;

&lt;p&gt;The decoder's own output flows from one decoder layer to the next, while the encoder's final representation is available to the cross-attention of every decoder layer.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decoder Layer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A decoder layer therefore contains:&lt;/p&gt;

&lt;p&gt;Target Embedding&lt;br&gt;
      +&lt;br&gt;
Positional Encoding&lt;br&gt;
      ↓&lt;br&gt;
Masked Multi-Head Self-Attention&lt;br&gt;
      ↓&lt;br&gt;
Add &amp;amp; LayerNorm&lt;br&gt;
      ↓&lt;br&gt;
Multi-Head Cross-Attention&lt;br&gt;
      ↑&lt;br&gt;
Final Encoder Output&lt;br&gt;
      ↓&lt;br&gt;
Add &amp;amp; LayerNorm&lt;br&gt;
      ↓&lt;br&gt;
Feed-Forward Network&lt;br&gt;
      ↓&lt;br&gt;
Add &amp;amp; LayerNorm&lt;br&gt;
      ↓&lt;br&gt;
Decoder Output&lt;/p&gt;

&lt;p&gt;The original Transformer contains 6 decoder layers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Final Output&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After passing through the decoder layers, a linear layer is applied.&lt;/p&gt;

&lt;p&gt;Then Softmax converts the final values into probabilities.&lt;/p&gt;

&lt;p&gt;Decoder Output&lt;br&gt;
      ↓&lt;br&gt;
Linear Layer&lt;br&gt;
      ↓&lt;br&gt;
Softmax&lt;br&gt;
      ↓&lt;br&gt;
Probability of each token&lt;br&gt;
      ↓&lt;br&gt;
Most suitable next token&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;I am a ______&lt;/p&gt;

&lt;p&gt;boy      → 0.72&lt;br&gt;
girl     → 0.12&lt;br&gt;
student  → 0.08&lt;br&gt;
doctor   → 0.03&lt;br&gt;
...&lt;/p&gt;

&lt;p&gt;The model selects a token according to the generation strategy being used.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Complete Transformer Workflow&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is the complete flow of the original encoder-decoder Transformer:&lt;/p&gt;

&lt;p&gt;INPUT SENTENCE&lt;br&gt;
                          ↓&lt;br&gt;
                     TOKENIZATION&lt;br&gt;
                          ↓&lt;br&gt;
                       EMBEDDING&lt;br&gt;
                          ↓&lt;br&gt;
                 POSITIONAL ENCODING&lt;br&gt;
                          ↓&lt;br&gt;
              ┌──────────────────────┐&lt;br&gt;
              │      ENCODER × 6     │&lt;br&gt;
              │                      │&lt;br&gt;
              │ Multi-Head Attention │&lt;br&gt;
              │          ↓           │&lt;br&gt;
              │     Add &amp;amp; Norm       │&lt;br&gt;
              │          ↓           │&lt;br&gt;
              │        FFN           │&lt;br&gt;
              │          ↓           │&lt;br&gt;
              │     Add &amp;amp; Norm       │&lt;br&gt;
              └──────────┬───────────┘&lt;br&gt;
                         ↓&lt;br&gt;
               FINAL ENCODER OUTPUT&lt;br&gt;
                         │&lt;br&gt;
                         │&lt;br&gt;
                         ↓&lt;br&gt;
       TARGET → Embedding + Positional Encoding&lt;br&gt;
                         ↓&lt;br&gt;
              ┌──────────────────────┐&lt;br&gt;
              │      DECODER × 6     │&lt;br&gt;
              │                      │&lt;br&gt;
              │ Masked Self-Attention│&lt;br&gt;
              │          ↓           │&lt;br&gt;
              │     Add &amp;amp; Norm       │&lt;br&gt;
              │          ↓           │&lt;br&gt;
              │    Cross-Attention  ←┘&lt;br&gt;
              │          ↓&lt;br&gt;
              │     Add &amp;amp; Norm&lt;br&gt;
              │          ↓&lt;br&gt;
              │        FFN&lt;br&gt;
              │          ↓&lt;br&gt;
              │     Add &amp;amp; Norm&lt;br&gt;
              └──────────┬───────────┘&lt;br&gt;
                         ↓&lt;br&gt;
                    Linear Layer&lt;br&gt;
                         ↓&lt;br&gt;
                      Softmax&lt;br&gt;
                         ↓&lt;br&gt;
                  OUTPUT TOKEN&lt;br&gt;
                         ↓&lt;br&gt;
                 NEXT TOKEN ...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Putting the Whole Idea Together&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Transformer can now be understood as a sequence of operations:&lt;/p&gt;

&lt;p&gt;Words&lt;br&gt;
 ↓&lt;br&gt;
Tokens&lt;br&gt;
 ↓&lt;br&gt;
Embeddings&lt;br&gt;
 ↓&lt;br&gt;
Positional Information&lt;br&gt;
 ↓&lt;br&gt;
Self-Attention&lt;br&gt;
 ↓&lt;br&gt;
Multi-Head Attention&lt;br&gt;
 ↓&lt;br&gt;
Feed-Forward Network&lt;br&gt;
 ↓&lt;br&gt;
Repeat through encoder layers&lt;br&gt;
 ↓&lt;br&gt;
Final Encoder Representation&lt;br&gt;
 ↓&lt;br&gt;
Decoder&lt;br&gt;
 ↓&lt;br&gt;
Masked Self-Attention&lt;br&gt;
 ↓&lt;br&gt;
Cross-Attention with Encoder Output&lt;br&gt;
 ↓&lt;br&gt;
Feed-Forward Network&lt;br&gt;
 ↓&lt;br&gt;
Repeat through decoder layers&lt;br&gt;
 ↓&lt;br&gt;
Linear + Softmax&lt;br&gt;
 ↓&lt;br&gt;
Output&lt;/p&gt;

&lt;p&gt;The most important idea is that attention allows the model to decide which other tokens are relevant when building the representation of a particular token.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Applications&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Transformers have become extremely important in modern AI.&lt;/p&gt;

&lt;p&gt;Some applications include:&lt;/p&gt;

&lt;p&gt;Machine translation&lt;/p&gt;

&lt;p&gt;Text generation&lt;/p&gt;

&lt;p&gt;Question answering&lt;/p&gt;

&lt;p&gt;Text summarization&lt;/p&gt;

&lt;p&gt;Sentiment analysis&lt;/p&gt;

&lt;p&gt;Code generation&lt;/p&gt;

&lt;p&gt;Image understanding&lt;/p&gt;

&lt;p&gt;Multimodal AI&lt;/p&gt;

&lt;p&gt;Models such as BERT, GPT and Vision Transformers are based on the Transformer idea, although their architectures can differ from the original encoder-decoder Transformer.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Transformers changed the way we process sequential data by introducing attention as the central mechanism for understanding relationships between tokens.&lt;/p&gt;

&lt;p&gt;Instead of processing information strictly one token after another like traditional recurrent architectures, Transformers can use attention to determine which tokens are important to each other.&lt;/p&gt;

&lt;p&gt;The major components are:&lt;/p&gt;

&lt;p&gt;Tokenization&lt;br&gt;
     ↓&lt;br&gt;
Embeddings&lt;br&gt;
     ↓&lt;br&gt;
Positional Encoding&lt;br&gt;
     ↓&lt;br&gt;
Self-Attention&lt;br&gt;
     ↓&lt;br&gt;
Multi-Head Attention&lt;br&gt;
     ↓&lt;br&gt;
Feed-Forward Network&lt;br&gt;
     ↓&lt;br&gt;
Residual Connections + Layer Normalization&lt;br&gt;
     ↓&lt;br&gt;
Encoder / Decoder&lt;br&gt;
     ↓&lt;br&gt;
Cross-Attention&lt;br&gt;
     ↓&lt;br&gt;
Linear + Softmax&lt;br&gt;
     ↓&lt;br&gt;
Output&lt;/p&gt;

&lt;p&gt;Understanding these components makes it much easier to understand modern architectures such as BERT, GPT and other Transformer-based models.&lt;/p&gt;

&lt;p&gt;This is how Transformers work — from converting words into vectors to using attention to understand relationships and finally generating an output.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>deeplearning</category>
      <category>machinelearning</category>
      <category>nlp</category>
    </item>
    <item>
      <title>XGBoost Explained: From Gradient Boosting to Weighted Quantile Sketch</title>
      <dc:creator>arham ahmed</dc:creator>
      <pubDate>Fri, 28 Aug 2026 18:01:00 +0000</pubDate>
      <link>https://dev.to/arham_ahmed_63699c0d1def9/xgboost-explained-from-gradient-boosting-to-weighted-quantile-sketch-145d</link>
      <guid>https://dev.to/arham_ahmed_63699c0d1def9/xgboost-explained-from-gradient-boosting-to-weighted-quantile-sketch-145d</guid>
      <description>&lt;h1&gt;
  
  
  XGBoost
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction: XGBoost
&lt;/h2&gt;

&lt;p&gt;XGBoost is a tree-based algorithm. It is a variant of Gradient Boosting which is one of the highest-scaled versions of it. It performs calculations 10x faster than Gradient Boosting. Its success is also witnessed in Kaggle's competitions wherein out of 29, 17 solutions used XGBoost. Its capability also shines in handling sparse data and null values.&lt;/p&gt;

&lt;p&gt;Before understanding XGBoost let's understand boosting first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Boosting
&lt;/h3&gt;

&lt;p&gt;In simple words, boosting is: take the mistakes/errors of the first model and send it to another model to correct it. Then again predict the actual target, identify the errors, and again send them further for correction.&lt;/p&gt;

&lt;p&gt;It is the working principle for both normal GB and XGBoost. However, XGBoost is far better than the normal one in scaling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Objective Function
&lt;/h2&gt;

&lt;p&gt;L(φ) = Σᵢ l(ŷᵢ, yᵢ) + Σₖ Ω(fₖ)&lt;/p&gt;

&lt;p&gt;where:&lt;/p&gt;

&lt;p&gt;Ω(f) = γT + ½λ Σⱼ wⱼ²&lt;/p&gt;

&lt;p&gt;Above is an objective function which XGBoost has to minimize.&lt;/p&gt;

&lt;p&gt;where:&lt;/p&gt;

&lt;p&gt;T → Number of leaves&lt;/p&gt;

&lt;p&gt;w → Weight of leaf&lt;/p&gt;

&lt;p&gt;But the main drawback is that the above equation is non-optimizable in Euclidean space as each fₖ is a decision tree. It can't be optimized simply as Linear Regression because each tree structure is different.&lt;/p&gt;

&lt;p&gt;Hence, some more modifications are done. The 2nd-order Taylor series is used.&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾ = Σᵢ l(yᵢ, ŷᵢ⁽ᵗ⁻¹⁾ + fₜ(xᵢ)) + Ω(fₜ)&lt;/p&gt;

&lt;p&gt;Using the second-order Taylor expansion:&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾ ≈ Σᵢ [l(yᵢ, ŷᵢ⁽ᵗ⁻¹⁾) + gᵢfₜ(xᵢ) + ½hᵢfₜ(xᵢ)²] + Ω(fₜ)&lt;/p&gt;

&lt;p&gt;Removing the constant term:&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾ ≈ Σᵢ [gᵢfₜ(xᵢ) + ½hᵢfₜ(xᵢ)²] + Ω(fₜ)&lt;/p&gt;

&lt;p&gt;Gradient tells which direction to move.&lt;/p&gt;

&lt;p&gt;Hessian tells the shape/curvature of the curve.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾ = Σᵢ [gᵢfₜ(xᵢ) + ½hᵢfₜ(xᵢ)²] + γT + ½λΣⱼwⱼ²&lt;/p&gt;

&lt;p&gt;On solving the above equation and differentiating with respect to w, it gives the optimal w value:&lt;/p&gt;

&lt;p&gt;wⱼ* = − [Σᵢ∈Iⱼ gᵢ] / [Σᵢ∈Iⱼ hᵢ + λ]&lt;/p&gt;

&lt;p&gt;where Iⱼ represents the instances that fall into leaf j.&lt;/p&gt;




&lt;h1&gt;
  
  
  Split Finding
&lt;/h1&gt;

&lt;p&gt;The objective function can be written as:&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾ = Σⱼ [ (Σᵢ∈Iⱼ gᵢ)wⱼ + ½(Σᵢ∈Iⱼ hᵢ + λ)wⱼ² ] + γT&lt;/p&gt;

&lt;p&gt;Putting the optimal wⱼ back into the equation:&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾(q) = −½ Σⱼ [ (Σᵢ∈Iⱼ gᵢ)² / (Σᵢ∈Iⱼ hᵢ + λ) ] + γT&lt;/p&gt;

&lt;p&gt;This is a scoring function to measure the quality of a tree, like Gini impurity for a Decision Tree.&lt;/p&gt;

&lt;p&gt;Since a tree has too many possible structures which are impossible to enumerate, we use a greedy approach.&lt;/p&gt;

&lt;p&gt;Assume Iᴸ and Iᴿ are the left and right instances of a tree.&lt;/p&gt;

&lt;p&gt;The split gain can be calculated as:&lt;/p&gt;

&lt;p&gt;Gain = ½ [ Gᴸ²/(Hᴸ+λ) + Gᴿ²/(Hᴿ+λ) − G²/(H+λ) ] − γ&lt;/p&gt;

&lt;p&gt;where:&lt;/p&gt;

&lt;p&gt;Gᴸ = Σᵢ∈Iᴸ gᵢ&lt;/p&gt;

&lt;p&gt;Hᴸ = Σᵢ∈Iᴸ hᵢ&lt;/p&gt;

&lt;p&gt;Gᴿ = Σᵢ∈Iᴿ gᵢ&lt;/p&gt;

&lt;p&gt;Hᴿ = Σᵢ∈Iᴿ hᵢ&lt;/p&gt;

&lt;p&gt;G = Gᴸ + Gᴿ&lt;/p&gt;

&lt;p&gt;H = Hᴸ + Hᴿ&lt;/p&gt;

&lt;p&gt;It is used in practice for evaluating split candidates.&lt;/p&gt;




&lt;h1&gt;
  
  
  Greedy Algorithm
&lt;/h1&gt;

&lt;p&gt;Input: I, instance set of current node&lt;/p&gt;

&lt;p&gt;Input: d, feature dimension&lt;/p&gt;

&lt;p&gt;First calculate:&lt;/p&gt;

&lt;p&gt;G = Σᵢ∈I gᵢ&lt;/p&gt;

&lt;p&gt;H = Σᵢ∈I hᵢ&lt;/p&gt;

&lt;p&gt;Then sort the instances according to the feature value.&lt;/p&gt;

&lt;p&gt;For every possible split:&lt;/p&gt;

&lt;p&gt;Gᴸ ← Gᴸ + gⱼ&lt;/p&gt;

&lt;p&gt;Hᴸ ← Hᴸ + hⱼ&lt;/p&gt;

&lt;p&gt;The right-side statistics can then be obtained as:&lt;/p&gt;

&lt;p&gt;Gᴿ ← G − Gᴸ&lt;/p&gt;

&lt;p&gt;Hᴿ ← H − Hᴸ&lt;/p&gt;

&lt;p&gt;Then calculate the gain for the split and keep the maximum:&lt;/p&gt;

&lt;p&gt;Score = max(Score, Gain)&lt;/p&gt;

&lt;p&gt;Output: Split with maximum score.&lt;/p&gt;

&lt;p&gt;The above approach is greedy, which is good only for small datasets as it has to try all possible split points.&lt;/p&gt;

&lt;p&gt;The approximate approach comes into the picture here.&lt;/p&gt;




&lt;h1&gt;
  
  
  Approximate Split Finding
&lt;/h1&gt;

&lt;p&gt;The second-order objective can be rewritten as:&lt;/p&gt;

&lt;p&gt;L⁽ᵗ⁾ = Σᵢ ½hᵢ [ fₜ(xᵢ) − gᵢ/hᵢ ]² + Ω(fₜ) + constant&lt;/p&gt;

&lt;p&gt;This has the form of a weighted squared-loss problem.&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;p&gt;gᵢ/hᵢ → acts like the target&lt;/p&gt;

&lt;p&gt;hᵢ → acts as the weight&lt;/p&gt;

&lt;p&gt;Therefore, XGBoost uses hᵢ as the weight when constructing weighted quantiles.&lt;/p&gt;




&lt;h1&gt;
  
  
  Weighted Quantile Sketch
&lt;/h1&gt;

&lt;p&gt;For a particular feature k, we can represent the feature values and their corresponding Hessians as:&lt;/p&gt;

&lt;p&gt;Dₖ = {(x₁ₖ,h₁), (x₂ₖ,h₂), ..., (xₙₖ,hₙ)}&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;p&gt;xᵢₖ = value of feature k for instance i&lt;/p&gt;

&lt;p&gt;hᵢ = second-order gradient/Hessian of instance i&lt;/p&gt;

&lt;p&gt;XGBoost defines a weighted rank function:&lt;/p&gt;

&lt;p&gt;rₖ(z) = [Σ₍ₓ,ₕ₎∈Dₖ, x&amp;lt;z h] / [Σ₍ₓ,ₕ₎∈Dₖ h]&lt;/p&gt;

&lt;p&gt;In simple words, this tells us:&lt;/p&gt;

&lt;p&gt;"What fraction of the total weight lies below a certain feature value z?"&lt;/p&gt;

&lt;p&gt;Since XGBoost wants consecutive candidate split points to be reasonably close, ε is used as an approximation factor.&lt;/p&gt;

&lt;p&gt;The candidate split points satisfy:&lt;/p&gt;

&lt;p&gt;|rₖ(sₖ,ⱼ) − rₖ(sₖ,ⱼ₊₁)| &amp;lt; ε&lt;/p&gt;

&lt;p&gt;Here, ε controls how finely the weighted distribution is approximated.&lt;/p&gt;

&lt;p&gt;Approximately:&lt;/p&gt;

&lt;p&gt;1/ε → number of candidate regions&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;ε = 0.1&lt;/p&gt;

&lt;p&gt;1/ε = 10&lt;/p&gt;

&lt;p&gt;So there are roughly 10 candidate regions.&lt;/p&gt;

&lt;p&gt;Similarly:&lt;/p&gt;

&lt;p&gt;ε = 0.01&lt;/p&gt;

&lt;p&gt;1/ε = 100&lt;/p&gt;

&lt;p&gt;So there are roughly 100 candidate regions.&lt;/p&gt;

&lt;p&gt;A smaller ε gives a finer approximation and therefore more candidate split points.&lt;/p&gt;

&lt;p&gt;The weighted quantile sketch allows XGBoost to generate a manageable number of candidate split points instead of checking every possible feature value.&lt;/p&gt;




&lt;h1&gt;
  
  
  Handling Missing Values
&lt;/h1&gt;

&lt;p&gt;XGBoost also handles null/missing values by assigning a default direction to each branch.&lt;/p&gt;

&lt;p&gt;During training, XGBoost considers where missing values should go and chooses the default direction that gives the better split score.&lt;/p&gt;

&lt;p&gt;Therefore, when a feature value is missing during prediction, the instance is sent in the learned default direction.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example: XGBoost with Numerical and Categorical Features
&lt;/h1&gt;

&lt;p&gt;Let's understand the above concepts with a small example.&lt;/p&gt;

&lt;p&gt;Suppose we want to predict whether a customer will buy a product.&lt;/p&gt;

&lt;p&gt;1 → Customer buys&lt;/p&gt;

&lt;p&gt;0 → Customer does not buy&lt;/p&gt;

&lt;p&gt;Our dataset contains one numerical feature, Age, and one categorical feature, Device.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;th&gt;Device&lt;/th&gt;
&lt;th&gt;Actual y&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;28&lt;/td&gt;
&lt;td&gt;Mobile&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;Mobile&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;35&lt;/td&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;Mobile&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;td&gt;45&lt;/td&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Step 1: Initial Prediction
&lt;/h2&gt;

&lt;p&gt;For simplicity, assume XGBoost starts with:&lt;/p&gt;

&lt;p&gt;ŷᵢ = 0.5&lt;/p&gt;

&lt;p&gt;for every customer.&lt;/p&gt;

&lt;p&gt;Using squared error:&lt;/p&gt;

&lt;p&gt;l(ŷ,y) = ½(ŷ − y)²&lt;/p&gt;

&lt;p&gt;The gradient is:&lt;/p&gt;

&lt;p&gt;gᵢ = ∂l / ∂ŷᵢ = ŷᵢ − yᵢ&lt;/p&gt;

&lt;p&gt;and the Hessian is:&lt;/p&gt;

&lt;p&gt;hᵢ = ∂²l / ∂ŷᵢ² = 1&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer&lt;/th&gt;
&lt;th&gt;y&lt;/th&gt;
&lt;th&gt;Prediction&lt;/th&gt;
&lt;th&gt;Gradient gᵢ&lt;/th&gt;
&lt;th&gt;Hessian hᵢ&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;-0.5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;-0.5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;-0.5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;td&gt;-0.5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The positive gradient for A and B tells us that their predictions need to move down, while the negative gradient for C, D, E and F tells us that their predictions need to move up.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Try a Numerical Split
&lt;/h2&gt;

&lt;p&gt;For the numerical feature Age, XGBoost can consider different split points.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Age &amp;lt; 27.5&lt;/p&gt;

&lt;p&gt;This creates two groups.&lt;/p&gt;

&lt;p&gt;Left:&lt;/p&gt;

&lt;p&gt;A, B&lt;/p&gt;

&lt;p&gt;Right:&lt;/p&gt;

&lt;p&gt;C, D, E, F&lt;/p&gt;

&lt;p&gt;For the left side:&lt;/p&gt;

&lt;p&gt;Gᴸ = 0.5 + 0.5 = 1&lt;/p&gt;

&lt;p&gt;Hᴸ = 1 + 1 = 2&lt;/p&gt;

&lt;p&gt;For the right side:&lt;/p&gt;

&lt;p&gt;Gᴿ = -0.5 - 0.5 - 0.5 - 0.5 = -2&lt;/p&gt;

&lt;p&gt;Hᴿ = 4&lt;/p&gt;

&lt;p&gt;The optimal leaf weight is:&lt;/p&gt;

&lt;p&gt;wⱼ* = −Gⱼ / (Hⱼ + λ)&lt;/p&gt;

&lt;p&gt;Assuming:&lt;/p&gt;

&lt;p&gt;λ = 0&lt;/p&gt;

&lt;p&gt;For the left leaf:&lt;/p&gt;

&lt;p&gt;wᴸ = −1/2 = -0.5&lt;/p&gt;

&lt;p&gt;For the right leaf:&lt;/p&gt;

&lt;p&gt;wᴿ = −(-2)/4 = 0.5&lt;/p&gt;

&lt;p&gt;So the tree can be represented as:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
text
              Age &amp;lt; 27.5?
              /          \
           Yes            No
            |              |
          -0.5            +0.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>ai</category>
      <category>algorithms</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
