Skip to Content
OchreML
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

ParameterTypeDefaultDescription
in_channelsintrequiredNumber of input feature map channels.
out_channelsintrequiredNumber of output feature map channels.
strideint | tuple[int, int]1Spatial stride of the first 3x3 conv and optional 1x1 shortcut.
dtypestr"float32""float32", "bfloat16", or "float16".
devicestr | NoneNoneTarget device.
seedint42Initialization 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)

On this page