Recurrence repeatedly reuses a computation’s state or output while processing a sequence. In an RNN, each token updates a hidden state that is carried to the next token, allowing the model to use information from the preceding sequence rather than only a fixed local window. section 8.0 section 8.1.3
A recurrence loop works through a small set of practical steps:
- Read one token and combine its vector with the previous hidden state. section 8.1.3
- Transform the combined representation into a new hidden state and an output or prediction. section 8.1.3
- Pass the new hidden state into the same computation for the next token. section 8.1.3
- Provide an exit or full-stop token when generation must end; otherwise, the loop can continue indefinitely. section 8.1.3
Minimal recurrent computation
A minimal PyTorch RNN stores the hidden state separately, concatenates it with the current input, applies learned transformations, and returns both the prediction and updated state: `combined = torch.cat((x, hidden), axis=1)`, followed by `return y, hidden`. This implements recurrence without a recursive function call or PyTorch’s RNNBase class. section 8.1.3
Unrolling recurrence turns the loop into a sequential chain of computations. Unlike recurrence inside RNNs, LSTMs, and GRUs, autoregressive transformers recycle a final output into the next input without recurrent neurons; their internal layers can run in parallel. section 9.1 section 8.1.3
Figure 8.6 A neuron with recurrence
Use the recurrence-loop figure to visualize how a neuron passes its hidden state to its future self. section 8.1.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
Use the PyTorch listing to inspect a from-scratch recurrence implementation with input-state combination, state updates, output prediction, and return values. section 8.1.3


