<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Attention on XEDCZQ Blog</title><link>https://xedczq.cn/en/tags/attention/</link><description>Recent content in Attention on XEDCZQ Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 05 Jun 2026 23:10:00 +0800</lastBuildDate><atom:link href="https://xedczq.cn/en/tags/attention/index.xml" rel="self" type="application/rss+xml"/><item><title>Transformer 20-Step Visual Study Notes</title><link>https://xedczq.cn/en/post/transformerexplainer/</link><pubDate>Fri, 05 Jun 2026 23:10:00 +0800</pubDate><guid>https://xedczq.cn/en/post/transformerexplainer/</guid><description>&lt;img src="https://xedczq.cn/img/transformer-explainer/steps/step-03.jpg" alt="Featured image of post Transformer 20-Step Visual Study Notes" /&gt;&lt;h1 id="transformer-20-step-visual-study-notes"&gt;&lt;a href="#transformer-20-step-visual-study-notes" class="header-anchor"&gt;&lt;/a&gt;Transformer 20-Step Visual Study Notes
&lt;/h1&gt;&lt;p&gt;This note is based on the interactive explanation from &lt;a class="link" href="https://poloclub.github.io/transformer-explainer/" target="_blank" rel="noopener"
 &gt;Transformer Explainer&lt;/a&gt;. It follows the site&amp;rsquo;s 20 steps and organizes them into a study note for understanding how GPT-style Transformers perform next-token prediction. The site uses GPT-2 small as the example model and visualizes the full pipeline from input tokens to output probabilities.&lt;/p&gt;
&lt;p&gt;First, remember one sentence: &lt;strong&gt;the core task of GPT-style Transformers is next-token prediction&lt;/strong&gt;. Given the prompt:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Data visualization empowers users to
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The model needs to answer:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;What is the most likely next token?
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To answer this, a Transformer goes through tokenization, embedding, positional encoding, repeated Transformer blocks, self-attention, MLP, logits, probability distribution, sampling strategy, and other steps.&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;All screenshots in this note are taken from &lt;a class="link" href="https://poloclub.github.io/transformer-explainer/" target="_blank" rel="noopener"
 &gt;Transformer Explainer&lt;/a&gt;, developed by the Georgia Tech Polo Club team. They are used here for personal study notes. It is best to read this together with the original interactive site.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="what-is-transformer"&gt;&lt;a href="#what-is-transformer" class="header-anchor"&gt;&lt;/a&gt;What Is Transformer
&lt;/h2&gt;&lt;p&gt;Transformer is the most common foundation architecture for modern large language models. Text-generation models such as GPT, Llama, and Gemini can all be understood as expanded versions of the Transformer architecture.&lt;/p&gt;
&lt;p&gt;Its most important ability is not &amp;ldquo;memorizing answers&amp;rdquo;, but learning language patterns from large amounts of text and then predicting the next token from context at inference time. This prediction is repeated: predict one token, append it to the text, then predict the next one.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 1: What is Transformer" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-01.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer, &lt;a class="link" href="https://poloclub.github.io/transformer-explainer/" target="_blank" rel="noopener"
 &gt;https://poloclub.github.io/transformer-explainer/&lt;/a&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="how-transformers-work"&gt;&lt;a href="#how-transformers-work" class="header-anchor"&gt;&lt;/a&gt;How Transformers Work
