Convolution
conv2d applies a 2-D cross-correlation between an input feature map and a set of learned filters, which is the core operation in convolutional neural networks. pad adds zero-padding around the spatial dimensions of a tensor before the convolution, allowing control over output size. Both operations are fully differentiable and support backpropagation through both input and kernel.
import simplegrad as sg
x = sg.normal((1, 3, 28, 28)) # (N, C, H, W)
kernel = sg.normal((16, 3, 3, 3), requires_grad=True) # (out_ch, in_ch, kH, kW)
out = sg.conv2d(x, kernel, stride=1, padding=1)
pad(x: Tensor, width: int | tuple[int, int, int, int], mode: str = 'constant', value: int = 0) -> Tensor
Pad a tensor along its spatial dimensions.
Parameters:
-
x(Tensor) –Input tensor.
-
width(int | tuple[int, int, int, int]) –Padding widths. An int or nested tuples as accepted by
numpy.pad. -
mode(str, default:'constant') –Padding mode (e.g.
"constant","reflect"). See numpy.pad docs. -
value(int, default:0) –Fill value for
"constant"mode.
conv2d(x: Tensor, weight: Tensor, bias: Tensor | None = None, stride: int | tuple[int, int] = 1, pad_width: int | tuple[int, int, int, int] = 0, pad_mode: str = 'constant', pad_value: int = 0) -> Tensor
Apply a 2D convolution over the input tensor.
Implements convolution as a single matrix multiplication (im2col). Supports batched inputs (4D) and single-sample inputs (3D).
Parameters:
-
x(Tensor) –Input tensor of shape
(batch, in_channels, H, W)or(in_channels, H, W). -
weight(Tensor) –Kernel tensor of shape
(out_channels, in_channels, kH, kW). -
bias(Tensor | None, default:None) –Optional bias of shape
(out_channels,). -
stride(int | tuple[int, int], default:1) –Convolution stride. Int or
(height_stride, width_stride). -
pad_width(int | tuple[int, int, int, int], default:0) –Padding to apply before convolution. Int (same on all sides) or
(top, bottom, left, right). -
pad_mode(str, default:'constant') –Padding mode passed to
numpy.pad. Defaults to"constant". -
pad_value(int, default:0) –Fill value for constant padding. Defaults to 0.