<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manish Purve on Medium]]></title>
        <description><![CDATA[Stories by Manish Purve on Medium]]></description>
        <link>https://medium.com/@manishpurve?source=rss-ac5599f47006------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZZN3gjIzusfcmqMyTgx_bw.jpeg</url>
            <title>Stories by Manish Purve on Medium</title>
            <link>https://medium.com/@manishpurve?source=rss-ac5599f47006------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:30:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manishpurve/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An intuitive guide to understanding Transformers — from tokenization and embeddings to…]]></title>
            <link>https://medium.com/@manishpurve/an-intuitive-guide-to-understanding-transformers-from-tokenization-and-embeddings-to-21a22d5a679c?source=rss-ac5599f47006------2</link>
            <guid isPermaLink="false">https://medium.com/p/21a22d5a679c</guid>
            <dc:creator><![CDATA[Manish Purve]]></dc:creator>
            <pubDate>Sun, 04 Oct 2026 20:57:55 GMT</pubDate>
            <atom:updated>2026-10-04T20:57:55.551Z</atom:updated>
            <content:encoded><![CDATA[<h3>An intuitive guide to understanding Transformers — from tokenization and embeddings to self-attention, Q/K/V, multi-head attention, encoder, decoder, and training.</h3><p>Transformers are at the heart of modern artificial intelligence.</p><p>Models such as GPT and BERT, along with many modern language and multimodal systems, are built using Transformer architecture.</p><p>But when you first start learning Transformers, you quickly encounter terms such as <strong>attention, Query, Key, Value, multi-head attention, positional encoding, feed-forward networks, encoders, decoders, and layer normalization</strong>.</p><p>Individually, these ideas are not extremely difficult.</p><p>The real challenge is understanding how all of them work together.</p><p>In this article, I will explain the Transformer architecture from the ground up, with special focus on the most important part:</p><blockquote><strong><em>How attention works, what Query, Key, and Value actually mean, and how their weight matrices are learned during training.</em></strong></blockquote><p>You do not need advanced mathematics to follow this explanation.</p><h3>The Big Idea Behind a Transformer</h3><p>A Transformer converts text into numerical vectors and repeatedly allows tokens to exchange information with one another.</p><p>At a high level, the flow looks like this:</p><p><strong>Text → Tokenization → Embeddings → Positional Information → Transformer Layers → Contextual Representations → Prediction</strong></p><p>The most important mechanism inside those Transformer layers is <strong>attention</strong>.</p><p>Attention helps answer a simple question:</p><blockquote><strong><em>Which other tokens are important for understanding the token I am currently processing?</em></strong></blockquote><p>Let us build up to that idea step by step.</p><h3>1. Why Do We Need Attention?</h3><p>Consider this sentence:</p><blockquote><strong><em>“The animal did not cross the road because it was tired.”</em></strong></blockquote><p>When we read the word <strong>“it”</strong>, we naturally understand that it probably refers to <strong>“animal.”</strong></p><p>For a neural network, however, this relationship is not automatically obvious.</p><p>The model somehow needs to learn that:</p><p><strong>“it” is strongly related to “animal.”</strong></p><p>At the same time, it may learn that “it” is less related to words such as “road.”</p><p>Attention provides a mechanism for discovering these relationships.</p><p>Instead of processing every token completely independently, attention allows each token to examine other tokens and gather useful information from them.</p><h3>2. Tokenization</h3><p>Before a Transformer can process text, the text is divided into smaller units called <strong>tokens</strong>.</p><p>For example:</p><p><strong>“I love pizza”</strong></p><p>might become:</p><p><strong>“I” — “love” — “pizza”</strong></p><p>In real language models, a token does not always represent a complete word.</p><p>A token can be:</p><ul><li>A complete word</li><li>Part of a word</li><li>Punctuation</li><li>A number</li><li>A special symbol</li></ul><p>Each token is then converted into a numerical token ID.</p><p>For example:</p><p><strong>“I” → 42</strong></p><p><strong>“love” → 918</strong></p><p><strong>“pizza” → 7312</strong></p><p>These IDs themselves do not contain meaningful semantic information.</p><p>They mainly act as indexes that allow the model to retrieve learned numerical representations.</p><p>Those numerical representations are called <strong>embeddings</strong>.</p><h3>3. Embeddings: Turning Tokens Into Vectors</h3><p>Neural networks work with numbers, not words.</p><p>So every token is converted into a numerical vector.</p><p>For example:</p><p><strong>“cat” → [0.21, -0.73, 0.44, …]</strong></p><p><strong>“dog” → [0.18, -0.69, 0.42, …]</strong></p><p><strong>“pizza” → [0.81, 0.12, -0.30, …]</strong></p><p>Suppose our Transformer uses a model dimension of 512.</p><p>Then each token might initially be represented by a vector containing <strong>512 numbers</strong>.</p><p>A useful mental model is:</p><blockquote><strong><em>Embedding tells the model: “What token am I?”</em></strong></blockquote><p>But the Transformer still needs another piece of information.</p><p>It needs to know <strong>where that token appears in the sequence</strong>.</p><h3>4. Positional Information</h3><p>Consider these two sentences:</p><p><strong>Dog bites man.</strong></p><p><strong>Man bites dog.</strong></p><p>They contain exactly the same words.</p><p>But their meanings are completely different because the word order changed.</p><p>Attention by itself does not automatically know sequence order.</p><p>Therefore Transformers add <strong>positional information</strong>.</p><p>Conceptually:</p><p><strong>“I” → token representation + position 0</strong></p><p><strong>“love” → token representation + position 1</strong></p><p><strong>“pizza” → token representation + position 2</strong></p><p>The original Transformer used sinusoidal positional encodings.</p><p>Many modern Transformer architectures use other techniques, such as <strong>Rotary Positional Embeddings, or RoPE</strong>.</p><p>The easiest way to remember this is:</p><blockquote><strong><em>Embedding tells the model what the token is.</em></strong></blockquote><blockquote><strong><em>Positional information tells the model where the token is.</em></strong></blockquote><p>Now the Transformer has enough information to start understanding relationships between tokens.</p><h3>5. Self-Attention</h3><p>Consider the sentence:</p><blockquote><strong><em>“The cat drank milk because it was thirsty.”</em></strong></blockquote><p>Suppose the Transformer is currently processing the token <strong>“it.”</strong></p><p>The model may need information from several other tokens.</p><p>It might determine that:</p><p><strong>cat → highly relevant</strong></p><p><strong>thirsty → relevant</strong></p><p><strong>milk → somewhat relevant</strong></p><p><strong>other words → less relevant</strong></p><p>For example, the resulting attention weights might look something like this:</p><p><strong>cat → 0.70</strong></p><p><strong>thirsty → 0.15</strong></p><p><strong>milk → 0.10</strong></p><p><strong>others → 0.05</strong></p><p>These numbers indicate how much attention should be given to the information coming from different tokens.</p><p>But how does the model calculate these relationships?</p><p>This is where <strong>Query, Key, and Value</strong> become important.</p><h3>6. Query, Key, and Value</h3><p>Query, Key, and Value are often the most confusing concepts for people learning Transformers.</p><p>A simple way to understand them is to think about a search system.</p><h3>Query</h3><p>The Query represents:</p><blockquote><strong><em>What am I looking for?</em></strong></blockquote><h3>Key</h3><p>The Key represents:</p><blockquote><strong><em>What kind of information can I be matched on?</em></strong></blockquote><h3>Value</h3><p>The Value represents:</p><blockquote><strong><em>What information should I actually provide if I am selected?</em></strong></blockquote><p>So the simplest interpretation is:</p><p><strong>Query (Q): What am I looking for?</strong></p><p><strong>Key (K): What can I be matched on?</strong></p><p><strong>Value (V): What information should I provide?</strong></p><p>This is the core idea behind attention.</p><h3>7. Where Do Query, Key, and Value Come From?</h3><p>Suppose a token currently has a representation called <strong>X</strong>.</p><p>The Transformer contains three learned weight matrices:</p><p><strong>WQ</strong></p><p><strong>WK</strong></p><p><strong>WV</strong></p><p>The model uses these matrices to create the Query, Key, and Value vectors.</p><p><strong>Q = X × WQ</strong></p><p><strong>K = X × WK</strong></p><p><strong>V = X × WV</strong></p><p>In other words, the same token representation is transformed in three different ways.</p><p>One transformation creates a Query.</p><p>Another creates a Key.</p><p>Another creates a Value.</p><p>This distinction is extremely important.</p><h3>8. Q, K, and V Are Not the Same as WQ, WK, and WV</h3><p>There are two separate concepts here.</p><p>The first is:</p><p><strong>Q, K, and V</strong></p><p>The second is:</p><p><strong>WQ, WK, and WV</strong></p><p>They are not the same thing.</p><p>A useful way to think about them is:</p><p><strong>WQ is a machine that creates Queries.</strong></p><p><strong>WK is a machine that creates Keys.</strong></p><p><strong>WV is a machine that creates Values.</strong></p><p>The outputs of those machines are the actual Q, K, and V vectors.</p><p>So:</p><blockquote><strong><em>WQ, WK, and WV are learned model parameters.</em></strong></blockquote><p>While:</p><blockquote><strong><em>Q, K, and V are dynamically calculated from the current input.</em></strong></blockquote><p>For example:</p><p><strong>X for “cat” × WQ → Query for “cat”</strong></p><p><strong>X for “dog” × WQ → Query for “dog”</strong></p><p>Both tokens use the same WQ matrix within that particular attention head.</p><p>But because the input representations are different, the resulting Query vectors are also different.</p><h3>9. How Attention Matching Works</h3><p>Let us return to the sentence:</p><blockquote><strong><em>“The cat drank milk because it was thirsty.”</em></strong></blockquote><p>Suppose we are processing the token <strong>“it.”</strong></p><p>The Query vector for “it” is compared with the Key vectors of the other tokens.</p><p>So conceptually the model performs comparisons such as:</p><p><strong>Query of “it” with Key of “cat”</strong></p><p><strong>Query of “it” with Key of “milk”</strong></p><p><strong>Query of “it” with Key of “thirsty”</strong></p><p><strong>Query of “it” with Key of “because”</strong></p><p>These comparisons are performed using a mathematical operation called a <strong>dot product</strong>.</p><p>A higher Query-Key similarity usually means that the other token is more relevant for the current attention calculation.</p><p>Imagine the scores looked like this:</p><p><strong>Query(it) · Key(cat) → 8.2</strong></p><p><strong>Query(it) · Key(thirsty) → 3.7</strong></p><p><strong>Query(it) · Key(milk) → 2.1</strong></p><p><strong>Query(it) · Key(because) → 0.6</strong></p><p>The model therefore has strong evidence that information associated with “cat” may be particularly useful while processing “it.”</p><h3>10. Why Are Attention Scores Scaled?</h3><p>The Query-Key dot products can become large when the vectors contain many dimensions.</p><p>Large values can make the Softmax operation unstable or excessively sharp.</p><p>So the Transformer divides the Query-Key scores by the square root of the Key dimension.</p><p>The scaled score is:</p><p><strong>QKᵀ / √dₖ</strong></p><p>Here, <strong>dₖ</strong> is the dimension of the Key vector.</p><p>The main purpose of this scaling is to keep the numerical values well behaved during training.</p><h3>11. Softmax Creates Attention Weights</h3><p>After calculating Query-Key similarity scores, the Transformer applies <strong>Softmax</strong>.</p><p>Softmax converts the scores into normalized attention weights.</p><p>For example:</p><p><strong>cat → 0.70</strong></p><p><strong>thirsty → 0.15</strong></p><p><strong>milk → 0.10</strong></p><p><strong>others → 0.05</strong></p><p>Together, these weights add up to approximately 1.</p><p>The model has now decided how much importance to assign to the information coming from each token.</p><h3>12. What Is the Role of the Value Vector?</h3><p>Query and Key determine <strong>which tokens are relevant</strong>.</p><p>Value determines <strong>what information those tokens actually contribute</strong>.</p><p>Suppose the attention weights are:</p><p><strong>cat → 0.70</strong></p><p><strong>milk → 0.10</strong></p><p><strong>thirsty → 0.15</strong></p><p><strong>others → 0.05</strong></p><p>The attention output is approximately a weighted combination of the Value vectors:</p><p><strong>0.70 × Value(cat)</strong></p><p>plus</p><p><strong>0.10 × Value(milk)</strong></p><p>plus</p><p><strong>0.15 × Value(thirsty)</strong></p><p>plus</p><p><strong>0.05 × Values from other tokens</strong></p><p>The resulting representation contains information gathered from several tokens, with more influence coming from the tokens that received higher attention weights.</p><p>This is the central idea behind attention.</p><h3>13. The Complete Attention Formula</h3><p>Everything we have discussed can be summarized using the famous attention equation:</p><p><strong>Attention(Q, K, V) = softmax(QKᵀ / √dₖ) V</strong></p><p>The equation looks complicated at first.</p><p>But conceptually, it simply means:</p><p><strong>Create a Query.</strong></p><p><strong>Compare the Query with Keys.</strong></p><p><strong>Calculate similarity scores.</strong></p><p><strong>Scale those scores.</strong></p><p><strong>Apply Softmax.</strong></p><p><strong>Obtain attention weights.</strong></p><p><strong>Use those weights to combine Value vectors.</strong></p><p>That is attention.</p><h3>14. A Simple Real-World Analogy for Q, K, and V</h3><p>Imagine you are attending a large technology conference.</p><p>You want to learn about:</p><blockquote><strong><em>Deploying machine learning models on AWS.</em></strong></blockquote><p>Your question is your <strong>Query</strong>.</p><p>Every person at the conference has a badge describing their expertise.</p><p>Those badges are their <strong>Keys</strong>.</p><p>One person’s badge might say:</p><p><strong>Machine Learning Infrastructure</strong></p><p>Another might say:</p><p><strong>Accounting</strong></p><p>Another might say:</p><p><strong>AWS and Kubernetes</strong></p><p>Your Query matches strongly with the people whose Keys indicate relevant expertise.</p><p>After deciding whom to listen to, those people provide their actual knowledge.</p><p>That knowledge is the <strong>Value</strong>.</p><p>So:</p><blockquote><strong><em>Query = What am I looking for?</em></strong></blockquote><blockquote><strong><em>Key = What can I be matched on?</em></strong></blockquote><blockquote><strong><em>Value = What information do I provide?</em></strong></blockquote><p>This is essentially what attention does mathematically.</p><h3>15. Are WQ, WK, and WV Random?</h3><p>At the beginning of training, the weight matrices WQ, WK, and WV do not yet contain useful learned knowledge.</p><p>They are initialized using suitable weight initialization methods.</p><p>Initially, the resulting Queries, Keys, and Values will therefore not create particularly meaningful attention patterns.</p><p>The model must learn better WQ, WK, and WV matrices through training.</p><p>This leads to an important question:</p><blockquote><strong><em>How do those matrices actually learn?</em></strong></blockquote><h3>16. How WQ, WK, and WV Are Updated During Training</h3><p>Suppose the model is being trained for next-token prediction.</p><p>The input might be:</p><blockquote><strong><em>“The cat drank the”</em></strong></blockquote><p>and the correct next token might be:</p><blockquote><strong><em>“milk”</em></strong></blockquote><p>But suppose the model predicts:</p><blockquote><strong><em>“water”</em></strong></blockquote><p>The training process calculates a <strong>loss</strong> representing how incorrect the model’s prediction was.</p><p>The overall process is:</p><p><strong>Input → Transformer → Prediction → Compare with correct answer → Loss</strong></p><p>Now backpropagation moves backward through the network.</p><p>The loss sends gradient information backward through:</p><p><strong>The output layer</strong></p><p><strong>The Transformer layers</strong></p><p><strong>The Feed-Forward Networks</strong></p><p><strong>The attention mechanism</strong></p><p><strong>The Query, Key, and Value calculations</strong></p><p>and eventually to:</p><p><strong>WQ, WK, and WV</strong></p><p>The system calculates gradients such as:</p><p><strong>∂Loss / ∂WQ</strong></p><p><strong>∂Loss / ∂WK</strong></p><p><strong>∂Loss / ∂WV</strong></p><p>These gradients tell the optimizer how changing each matrix would affect the loss.</p><p>An optimizer such as Adam then updates the matrices.</p><p>Conceptually:</p><p><strong>New WQ = Old WQ − learning rate × gradient</strong></p><p>The same idea applies to WK and WV.</p><p>This happens repeatedly across enormous numbers of training examples.</p><p>Over time, the weight matrices become better at producing useful Query, Key, and Value representations.</p><h3>17. What Do WQ, WK, and WV Eventually Learn?</h3><p>Imagine the training data repeatedly contains relationships such as:</p><p><strong>“John said he…”</strong></p><p><strong>“Alice said she…”</strong></p><p><strong>“The dog was tired because it…”</strong></p><p>During training, attention patterns that help the model make better predictions are reinforced.</p><p>For example, one attention head might become useful at connecting pronouns with related nouns.</p><p>Another might focus on grammatical relationships.</p><p>Another might capture nearby word dependencies.</p><p>Another may become useful for long-distance relationships.</p><p>Another may respond to positional or structural patterns.</p><p>The important point is that nobody manually programs:</p><p><strong>Head 3 = pronoun detector</strong></p><p>These behaviors emerge because they help the model reduce its training loss.</p><h3>18. Multi-Head Attention</h3><p>Transformers do not usually perform attention only once.</p><p>Instead, they use <strong>multiple attention heads in parallel</strong>.</p><p>Why?</p><p>Because language contains many different relationships simultaneously.</p><p>One head might focus more on syntax.</p><p>Another might focus more on semantic meaning.</p><p>Another might focus on nearby words.</p><p>Another might detect long-distance dependencies.</p><p>Using several heads allows the Transformer to examine the same sequence from multiple learned perspectives.</p><h3>19. Do Attention Heads Receive Only Part of the Original Embedding?</h3><p>This is a common misunderstanding.</p><p>Suppose:</p><p><strong>Model dimension = 512</strong></p><p>and:</p><p><strong>Number of attention heads = 8</strong></p><p>Each head might work with 64-dimensional Query, Key, and Value vectors.</p><p>But that does not mean:</p><p><strong>Head 1 simply receives dimensions 1 to 64</strong></p><p><strong>Head 2 simply receives dimensions 65 to 128</strong></p><p>and so on.</p><p>Instead, each attention head typically learns its own projection from the full input representation.</p><p>For example, a head could have matrices shaped approximately like:</p><p><strong>WQ: 512 × 64</strong></p><p><strong>WK: 512 × 64</strong></p><p><strong>WV: 512 × 64</strong></p><p>The original 512-dimensional token representation is transformed into a 64-dimensional Query, Key, and Value for that head.</p><p>Different heads have different learned projection matrices.</p><p>This is what allows them to learn different kinds of relationships.</p><h3>20. What Happens After Multi-Head Attention?</h3><p>After the attention heads finish their calculations, their outputs are combined.</p><p>If there are eight heads and each head produces a 64-dimensional representation, combining them gives:</p><p><strong>8 × 64 = 512 dimensions</strong></p><p>The combined representation is then passed through another learned linear projection.</p><p>Now each token contains contextual information gathered from other tokens.</p><p>The Transformer then passes the token through another important component: the <strong>Feed-Forward Network</strong>.</p><h3>21. The Feed-Forward Network</h3><p>The Feed-Forward Network, or FFN, processes each token representation independently.</p><p>A simplified FFN looks like:</p><p><strong>Token representation → Linear layer → Activation function → Linear layer → Updated representation</strong></p><p>For example, the dimensions might conceptually expand like this:</p><p><strong>512 → 2048 → 512</strong></p><p>The larger intermediate representation gives the network additional capacity to perform complex nonlinear transformations.</p><p>A very important distinction to remember is:</p><blockquote><strong><em>Attention mixes information between tokens.</em></strong></blockquote><blockquote><strong><em>The Feed-Forward Network processes each token’s resulting representation independently.</em></strong></blockquote><p>This distinction makes Transformer layers much easier to understand.</p><h3>22. Residual Connections</h3><p>Transformers also use <strong>residual connections</strong>.</p><p>Instead of completely replacing the previous representation, the Transformer adds the original input back to the result of a sub-layer.</p><p>Conceptually:</p><p><strong>Output = Original Input + New Information</strong></p><p>For an attention sub-layer, this might be thought of approximately as:</p><p><strong>Output = X + Attention(X)</strong></p><p>Residual connections help preserve useful information and make very deep networks easier to train.</p><h3>23. Layer Normalization</h3><p>Transformer networks contain many layers.</p><p>As representations move through those layers, their numerical properties can change.</p><p><strong>Layer Normalization</strong> helps keep these signals well behaved.</p><p>Its purpose is closely related to training stability.</p><p>Modern Transformer architectures may place LayerNorm differently from the original Transformer, but the general idea remains the same:</p><blockquote><strong><em>Keep the representations numerically stable as they pass through many layers.</em></strong></blockquote><h3>24. The Transformer Encoder</h3><p>The encoder’s job is to build rich contextual representations of the input.</p><p>A simplified encoder layer contains:</p><p><strong>Multi-Head Self-Attention</strong></p><p>followed by:</p><p><strong>Residual connection and normalization</strong></p><p>followed by:</p><p><strong>Feed-Forward Network</strong></p><p>followed by:</p><p><strong>Another residual connection and normalization</strong></p><p>This structure is repeated multiple times.</p><p>So the overall flow might be:</p><p><strong>Embeddings → Encoder Layer 1 → Encoder Layer 2 → Encoder Layer 3 → … → Final Encoder Representation</strong></p><p>As a token moves through these layers, its representation becomes increasingly dependent on context.</p><p>For example, consider the word <strong>“bank.”</strong></p><p>In:</p><blockquote><strong><em>“I deposited money in the bank.”</em></strong></blockquote><p>the representation should capture the financial meaning.</p><p>But in:</p><blockquote><strong><em>“We sat beside the river bank.”</em></strong></blockquote><p>the same word should represent something completely different.</p><p>The token itself is the same.</p><p>The context changes its representation.</p><h3>25. The Transformer Decoder</h3><p>The original Transformer was designed for sequence-to-sequence tasks such as translation.</p><p>The decoder generates an output sequence one token at a time.</p><p>A decoder layer contains three major components:</p><p><strong>Masked Self-Attention</strong></p><p><strong>Cross-Attention</strong></p><p><strong>Feed-Forward Network</strong></p><p>Residual connections and normalization are also used around these components.</p><h3>26. Masked Self-Attention</h3><p>When generating text, the model should not look into the future.</p><p>Suppose the target sentence is:</p><blockquote><strong><em>“I love machine learning.”</em></strong></blockquote><p>While predicting the word <strong>“machine,”</strong> the model can use:</p><p><strong>“I love”</strong></p><p>But it should not already know that the next word is:</p><p><strong>“learning.”</strong></p><p>A causal mask prevents attention from accessing future tokens.</p><p>This is why this mechanism is often called <strong>masked self-attention</strong> or <strong>causal self-attention</strong>.</p><p>Decoder-only language models such as GPT-style models rely heavily on this form of attention.</p><h3>27. Cross-Attention</h3><p>In the original encoder-decoder Transformer, the decoder also needs information from the encoder.</p><p>This is handled using <strong>cross-attention</strong>.</p><p>The important relationship is:</p><p><strong>Query comes from the decoder.</strong></p><p><strong>Key comes from the encoder output.</strong></p><p><strong>Value comes from the encoder output.</strong></p><p>The decoder is effectively asking:</p><blockquote><strong><em>Which parts of the input sequence are useful while generating this output token?</em></strong></blockquote><p>This mechanism is especially useful in sequence-to-sequence tasks such as translation.</p><h3>28. Final Token Prediction</h3><p>After the final Transformer layer, the model has a contextual representation.</p><p>But it still needs to choose an actual token.</p><p>A linear layer maps the hidden representation into a score for every possible token in the vocabulary.</p><p>These scores are called <strong>logits</strong>.</p><p>Softmax can then convert those logits into probabilities.</p><p>For example, after:</p><blockquote><strong><em>“I love…”</em></strong></blockquote><p>the model might assign probabilities such as:</p><p><strong>pizza → 31%</strong></p><p><strong>coding → 24%</strong></p><p><strong>music → 17%</strong></p><p><strong>coffee → 11%</strong></p><p>A decoding strategy then chooses the next token.</p><p>That token becomes part of the context for predicting the following token.</p><p>The process repeats again and again.</p><h3>29. Putting the Entire Transformer Together</h3><p>We can now summarize the complete process.</p><p>First, text is tokenized.</p><p>The tokens are converted into embeddings.</p><p>Positional information is added.</p><p>The resulting representations enter Transformer layers.</p><p>Inside those layers, attention creates Query, Key, and Value vectors.</p><p>Queries are compared with Keys.</p><p>Softmax converts the similarity scores into attention weights.</p><p>Those weights determine how Value vectors are combined.</p><p>Multi-head attention performs this process using several learned perspectives.</p><p>Residual connections preserve earlier information.</p><p>Layer normalization helps stabilize the representations.</p><p>Feed-Forward Networks transform each token representation.</p><p>The process repeats across many layers.</p><p>Finally, the model produces a contextual representation that can be converted into token probabilities.</p><p>During training, predictions are compared with the correct targets.</p><p>A loss is calculated.</p><p>Backpropagation calculates gradients.</p><p>An optimizer updates the model’s learned parameters, including WQ, WK, and WV.</p><h3>30. The Most Important Concepts to Remember</h3><p><strong>Tokenization</strong> — Break text into tokens.</p><p><strong>Embedding</strong> — Convert each token into a numerical representation.</p><p><strong>Positional information</strong> — Tell the model where tokens occur.</p><p><strong>Query</strong> — What information am I looking for?</p><p><strong>Key</strong> — What information can I be matched with?</p><p><strong>Value</strong> — What information should I provide?</p><p><strong>WQ, WK, WV</strong> — Learned matrices used to create Query, Key, and Value vectors.</p><p><strong>Attention</strong> — Allow tokens to gather useful information from other tokens.</p><p><strong>Multi-Head Attention</strong> — Examine relationships from multiple learned perspectives.</p><p><strong>Feed-Forward Network</strong> — Transform each token’s contextual representation independently.</p><p><strong>Residual connection</strong> — Preserve and reuse earlier information.</p><p><strong>Layer Normalization</strong> — Help stabilize deep Transformer networks.</p><p><strong>Encoder</strong> — Build contextual representations of the input.</p><p><strong>Decoder</strong> — Generate an output sequence.</p><p><strong>Cross-Attention</strong> — Allow the decoder to use information from the encoder.</p><p><strong>Softmax</strong> — Convert scores into probabilities or normalized attention weights.</p><h3>A Mental Model for the Entire Transformer</h3><p>Imagine every token as a person sitting in a meeting.</p><p><strong>Embedding</strong> gives every person an identity.</p><p><strong>Positional information</strong> gives everyone a seat number.</p><p><strong>Query</strong> represents what each person wants to know.</p><p><strong>Key</strong> describes what each person can be matched on.</p><p><strong>Value</strong> represents the information each person can contribute.</p><p><strong>Attention</strong> determines whom each person should listen to.</p><p><strong>Multi-Head Attention</strong> is like having several specialist groups examining different types of relationships.</p><p><strong>The Feed-Forward Network</strong> allows each person to privately process what they learned.</p><p><strong>Residual connections</strong> preserve their previous knowledge.</p><p><strong>Layer Normalization</strong> keeps the numerical communication stable.</p><p>This entire process repeats through many layers.</p><p>Eventually, the representation of a token no longer means only:</p><blockquote><strong><em>“What word is this?”</em></strong></blockquote><p>It represents something much richer:</p><blockquote><strong><em>“What does this token mean in this exact position, sentence, and context?”</em></strong></blockquote><p>That is one of the core reasons Transformers are so powerful.</p><h3>Encoder-Only, Decoder-Only, and Encoder-Decoder Transformers</h3><p>The original Transformer architecture contains both an encoder and a decoder.</p><p>Modern Transformer models often use only part of this architecture.</p><h3>Encoder-only models</h3><p>BERT-style models primarily use Transformer encoders.</p><p>They are well suited for building contextual representations of input text.</p><h3>Decoder-only models</h3><p>GPT-style language models primarily use decoder-style Transformer blocks with causal attention.</p><p>They are designed around predicting the next token.</p><h3>Encoder-decoder models</h3><p>These architectures use both an encoder and decoder.</p><p>They are particularly useful for sequence-to-sequence problems such as translation.</p><p>So the term <strong>Transformer</strong> does not refer to one single fixed architecture.</p><p>It refers to a broader family of neural network architectures built around attention and related components.</p><h3>Final Takeaway</h3><p>The Transformer architecture becomes much easier to understand once the distinction between Query, Key, Value and their weight matrices becomes clear.</p><p>The simplest interpretation is:</p><blockquote><strong><em>Query asks what information is needed.</em></strong></blockquote><blockquote><strong><em>Key determines whether another token is relevant to that request.</em></strong></blockquote><blockquote><strong><em>Value carries the information that can actually be retrieved.</em></strong></blockquote><p>And one of the most important technical details is:</p><blockquote><strong><em>Q, K, and V are generated dynamically from the current input. WQ, WK, and WV are learned model parameters that generate them.</em></strong></blockquote><p>During training, backpropagation calculates how those weight matrices should change.</p><p>An optimizer updates them.</p><p>After seeing enormous amounts of training data, the model becomes increasingly good at producing attention patterns that help solve its training objective.</p><p>If you remember only one sentence from this entire article, remember this:</p><blockquote><strong><em>Attention lets tokens communicate. Query and Key decide who should communicate, Value carries the information, and training teaches the model how those interactions should work.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21a22d5a679c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>