Mini-course · 14 questions
PreviewAttention Is All You Need: two weeks with the Transformer
Stay with one question long enough to think for yourself.
Inside the course
A glimpse of the questions.
Question 01
Day 1: The problem the paper set out to solve
Allow 10–15 minutes. No prior reading is needed; for more detail, see Sections 1 and 2 of the paper.
The idea
In 2017, the best machine-translation systems read a sentence with a recurrent neural network (RNN). An RNN keeps a hidden state \(h_t\), a vector that summarizes the words read so far, and updates it once per word:
\[ h_t = f(h_{t-1},\ x_t), \]
where \(x_t\) is the vector for word \(t\) and \(f\) is a learned function. An encoder RNN reads the source sentence. A decoder RNN then writes the translation one word at a time. Earlier work had already added an attention mechanism, which lets the decoder look back at all the encoder's states instead of a single summary vector.
The paper, "Attention Is All You Need" (Vaswani et al., 2017), asked a bolder question: what if the model used attention everywhere and dropped recurrence entirely? Its architecture, the Transformer, did exactly that. This course builds it up one idea at a time.
Your task
- Using only the update rule, explain why \(h_{50}\) cannot be computed before \(h_{49}\). What does this imply for training on a GPU, which is fast when it can do many independent operations at once?
- Information about word 1 must reach the state used at word \(n\). Counting each application of \(f\) as one step, how many steps does it pass through? Why might a long chain of steps make it hard for the network to learn a relationship between word 1 and word \(n\)? Think about what happens to a signal, or a gradient, that is transformed many times.
- In one or two sentences, state the two limitations of recurrence that a recurrence-free model would need to fix.
Question 02
Day 2: Attention as a soft lookup
Allow 10–15 minutes. No prior reading is needed; for more detail, see Section 3.2 of the paper.
The idea
Think of a dictionary: you present a query, find the matching key, and get back its value. Attention is a soft version of this. Given a query vector \(q\), keys \(k_1, \ldots, k_n\), and values \(v_1, \ldots, v_n\):
- Score each key by how well it matches the query: \(s_j = q \cdot k_j\).
- Turn the scores into weights with the softmax: \( \alpha_j = \dfrac{e^{s_j}}{\sum_{m} e^{s_m}} \).
- Return the weighted sum of the values: \( \text{output} = \sum_j \alpha_j v_j \).
Instead of picking one entry, attention blends all values, favoring those whose keys match best. In a sentence, every word can act as a query that gathers information from the other words.
Your task
- Show that every \(\alpha_j\) is positive and that the weights sum to 1. What does this say about where the output can lie relative to the values?
- Replace \(s_j\) with \(s_j / \tau\) for a "temperature" \(\tau > 0\). Describe the output as \(\tau \to 0\), assuming one score is strictly the largest, and as \(\tau \to \infty\).
- A hard lookup would return the value with the single best score. Why can't a network learn which keys to match by gradient descent if it uses a hard lookup? Why does the softmax fix this?
- Two keys match the query equally well and far better than the rest. What does the output become? Keep this in mind for Day 7.
Question 03
Day 3: Self-attention: every word queries every word
Allow 10–15 minutes. No prior reading is needed; for more detail, see Section 3.2.1 of the paper.
The idea
In self-attention, a sentence attends to itself. Stack the \(n\) word vectors as the rows of a matrix \(X\), with \(n\) rows and \(d\) columns. Each word produces a query, a key, and a value through three learned matrices:
\[ Q = XW^Q, \qquad K = XW^K, \qquad V = XW^V. \]
Row \(i\) of \(Q\) is word \(i\)'s query \(q_i\); similarly for \(K\) and \(V\). The paper writes all of attention as one formula:
\[ \mathrm{Attention}(Q,K,V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V, \]
where the softmax is applied to each row and \(d_k\) is the width of the queries and keys. Ignore the \(\sqrt{d_k}\) until tomorrow.
Your task
- Starting from the Day 2 recipe for one query \(q_i\), show that the formula's row \(i\) is exactly word \(i\)'s attention output. What does entry \((i, j)\) of \(QK^\top\) represent?
- Does computing row \(i\) of the output require row \(i-1\) first? Connect your answer to Day 1's first limitation.
- How many steps separate word 1 and word \(n\) in one self-attention layer? Connect this to Day 1's second limitation.
- Suppose there were no projections, so \(Q = K = X\), and every word vector had the same length. Use the Cauchy–Schwarz inequality to show that each word's highest score is with itself. Why is that a problem, and how do separate \(W^Q\) and \(W^K\) fix it?