RNNs and LSTMs
Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14
50 cards
Introduction
- How do recurrent neural networks represent time differently from feedforward and transformer networks?They process the sequence one item at a time and carry the prior context in recurrent connections, without a fixed-size window, so decisions can depend on information from hundreds of words back.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, Introduction, p. 302
14.1 Recurrent neural networks
- What is a recurrent neural network (RNN), and what is an Elman network?Any network with a cycle in its connections, so some unit’s value depends (directly or indirectly) on its own earlier outputs. Elman networks (simple recurrent networks) are a constrained, effective class in which the hidden layer receives its own previous value as extra input.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 302
- What does the recurrent link in an Elman network provide?The previous time step’s hidden layer is added to the hidden layer’s input, acting as memory or context of earlier processing, with no fixed length limit: it can carry information back to the sequence start.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 303
- What new set of weights does an RNN add compared with a feedforward network?\(U\), connecting the previous hidden layer \(h_{t-1}\) to the current hidden layer; like the other weights, it is trained by backpropagation.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 303
- Write the equations of a simple RNN.\(h_t = g(Uh_{t-1} + Wx_t)\) and \(y_t = f(Vh_t)\), usually \(y_t = \text{softmax}(Vh_t)\), with \(W\) \([d_h \times d_{in}]\), \(U\) \([d_h \times d_h]\), \(V\) \([d_{out} \times d_h]\).
Unrolled in time: the same U, V, W are shared at every step. - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 304
- Why must RNN inference proceed incrementally from start to end?Computing the hidden layer at time \(t\) requires the hidden layer from time \(t-1\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 304
- What does it mean to unroll an RNN in time?Copy the network’s layers for each time step to show their different values over time, while the weight matrices \(U\), \(V\), \(W\) are shared across all steps.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 304
- What two complications does training an RNN add over a feedforward network?The loss at time \(t\) depends on the hidden layer at \(t-1\), and the hidden layer at \(t\) affects both the output at \(t\) and all later hidden layers and losses; its error must account for both.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, pp. 304–305
- What is backpropagation through time (BPTT)?A two-pass algorithm: a forward pass computing and saving hidden states and accumulating the loss, then a backward pass over the sequence in reverse computing gradients and error terms for each step.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 305
- How are RNNs trained in practice with modern frameworks, and what is done for very long inputs?Unroll the network for the given input into a feedforward computation graph and train with ordinary backpropagation. For very long inputs (speech, character streams), unroll fixed-length segments treated as separate training items.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.1, p. 305
14.2 RNNs as language models
- What advantage do RNN language models have over n-gram and feedforward LMs?No limited or fixed context window: the hidden state can in principle represent information about all preceding words back to the start of the sequence.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.2, p. 306
- Write the forward computation of an RNN language model.\(e_t = Ex_t\) (embedding of the one-hot input), \(h_t = g(Uh_{t-1} + We_t)\), \(\hat{y}_t = \text{softmax}(Vh_t)\); \(P(w_{t+1} = k \mid w_{1:t}) = \hat{y}_t[k]\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.2, pp. 306–307
- How is the probability of a whole sequence computed with an RNN LM?The product of the probabilities of each true word: \(P(w_{1:n}) = \prod_i \hat{y}_i[w_i]\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.2, p. 307
- Why is RNN language-model training called self-supervised?The text supplies its own labels: at each step the model is trained (with cross-entropy) to predict the true next word, with no added gold annotation.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.2, p. 307
- What is teacher forcing in training an RNN LM?At each step the model is given the correct previous word (with the hidden state) rather than its own prediction from the previous step.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.2, p. 308
- What is weight tying in an RNN language model, and what does it gain?Using the embedding matrix \(E\) both at the input and, transposed, in place of the output matrix \(V\): \(\hat{y}_t = \text{softmax}(E^\top h_t)\). Both encode word embeddings; tying improves perplexity and greatly reduces parameters.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.2, p. 308
14.3 RNNs for other NLP tasks
- How is an RNN used for sequence labeling, e.g. part-of-speech tagging?Input word embeddings; at each step a softmax over the tagset gives tag probabilities; pick the most likely tag per step; train with cross-entropy.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.3, p. 309
- How is an RNN used for sequence classification?Run the text through the RNN and pass the last hidden state \(h_n\), a compressed representation of the whole sequence, to a feedforward network with a softmax over classes. Only the final classification produces a loss.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.3, p. 310
- What is end-to-end training?Using the loss from the downstream task to adjust weights all the way through the network (classifier and RNN).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.3, p. 311
- What alternative to the last hidden state can represent a sequence for classification?Pooling all hidden states, e.g. the element-wise mean \(\frac{1}{n}\sum_i h_i\) or element-wise max.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.3, p. 311
- How does an RNN LM generate text autoregressively?Feed
<s>, sample a word from the softmax, feed that word’s embedding as the next input, sample again, until</s>or a length limit.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.3, pp. 311–312
- How is autoregressive generation adapted to tasks like translation or summarization?Prime the generator with a task-appropriate context instead of just
<s>: the source sentence for translation, the long text for summarization.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.3, p. 312
14.4 Stacked and bidirectional RNNs
- What is a stacked RNN, and why does it help?Multiple RNNs where the output sequence of one is the input of the next. Layers induce representations at different levels of abstraction (like early vision detecting edges, later stages shapes), but training cost rises quickly with depth.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.4, p. 312
- What is a bidirectional RNN?Two independent RNNs, one reading left to right and one right to left; at each step their hidden states are concatenated, \(h_t = [h^f_t; h^b_t]\), capturing both left and right context.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.4, p. 313
- Why are bidirectional RNNs useful for sequence classification?A forward RNN’s final state reflects the end of the sentence more than its beginning; combining the final states of the forward and backward passes represents the whole sequence more evenly.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.4, p. 314
14.5 The LSTM
- Why is “The flights the airline was canceling were full” hard for an RNN LM?Predicting were requires remembering the distant plural flights, while the closer singular airline intervenes; RNN hidden states tend to encode mostly local information.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, pp. 314–315
- What two problems make simple RNNs bad at using distant information?The hidden layer must both provide information for the current decision and carry forward information for future ones; and backpropagation through time multiplies gradients repeatedly, driving them toward zero (vanishing gradients).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 315
- How do LSTMs manage context?Long short-term memory networks (Hochreiter & Schmidhuber, 1997) add an explicit context (cell) layer and learn, via gates, to remove information no longer needed and add information likely to be needed later.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 315
- What is the common design of an LSTM gate, and why the sigmoid?A feedforward layer, then a sigmoid, then element-wise multiplication with the layer being gated. The sigmoid pushes values toward 0 or 1, so the product acts like a binary mask: values passed nearly unchanged or erased.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 316
- What does the forget gate do? Write its equations.It deletes information from the context that is no longer needed: \(f_t = \sigma(U_fh_{t-1} + W_fx_t)\), \(k_t = c_{t-1} \odot f_t\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 316
- What is the Hadamard product?Element-wise multiplication of two vectors of the same dimension, written \(\odot\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 316
- How does an LSTM add new information to its context?Compute candidate information \(g_t = \tanh(U_gh_{t-1} + W_gx_t)\), mask it with the add (input) gate \(i_t = \sigma(U_ih_{t-1} + W_ix_t)\), \(j_t = g_t \odot i_t\), and add it to the filtered context: \(c_t = j_t + k_t\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 316
- What does the output gate do?Decides what information the current hidden state needs (as opposed to what to preserve for the future): \(o_t = \sigma(U_oh_{t-1} + W_ox_t)\), \(h_t = o_t \odot \tanh(c_t)\).
Forget (×), add (i ⊙ g, +), then output gate on tanh(c). - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 316
- What are the inputs and outputs of an LSTM unit?Inputs: current input \(x_t\), previous hidden state \(h_{t-1}\), previous context \(c_{t-1}\). Outputs: new hidden state \(h_t\) (the unit’s output) and updated context \(c_t\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, pp. 316–317
- Why is the LSTM’s complexity not a problem for building architectures?It is encapsulated within the unit: externally an LSTM differs from a simple recurrent unit only by the extra context vector, so LSTM (or GRU) units can be swapped into any RNN architecture (stacked, bidirectional) and trained by unrolling.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 317
- Which recurrent unit became standard in practice?LSTMs (rather than simple RNNs) for any modern system using recurrent networks.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.5, p. 318
14.6 Common RNN architectures
- Summarize the four common RNN architectures for NLP tasks.Sequence labeling (one output per input token, e.g. POS); sequence classification (one output from the end of the sequence, e.g. sentiment); language modeling (predict the next token at each step); encoder-decoder (map an input sequence via a context to an output sequence of different length).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.6, p. 318
14.7 Encoder-decoder with RNNs
- When is an encoder-decoder (sequence-to-sequence) model needed instead of sequence labeling?When input and output sequences can differ in length and the mapping between their tokens is indirect, as in machine translation; it is also used for summarization, question answering and dialogue.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.7, p. 319
- What are the three conceptual components of an encoder-decoder network?An encoder producing contextualized representations \(h_{1:n}\) of the input; a context vector \(c\), a function of \(h_{1:n}\) conveying the essence of the input; and a decoder generating an arbitrary-length sequence of hidden states and outputs from \(c\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.7, pp. 319–320
- How can an RNN language model be turned into a basic translation model?Concatenate the source sentence, a separator token
<s>, and the target sentence, and model \(p(y \mid x) = \prod_t p(y_t \mid y_{1:t-1}, x)\): after reading the source, generate the target autoregressively.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.7, p. 320
- In the basic RNN encoder-decoder, how is the context passed to the decoder?The encoder’s final hidden state \(c = h^e_n\) initializes the decoder (\(h^d_0 = c\)) and is also given to every decoder step, so its influence doesn’t wane: \(h^d_t = g(\hat{y}_{t-1}, h^d_{t-1}, c)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.7, pp. 321–322
- Why is teacher forcing used when training an encoder-decoder?At inference the decoder feeds its own outputs back and drifts from the gold target; in training, feeding the gold token as the next input instead speeds up learning.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.7, p. 322
14.8 Attention
- What is the bottleneck problem of the basic encoder-decoder?The final encoder hidden state must represent everything about the source sentence, since it is all the decoder sees; information from the beginning of long sentences may be poorly represented.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.8, p. 323
- How does the attention mechanism solve the bottleneck?It lets the decoder use all encoder hidden states: at each decoding step \(i\), a new context vector \(c_i\) is computed as a weighted average of the encoder states, with weights depending on the decoder’s previous state; then \(h^d_i = g(\hat{y}_{i-1}, h^d_{i-1}, c_i)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.8, p. 323
- How is the attention context vector computed with dot-product attention?Score each encoder state by similarity to the previous decoder state, \(\text{score} = h^d_{i-1} \cdot h^e_j\); softmax the scores into weights \(\alpha_{ij}\); take \(c_i = \sum_j \alpha_{ij}h^e_j\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.8, p. 324
- What is a more powerful alternative to dot-product attention scoring?A bilinear score with learned weights, \(\text{score} = h^d_{i-1}W_sh^e_j\), which learns which aspects of similarity matter and allows encoder and decoder states of different dimensionality.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, §14.8, p. 325
Historical notes
- Where were RNNs investigated in the 1980s, and with what aim?In the Parallel Distributed Processing (PDP) group at UC San Diego, mostly for human cognitive modeling; Elman (1990) introduced recurrence at the hidden layer, Jordan (1986) from the output layer.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, Historical notes, p. 326
- What changed the practical prospects of RNNs?LSTMs (Hochreiter & Schmidhuber, 1997; Gers et al., 2000), which brought big gains in phoneme recognition, handwriting recognition and, most significantly, speech recognition.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, Historical notes, p. 326
- What did Collobert and Weston’s work (2008, 2011) demonstrate?Near state-of-the-art performance on POS tagging, chunking, NER and semantic role labeling using learned word embeddings, convolutional networks and end-to-end training, without hand-engineered features.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, Historical notes, p. 326
- Who coined “encoder-decoder”, and where did attention come from?Cho et al. (2014) coined the name (with Sutskever et al. 2014 using RNNs for both parts). Attention, a soft weighting over inputs for a generative decoder, was first developed by Graves (2013) for handwriting; Bahdanau et al. (2015) named it attention and applied it to MT.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 14, Historical notes, p. 327
Source
- Daniel Jurafsky and James H. Martin. Speech and Language Processing. 3rd edition (draft). https://web.stanford.edu/~jurafsky/slp3/