Skip to Content
OchreML
Sequence Models

LSTM

Multi-layer and bidirectional Long Short-Term Memory network with fused 4x gate projection, BPTT, and zero-GIL train_steps().

ochreml.nn.LSTM implements a multi-layer, optionally bidirectional Long Short-Term Memory network with fused 4 * hidden_size gate projections (N7), full BPTT, and zero-GIL train_steps(). Inputs are batch-first: (batch, seq_len, input_size).

Import & Signature

import ochreml.nn as nn

lstm = nn.LSTM(
    input_size,
    hidden_size,
    num_layers=1,
    bias=True,
    bidirectional=False,
    dtype="float32",
    device=None,
    seed=42,
)

Parameters

ParameterTypeDefaultDescription
input_sizeintrequiredNumber of input features per time step.
hidden_sizeintrequiredHidden and cell state dimensionality.
num_layersint1Number of stacked LSTM layers.
biasboolTrueInclude learnable bias terms.
bidirectionalboolFalseIf True, output width is 2 * hidden_size.
dtypestr"float32""float32", "bfloat16", or "float16".
devicestr | NoneNoneTarget device.
seedint42Initialization seed.

Methods

  • forward(input, hx=None) -> tuple[Tensor, tuple[Tensor, Tensor]] — accepts optional hx=(h0, c0) and returns (out, (hn, cn)), where hn and cn each have shape (num_layers * num_directions, batch, hidden_size).
  • train_steps(x, target, steps=1, lr=1e-3, weight_decay=0.0, grad_clip=None) -> list[float] — runs multi-step zero-GIL AdamW + MSE training in Rust.

Usage Example

import ochreml.nn as nn

lstm = nn.LSTM(input_size=16, hidden_size=32, num_layers=2)
x = nn.Tensor.randn([8, 12, 16])
out, (hn, cn) = lstm(x)  # out: (8, 12, 32), hn/cn: (2, 8, 32)

On this page