chickadee » nanograd » global-avg-pool2d

global-avg-pool2d inputprocedure

Global average pooling over spatial dimensions with batch support. Reduces spatial dimensions to 1x1 by averaging.

Input shapes
3D
(C, H, W) → Output: (C,)
4D
(N, C, H, W) → Output: (N, C)

Gradient: Distributed uniformly over all spatial positions for each channel.

;; Single image
(define feature-maps (make-tensor32 (make-f32vector (* 128 8 8)) '(128 8 8)))
(define pooled (global-avg-pool2d feature-maps))  ; Shape: (128,)

;; Batch of images
(define batch-features (make-tensor32 (make-f32vector (* 32 128 8 8)) '(32 128 8 8)))
(define batch-pooled (global-avg-pool2d batch-features))  ; Shape: (32, 128)

;; Use in classification network
(define logits (forward fc-layer batch-pooled))  ; Shape: (32, num_classes)

Global average pooling is commonly used to replace large fully-connected layers:

  • Reduces number of parameters dramatically
  • Improves generalization
  • Makes networks translation-invariant
  • Standard in modern architectures (ResNet, MobileNet, EfficientNet)