Exercises

Practice deriving self-attention shapes, calculating projections and masked weights, applying W_O, diagnosing axis and batching errors, counting parameters, and bounding interpretation claims.

These exercises move from shapes and individual calculations to debugging and evidence. Each prompt contains the values and conventions it needs.

Read and Derive Shapes

Q1. Complete the shape trace

A batch-major single-head layer uses B=4B=4, T=12T=12, dmodel=8d_{model}=8, dk=3d_k=3, and dv=5d_v=5. What is the shape of the attention reading H=AVH=AV?

Choose one

Select one choice, then check.

Hint
For each batch item, (T,T)(T,dv)=(T,dv)(T,T)(T,d_v)=(T,d_v).
Solution
HH has shape (4,12,5)(4,12,5). The output projection will later map its last axis from 5 to dmodel=8d_{model}=8.
Not attempted
Review

Not marked done.

Q2. Derive the output-projection shape

The same layer produces HH with shape (4,12,5)(4,12,5) and must return YY with shape (4,12,8)(4,12,8). What shape must WOW_O have?

Choose one

Select one choice, then check.

Hint
Complete (4,12,5)(?,?)=(4,12,8)(4,12,5)(?,?)=(4,12,8).
Solution
WO:(5,8)W_O:(5,8).
Not attempted
Review

Not marked done.

Calculate the Operations

Q3. Project one query

Let x=[2,1]x=[2,-1] and

WQ=[1201].W_Q=\begin{bmatrix}1&2\\0&-1\end{bmatrix}.

What is the second coordinate of q=xWQq=xW_Q?

Compute it first, then check your number.

Hint
Multiply xx by the second column of WQW_Q.
Solution
q=[2,5]q=[2,5], so its second coordinate is 5.
Not attempted
Review

Not marked done.

Q4. Normalize an allowed causal row

A causal query has two allowed scores, [ln3,0][\ln 3,0], followed by one forbidden future score. What are the three attention weights after masking and softmax?

Choose the weight row

Select one choice, then check.

Hint
eln3=3e^{\ln 3}=3 and e0=1e^0=1.
Solution
The allowed weights are 3/(3+1)=0.753/(3+1)=0.75 and 1/(3+1)=0.251/(3+1)=0.25. The forbidden future weight is 0.
Not attempted
Review

Not marked done.

Q5. Finish the value and output calculation

Weights are [0.75,0.25][0.75,0.25], values are v1=[2,0]v_1=[2,0] and v2=[0,4]v_2=[0,4], and

WO=[1102].W_O=\begin{bmatrix}1&1\\0&2\end{bmatrix}.

What is the second coordinate of y=(0.75v1+0.25v2)WOy=(0.75v_1+0.25v_2)W_O?

Compute it first, then check your number.

Hint
Compute hh first, then multiply it by the second column of WOW_O.
Solution
h=0.75[2,0]+0.25[0,4]=[1.5,1]h=0.75[2,0]+0.25[0,4]=[1.5,1]. Its second projected coordinate is 1.5(1)+1(2)=3.51.5(1)+1(2)=3.5.
Not attempted
Review

Not marked done.

Diagnose and Repair

Q6. Find the wrong axis

An attention matrix is

A=[0.80.30.20.7].A=\begin{bmatrix}0.8&0.3\\0.2&0.7\end{bmatrix}.

Its columns sum to 1, but its rows do not. Which implementation error best explains the matrix?

Choose one

Select one choice, then check.

Hint
Each query needs one distribution over keys.
Solution
Softmax used the wrong axis. It must normalize each row across its key-position columns.
Not attempted
Review

Not marked done.

Q7. Detect cross-batch mixing

A batch contains sequences A and B. After changing only one token in B, the output for A also changes. All model parameters are fixed and there is no batch-dependent normalization. What does this observation establish?

Choose one

Select one choice, then check.

Hint
A deterministic per-sequence operation must preserve independence between batch items.
Solution
The batch dimension was likely transposed, flattened, normalized, or multiplied incorrectly. Changing B must not alter A under the stated layer.
Not attempted
Review

Not marked done.

Count and Interpret Carefully

Q8. Count projection parameters

A bias-free single head has dmodel=8d_{model}=8, dk=3d_k=3, and dv=4d_v=4. Count all entries in WQW_Q, WKW_K, WVW_V, and WOW_O.

Compute it first, then check your number.

Hint
Count each matrix separately, including the map from dvd_v back to dmodeld_{model}.
Solution
24+24+32+32=11224+24+32+32=112 parameters.
Not attempted
Review

Not marked done.

Q9. Bound an interpretation claim

For one prediction, position 7 receives the largest attention weight in one head. Which conclusion is justified by that observation alone?

Choose the supported claim

Select one choice, then check.

Hint
Values, output projections, residual paths, and later layers have not been examined.
Solution
The supported claim is only that the value at position 7 received the largest scalar multiplier in that attention row and head. Causal or reasoning claims require additional evidence.
Not attempted
Review

Not marked done.

Integrated Audit

Q10. Specify a complete layer audit

A colleague reports only the final output shape of a new causal self-attention implementation. Choose the smallest audit below that checks the chapter's main failure modes.

Choose the audit

Select one choice, then check.

Hint
Prefer checks that can locate the first incorrect intermediate.
Solution
The complete audit is required. It checks the computation in dependency order and separates observed tensors from interpretation.
Not attempted
Review

Not marked done.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 10 exercises marked done

Review

Not marked done.