April 7, 2026
\[ \newcommand\hbb{{\hat{\boldsymbol \beta}}} \newcommand\bb{{\boldsymbol \beta}} \newcommand\expn{{\frac{1}{N} \sum \limits_{i = 1}^N}} \newcommand\sumk{\sum \limits_{k = 1}^K} \newcommand\argminb{\underset{\bb}{\text{argmin }}} \newcommand\argmaxb{\underset{\bb}{\text{argmax }}} \newcommand\gtheta{\mathbf g(\boldsymbol \theta)} \newcommand\htheta{\mathbf H(\boldsymbol \theta)} \]
Seq2Seq models are RNNs that convert an input sequence to output sequences
We’re going to restrict our attention to the unaligned variant
The input sequence and the output sequence are not necessarily of the same length and may not have any direct one-to-one correspondence
Most commonly seen as question and answer or translation
My screen is blank. \(\rightarrow\) Please check if the computer is plugged in.
Bless your little heart \(\rightarrow\) You are sorely mistaken
The “simple” model:
In the training case
At test time
In either case, the model (after embedding and decoding) returns a prediction of what word comes next in the decoder
Usually a probability vector over words in the vocabulary
Sample from this distribution to get the next token!
The “simple” model:
The hidden state updates as we move through the sequence
The hidden state of the encoder is used as the input for the decoder
Remember the issue with this from last time?
The decoder may do better if it is allowed to look at all hidden states in the encoder instead of just looking at the final state
All decoder hidden values carry information
Every output token is related to the input directly
Instead, allow the decoder to use the encoder with attention
At \(t = 1\) of the decoder:
Start with the input embedding for <sos>
Using the previous decoder hidden state, \(\mathbf s_0\), find the dot product between the previous hidden state and all encoder hidden states
Convert these dot products to attention weights
Make the context vector a convex combination of all encoder hidden states weighted by the attention weights
Proceed like a RNN
Attention allows the decoder to find relevant parts of the input for determining what should come next in the decoder.
Input:
My computer won’t turn on
Output:
Is…
[it,she,there,…]
After Is:
My computer won’t turn on
Output:
Is…
[it,she,there,…]
With attention, we’re allowed to update context in the decoder!
A step forward from “simple” seq2seq models
Problems:
Slow - each update must be done iteratively
Very deep - unrolling this entire process into a feedforward style model shows that the number of layers is really high; one for each input and output token
Relatively poor memory - the encoder still requires only looking one step back and can lose older information
A warning: this is all going to move relatively quickly
Just keep in mind that autodiff will be able to backprop through everything here
Move towards a machine that understands(?) the complexities of human language/images
Given the attention setup, do we need our encoder to be recurrent?
The decoder looks back at every step each time
Does it matter if we understand the sequential nature of the input?
Yes:
Context of the input is sequential - different words make sense in the context of other input words
Need to know that “Little” in “Bless Your Little Heart” is pejorative instead of a descriptor!
No:
Recurrence is only needed to find the hidden states of the encoder!
Allow the encoder to develop context of the input by looking at all other words in the input
In words:
For each word in the input, compute query-key-value sets (linear transformations of the input embeddings)
For each word \(i\):
Compute the dot product similarity between the query for \(i\) and all keys \(j \in T_e\) - \(\mathbf q_i^T \mathbf k_j\)
Softmax these similarities to get attention weights for each words - \(\mathbf w_i = [w_{i1},w_{i2},...,w_{iT_e}]\)
Compute the output, \(\mathbf o_i\), as the weighted combination of each \(v_j\) and the corresponding attention weight \(w_{ij}\)
The encoder looks forwards and backwards to see which words of the input correspond to each input word!
Each self-attention operation will correspond to one notion of context:
But, there are often layers of context:
Allow each layer of context to be uncovered using multiple self-attention operators
Multiheaded Self Attention: