Implement Global Average Pooling. Given a feature map tensor of shape (batch_size, channels, height, width), compute the spatial average for each channel, reducing the spatial dimensions to 1x1. This replaces fully connected layers at the end of CNNs.
import numpy as np
def global_average_pooling(x: np.ndarray) -> np.ndarray:
# x shape: (batch_size, channels, height, width)
# Output shape: (batch_size, channels)
return np.mean(x, axis=(2, 3))(B, C, H, W) representing a batch of feature maps from a convolutional layer.np.mean over axes 2 and 3.(B, C) where each value is the average of an entire feature map.