liveBook · Manning

Nn.GRUCell

As explained in Inside Deep Learning: Math, Algorithms, Models

A PyTorch recurrent-cell module that processes one time step at a time and maintains a hidden state.

nn.GRUCell is a PyTorch recurrent-cell module for advancing an RNN one time step at a time. Each call receives the current input and a previous hidden state, then returns the updated hidden state. Unlike nn.GRU, it does not consume an entire sequence in one operation, so the surrounding code must manage the time-step loop and hidden states. section 7.5.1 section 11.4.1

Use the cell when explicit sequence control matters:

  • Process inputs incrementally when the complete sequence is unavailable or has unknown length; accept slower execution than the normal nn.GRU path. section 7.5.1
  • Maintain one hidden-state tensor per layer and update it after every call. A typical stacked design uses nn.ModuleList, with the first cell mapping embedding dimensions to hidden dimensions and later cells mapping hidden dimensions to hidden dimensions. section 7.5.1
  • Feed each layer's output into the next layer, optionally apply LayerNorm, and pass the final hidden state to a prediction network. section 7.5.1

Manual time-step loop

A typical step function embeds token IDs when needed, initializes hidden states when absent, then updates every layer: h = self.norms[l](self.layers[l](x_in, h_prevs[l])); h_prevs[l] = h; x_in = h. The forward method can call this function for each t and stack the returned predictions across time. section 7.5.1

Autoregressive sampling

For generation, run the seed through the cell once, retain h_prevs, sample the next token, and call the cell again with the updated token and hidden states. This changes generation from repeatedly reprocessing the full prefix to one update per generated token. section 7.5.4

Variable-length sequence-to-sequence decoding

Use nn.GRU for an encoder when the input is available at once, but use stacked nn.GRUCell layers for a decoder that produces tokens until an end condition or max_decode_length. The decoder manually tracks hidden states and selects the next token at each step. section 11.4.1

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains construction, stacked-layer state management, explicit loops, normalization, and sampling, but does not derive GRU gate equations.
Examples
Demonstrates character-level text generation, incremental sampling, stacked cells, and variable-length sequence-to-sequence decoding.
What it assumes
Assumes basic PyTorch modules, embeddings, tensor shapes, recurrent layers, and hidden states while demonstrating state initialization from zero.
What you can do after
Build it from scratch by managing inputs, hidden states, layer loops, decoding limits, and per-step predictions.
How it connects
Connects recurrent neural networks with embeddings, hidden states, LayerNorm, autoregressive models, sequence-to-sequence models, and attention.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.