nn.ModuleList groups variable numbers of PyTorch submodules while keeping them available for indexed iteration. It is useful when the forward pass must manually run a sequence, such as stacked GRUCell layers processing one time step at a time. section 7.5.1
A layered recurrent model uses ModuleList for both recurrent layers and their matching normalization layers:
- Create the first GRUCell with embedding-sized input, then create layers-1 additional cells with hidden_size input and output. section 7.5.1
- Create one LayerNorm per recurrent layer in a second ModuleList. section 7.5.1
- Loop over the lists by index, update each hidden state, normalize it, and pass the result to the next layer. section 7.5.1
Two-layer character autoregression
A character model can define `self.layers = nn.ModuleList([nn.GRUCell(embd_size, hidden_size)] + [nn.GRUCell(hidden_size, hidden_size) for _ in range(layers-1)])` and matching normalization layers. Its `step` method loops through `range(len(self.layers))`, updates each hidden state, and sends the final activation to `self.pred_class`. The model therefore supports a configurable number of recurrent layers while processing each character sequentially. section 7.5.1
ModuleList favors explicit control over convenience: cell-based processing is slower than whole-sequence RNN modules, but it accommodates inputs whose lengths are unknown or variable. section 7.5.1
