Skip to Content
OchreML
Core & Containers

Sequential

Ordered neural network container with PyTorch-style forward(), zero-GIL Rust fit()/predict(), and .dit Tag 100 persistence.

ochreml.nn.Sequential executes a sequence of layers in order. When all layers are built-in Rust Sequential layers (Linear, Conv2d, DepthwiseConv2d, MaxPool2d, AvgPool2d, GlobalAvgPool2d, BatchNorm2d, ResidualBlock, LayerNorm, RMSNorm, Embedding, Dropout, Flatten, and activations), Sequential compiles into a native Rust graph enabling zero-GIL fit(), batched predict()/predict_proba()/predict_classes(), and .dit Tag 100 binary persistence.

Import & Signature

import ochreml.nn as nn

model = nn.Sequential(
    *layers,
    dtype="float32",
    device=None,
)

Key Methods

MethodSignatureDescription
fit(X, y, out_dim=None, epochs=10, batch_size=32, lr=1e-3, optimizer="adamw", loss="cross_entropy", weight_decay=0.0, grad_clip=None, shuffle=True, seed=42) -> list[float]Runs the full epoch/mini-batch training loop zero-GIL in Rust; returns per-epoch mean losses.
predict(X, batch_size=8192) -> np.ndarrayBatched eval-mode forward pass returning float32 logits or regression outputs.
predict_proba(X, batch_size=8192) -> np.ndarrayBatched softmax probabilities (float32).
predict_classes(X, batch_size=8192) -> np.ndarrayBatched argmax class indices (int64).
forward(x: Tensor) -> TensorStandard Module forward pass for custom autograd loops.
save / loadsave(path) / Sequential.load(path, device=None)Binary .dit persistence (Tag 100); also works via ochreml.save() / ochreml.load().
to_dit_bytes / from_dit_bytesto_dit_bytes() -> bytes / Sequential.from_dit_bytes(data, device=None)In-memory .dit serialization.

Usage Example

import numpy as np
import ochreml
import ochreml.nn as nn

rng = np.random.default_rng(42)
X = rng.standard_normal((512, 16)).astype(np.float32)
y = (X[:, 0] > 0).astype(np.int64)

model = nn.Sequential(
    nn.Linear(16, 32),
    nn.GELU(),
    nn.Linear(32, 2),
    device="auto",
)

losses = model.fit(X, y, epochs=15, batch_size=32, lr=1e-3, optimizer="adamw", loss="cross_entropy")
preds = model.predict_classes(X[:8])

ochreml.save(model, "seq_mlp.dit")
loaded = nn.Sequential.load("seq_mlp.dit", device="cpu")

On this page