The keras BatchNormalization layer uses axis=-1
as a default value and states that the feature axis is typically normalized. Why is this the case?
I sup
The confusion is due to the meaning of axis
in np.mean
versus in BatchNormalization
.
When we take the mean along an axis, we collapse that dimension and preserve all other dimensions. In your example data.mean(axis=0)
collapses the 0-axis
, which is the vertical dimension of data
.
When we compute a BatchNormalization
along an axis, we preserve the dimensions of the array, and we normalize with respect to the mean and standard deviation over every other axis. So in your 2D
example BatchNormalization
with axis=1
is subtracting the mean for axis=0
, just as you expect. This is why bn.moving_mean
has shape (4,)
.