Natural Language Processing · Advanced level

Advanced level · Module 2: Recurrent Neural Networks for Language · Lesson 3 of 3

Language Models, Generation and Vanishing Gradients

Learners train a small character-level language model on public-domain text, generate text with temperature sampling, measure perplexity and observe why plain RNNs forget long contexts.

In this lesson you will

  • Train and sample from a character language model
  • Explain perplexity and vanishing gradients

Key concepts

language modelnext-token predictionperplexitytemperature samplingvanishing gradients

The lesson

A language model predicts the next token from the previous ones. Trained on characters, it learns spelling and common words. Perplexity, the exponential of the average cross-entropy, measures how surprised the model is by held-out text: lower is better.

Generation samples one token at a time. Temperature controls randomness: low temperature repeats safe patterns, high temperature produces creative nonsense.

Gradients shrink as they travel back through many steps, so plain RNNs struggle to learn dependencies across long texts. This vanishing-gradient problem motivates LSTMs in the next module.

Step by step

  1. Train a character RNN language model on a public-domain English text.
  2. Report validation perplexity.
  3. Generate text at temperatures 0.5, 1.0 and 1.5 and compare.
  4. Test whether the model can close a bracket opened 100 characters earlier.

Practice activity

Train a small language model, report perplexity and generated samples, and demonstrate the long-range memory problem.

Expected result: A model with perplexity, samples at three temperatures and a clear demonstration of forgetting.

Check your understanding

  1. What does perplexity measure?

    Show answer

    How well a language model predicts held-out text; lower means better prediction.

  2. What problem makes plain RNNs forget long contexts?

    Show answer

    Vanishing gradients.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.