Stabilize hidden-layer activations
A convolutional or dense network needs better-conditioned intermediate activations.
TensorFlow · Class
BatchNormalization normalizes layer inputs toward zero mean and unit standard deviation. It uses current-batch statistics during training and moving statistics during inference.
The layer standardizes selected input channels while retaining trainable scale and offset parameters. During training it uses the current batch's statistics and updates moving averages; during inference it uses those accumulated moving statistics instead.
tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None, synchronized=False, **kwargs)| Name | Type | Description |
|---|---|---|
axis | int | Dimension or dimensions to normalize; defaults to the last dimension. |
momentum | float | Weight given to old moving statistics when updating them. |
epsilon | float | Small value added to variance for numerical stability. |
center | bool | Whether to add the learned offset beta. |
scale | bool | Whether to multiply by the learned scale gamma. |
beta_initializer | Initializer | Initializer for beta. |
gamma_initializer | Initializer | Initializer for gamma. |
moving_mean_initializer | Initializer | Initializer for the moving mean. |
moving_variance_initializer | Initializer | Initializer for the moving variance. |
beta_regularizer | Regularizer | Regularizer for beta. |
gamma_regularizer | Regularizer | Regularizer for gamma. |
beta_constraint | Constraint | Constraint for beta. |
A layer that normalizes its inputs; calling it returns the normalized tensor.
import tensorflow as tf
layer = tf.keras.layers.BatchNormalization()
x = tf.constant([[1., 2.], [3., 4.]])
training_output = layer(x, training=True)
inference_output = layer(x, training=False)
print(training_output)
print(inference_output)This runnable example contrasts batch-statistics normalization during training with moving-statistics normalization during inference.
A convolutional or dense network needs better-conditioned intermediate activations.
A model should normalize features separately along a chosen channel dimension.
Training and serving need distinct statistic handling.
BatchNormalization keeps outputs near zero mean and unit standard deviation. In training mode it computes statistics from the current batch, whereas inference uses moving averages accumulated from training.
Follow-up: Why can inference differ from training? Inference substitutes stored moving mean and variance for the current batch's statistics, producing deterministic behavior independent of the evaluation batch.
The default axis is -1, meaning the last input dimension is normalized as the channel or feature axis. The constructor also exposes axis so another dimension can be selected.
Follow-up: What should be checked when using channels-first tensors? Set axis to the dimension containing channels rather than relying on the last-dimension default.
The normalized value is scaled by trainable gamma and shifted by trainable beta. scale=False removes gamma, while center=False removes beta; epsilon protects the variance denominator.
Follow-up: When might scale be disabled? If a following linear operation already provides scaling, disabling scale can avoid a redundant learned factor.
tf.keras.layers.LayerNormalizationtf.keras.layers.Normalizationtf.keras.layers.Layer