A gated recurrent unit (GRU) is a recurrent architecture that makes its hidden state serve as both short- and long-term memory. Compared with an LSTM, it uses less memory, runs faster, and is simpler to implement, although it may be less accurate. section 6.6.2 section 6.6.3
Choose the PyTorch form according to how the sequence is available:
- Use nn.GRU when all time steps are available together; it processes a sequence tensor in one operation and supports bidirectional encoding. section 11.4.1
- Use nn.GRUCell when inputs arrive one item at a time or output length is unknown; manually preserve hidden states and iterate over steps. section 7.5.1 section 11.4.1
Character-level autoregression
Embed each character, initialize one zero hidden state per GRU layer, and iterate through the sequence one character at a time. Feed every resulting hidden state through a prediction head to estimate the next character, then sum cross-entropy across time steps. Gradient clipping helps stabilize training.






