Embedding Input Tokens for Attention
The number of columns in each weight matrix is the only thing that sets the output length. Changing d_out changes the length of q, k, and v together, but the input length d_in is fixed by the token itself.
Why One Token Needs Three Views
Self-attention lets each token in a sequence look at the other tokens and decide how much they matter. To make that possible, the model does not use the raw embedding of a token directly. Instead, it creates three new views of the same token: a query, a key, and a value. Each view is produced by multiplying the embedded token by its own trainable weight matrix.
This article focuses on the step that produces those three views. We start from a single input token, run the projection, and trace where each shape comes from. The attention scores and context vectors that follow are handled in other articles in this module.
What do you think happens?
The input token has three numbers, and d_out is set to 2. Before reading any further, what kind of object do you expect the query to be, and what is its shape?
Reveal answer
A row vector with 2 numbers, shape (1 x 2)
The input is a row of 3 numbers. The weight matrix maps 3 inputs to d_out = 2 outputs, so the query is a single row with 2 entries. The number of columns in the weight matrix sets the output length.
The Token as a Row Vector
Think of the embedded token as a row of numbers. A projection is a fixed rule, stored as a matrix, that converts that row into a different row. Write the input token as a row vector $x$. In this lesson we pay attention to the second input element, $x^{(2)}$, because it is the current token. Its role is to ask questions, so its query is the first one we compute.
The shape of $x^{(2)}$ is $(1 \times d_{in})$. It has one row and as many columns as the token has numbers. Shape is the first thing to check, because the rest of the projection depends on it.
The diagram shows the same token feeding three branches. Each branch multiplies the same input by a different matrix. Because the input is identical and only the matrix changes, the three outputs take on separate roles even though they start from one token.
Shape Rules for a Projection
The same pattern applies to all three vectors. The query is a single product of the input row and the query weight matrix: $q^{(2)} = x^{(2)} W_q$. The key and value follow exactly the same rule with different matrices: $k^{(2)} = x^{(2)} W_k$ and $v^{(2)} = x^{(2)} W_v$.
Each weight matrix has shape $(d_{in} \times d_{out})$. Its rows must match the input length $d_{in}$, because each input number multiplies one row of the matrix. Its columns set the output length $d_{out}$. Putting these together, each projected vector has shape $(1 \times d_{out})$.
The number of columns in each weight matrix is the only thing that sets the output length. Changing d_out changes the length of q, k, and v together, but the input length d_in is fixed by the token itself.
Tracing the Three Projections in Code
The code below picks out the second input token, records its shape, and applies three weight matrices. The values are small hand-picked numbers chosen so the arithmetic can be checked by hand. In the source setup the matrices are filled with random starting values after a fixed random seed, so the same numbers appear on every run. Those values are parameters that training would adjust.
import numpy as np
x = np.array([[1.0, 2.0, 3.0]])
print(x.shape)
W_q = np.array([[1.0, 0.0],
[0.0, 1.0],
[1.0, 1.0]])
W_k = np.array([[1.0, 1.0],
[0.0, 1.0],
[2.0, 0.0]])
W_v = np.array([[0.0, 1.0],
[2.0, 0.0],
[1.0, 2.0]])
q = x @ W_q
k = x @ W_k
v = x @ W_v
print(q.shape, q)
print(k.shape, k)
print(v.shape, v)- 1Line 1 creates the input row x. Its shape (1, 3) means one row and
d_in= 3 columns. - 2Line 2 prints the input shape so you can check it before any multiplication.
- 3The three weight matrices each have 3 rows, matching
d_in, and 2 columns, matchingd_out= 2. - 4q = x @ W_q multiplies the input row by the query matrix. Column 1 of the result is 1x1 + 2x0 + 3x1 = 4, and column 2 is 1x0 + 2x1 + 3x1 = 5, giving [4, 5].
- 5k = x @ W_k uses the same input with a different matrix. Column 1 is 1x1 + 2x0 + 3x2 = 7, and column 2 is 1x1 + 2x1 + 3x0 = 3, giving [7, 3].
- 6v = x @ W_v gives [7, 7]. The three results differ only because the matrices differ, not because the input changed.
- x holds the second token as a row with three numbers, shape (1, 3)
- Each W matrix has shape (3, 2): three rows to match
d_inand two columns ford_out - The @ operator multiplies the same input by a different matrix on each line
- All three outputs have shape (1, 2), because only the columns of each matrix set the output length
(1, 3)
(1, 2) [[4. 5.]]
(1, 2) [[7. 3.]]
(1, 2) [[7. 7.]]Computing the query for one token
The input token is x = [1, 2, 3] with d_in = 3, and W_q has shape 3 x 2 with columns [1, 0, 1] and [0, 1, 1]. Find the query q and its shape.
- 1Identify the shapes
x has shape (1 x 3). W_q has shape (3 x 2). The inner sizes are 3 and 3, so the product is defined.
- 2Compute column 1
Multiply the input row by the first column: 1x1 + 2x0 + 3x1 = 4.
- 3Compute column 2
Multiply the input row by the second column: 1x0 + 2x1 + 3x1 = 5.
- 4Check the output shape
The result has one row and two columns, so q has shape (1 x 2), matching (1 x
d_out).
q = [4, 5], with shape (1 x 2)
Why Query, Key, and Value Play Different Roles
The three vectors come from one token, but each has a different job in attention. The query is the view used when a token asks questions about the other tokens. The key is the view other tokens use when they check how relevant this token is. The value is the view that carries content forward once relevance is decided. The same token produces all three, and other tokens use them when they attend to it.
| Vector | Weight matrix | Shape | Role in attention |
|---|---|---|---|
| Query q | W_q | (1 x d_out) | Asks what this token is looking for |
| Key k | W_k | (1 x d_out) | Used by other tokens to judge relevance to this token |
| Value v | W_v | (1 x d_out) | Carries the content that gets combined once relevance is known |
Three projections of one input token
Notice that the three vectors have the same shape. The shape comes only from d_out, so the roles are separated by the matrices, not by the sizes of the outputs.
Finding Where a Projection Goes Wrong
Most projection errors show up first in shape, not in values. When a result is not what you expected, follow the chain from the input shape to the weight matrix shape and check each step.
Common mistake Using the wrong number of rows in a weight matrix
What it looks likeA 2 x 2 matrix is used for W_q when the input has d_in = 3.
Why it failsEach input number multiplies one row of the weight matrix, so the rows must equal
d_in.Do this insteadCreate each matrix with shape d_in x d_out, here 3 x 2, and check the row count before multiplying.
Common mistake Assuming the output length comes from the number of input numbers
What it looks likeExpecting the query to have 3 entries because the token has 3 numbers.
Why it failsThe output length is set by the columns of the weight matrix, which is
d_out.Do this insteadRead the output length from d_out. With d_out = 2, the query is shape (1 x 2).
Common mistake Using the same matrix for all three projections
What it looks likeComputing q, k, and v all with W_q.
Why it failsThe three vectors are different only because each uses its own matrix. Identical matrices give identical vectors.
Do this insteadCheck which matrix each line uses. Each line should use W_q, W_k, or W_v respectively.
Common mistake Printing only values and ignoring shapes, which hides the first sign of an error
Practice Tracing a Projection
A token has d_in = 4 numbers, and d_out = 3. Predict the shape of the key matrix W_k and of the resulting key k for this token. Then state how you would check your answer in code.
Hints
- The rows of W_k must match the length of the input row.
- The columns of W_k set the output length.
- A key is one row, so its first dimension is 1.
Suppose a student writes q = x @ W_q with x of shape (1 x 3) and W_q of shape (2 x 2), and gets an error. Identify the step that diverges from expectation, state the expected shape of W_q, and rewrite the shape of the corrected matrix.
Hints
- Compare the columns of x with the rows of W_q.
- The expected shape is
d_inxd_out, which here is 3 x 2.
Summary of the Projection Step
- 1Self-attention creates three views of each token: a query, a key, and a value, each made by multiplying the token by its own trainable weight matrix.
- 2The input row has shape (1 x
d_in), each weight matrix has shape (d_inxd_out), and each projected vector has shape (1 xd_out). - 3The columns of each weight matrix set the output length, so the three projected vectors share one shape while carrying different roles.
- 4When a result is wrong, check the shapes step by step, starting with the inner sizes of each matrix multiplication.
At a glance
- Self-attention creates three views of each input token: a query, a key, and a value.
- Each view comes from multiplying the token by its own trainable weight matrix: W_q, W_k, or W_v.
- A token of length
d_intimes ad_inxd_outmatrix gives a vector of shape (1 xd_out). - Shape errors show up first in the inner dimensions, so check shapes before reading values.