liveBook · Manning

State

As explained in Natural Language Processing in Action, Second Edition

A hidden state vector is the numerical state carried through a recurrent network to summarize information from earlier sequence inputs.

A recurrent neural network reads a sequence one token at a time. Its hidden state is a numerical vector passed from one step to the next: a running representation of what the network has processed so far. Unlike a feedforward layer that transforms an input vector once, the same recurrent computation uses the previous state alongside each new token. section 8.1.3

Think of the state as a relay baton that changes hands—and changes contents—at every token. An RNN can produce an output after any step, which makes token-level predictions possible. The vector is not a readable transcript or a guarantee that every earlier detail survives; inspect its values and predictions to see what a particular model retains. section 8.1.4 section 8.2.3

In a small PyTorch RNN, state moves through the sequence as follows:

  • Choose the hidden-vector size and initialize a state before a new text; an all-zero vector is one starting point. section 8.2.1 section 8.3.1
  • For each token, concatenate its input vector with the current hidden vector. Compute a new hidden vector and an output, then pass the new vector to the next step. section 8.1.3
  • During training, propagate prediction error backward through the token steps. Each step depends on the preceding state, so this work cannot be collapsed into one independent calculation per token. section 8.3
Listing 8.1 Recurrence in PyTorch
>>> from torch import nn

>>> class RNN(nn.Module):
...
...     def __init__(self,
...             vocab_size, hidden_size, output_size):     #1
...         super().__init__()
...         self.W_c2h = nn.Linear(
...             vocab_size + hidden_size, hidden_size)     #2
...         self.W_c2y = nn.Linear(vocab_size + hidden_size, output_size)
...         self.softmax = nn.LogSoftmax(dim=1)
...
...     def forward(self, x, hidden):                      #3
...         combined = torch.cat((x, hidden), axis=1)   #4
...         hidden = self.W_c2h(combined)            #5
...         y = self.W_c2y(combined)         #6
...         y = self.softmax(y)
...         return y, hidden    #7

The forward method shows the handoff directly: it accepts `x` and `hidden`, concatenates them, and returns both an output and the next `hidden`. The output in this implementation is calculated from the same concatenated input, not from the newly computed hidden vector. section 8.1.3

Watch a prediction change

For a character-by-character surname classifier, reset the hidden layer and run successively longer prefixes through the model. A `predict_hidden` helper exposes the 128-dimensional vector after each prefix. For “Khalid,” the displayed category changes as characters arrive; a table of the six hidden vectors lets you inspect the accompanying numerical changes. This is evidence of an evolving representation, not a plain-language explanation of any individual dimension. section 8.2.3

An LSTM distinguishes its hidden state, associated with recently processed tokens, from a cell state that carries longer-term information; gates regulate how those states change. In chatbot design, “state” can instead mean a node in a conversation graph, with conversation context used to decide what happens next. That design state is not the RNN’s hidden vector. section 8.4.3 section 12.3.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through state initialization, a PyTorch forward pass, prefix-level inspection, and backpropagation through time.
Examples
Uses a PyTorch RNN and a surname classifier whose hidden vectors and nationality predictions change across character prefixes.
What it assumes
Builds recurrence from feedforward vector transformations while using Python, PyTorch tensors, and token vectors for implementation.
What you can do after
Build it from scratch in PyTorch and inspect how its hidden vector changes during character-by-character classification.
How it connects
Links recurrent neural networks to backpropagation through time and distinguishes LSTM hidden state from cell state.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.