Conv1d
1D Convolutional Layer. Applies a 1D convolution over an input signal composed of several input planes.Constructor
inChannels- Number of input channelsoutChannels- Number of output channels (filters)kernelSize- Size of the convolving kerneloptions.stride- Stride of the convolution (default: 1)options.padding- Zero-padding added to input (default: 0)options.bias- If true, adds learnable bias (default: true)
InvalidParameterError- If parameters are invalid
Shape
Input:(batch, in_channels, length)
Output: (batch, out_channels, length_out)
Where: length_out = floor((length + 2 * padding - kernel_size) / stride) + 1
Properties
weight: GradTensor- Convolution kernels of shape(out_channels, in_channels, kernel_size)
Example
Conv2d
2D Convolutional Layer. Applies a 2D convolution over an input signal composed of several input planes.Constructor
inChannels- Number of input channelsoutChannels- Number of output channels (filters)kernelSize- Size of the convolving kernel (single number or [height, width])options.stride- Stride of the convolution (default: 1)options.padding- Zero-padding added to input (default: 0)options.bias- If true, adds learnable bias (default: true)
InvalidParameterError- If parameters are invalid
Shape
Input:(batch, in_channels, height, width)
Output: (batch, out_channels, height_out, width_out)
Where:
Properties
weight: GradTensor- Convolution kernels of shape(out_channels, in_channels, kernel_height, kernel_width)
Examples
Basic Image Processing
Non-square Kernels
CNN Architecture
MaxPool2d
2D Max Pooling Layer. Applies a 2D max pooling over an input signal.Constructor
kernelSize- Size of the pooling windowoptions.stride- Stride of the pooling (default: same as kernel_size)options.padding- Zero-padding added to input (default: 0)
InvalidParameterError- If parameters are invalid
Shape
Input:(batch, channels, height, width)
Output: (batch, channels, height_out, width_out)
Where:
Properties
- Downsamples by taking maximum value in each window
- Reduces spatial dimensions
- Provides translation invariance
- No learnable parameters
Examples
Basic Pooling
Non-square Pooling
With Overlapping Windows
AvgPool2d
2D Average Pooling Layer. Applies a 2D average pooling over an input signal.Constructor
kernelSize- Size of the pooling windowoptions.stride- Stride of the pooling (default: same as kernel_size)options.padding- Zero-padding added to input (default: 0)
InvalidParameterError- If parameters are invalid
Shape
Input:(batch, channels, height, width)
Output: (batch, channels, height_out, width_out)
Properties
- Downsamples by taking average value in each window
- Smoother than max pooling
- Preserves more information
- No learnable parameters
Example
Complete CNN Example
Image Classifier
ResNet-style Block
Common Patterns
Padding Calculations
Downsampling
Channel Expansion
Performance Tips
- Use Padding: Preserve spatial dimensions with
padding: (kernel_size - 1) / 2 - Batch Processing: Process multiple images together for efficiency
- Channel Order: Standard format is
(batch, channels, height, width) - Memory: Convolutions can use significant memory for large images
See Also
- Activation Functions - ReLU, etc.
- Normalization - BatchNorm for CNNs
- Module - Base class