chickadee » nanograd » make-conv2d-layer

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

Creates a 2-D convolutional layer. Unless weight-values is given, weights start as uniform random values scaled by sqrt(2 / fan-in) (He scaling).

in-channels
number of input channels
out-channels
number of output channels
kernel-size
size of the square kernel
stride
convolution stride (default 1)
padding
zero padding (default 0)
activation
activation object
dtype
'f32 or 'f64
weight-values
optional SRFI-4 vector of initial weights, laid out as (out-channels, in-channels, kernel-size, kernel-size)
bias-values
optional SRFI-4 vector of initial biases
name
layer name

Input shapes:

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

Output shapes:

  • 3-D: (C_out, H_out, W_out)
  • 4-D: (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)