Long Short-Term Memory Models #
Higher‑level LSTM architectures built from module specs (Spec.Module.Chain), including:
- sequence‑to‑sequence outputs,
- classifier heads (many‑to‑one),
- multi‑layer compositions.
Cell equations are in NN/Spec/Layers/Lstm.lean; this file focuses on composing modules.
References (math + PyTorch behavior):
- Hochreiter and Schmidhuber (1997), "Long Short-Term Memory" (original LSTM): https://www.bioinf.jku.at/publications/older/2604.pdf
- PyTorch
nn.LSTMdocs: https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html - PyTorch
nn.LSTMCelldocs: https://pytorch.org/docs/stable/generated/torch.nn.LSTMCell.html
Model and gradient types #
The LSTM model layer exposes first-class model objects with:
- a forward pass,
- a standard training objective, and
- an explicit reverse-mode / BPTT backward pass producing parameter gradients.
The backward functions reuse the gate-aware implementation in NN.Spec.Layers.Lstm.
Gradient records #
Parameter gradients for Lstm.Model.
This bundles the LSTM cell gradients and the time-distributed linear head gradients.
- cell : LSTMGateGradients α inputSize hiddenSize
Gradients of the recurrent cell.
- output : LinearParameterGradients α hiddenSize outputSize
Gradients of the output projection.
Instances For
Parameter gradients for an LSTM classifier.
- cell : LSTMGateGradients α inputSize hiddenSize
Gradients of the recurrent cell.
- classifier : LinearParameterGradients α hiddenSize numClasses
Gradients of the classifier head.
Instances For
Sequence-to-sequence LSTM model as a Spec.Module.Chain: LSTM over time, then a per-timestep linear
head.
PyTorch analogue: nn.LSTM producing an output sequence, followed by nn.linear applied at each
time step.
Instances For
Many-to-one LSTM classifier as a Spec.Module.Chain.
This runs an LSTM over the sequence and applies a linear classifier head to the final hidden state.
PyTorch analogue: nn.LSTM + nn.linear, taking the last output/hidden.
Instances For
Two-layer LSTM stack (sequence-to-sequence), followed by a per-timestep linear head.
The second LSTM consumes the hidden stream produced by the first.
Instances For
Simple LSTM language-model pipeline as a Spec.Module.Chain: embedding, LSTM core, and output
projection.
In this spec layer we represent the embedding/projection as LinearSpecs (often used with one-hot
token vectors). PyTorch analogue: nn.Embedding (conceptually) + nn.LSTM + nn.linear.
Instances For
Bidirectional LSTM followed by a classifier on the final concatenated state.
Instances For
Bundle of parameters for a single-layer LSTM model with a linear output head.
This is a direct record representation (as opposed to the Spec.Module.Chain representation above).
- lstm : LSTMSpec α inputSize hiddenSize
Recurrent cell parameters.
- outputLayer : LinearSpec α hiddenSize outputSize
Linear output projection.
Instances For
Bundle of parameters for a multi-layer LSTM model with a linear output head.
The first layer consumes inputSize, and all subsequent layers consume hiddenSize.
- firstLayer : LSTMSpec α inputSize hiddenSize
First recurrent layer, whose input may differ from the hidden width.
- outputLayer : LinearSpec α hiddenSize outputSize
Linear output projection.
Instances For
Bundle of parameters for a many-to-one LSTM classifier.
The classifier head is applied to the final hidden state.
- lstm : LSTMSpec α inputSize hiddenSize
Recurrent cell parameters.
- classifier : LinearSpec α hiddenSize numClasses
Linear classifier head.
Instances For
Bundle of parameters for a many-to-many LSTM 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.
- lstm : LSTMSpec α hiddenSize hiddenSize
Recurrent cell parameters.
- outputProjection : LinearSpec α hiddenSize vocabularySize
Projection from hidden states to vocabulary logits.
Instances For
Bundle of parameters for a bidirectional LSTM model with an output head.
The head consumes the concatenation of forward and backward hidden states.
PyTorch analogue: nn.LSTM(..., bidirectional=true) plus a linear projection.
- forwardLstm : LSTMSpec α inputSize hiddenSize
Recurrent cell for the original sequence order.
- backwardLstm : LSTMSpec α 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 LSTM language model with deterministic dropout.
This model uses an array of LSTM layers (all with hiddenSize input/output) and applies an
evaluation-mode dropout step between the recurrent 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
One-step forward pass for Lstm.Model.
Given an input vector and the previous LSTM state (hidden, cell), compute (output, new_state).
PyTorch analogue: nn.LSTMCell step followed by a nn.linear head.
Instances For
Sequence forward pass for Lstm.Model.
Runs the LSTM over all timesteps (time-major), applies the output head to each hidden state, and
returns (outputs, final_state).
Instances For
Backward pass (BPTT) for the simple LSTM sequence model #
This is the model-level analogue of Spec.lstmSequenceBackwardSpec. The only extra work we do
here is to backprop through the per-timestep output projection and feed its gradient into the LSTM
sequence backward pass.
Backward pass for Lstm.Model.forwardSequence.
Returns:
- parameter gradients (
Lstm.Grads) - gradient w.r.t. input sequence (
dInputs) - gradient w.r.t. initial recurrent state (
dInitialState)
Instances For
MSE loss for the simple LSTM sequence model.
This runs Lstm.Model.forwardSequence and compares the predicted output sequence against
targets using mseSpec.
Instances For
Compute (loss, grads) for the simple LSTM sequence model under MSE.
This is the “full training API” building block: an optimizer (SGD/Adam) can consume these grads.
Instances For
Forward pass for an Lstm.Classifier (many-to-one).
This uses the final hidden state of the LSTM sequence as the classifier input.
Instances For
Backward for the classifier head (many-to-one) #
The classifier only consumes the final hidden state. We express that by feeding a gradient sequence that is zero everywhere except the last timestep.
Backward pass for an Lstm.Classifier (many-to-one).
This backprops through the classifier head, then runs an LSTM sequence backward pass where the hidden-state gradient is zero at all timesteps except the last.
Instances For
Forward pass for an Lstm.Generator (many-to-many).
This applies an embedding linear map to each token vector, runs the LSTM, and projects each hidden state back into vocabulary space.
Instances For
Forward pass for a bidirectional LSTM model (time-major).
This runs a forward LSTM on the sequence, a backward LSTM on the reversed sequence, concatenates the two hidden streams per timestep, and applies an output head.
Instances For
Forward pass for a Lstm.StackedModel.
This runs the first layer on the input sequence, then threads the resulting hidden stream through each additional hidden layer, and finally applies the output head per timestep.
Instances For
Instances For
Forward pass for Lstm.LanguageModel (teacher forcing, time-major).
This runs the embedding, then a stack of LSTM layers with provided initial states, applies
evaluation-mode dropout (dropoutInferenceSpec), and projects to vocabulary logits.
Instances For
Instances For
Package Lstm.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 Lstm.Classifier as an Spec.Module.
PyTorch analogue: nn.LSTM feeding a nn.linear classifier head.
Instances For
Package Lstm.BidirectionalModel as an Spec.Module.
PyTorch analogue: nn.LSTM(..., bidirectional=true) feeding a per-timestep linear head.