&lt;/h2&gt;&lt;p&gt;When a Transformer generates text, it does not write an entire paragraph at once. It generates step by step. Each step performs the same task: &lt;strong&gt;predict a probability distribution for the next token based on the existing context&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For example, if the current input is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Data visualization empowers users to
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The model may assign the highest probability to &lt;code&gt;visualize&lt;/code&gt;, while also assigning relatively high probabilities to tokens such as &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;see&lt;/code&gt;, and &lt;code&gt;make&lt;/code&gt;. The final selected token is also affected by sampling parameters such as temperature, top-k, and top-p.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 2: How Transformers Work" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-02.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="transformer-architecture"&gt;&lt;a href="#transformer-architecture" class="header-anchor"&gt;&lt;/a&gt;Transformer Architecture
&lt;/h2&gt;&lt;p&gt;A text-generation Transformer can be divided into three major parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Embedding&lt;/strong&gt;: converts human-readable text into vectors the model can process.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transformer Blocks&lt;/strong&gt;: repeatedly refine each token representation, mainly through Self-Attention and MLP.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output Probabilities&lt;/strong&gt;: converts the final vector into probabilities over all tokens in the vocabulary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At a high level, the information flow is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;text input -&amp;gt; token -&amp;gt; embedding -&amp;gt; Transformer Blocks -&amp;gt; logits -&amp;gt; probabilities -&amp;gt; sample next token
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img alt="Step 3: Transformer Architecture" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-03.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="embedding"&gt;&lt;a href="#embedding" class="header-anchor"&gt;&lt;/a&gt;Embedding
&lt;/h2&gt;&lt;p&gt;The model cannot directly understand strings. Embedding converts each token into a sequence of numbers, or a vector. This vector is not manually designed; it is learned during training.&lt;/p&gt;
&lt;p&gt;If two tokens often appear in similar contexts, their embeddings tend to be closer in high-dimensional space. You can think of embedding as an internal &amp;ldquo;semantic coordinate&amp;rdquo; used by the model.&lt;/p&gt;
&lt;p&gt;GPT-2 small has a hidden dimension of 768, so each token is represented as a 768-dimensional vector.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 4: Embedding" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-04.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="token-embedding"&gt;&lt;a href="#token-embedding" class="header-anchor"&gt;&lt;/a&gt;Token Embedding
&lt;/h2&gt;&lt;p&gt;Tokenization splits input text into tokens. A token can be a full word or a subword. For example, &lt;code&gt;empowers&lt;/code&gt; in the example is split into &lt;code&gt;em&lt;/code&gt; and &lt;code&gt;powers&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Each token has a unique ID. GPT-2 has a vocabulary size of 50,257, so its token embedding matrix is roughly:&lt;/p&gt;
$$
50257 \times 768
$$&lt;p&gt;After the model gets the token ID, it looks up the corresponding 768-dimensional vector from this large matrix.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 5: Token Embedding" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-05.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="positional-encoding"&gt;&lt;a href="#positional-encoding" class="header-anchor"&gt;&lt;/a&gt;Positional Encoding
&lt;/h2&gt;&lt;p&gt;Self-Attention does not naturally know sequence order. If the model only receives a set of token vectors, it does not know which token comes first and which comes later.&lt;/p&gt;
&lt;p&gt;So positional encoding is needed. GPT-2 uses learnable positional embeddings and adds the semantic token vector and the position vector:&lt;/p&gt;
$$
x_i = \text{TokenEmbedding}_i + \text{PositionEmbedding}_i
$$&lt;p&gt;This lets the model know both &amp;ldquo;what this token is&amp;rdquo; and &amp;ldquo;where it is.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 6: Positional Encoding" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-06.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="repetitive-transformer-blocks"&gt;&lt;a href="#repetitive-transformer-blocks" class="header-anchor"&gt;&lt;/a&gt;Repetitive Transformer Blocks
&lt;/h2&gt;&lt;p&gt;Embedding is only the input representation, not a fully contextualized semantic representation. The real context modeling happens inside Transformer Blocks.&lt;/p&gt;
&lt;p&gt;GPT-2 small has 12 Transformer Blocks. Each block roughly contains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Multi-Head Self-Attention: lets tokens exchange information.&lt;/li&gt;
&lt;li&gt;MLP: nonlinearly processes each token representation.&lt;/li&gt;
&lt;li&gt;Residual, LayerNorm, and Dropout: stabilize training and improve generalization.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The meaning of stacking multiple layers is that lower layers tend to capture local and lexical information, while higher layers more easily form complex semantic and task-related representations.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 7: Repetitive Transformer Blocks" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-07.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="multi-head-self-attention"&gt;&lt;a href="#multi-head-self-attention" class="header-anchor"&gt;&lt;/a&gt;Multi-Head Self Attention
&lt;/h2&gt;&lt;p&gt;The goal of Self-Attention is to let each token update itself based on context. For example, the token &lt;code&gt;to&lt;/code&gt; has different meanings in different sentences. It needs to &amp;ldquo;look at&amp;rdquo; the previous context &lt;code&gt;Data visualization empowers users&lt;/code&gt; to form a more accurate representation.&lt;/p&gt;
&lt;p&gt;Multi-Head means the model does not use just one attention perspective. It uses multiple heads in parallel. GPT-2 small has 12 attention heads. Different heads can learn different relationships, such as syntactic relations, short-distance collocations, and long-distance semantic dependencies.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 8: Multi-Head Self Attention" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-08.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="query-key-value"&gt;&lt;a href="#query-key-value" class="header-anchor"&gt;&lt;/a&gt;Query, Key, Value
&lt;/h2&gt;&lt;p&gt;Self-Attention maps each token&amp;rsquo;s input vector into three vectors:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Query (Q)&lt;/strong&gt;: what information the current token wants to search for.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key (K)&lt;/strong&gt;: features by which each token can be matched by others.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value (V)&lt;/strong&gt;: the actual information content to be aggregated and passed along.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;They come from linear transformations:&lt;/p&gt;
$$
Q = XW_Q,\quad K = XW_K,\quad V = XW_V
$$&lt;p&gt;A simple analogy is a search engine:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Query is the search query.&lt;/li&gt;
&lt;li&gt;Key is the page title or index.&lt;/li&gt;
&lt;li&gt;Value is the page content.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The model first computes relevance between Query and Key, then reads Value with weighted aggregation.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 9: Query, Key, Value" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-09.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="multi-head"&gt;&lt;a href="#multi-head" class="header-anchor"&gt;&lt;/a&gt;Multi-Head
&lt;/h2&gt;&lt;p&gt;GPT-2 small has an embedding dimension of 768 and 12 attention heads, so each head usually processes:&lt;/p&gt;
$$
768 / 12 = 64
$$&lt;p&gt;The benefit of multi-head attention is learning multiple relationships in parallel. One head may focus on adjacent words, another on subject-verb relationships, and another on more distant semantic hints.&lt;/p&gt;
&lt;p&gt;Multiple heads are not duplicate work; they give the model multiple &amp;ldquo;viewing angles.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 10: Multi-head" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-10.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="masked-self-attention"&gt;&lt;a href="#masked-self-attention" class="header-anchor"&gt;&lt;/a&gt;Masked Self Attention
&lt;/h2&gt;&lt;p&gt;GPT-style models generate text from left to right. When predicting the current position, they must not peek at future tokens, so causal mask, also called masked self-attention, is used.&lt;/p&gt;
&lt;p&gt;The core formula is:&lt;/p&gt;
$$
\text{Attention}(Q,K,V)=\text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + M\right)V
$$&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$QK^T$: computes pairwise similarity between tokens.&lt;/li&gt;
&lt;li&gt;$\sqrt{d_k}$: scaling factor that prevents dot products from becoming too large and making softmax too sharp.&lt;/li&gt;
&lt;li&gt;$M$: mask matrix that sets future positions to $-\infty$.&lt;/li&gt;
&lt;li&gt;softmax: turns scores into probabilities.&lt;/li&gt;
&lt;li&gt;multiplying by $V$: aggregates information according to attention weights.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Step 11: Masked Self Attention" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-11.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="attention-output-and-concatenation"&gt;&lt;a href="#attention-output-and-concatenation" class="header-anchor"&gt;&lt;/a&gt;Attention Output and Concatenation
&lt;/h2&gt;&lt;p&gt;Each head outputs a context-enhanced token representation. Since GPT-2 small has 12 heads, it produces 12 sets of results.&lt;/p&gt;
&lt;p&gt;The model then concatenates the outputs from these heads and applies a linear projection back to the original hidden dimension 768:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;head_1, head_2, ..., head_12 -&amp;gt; concat -&amp;gt; linear projection
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The purpose is to let different heads extract information separately, then fuse those perspectives into one unified representation.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 12: Attention Output &amp; Concatenation" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-12.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="mlp"&gt;&lt;a href="#mlp" class="header-anchor"&gt;&lt;/a&gt;MLP
&lt;/h2&gt;&lt;p&gt;Attention handles information flow between tokens. MLP processes each token&amp;rsquo;s own representation nonlinearly.&lt;/p&gt;
&lt;p&gt;GPT-2&amp;rsquo;s MLP usually contains two linear transformations with GELU activation in between:&lt;/p&gt;
$$
\text{MLP}(x)=W_2\cdot \text{GELU}(W_1x+b_1)+b_2
$$&lt;p&gt;The first layer expands the dimension from 768 to 3072, and the second layer compresses it back to 768. Expanding the dimension lets the model represent more complex features in a higher-dimensional space.&lt;/p&gt;
&lt;p&gt;Note that MLP does not communicate across tokens like Attention. It processes each token independently.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 13: MLP" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-13.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="output-logit"&gt;&lt;a href="#output-logit" class="header-anchor"&gt;&lt;/a&gt;Output Logit
&lt;/h2&gt;&lt;p&gt;After all Transformer Blocks, the model takes the output vector at the last position and uses it to predict the next token.&lt;/p&gt;
&lt;p&gt;This vector goes through the final linear layer and is mapped to the vocabulary size:&lt;/p&gt;
$$
\text{logits}=h_{\text{last}}W_{\text{vocab}}+b
$$&lt;p&gt;GPT-2 has a vocabulary size of 50,257, so logits are a vector of length 50,257. Each number corresponds to the raw score of a candidate token.&lt;/p&gt;
&lt;p&gt;A logit is not a probability. It can be any real number and must go through softmax to become a probability distribution.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 14: Output Logit" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-14.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="probabilities"&gt;&lt;a href="#probabilities" class="header-anchor"&gt;&lt;/a&gt;Probabilities
&lt;/h2&gt;&lt;p&gt;Softmax converts logits into probabilities:&lt;/p&gt;
$$
p_i=\frac{e^{z_i}}{\sum_j e^{z_j}}
$$&lt;p&gt;After conversion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every token probability is between 0 and 1.&lt;/li&gt;
&lt;li&gt;The probabilities of all tokens sum to 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the figure, after the example input, the model considers tokens such as &lt;code&gt;visualize&lt;/code&gt;, &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;see&lt;/code&gt;, and &lt;code&gt;make&lt;/code&gt; to be likely next tokens.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 15: Probabilities" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-15.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="temperature"&gt;&lt;a href="#temperature" class="header-anchor"&gt;&lt;/a&gt;Temperature
&lt;/h2&gt;&lt;p&gt;Temperature scales logits before softmax:&lt;/p&gt;
$$
p_i=\frac{\exp(z_i/T)}{\sum_j \exp(z_j/T)}
$$&lt;p&gt;Where $T$ is the temperature:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$T &amp;lt; 1$: the probability distribution becomes sharper. High-score tokens are more likely to be selected, and output is more stable.&lt;/li&gt;
&lt;li&gt;$T = 1$: logits are not additionally adjusted.&lt;/li&gt;
&lt;li&gt;$T &amp;gt; 1$: the probability distribution becomes flatter. Low-probability tokens have more chances to be selected, and output becomes more diverse.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In simple terms, lower temperature is more conservative, while higher temperature is more divergent.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 16: Temperature" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-16.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="sampling-strategy"&gt;&lt;a href="#sampling-strategy" class="header-anchor"&gt;&lt;/a&gt;Sampling Strategy
&lt;/h2&gt;&lt;p&gt;After obtaining a probability distribution, the model still needs to decide how to choose the next token. Common strategies include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Greedy Search&lt;/strong&gt;: always choose the highest-probability token. Stable, but can be rigid.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-k&lt;/strong&gt;: keep only the k highest-probability tokens, then sample from them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-p&lt;/strong&gt;: keep the smallest token set whose cumulative probability reaches p. Also called nucleus sampling.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Top-k is more like a fixed candidate pool, while top-p is a dynamic candidate pool. In practice, temperature and top-k/top-p are often tuned together.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 17: Sampling Strategy" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-17.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="residual-connection"&gt;&lt;a href="#residual-connection" class="header-anchor"&gt;&lt;/a&gt;Residual Connection
&lt;/h2&gt;&lt;p&gt;A residual connection adds a layer&amp;rsquo;s input directly to its output:&lt;/p&gt;
$$
y = x + F(x)
$$&lt;p&gt;Its purpose is to preserve original information and make gradients pass through deep networks more easily. Without residual connections, training becomes harder as the model gets deeper, and information from early layers is more likely to be lost.&lt;/p&gt;
&lt;p&gt;In Transformers, residual connections usually surround both Attention and MLP.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 18: Residual Connection" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-18.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="layer-normalization"&gt;&lt;a href="#layer-normalization" class="header-anchor"&gt;&lt;/a&gt;Layer Normalization
&lt;/h2&gt;&lt;p&gt;Layer Normalization normalizes the values inside a token vector, making the mean and variance more stable:&lt;/p&gt;
$$
\text{LayerNorm}(x)=\gamma\frac{x-\mu}{\sqrt{\sigma^2+\epsilon}}+\beta
$$&lt;p&gt;It reduces training instability and makes the input distribution of each layer more controllable. GPT-2 uses a pre-norm style: LayerNorm is applied before entering Attention and MLP.&lt;/p&gt;
&lt;p&gt;Intuitively, LayerNorm is like adjusting the numerical scale to a more suitable range before each key computation.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Step 19: Layer Normalization" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-19.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="dropout"&gt;&lt;a href="#dropout" class="header-anchor"&gt;&lt;/a&gt;Dropout
&lt;/h2&gt;&lt;p&gt;Dropout is a regularization method used during training. It randomly sets part of the connections or activations to zero, preventing the model from over-relying on certain local features.&lt;/p&gt;
&lt;p&gt;The intuition is: during training, do not let the model follow the exact same path every time, forcing it to learn more robust representations.&lt;/p&gt;
&lt;p&gt;Important notes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Dropout is mainly used during training.&lt;/li&gt;
&lt;li&gt;Dropout is disabled during inference.&lt;/li&gt;
&lt;li&gt;Many newer large models use less Dropout than early models because their training data is extremely large.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Step 20: Dropout" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://xedczq.cn/img/transformer-explainer/steps/step-20.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Source: Transformer Explainer&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="one-flowchart-summary"&gt;&lt;a href="#one-flowchart-summary" class="header-anchor"&gt;&lt;/a&gt;One Flowchart Summary
&lt;/h2&gt;&lt;p&gt;The inference flow of a GPT-style Transformer can be compressed into this chain:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;flowchart LR
 A["Input text"] --&gt; B["Tokenization"]
 B --&gt; C["Token Embedding"]
 C --&gt; D["Positional Encoding"]
 D --&gt; E["Transformer Block x N"]
 E --&gt; F["Multi-Head Self-Attention"]
 F --&gt; G["MLP"]
 G --&gt; H["Final Linear"]
 H --&gt; I["Logits"]
 I --&gt; J["Softmax Probabilities"]
 J --&gt; K["Temperature / Top-k / Top-p"]
 K --&gt; L["Sample next token"]&lt;/pre&gt;&lt;hr&gt;
