Chapter 4 · Application第 4 章 · 應用

Transformer Vector LabTransformer 向量與矩陣

How can a list of numbers help predict a token? Change the vectors, inspect attention, and multiply a matrix yourself.一串數字怎麼幫忙預測下一個詞元(token)?改動向量、觀察注意力(attention),再自己做一次矩陣乘法。

Toy model: 2 coordinates · 1 attention head · 3 output candidates玩具模型:2 個座標 · 1 個注意力頭 · 3 個輸出候選詞元

Real models use thousands of coordinates and many layers. This page keeps only the linear-algebra skeleton.真實模型有上千個座標與許多層;這裡只保留線性代數的骨架。

Tokens → vectors詞元 → 向量Attention → weighted sum注意力 → 加權和Wx → scores → probabilities分數 → 機率
Suggested beginner route高中生建議路線
  1. Pick a token and move its two coordinate sliders to see how the vector changes.先選一個詞元(token),移動兩個座標滑桿,看看向量如何改變。
  2. Press “Start with Wx” and work with the simple x=(2,1). You can skip attention for now.按「從矩陣範例開始」,先算簡單的 x=(2,1),不用先看注意力。
  3. Do the two exercises on multiplying row by row and on one matrix entry. To see where x comes from, return to attention (optional).做「逐列乘法」與「一個矩陣元素」兩題。想知道 x 如何產生,再回到注意力(選讀)。

Predict → change one value → inspect the calculation → read the key idea.先預測 → 改一個值 → 檢查算式 → 看懂重點概念。

Each token starts as a vector.每個詞元(token)一開始都是一個向量。

A token embedding is a learned list of numbers. Tokens need not be whole words: a word split into three tokens receives three initial vectors.詞元的嵌入向量(embedding)是一串經由訓練學到的數字。詞元不一定是完整的單字:一個字若被切成三個詞元,就會得到三個初始向量。

one word一個字 → (t₁, t₂, t₃) → (e(t₁), e(t₂), e(t₃)),   e(t) ∈ ℝ² here.(本例)。
Choose a token, then change its coordinates選一個詞元,再改它的座標

Changing the embedding of the changes both occurrences. They share an initial vector, but attention can give their positions different processed vectors.改變 the 的嵌入向量,兩個 the 會一起改變。它們共用同一個初始向量,但注意力可以讓這兩個位置得到不同的處理後向量。

The coordinates here are invented. Individual coordinates in a trained LLM do not usually have a simple name such as “catness.”這裡的座標是虛構的。訓練好的大型語言模型(LLM)中,單一座標通常沒有「像貓的程度」這類簡單的名稱。
How does this relate to training?這和訓練有什麼關係?

Training adjusts an embedding table and other model weights using many prediction examples. The controls here edit those numbers by hand; this simulator does not train a model. During ordinary generation the weights stay fixed while the input changes.

訓練會用大量的預測例子,調整嵌入向量表和模型的其他權重(weight)。這裡的控制項是用手修改這些數字;這個模擬器不會訓練模型。一般產生文字時,權重固定不變,改變的只有輸入。

See the vector in ℝ²在 ℝ² 中看這個向量

Use either slider to move the vector. The two coordinates are its horizontal and vertical components.用任一個滑桿移動向量。兩個座標就是它的水平分量與鉛直分量。

