Stabilize dense-layer activations
Feature-based neural networks
Keras · Class
BatchNormalization normalizes activations using batch statistics during training and moving statistics during inference. It can apply learned scale and offset parameters after normalization.
BatchNormalization normalizes selected input channels and then optionally applies learned scale and offset. Training uses statistics from the current batch, while inference uses moving statistics accumulated during training.
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, renorm=False, renorm_clipping=None, renorm_momentum=0.99, synchronized=False, **kwargs)| Name | Type | Description |
|---|---|---|
axis | int or tuple of ints | Axis or axes whose channels are normalized; defaults to the last axis. |
momentum | float | Decay factor for the moving statistics. |
epsilon | float | Small value added to variance for numerical stability. |
center | bool | Whether to add the learned beta offset. |
scale | bool | Whether to multiply by the learned gamma scale. |
beta_initializer | str or initializer | Initializer for beta. |
gamma_initializer | str or initializer | Initializer for gamma. |
moving_mean_initializer | str or initializer | Initializer for moving mean. |
moving_variance_initializer | str or initializer | Initializer for moving variance. |
beta_regularizer | regularizer | Regularizer for beta. |
gamma_regularizer | regularizer | Regularizer for gamma. |
beta_constraint | constraint | Constraint for beta. |
A layer instance that normalizes inputs.
import keras
from keras import layers
inputs = keras.Input(shape=(32,))
x = layers.BatchNormalization()(inputs)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="adam", loss="mse")
model.fit(
keras.ops.ones((8, 32)),
keras.ops.zeros((8, 1)),
epochs=1,
verbose=0,
)Builds a model that normalizes 32-feature inputs before a dense output layer.
Feature-based neural networks
Convolutional networks
Transfer learning
BatchNormalization normalizes each selected channel using the current batch's mean and variance during training. It then applies learned gamma scaling and beta offset, with epsilon protecting the denominator.
Follow-up: How does inference differ? It uses moving mean and moving variance collected during training instead of statistics from the current batch.
By default, axis=-1, so the last input axis is normalized. For convolutional data, the channel axis should be selected explicitly when it is not the last axis.
Follow-up: Why does axis matter? It determines which dimension receives separate normalization statistics and trainable parameters.
The layer keeps moving averages of batch mean and variance for inference. These are non-trainable weights, whereas the learned scale and offset are trainable when enabled.
Follow-up: What fine-tuning precaution is recommended? Keep BatchNormalization in inference mode by passing training=False when calling the base model.
layers.LayerNormalizationlayers.Normalizationlayers.Layer