chickadee » nanograd » make-conv2d-layer

(make-conv2d-layer in-channels out-channels kernel-size #!key (stride 1) (padding 0) (activation (make-identity)) (dtype 'f32) (name "Conv2D")) -> layerprocedure

Creates a 2D convolutional layer with He initialization. Supports both single images and batches.

in-channels
number of input channels
out-channels
number of output channels
kernel-size
size of convolution kernel (square)
stride
convolution stride (default 1)
padding
zero-padding (default 0)
activation
activation function object
dtype
'f32 or 'f64
name
layer name

Input shapes:

  • 3D: (C_in, H, W) - single image
  • 4D: (N, C_in, H, W) - batch of images

Output shapes:

  • 3D: (C_out, H_out, W_out)
  • 4D: (N, C_out, H_out, W_out)
(define conv (make-conv2d-layer 3 32 3 
                                stride: 1 
                                padding: 1
                                activation: (make-relu)))

; Single image
(define img (make-tensor32 img-data '(3 32 32)))
(define features (forward conv img))  ; Shape: (32, 32, 32)

; Batch of images
(define batch (make-tensor32 batch-data '(16 3 32 32)))
(define batch-features (forward conv batch))  ; Shape: (16, 32, 32, 32)