RNN/LSTM/GRU module wrappers #
The layer specs (NN/Spec/Layers/Rnn.lean, lstm.lean, gru.lean) expose step-level and
sequence-level recurrence definitions.
This file wraps the "sequence forward" functions as NNModuleSpecs so recurrent blocks can be
composed with other modules in a SpecChain.
Design choices:
- These wrappers are stateless modules: they pick a canonical initial hidden/state (all zeros).
This keeps
NNModuleSpecsimple (pureforward), and makes the semantics explicit. More stateful variants can always be built at the layer-spec level if needed. - The exported
forwardreturns the full output sequence (not just the final hidden state), matching common encoder usage.
If you think in PyTorch: these are the nn.RNN/nn.LSTM/nn.GRU "return the full output sequence"
wrappers, with the initial hidden/state fixed to zeros.
RNN sequence wrapper with a zero initial hidden state.
Instances For
LSTM sequence wrapper with a zero initial state; returns the output sequence.
Instances For
GRU sequence wrapper with a zero initial hidden state; returns the output sequence.
Instances For
Bidirectional LSTM wrapper (concatenates forward/backward features).
Instances For
Wrap rnn_cell_spec as an NNModuleSpec for a single timestep.
Input convention: we take a single vector [x; h] (concatenated input and previous hidden state),
so the module is shape-safe and easy to compose.
Instances For
Wrap lstm_cell_spec as an NNModuleSpec for a single timestep.
Input convention: a single concatenated vector [x; h; c] (input, previous hidden, previous cell).
Output convention: the concatenated new state [h'; c'].
Instances For
Wrap gru_cell_spec as an NNModuleSpec for a single timestep, using input [x; h].
Instances For
Bidirectional RNN wrapper (concatenates forward/backward features).
We run the RNNSpec forward over x, run it again over the reversed sequence, then reverse outputs
back and concatenate along the feature axis.