def activation_channels_max(activation):
    """Calculate the max of each of an activation's channels.
    The activation usually has the shape: (batch_size, num_channels, h, w).
    "We first use max pooling to convert the output of layer i, which is a
    c x h x w tensor, into a 1 x c vector."
    Returns - for each channel: the batch-mean of its max magnitudes (i.e. over all of the
    activations in the mini-batch, compute the mean of the max magnitude of each channel).
    """
    if activation.ndim == 4:
        featuremap_max_mat = activation.max(axis=(2, 3))
    elif activation.ndim == 2:
        featuremap_max_mat = activation.max(axis=1)  # batch x 1
    else:
        raise ValueError("activation_channels_means: Unsupported shape: ".format(activation.shape))

    return featuremap_max_mat.mean(axis=0)