Skip to content

Conv2d

Conv2d is a 2-D convolutional layer that learns a bank of spatial filters applied to each input channel. It stores a weight tensor of shape (out_channels, in_channels, kH, kW) and an optional bias vector, both initialised with Kaiming uniform initialisation. The layer wraps the functional conv2d op and is fully compatible with Sequential and any Optimizer.

import simplegrad as sg
import simplegrad.nn as nn

conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1)
x = sg.normal((1, 3, 32, 32))
out = conv(x)  # shape: (1, 16, 32, 32)

Conv2d

Bases: Module

2D convolutional layer.

Applies a learned 2D convolution over an input signal. Weights are initialized with Kaiming uniform scaling.

Parameters:

  • in_channels (int | None, default: None ) –

    Number of input channels.

  • out_channels (int | None, default: None ) –

    Number of output channels (filters).

  • kernel_size (int | tuple[int, int], default: None ) –

    Kernel size. Int or (kH, kW).

  • weight (Tensor | None, default: None ) –

    Optional pre-built weight tensor of shape (out_channels, in_channels, kH, kW).

  • bias (Tensor | None, default: None ) –

    Optional pre-built bias tensor of shape (out_channels,).

  • use_bias (bool, default: True ) –

    Add a bias term. Defaults to True.

  • dtype (str | None, default: None ) –

    Data type string. Defaults to "float32".

  • stride (int, default: 1 ) –

    Convolution stride. Int or (sH, sW). Defaults to 1.

  • pad_width (int | tuple[int, int, int, int], default: 0 ) –

    Padding. Int (all sides) or (top, bottom, left, right). Defaults to 0.

  • pad_mode (str, default: 'constant' ) –

    Padding mode. Defaults to "constant".

  • pad_value (int, default: 0 ) –

    Fill value for constant padding. Defaults to 0.

  • weight_label (str, default: 'W' ) –

    Label for the weight tensor.

  • bias_label (str, default: 'b' ) –

    Label for the bias tensor.

Attributes

Attribute Type Description
.weight Tensor Filter bank of shape (out_channels, in_channels, kH, kW). Learnable.
.bias Tensor \| None Bias vector of shape (out_channels,). None if use_bias=False.
.in_channels int Number of input channels.
.out_channels int Number of output channels (filters).
.kernel_size int \| tuple Height and width of the convolution kernel.
.stride int \| tuple Step size of the sliding window.
.padding int \| tuple Zero-padding added to both spatial dimensions.

Methods

Method Description
.forward() Apply the convolution to input tensor x.

Inherits all methods from Module: .parameters(), .submodules(), .to_device(), .summary(), .set_train_mode(), .set_eval_mode().