Layers & Activations
ResidualBlock
ResNet-style basic residual block executed natively in Rust with automatic projection shortcuts.
ochreml.nn.ResidualBlock implements a standard ResNet basic block (Conv3x3 -> BatchNorm2d -> ReLU -> Conv3x3 -> BatchNorm2d + shortcut -> ReLU) in Rust. When stride != (1, 1) or in_channels != out_channels, it automatically constructs a 1x1 strided convolution + BatchNorm2d shortcut to match shapes.
Import & Signature
import ochreml.nn as nn
block = nn.ResidualBlock(
in_channels,
out_channels,
stride=1,
dtype="float32",
device=None,
seed=42,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
in_channels | int | required | Number of input feature map channels. |
out_channels | int | required | Number of output feature map channels. |
stride | int | tuple[int, int] | 1 | Spatial stride of the first 3x3 conv and optional 1x1 shortcut. |
dtype | str | "float32" | "float32", "bfloat16", or "float16". |
device | str | None | None | Target device. |
seed | int | 42 | Initialization seed. |
Usage Example
import ochreml.nn as nn
res1 = nn.ResidualBlock(16, 16, stride=1)
res2 = nn.ResidualBlock(16, 32, stride=2) # downsamples spatial dims by 2x
x = nn.Tensor.randn([4, 16, 16, 16])
out = res2(res1(x)) # shape: (4, 32, 8, 8)