Sequence Models
RNN
Multi-layer and bidirectional Elman RNN with Rust BPTT and zero-GIL train_steps().
ochreml.nn.RNN implements a multi-layer, optionally bidirectional Elman RNN with tanh or relu nonlinearity, full Rust Backpropagation Through Time (BPTT), and zero-GIL train_steps(). Inputs are batch-first: (batch, seq_len, input_size).
Import & Signature
import ochreml.nn as nn
rnn = nn.RNN(
input_size,
hidden_size,
num_layers=1,
nonlinearity="tanh",
bias=True,
bidirectional=False,
dtype="float32",
device=None,
seed=42,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input_size | int | required | Number of features per time step. |
hidden_size | int | required | Hidden state dimensionality. |
num_layers | int | 1 | Number of stacked recurrent layers. |
nonlinearity | str | "tanh" | Activation function: "tanh" or "relu". |
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, h0=None) -> tuple[Tensor, Tensor]— returns(out, hn)whereouthas shape(batch, seq_len, hidden_size * num_directions)andhnhas 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
rnn = nn.RNN(input_size=16, hidden_size=32, num_layers=2, nonlinearity="tanh")
x = nn.Tensor.randn([8, 12, 16])
out, hn = rnn(x) # out: (8, 12, 32), hn: (2, 8, 32)