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 Spec.Modules so recurrent blocks can be
composed with other modules in a Spec.Module.Chain.
Design choices:
- These wrappers are stateless modules: they pick a canonical initial hidden/state (all zeros).
Spec.Moduleremains a pureforward; more stateful variants can be built at the layer-spec level if needed. - The exported
forwardreturns the full output sequence, including every intermediate 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.
Bidirectional LSTM wrapper (concatenates forward/backward features).
Instances For
Wrap rnnCellSpec as an Spec.Module 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 lstmCellSpec as an Spec.Module 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 gruCellSpec as an Spec.Module 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.