TF 2.0 is out! Get hands-on practice at TF World, Oct 28-31. Use code TF20 for 20% off select passes. Register now


View source on GitHub

Class VBN

A class to perform virtual batch normalization.

This technique was first introduced in Improved Techniques for Training GANs (Salimans et al, Instead of using batch normalization on a minibatch, it fixes a reference subset of the data to use for calculating normalization statistics.

To do this, we calculate the reference batch mean and mean square, and modify those statistics for each example. We use mean square instead of variance, since it is linear.

Note that if center or scale variables are created, they are shared between all calls to this object.

The __init__ API is intended to mimic tf.compat.v1.layers.batch_normalization as closely as possible.


View source


Initialize virtual batch normalization object.

We precompute the 'mean' and 'mean squared' of the reference batch, so that __call__ is efficient. This means that the axis must be supplied when the object is created, not when it is called.

We precompute 'square mean' instead of 'variance', because the square mean can be easily adjusted on a per-example basis.


  • reference_batch: A minibatch tensors. This will form the reference data from which the normalization statistics are calculated. See for more details.
  • axis: Integer, the axis that should be normalized (typically the features axis). For instance, after a Convolution2D layer with data_format="channels_first", set axis=1 in BatchNormalization.
  • epsilon: Small float added to variance to avoid dividing by zero.
  • center: If True, add offset of beta to normalized tensor. If False, beta is ignored.
  • scale: If True, multiply by gamma. If False, gamma is not used. When the next layer is linear (also e.g. nn.relu), this can be disabled since the scaling can be done by the next layer.
  • beta_initializer: Initializer for the beta weight.
  • gamma_initializer: Initializer for the gamma weight.
  • beta_regularizer: Optional regularizer for the beta weight.
  • gamma_regularizer: Optional regularizer for the gamma weight.
  • trainable: Boolean, if True also add variables to the graph collection GraphKeys.TRAINABLE_VARIABLES (see tf.Variable).
  • name: String, the name of the ops.
  • batch_axis: The axis of the batch dimension. This dimension is treated differently in virtual batch normalization vs batch normalization.


  • ValueError: If reference_batch has unknown dimensions at graph construction.
  • ValueError: If batch_axis is the same as axis.



View source


Run virtual batch normalization on inputs.


  • inputs: Tensor input.


A virtual batch normalized version of inputs.


  • ValueError: If inputs shape isn't compatible with the reference batch.


View source


Return the reference batch, but batch normalized.