Gated Recurrent Models #
TorchLean provides GRU layers/cells in NN.Spec.Layers.Gru. This file builds models on top of
that layer API: common compositions, heads, and a couple of end-to-end forward/backward routines.
Higher‑level GRU architectures built from module specs (Spec.Module.Chain):
- sequence‑to‑sequence outputs,
- classifier heads (many‑to‑one),
- multi‑layer compositions.
GRU cell equations are in NN/Spec/Layers/Gru.lean; this file is primarily “wiring”.
References:
- Cho et al. (2014), "Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation" (introduces GRU): https://arxiv.org/abs/1406.1078
- Chung et al. (2014), "Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling" (GRU variants/ablation): https://arxiv.org/abs/1412.3555
- PyTorch
nn.GRUCelldocs: https://docs.pytorch.org/docs/stable/generated/torch.nn.GRUCell.html - PyTorch
nn.GRUdocs: https://pytorch.org/docs/stable/generated/torch.nn.GRU.html
The wiring matches torch.nn.GRU with linear heads and pooling over time (e.g. last hidden
state for classification), and each definition below names its wiring analogue. The comparison is
architectural only. The recurrent core is the original Cho reset-before GRU (GRUSpec), while
PyTorch uses the reset-after candidate equation (GRUResetAfterSpec). For general recurrent
matrices these are different function families, so PyTorch GRU weights cannot be converted into
these models.
A sequence-to-sequence GRU model, written as a Spec.Module.Chain.
Pipeline:
GRU(seqLen, inputSize → hiddenSize) then Linear applied at each timestep.
Wiring analogue: nn.GRU(..., batch_first=False) followed by an nn.linear on the output sequence.
Instances For
A many-to-one GRU classifier (use the last hidden state, then a linear head).
Wiring analogue: run nn.GRU over the sequence and feed the last output/hidden state into
nn.Linear(hiddenSize, numClasses).
Instances For
A recurrent stack of arbitrary depth and widths, followed by a per-timestep linear head.
Instances For
A simple GRU language-model style pipeline:
Linear as the embedding/projection map, then GRU, then a per-timestep projection back to
vocabularySize.
Wiring analogue: embedding (often nn.Embedding), nn.GRU, and nn.Linear(hiddenSize, vocabularySize). We use LinearSpec here as a spec-friendly stand-in for a one-hot embedding
matrix.
Instances For
Record-style model specs #
The Spec.Module.Chain builders above are the most uniform way to assemble models in TorchLean.
The declarations below use small record types with explicit forward functions. This is useful when you want to talk about a particular architecture directly (e.g. encoder-decoder), or when you need to carry extra per-model parameters (e.g. a dropout rate) without building a full module stack.
Bundle of parameters for a single-layer GRU model with a linear output head.
This is a direct record representation (as opposed to the Spec.Module.Chain representation above).
- gru : GRUSpec α inputSize hiddenSize
Recurrent cell parameters.
- outputLayer : LinearSpec α hiddenSize outputSize
Linear output projection.
Instances For
Gradients of the three GRU gates.
- resetWeight : TorchLean.Tensor α [hiddenSize, inputSize + hiddenSize]
Gradient of the reset-gate weight matrix.
- resetBias : TorchLean.Tensor α [hiddenSize]
Gradient of the reset-gate bias.
- updateWeight : TorchLean.Tensor α [hiddenSize, inputSize + hiddenSize]
Gradient of the update-gate weight matrix.
- updateBias : TorchLean.Tensor α [hiddenSize]
Gradient of the update-gate bias.
- candidateWeight : TorchLean.Tensor α [hiddenSize, inputSize + hiddenSize]
Gradient of the candidate-state weight matrix.
- candidateBias : TorchLean.Tensor α [hiddenSize]
Gradient of the candidate-state bias.
Instances For
Parameter gradients for a GRU model and its linear output head.
- cell : CellGrads α inputSize hiddenSize
Gradients of the recurrent cell.
- outputWeight : TorchLean.Tensor α [outputSize, hiddenSize]
Gradient of the output projection weight.
- outputBias : TorchLean.Tensor α [outputSize]
Gradient of the output projection bias.
Instances For
Recurrent cells with independently chosen widths and a linear output head.
The endpoint hiddenSize is the width of the last cell.
An empty stack has hiddenSize = inputSize.
- layers : RecurrentStack (GRUSpec α) inputSize hiddenSize
Layer dimensions determine the type of every intermediate hidden stream and state.
- outputLayer : LinearSpec α hiddenSize outputSize
Linear output projection.
Instances For
Run every layer with its own initial state, then project the final hidden stream.
The result retains all final states. Empty sequences leave each initial state unchanged, and an empty stack applies the head directly to the input sequence.
Instances For
Bundle of parameters for a many-to-one GRU classifier.
The classifier head is applied to the final hidden state.
- gru : GRUSpec α inputSize hiddenSize
Recurrent cell parameters.
- classifier : LinearSpec α hiddenSize numClasses
Linear classifier head.
Instances For
Bundle of parameters for a many-to-many GRU generator (language-model style).
This includes an (embedding) linear map, recurrent core, and output projection back to vocabulary.
- embedding : LinearSpec α vocabularySize hiddenSize
Token projection used by this one-hot specification.
- gru : GRUSpec α hiddenSize hiddenSize
Recurrent cell parameters.
- outputProjection : LinearSpec α hiddenSize vocabularySize
Projection from hidden states to vocabulary logits.
Instances For
Bundle of parameters for a bidirectional GRU model with an output head.
The head consumes the concatenation of forward and backward hidden states.
Wiring analogue: nn.GRU(..., bidirectional=true) plus a linear projection.
- forwardGru : GRUSpec α inputSize hiddenSize
Recurrent cell for the original sequence order.
- backwardGru : GRUSpec α inputSize hiddenSize
Recurrent cell for the reversed sequence order.
- outputLayer : LinearSpec α (hiddenSize + hiddenSize) outputSize
Projection from concatenated forward and backward states.
Instances For
Bundle of parameters for a stacked GRU language model with deterministic dropout.
This model uses an array of GRU layers (all with hiddenSize input/output) and applies
evaluation-mode dropout between the GRU stack and the output projection.
- embedding : LinearSpec α vocabularySize hiddenSize
Token projection used by this one-hot specification.
Recurrent layers, ordered from input to output.
- outputProjection : LinearSpec α hiddenSize vocabularySize
Projection from hidden states to vocabulary logits.
- dropoutRate : α
Dropout probability used between the recurrent stack and output projection.
Instances For
Bundle of parameters for a GRU encoder-decoder model (seq2seq).
This uses separate embeddings and GRU cores for encoder and decoder, plus an output projection.
Wiring analogue: an encoder nn.GRU and a decoder nn.GRU with teacher forcing.
- encoderEmbedding : LinearSpec α inputVocabSize hiddenSize
Source-token projection.
- encoderGru : GRUSpec α hiddenSize hiddenSize
Encoder recurrent cell.
- decoderEmbedding : LinearSpec α outputVocabSize hiddenSize
Target-token projection.
- decoderGru : GRUSpec α hiddenSize hiddenSize
Decoder recurrent cell.
- outputProjection : LinearSpec α hiddenSize outputVocabSize
Projection from decoder states to target-vocabulary logits.
Instances For
One-step forward for Gru.Model.
Input: (x_t, h_{t-1}). Output: (y_t, h_t).
Instances For
Sequence forward for Gru.Model (time-major).
Returns (outputs, final_hidden). Empty sequences preserve the initial hidden state.
Wiring analogue: run nn.GRU over the sequence, then apply nn.linear at each timestep.
Instances For
Forward pass for a Gru.Classifier (many-to-one).
This runs the GRU over the input sequence and applies the classifier head to the final hidden state.
Instances For
Forward pass for a Gru.Generator (many-to-many).
This applies an embedding linear map to each token vector, runs the GRU, and projects each hidden state back into vocabulary space. Empty sequences preserve the initial hidden state.
Instances For
Forward pass for a bidirectional GRU model (time-major).
This runs a forward GRU on the sequence, a backward GRU on the reversed sequence, concatenates the two hidden streams per timestep, and applies an output head.
Instances For
Forward pass for Gru.LanguageModel (teacher forcing, time-major).
This runs the embedding, then a stack of GRU layers with provided initial hiddens, applies
evaluation-mode dropout (dropoutInferenceSpec), and projects to vocabulary logits.
Instances For
Instances For
Encoder-decoder forward pass (GRU encoder + GRU decoder).
This is a small reference architecture:
- encode
sourceTokensinto a final hidden state, - decode
targetTokensstarting from that hidden state (teacher forcing), - project decoder states into output-vocabulary logits.
Wiring analogue: nn.GRU encoder + nn.GRU decoder with a linear output projection.
Instances For
Backward pass for Gru.Model using full backpropagation through time.
This assumes you already ran a forward pass that saved:
hiddenStates,- the GRU intermediates
resetGates,updateGates, andcandidates.
Those intermediates can be produced using Spec.gruExtractIntermediateValues from
NN.Spec.Layers.Gru. Supply the same initialHidden as the forward pass; omitting it retains
the zero-initial-state convention.
Instances For
Bundle of parameters for a residual GRU model.
This includes a projection from input space to hidden space so the input can be added as a residual to the GRU hidden stream.
- gru : GRUSpec α inputSize hiddenSize
Recurrent cell parameters.
- residualProjection : LinearSpec α inputSize hiddenSize
Projection that gives the input stream the recurrent hidden width.
- outputLayer : LinearSpec α hiddenSize outputSize
Linear output projection.
Instances For
Forward pass for Gru.ResidualModel.
This runs the GRU, adds a projected version of the input as a residual connection, and applies the
output head per timestep. The second result is the last residual feature
hiddenStates[last] + projectedInputs[last]. For a continuation chunk, retain the raw GRU hidden
state before this residual addition and use that as initialHidden.
Instances For
Package Gru.Model as a shape-indexed module.
The Python expression records the intended runtime analogue; forward remains the mathematical
meaning of the module.
Instances For
Package Gru.Classifier as an Spec.Module.
Wiring analogue: nn.GRU feeding a nn.linear classifier head.
Instances For
Package Gru.BidirectionalModel as an Spec.Module.
Wiring analogue: nn.GRU(..., bidirectional=true) feeding a per-timestep linear head.
Instances For
Package Gru.Generator as an Spec.Module.
Wiring analogue: GRU language model (nn.GRU + vocabulary projection) producing a sequence of
logits.