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
| Parameter | Type | Default | Description |
|---|---|---|---|
input_size | int | required | Number of input features per time step. |
hidden_size | int | required | Hidden and cell state dimensionality. |
num_layers | int | 1 | Number of stacked LSTM layers. |
bias | bool | True | Include learnable bias terms. |
bidirectional | bool | False | If True, output width is 2 * hidden_size. |
dtype | str | "float32" | "float32", "bfloat16", or "float16". |
device | str | None | None | Target device. |
seed | int | 42 | Initialization seed. |
Methods
forward(input, hx=None) -> tuple[Tensor, tuple[Tensor, Tensor]]— accepts optionalhx=(h0, c0)and returns(out, (hn, cn)), wherehnandcneach 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)