&lt;h2 id="key-differences-from-rnn"&gt;&lt;a href="#key-differences-from-rnn" class="header-anchor"&gt;&lt;/a&gt;Key Differences from RNN
&lt;/h2&gt;&lt;p&gt;Combining this with the previous RNN note, the difference can be understood like this:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Aspect&lt;/th&gt;
 &lt;th&gt;RNN&lt;/th&gt;
 &lt;th&gt;Transformer&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Information transfer&lt;/td&gt;
 &lt;td&gt;Passed step by step through hidden state&lt;/td&gt;
 &lt;td&gt;Self-Attention lets tokens directly read one another&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Parallelism&lt;/td&gt;
 &lt;td&gt;Strong time-step dependency, hard to parallelize&lt;/td&gt;
 &lt;td&gt;Tokens in the same layer can be computed in parallel&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Long-distance dependency&lt;/td&gt;
 &lt;td&gt;Long path, easy to decay&lt;/td&gt;
 &lt;td&gt;Any positions can directly connect&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Context representation&lt;/td&gt;
 &lt;td&gt;Compressed into hidden state&lt;/td&gt;
 &lt;td&gt;Keeps explicit token representations for the whole context&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Large-model training&lt;/td&gt;
 &lt;td&gt;Less efficient to scale&lt;/td&gt;
 &lt;td&gt;Better suited to large-scale GPU/TPU matrix computation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;This is why modern LLMs mainly use Transformers: they are not only strong in modeling, but also better engineered for large-scale pretraining.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="sources"&gt;&lt;a href="#sources" class="header-anchor"&gt;&lt;/a&gt;Sources
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://poloclub.github.io/transformer-explainer/" target="_blank" rel="noopener"
 &gt;Transformer Explainer: LLM Transformer Model Visually Explained&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/poloclub/transformer-explainer" target="_blank" rel="noopener"
 &gt;Transformer Explainer GitHub Repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://arxiv.org/abs/2408.04619" target="_blank" rel="noopener"
 &gt;Transformer Explainer Paper, arXiv:2408.04619&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Vaswani et al., &lt;a class="link" href="https://arxiv.org/abs/1706.03762" target="_blank" rel="noopener"
 &gt;Attention Is All You Need&lt;/a&gt;, 2017&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>