Skip to content
fn

lstm_op

→std::vector<TensorImplPtr>
int lstm_op(const int & input, const int & h0, const int & c0, const int & weights, const backend::IBackend::LstmOpts & opts)

Run a single-layer LSTM forward pass and return {output, hn, cn}.

Thin public entry point that delegates to LstmBackward::forward and is bound into Python as lucid._C.engine.nn.lstm_forward. The Python LSTM module in lucid.nn.modules.rnn calls this once per layer per direction and composes multi-layer, bidirectional and inter-layer dropout behaviour itself.

Math

Per-time-step gating (Hochreiter & Schmidhuber 1997):

it=σ(Wii xt+Whi ht−1+bi)ft=σ(Wif xt+Whf ht−1+bf)gt=tanh⁡(Wig xt+Whg ht−1+bg)ot=σ(Wio xt+Who ht−1+bo)ct=ft⊙ct−1+it⊙gt,ht=ot⊙tanh⁡(ct) \begin{aligned} i_t &= \sigma(W_{ii}\,x_t + W_{hi}\,h_{t-1} + b_i) \\ f_t &= \sigma(W_{if}\,x_t + W_{hf}\,h_{t-1} + b_f) \\ g_t &= \tanh(W_{ig}\,x_t + W_{hg}\,h_{t-1} + b_g) \\ o_t &= \sigma(W_{io}\,x_t + W_{ho}\,h_{t-1} + b_o) \\ c_t &= f_t \odot c_{t-1} + i_t \odot g_t,\qquad h_t = o_t \odot \tanh(c_t) \end{aligned}

References

Hochreiter & Schmidhuber, "Long Short-Term Memory" (Neural Computation 1997).

See Also

lucid.nn.LSTM — Python multi-layer / bidirectional / projection wrapper that composes single-layer engine calls. lucid.nn.LSTMCell — single-time-step variant.

Parameters

inputTensorImplPtr
Input sequence shaped (T, B, input_size).
h0TensorImplPtr
Initial hidden state shaped (1, B, H_rec) (zero-filled by the caller if no explicit initial state is supplied).
c0TensorImplPtr
Initial cell state shaped (1, B, hidden_size).
weightsstd::vector<TensorImplPtr>
Layer weights {W_ih, W_hh, b_ih, b_hh} (plus optional W_hr when projection is enabled). Gate matrices are stacked in [i, f, g, o] order along the leading axis.
optsbackend::IBackend::LstmOpts
Shape and structural parameters; see LstmBackward::forward.

Returns

std::vector<TensorImplPtr>

{output, hn, cn} — output sequence (T, B, H_out), final hidden state (1, B, H_out), final cell state (1, B, hidden_size).

Notes

output, hn and cn are all differentiable — see LstmBackward.