chickadee » nanograd » conv2d

conv2d input weight bias #!key (stride 1) (padding 0)procedure

2D convolution using im2col + GEMM algorithm with batch support.

input
tensor of shape (C_in, H, W) or (N, C_in, H, W)
weight
tensor of shape (C_out, C_in, KH, KW)
bias
tensor of shape (C_out) or #f
stride
stride for convolution (default 1)
padding
zero-padding (default 0)

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)
; Single image
(define img (make-tensor32 (make-f32vector (* 3 32 32)) '(3 32 32)))
(define output (conv2d img weights bias stride: 2 padding: 1))

; Batch of images
(define batch-imgs (make-tensor32 (make-f32vector (* 16 3 32 32)) '(16 3 32 32)))
(define batch-output (conv2d batch-imgs weights bias))  ; Shape: (16, C_out, H_out, W_out)