Embeddings at each input position每個輸入位置的嵌入向量
Position / token位置/詞元dim 1維度 1dim 2維度 2
Toy tokenization: lowercase, split at spaces; each displayed word is one token.玩具斷詞方式:全部小寫、以空格切開;每個顯示的單字就是一個詞元。
Glossary: each term in one sentence術語表:用一句話理解
Vector向量(Vector)
可做加法與純量乘法的物件。例:(2,1) 有兩個座標;多項式也可以是向量。
An object you can add and multiply by a scalar. (2,1) has two coordinates; a polynomial can also be a vector.
Scalar純量(Scalar)
拿來乘向量的數。例如 3(1,2)=(3,6),其中 3 是純量。
A number that multiplies a vector. In 3(1,2)=(3,6), the scalar is 3.
Coordinate座標(Coordinate)
描述向量的一個數值。(2,1) 的第一座標是 2,第二座標是 1。
One of the numbers describing a vector. For (2,1), the first coordinate is 2 and the second is 1.
Coefficient係數(Coefficient)
線性組合中乘在向量前面的純量。2u−v 的係數是 2 和 −1。
The scalar in front of each vector in a linear combination. In 2u−v the coefficients are 2 and −1.
Linear combination線性組合(Linear combination)
先把向量各自乘上純量,再相加:au+bv。
Multiply each vector by a scalar, then add the results: au+bv.
Symbols符號怎麼讀(Symbols)
∈:屬於;∉:不屬於;∀:每一個;∃:至少存在一個;⇒:如果前者成立,則後者成立。ℝ 是實數集合,ℂ 是複數集合。
∈: is in; ∉: is not in; ∀: for every; ∃: there is at least one; ⇒: if the first part holds, so does the second. ℝ is the set of real numbers and ℂ the set of complex numbers.
Token詞元(Token)
模型處理文字的單位,可以是字、詞的一部分或標點。本玩具模型為了方便,把每個顯示的英文單字視為一個詞元。
The unit of text a model works with: a word, part of a word, or punctuation. For simplicity, this toy model treats each displayed English word as one token.
Embedding嵌入向量(Embedding)
把一個詞元對應到一串數字,例如 e(cat)=(2,1)。本實驗的數值是示範用,並非真實模型量測值。
A list of numbers assigned to a token, such as e(cat)=(2,1). The values in this lab are for illustration, not measured from a real model.
Matrix / row / column矩陣/列/欄(Matrix / row / column)
矩陣是一個數字表格。row 是橫向的一列,column 是縱向的一欄。Wx 逐列與 x 做乘法再相加。
A matrix is a table of numbers. A row runs across; a column runs down. Wx multiplies each row by x and adds the products.
Dot product內積(Dot product)
相同位置的數相乘後加總:(a,b)·(x,y)=ax+by。
Multiply numbers in matching positions, then add: (a,b)·(x,y)=ax+by.
Context vector上下文向量(Context vector)
把前面的文字資訊整理成一個向量。本實驗把最後位置的注意力加權和當成 x;完整模型還有其他步驟。
One vector that summarises the earlier text. This lab uses the attention-weighted sum at the last position as x; a full model has further steps.
Weight權重(Weight)
表示一個數值對結果的影響程度。W 中的元素可以是負數;注意力權重則在 softmax 後非負且加總為 1。
How strongly a value affects the result. Entries of W can be negative; attention weights, after softmax, are non-negative and add up to 1.
Attention注意力(Attention)
先計算各位置的權重,再依權重混合向量。它是數學計算,不表示模型有人類的注意或理解。
Compute a weight for each position, then mix the vectors by those weights. It is a calculation, not human attention or understanding.
Query / key / value查詢/鍵/值向量(Query / key / value)
q 用來比較,k 是被比較的向量,v 是要混合的資訊。它們來自不同矩陣與輸入向量的乘法。
q does the comparing, k is what gets compared, and v is the information that gets mixed. Each comes from multiplying the input vector by a different matrix.
Score / probability分數/機率(Score / probability)
分數可正可負。softmax 把分數轉成非負且加總為 1 的數;本例只在三個候選詞元之間正規化。
A score can be positive or negative. Softmax turns scores into non-negative numbers that add up to 1; here it normalises over only three candidate tokens.
Softmax機率正規化函數(Softmax)
先把每個分數 s 變成正數 exp(s)=e^s,再除以這些正數的總和。這是非線性步驟。
Turn each score s into a positive number exp(s)=e^s, then divide by the total of those numbers. This step is non-linear.
Causal mask因果遮罩(Causal mask)
預測下一個詞元時,不能偷看後面的詞元。表格中的 × 表示該位置權重設為 0。
When predicting the next token, the model may not look at later tokens. A × in the table means that weight is set to 0.

Some terms are advanced; you do not need to memorise them all at once. Experiment first, then use examples to understand the definitions.有些名詞屬於進階內容,不必一次全部背起來。先操作,再用例子理解定義。