liveBook · Manning

Recurrent neural network

As explained in Learn Generative AI with PyTorch

A neural network architecture for sequential data that processes inputs with loops so information can persist across time steps.

A recurrent neural network (RNN) processes a sequence one element at a time. Unlike a network that treats inputs independently, it carries a hidden state forward through a loop. That state summarizes earlier inputs, letting the current prediction depend on what came before. This makes RNNs useful for ordered data such as text, music, and stock prices. section 8.1.2

Predicting the next word

Consider “a frog has four legs.” After reading “a,” an RNN predicts the next word. At the next step, it uses “frog” and the hidden state carried from the previous step. By the time it predicts “legs,” its state reflects the preceding words. Order matters: swapping the teams in “Kentucky defeated Vanderbilt” reverses who won, even though the words are otherwise unchanged. section 8.1.2 section 8.1.1

Figure 8.1 How a layer of recurrent neurons unfolds through time. When a recurrent neural network makes a prediction on sequential data, it takes the hidden state from the previous time step, h(t – 1), along with the input at the current time step, x(t), and generates the output, y(t), and the updated hidden state, h(t). The hidden state at time step t captures the information in all previous time steps, x(0), x(1), …, x(t).

The unfolded diagram shows the repeated step: current input x(t) and previous hidden state h(t − 1) produce an output y(t) and updated state h(t). section 8.1.2

For a text generator, connect the recurrent model to the surrounding text pipeline:

  • Tokenize the text into characters, words, or subwords, then represent words numerically with embeddings. section 8.6
  • Build and train an LSTM, an improved RNN variant, to generate text. section 8.0 section 8.1
  • Adjust generated text with temperature or top-K sampling: these change how predictable or varied the next-word choices are. section 8.6

The hidden state helps preserve context, but a standard RNN struggles with long-range dependencies because of vanishing gradients. LSTMs handle longer ranges better, though even they can falter over extremely long distances. RNNs also process positions sequentially, limiting training parallelism. Transformers offer a contrasting approach: self-attention captures distant relationships while allowing parallel training. section 8.1.2 section 1.3

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains hidden-state updates through a time-step example and diagram, then identifies long-range and training-speed limitations without deriving the update equations.
Examples
Works through next-word prediction for “a frog has four legs” and uses a reversed football result to show why word order matters.
What it assumes
Defines recurrence through a comparison with ordinary neural networks and introduces tokenization and embeddings for text inputs.
What you can do after
Build and train an LSTM-based text generator, then control its output with temperature and top-K sampling.
How it connects
Connects RNNs to LSTMs, tokenization, word embeddings, vanishing gradients, and Transformer self-attention.

Also explained in

Compare how 10 books explain recurrent neural network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.