Monash University · FACULTY OF COMPUTER SCIENCE

ECE5179 Chap.10 Attention and the Transformer Block

- one subject, every graph, every model, every mark
7 Chapters5-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 10 of 12 · ECE5179

Attention and the Transformer Block

Weeks 10 and 11 introduce attention, the operation that lets each token build its own vector from all the others. Queries, keys and values are projections of the token matrix, the scaled softmax of their dot products gives each token a budget of weights, and the output is a weighted average of the values.

The chapter works an attention row by hand, explains multi-head attention, Layer Norm and residual adds, and the quadratic cost in sequence length.

In this chapter

What this chapter covers

  • 01

    Why one word needs context-dependent vectors

  • 02

    Queries, keys and values as learned projections

  • 03

    Scaled dot-product attention and the √d_k divisor

  • 04

    Attention weights as convex combinations

  • 05

    Multi-head attention and per-head width

  • 06

    The transformer block: attention, MLP, residuals and Layer Norm

  • 07

    Computational cost and permutation equivariance

Worked example · free

Compute one row of an attention table

Q [5 marks]. Three tokens have queries and keys (1, 0), (0, 1) and (1, 1) with d_k = 2, and values (1, 2), (0, 1) and (3, 0). Treating rows as queries and columns as keys, find the softmax weights in the first row of the attention table and the output for the first token. The mark split shown is a revision weighting of our own, not a University-published scheme.
The full 3 × 3 attention table.The full 3 × 3 attention table. Rows are queries and columns are keys; each row is one softmax, so the shaded weights in a row add to one, and row 1 matches the worked example.0.400.200.400.200.400.400.250.250.50keys 1, 2, 3queries 1, 2, 3rows sum to 1
The full 3 × 3 attention table. Rows are queries and columns are keys; each row is one softmax, so the shaded weights in a row add to one, and row 1 matches the worked example.
  • 1Dot products of the first query with the three keys are 1, 0 and 1.
  • 1Dividing by √2 gives scaled scores 0.707, 0 and 0.707.
  • 1Exponentials 2.028, 1 and 2.028 sum to 5.056, so the weights are 0.401, 0.198 and 0.401.
  • 1The output is 0.401(1, 2) + 0.198(0, 1) + 0.401(3, 0).
  • 1Adding the terms gives (1.604, 1.000).
The first token attends with weights 0.401, 0.198 and 0.401 and outputs (1.604, 1.000), a convex combination of the three value vectors.
Sia tip — Show the division by √d_k on its own line; leaving it out sharpens the weights to 0.422, 0.155, 0.422 and loses the scaling mark.
Glossary

Key terms

Query vector
A query vector is the projection of a token that is compared against every key to decide where it attends.
Value vector
A value vector is the projection of a token that is averaged, with attention weights, to form the output.
Multi-head attention
Multi-head attention runs several attention operations with separate projections in parallel and concatenates their outputs.
Layer normalisation
Layer normalisation standardises each token across its own features, without using batch statistics.
FAQ

Attention and the Transformer Block FAQ

Why divide the attention scores by the square root of d_k?

If query and key entries are roughly independent with unit variance, their dot product has variance close to d_k. Large scores push softmax towards a one-hot output whose gradients nearly vanish. Dividing by √d_k restores unit scale, which the lectures describe as scaling the softmax for stability.

Why use separate query and key projections?

Scoring tokens with their raw dot products produces a symmetric matrix, so token A would attend to token B exactly as strongly as B attends to A. Language relationships are often one-sided, such as apple pointing to fruit more than the reverse, and separate learned projections allow that asymmetry.

Why does attention cost grow quadratically with sequence length?

Every token's query is compared with every token's key, producing an n by n table of scores, and that table then multiplies the values. Both steps scale with n squared times d, so doubling the context roughly quadruples the attention work, while the projections and MLP grow only linearly in n.

What does multi-head attention add over a single head?

A single head must either average several relationships or pick one and lose the others. Several heads with separate projections can each specialise, for instance one tracking syntax and another tracking topic, and their outputs are concatenated and projected back to the model width. With per-head width d/H the parameter count stays the same.

What is the difference between Pre-LN and Post-LN transformer blocks?

Post-LN applies Layer Normalisation after each residual addition, the original arrangement, which tends to need a learning-rate warm-up to train stably. Pre-LN normalises the input of each sublayer before attention or the MLP and leaves the residual path untouched, which the lectures present as easier to optimise.

What does the MLP inside a transformer block do?

Attention mixes information across tokens but applies no non-linearity to each token's features beyond the softmax weighting. The row-wise MLP, usually with a hidden width about four times the model width, transforms each token's features independently, giving the block the per-token processing that attention alone lacks.

Study strategy

Exam move

Practise one full attention row by hand with small vectors, writing raw scores, scaled scores, exponentials, weights and output on separate lines, and check that the weights sum to one. Then track shapes for a whole layer: X, Q, K, the score table, the output and the multi-head concatenation. Use published model sizes to compute per-head widths.

Finish by explaining permutation equivariance in two sentences, because it sets up the need for positional encodings. Add Layer Norm to your shape practice: compute the mean and standard deviation of one token's features, normalise it, and compare with batch normalisation, which would instead use one feature across many tokens or samples.

Working through Attention and the Transformer Block in ECE5179? Sia is AskSia’s AI Computer Science tutor — ask any ECE5179 Attention and the Transformer Block question and get a clear, step-by-step explanation grounded in how ECE5179 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 161 of your Monash University subjects - and 1,000+ Bibles across every Australian university.
Sia - your ECE5179 tutor, unlimited, worked the way the exam marks it
The full 5-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
ECE5179 · Neural networks and deep learning - independent study guide on the AskSia Library. More Monash University subjects · Microeconomics across all universities