UUtah CS 6340 NLP | Fall 2026 | seq2seq & attention
Overview
The lecture derives dot-product attention for RNN encoder-decoder sequence-to-sequence models, showing how decoder states use softmax-normalized scores over every source position to build a step-specific context vector. It connects attention to translation alignments, explains the training objective and gradient flow, and frames attention as a core idea behind Transformers while warning that attention visualizations are not causal explanations.
Key takeaways
- Dot-product attention replaces one fixed encoder summary with a separate source context for every decoder step, reducing the information bottleneck in long-input sequence-to-sequence tasks.
- For decoder position i, softmax over the dot products hᵗᵢ · hˢⱼ produces weights across all L source positions, and the weighted sum of hˢⱼ forms context cᵢ.
- The encoder and decoder are trained end to end: decoder initialization and attention both provide gradient paths back to source-token representations.
- Teacher forcing supplies human-written target tokens during training, while inference feeds generated tokens back into the decoder.
- Attention maps can reveal learned source-target correspondence patterns, such as diagonal translation alignments, but their weights alone do not prove what caused a model output.
- Attention generalizes beyond translation as a query-dependent weighted sum over value vectors, making it a central building block for Transformer architectures.
Chapters
- The lecture reviews RNN language modeling and sequence-to-sequence generation, where a prompt or source sequence conditions generated output.
- Transformer models use an encoder-decoder structure related to the RNN encoder-decoder architecture studied in earlier lectures.
- Attention is introduced as a foundational idea for understanding Transformers, not as the exact attention mechanism used in the Transformer.
- A conventional encoder compresses the entire source sequence into one final vector that conditions every decoder step.
- That single-vector bottleneck is especially problematic for long inputs, such as 100 documents with roughly 1,000 tokens each.
- Attention lets each decoder position revisit source representations and emphasize the parts relevant to its next-token prediction.
- The encoder produces one hidden representation for each source token and does not perform next-token prediction.
- At a decoder step, its current hidden state is dot-producted with every source hidden state to produce source-position relevance scores.
- Softmax normalizes the scores into weights that sum to one, and their weighted sum forms a source context specialized for that decoder step.
- The context vector is concatenated with the decoder hidden state before an output projection and vocabulary softmax predict the next token.
- An attention map places source tokens against target tokens and displays the learned attention weights for each target position.
- Strong diagonal patterns can reflect roughly word-by-word translation, while off-diagonal weights can indicate phrase-level correspondences.
- The weights are learned without explicitly supplied word alignments, but attention maps do not establish causality or reliably explain model decisions.
- A training example contains a tokenized source sequence of length L and a target sequence of length K; the lengths need not match.
- The source RNN begins with a zero hidden state and computes one hidden representation for each source token using an embedding, recurrent weights, bias, and nonlinearity.
- The final source hidden state initializes the decoder hidden state, connecting encoder computation to the decoder.
- Encoder and decoder use distinct recurrent parameters, and the encoder needs no vocabulary softmax because it only builds source representations.
- For decoder position i and source position j, the attention score is the dot product of decoder state hᵗᵢ and source state hˢⱼ.
- The L scores for one decoder position are collected into a vector and passed through softmax to obtain normalized weights.
- Each normalized weight expresses the relative contribution of one source position to the current decoder step.
- The context vector cᵢ is the sum of source hidden states hˢⱼ weighted by their attention values for decoder position i.
- The decoder computes its ordinary recurrent state from the previous decoder state and current target-side input embedding.
- Concatenating that decoder state with cᵢ produces an enriched representation, which an output matrix projects to vocabulary logits before softmax.
- The model repeats the attention and prediction process at each decoder position.
- Encoder representations receive gradients through both decoder initialization and attention, so the encoder is trained jointly with the decoder.
- With teacher forcing, training supplies human-written target tokens as decoder inputs; at inference, previously generated tokens are used instead.
- For supervised translation, negative log-likelihood maximizes the probability of the human-written target sequence given its source sequence.
- At each position, the loss uses the softmax probability assigned to the correct vocabulary ID, then takes its negative logarithm; PyTorch loss functions differ in whether they expect logits or probabilities.
- The notation [A; B] denotes concatenating vectors, here combining the decoder state with its attention context; the tilde marks an enhanced hidden representation.
- Concatenation is one simple design choice: an MLP or other transformations could combine the context and decoder state differently.
- The output weight matrix maps the combined representation to vocabulary logits, with its dimensions determined by the vocabulary size and input representation width.
- Model parameters are initialized before training; Xavier initialization is discussed as a common approach and as a likely default for PyTorch linear layers.
- Attention emerged in 2015 and produced a major performance boost on neural machine translation, then a central NLP benchmark.
- The lecture credits attention with easing the encoder bottleneck and helping with vanishing gradients, while treating interpretability claims cautiously.
- More generally, attention takes a query and a set of value vectors, then computes a query-dependent weighted sum of those values.
- In the sequence-to-sequence example, the decoder state is the query and the source-token hidden states are the values.
- The context vector can be viewed as a fixed-size, selective summary of source representations, with the decoder state determining what to emphasize.
- The lecture positions attention as a key component for the upcoming Transformer material.
- Class ends with a planned five-minute break followed by a 15-minute quiz; the quiz itself is not included in this lecture segment.
Transcript excerpt
0:14 All right. Good afternoon everyone. Uh we are going to take quiz somewhere in the middle of the lecture. I will f first finish what I wanted to say last time. Then we are going to take the quiz and as always see how much time we have left and if there is more we are going to start finally talking about uh the transformer. Um there might be the case I said something that's wrong last time. Uh so thanks Priya is it? Um I I will send a clarification about that but uh yeah not not just yet. Okay. So just as a reminder last time we were talking about um you know last two lectures we were talking about using RNN's to do language modeling and then doing sequence to sequence which today we also
1:02 refer to just as language modeling uh although it's previously what we were referred to as conditional language model and sequence to sequence problems are basically um any any anytime you use CH GPD it's a sequence to sequence problem you give some prompt and you generate something uh from it. Um okay. So today I'm going to uh uh go over another foundational concept that's needed to kind of um understand where the ideas that make the transformer architecture which is still uh the standard architecture we use uh for large language model. We want to understand where these ideas came from. So you will see as we jump into transformer we are going to say immediately it's encoder decoder
1:49 architecture exactly like encoder decoder RNN that we are using to for seek to seek problems. So great we are already familiar with that uh idea uh and now I'm going to introduce attention and you will see that a lot of
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofU Data Science.