Class BatchNorm

  • All Implemented Interfaces:
    Block
    Direct Known Subclasses:
    GhostBatchNorm

    public class BatchNorm
    extends AbstractBlock
    In batch training (training with more than one samples per iteration), a batch normalization layer works by normalizing the values of input data to have mean of 0 and variance of 1. Since this may alter the representation of a layer, two parameters (\ (\gamma\) and \(\beta\)) are learned along the normalization process to respectively scale and shift the normalized output (activations) to have any mean and variance so the network can utilize non-linear transformations such as sigmoid function as described in the paper. During backpropagation, both \(\gamma\) and \(\beta\) parameters are included following the chain-rule in derivation.

    The problem of varying distribution of input data requires the training process of a deep network to compensate for each different data distribution per batch, hence changing parameters' values as new batch data is processed and changes distribution of the network's (and each of its layers) activations. This condition is termed as internal covariate shift, and such occurrence prevents the network to learn faster and generalize better to unseen data.

    With batch normalization, one benefits by having faster learning process as batch normalization allows larger learning rate without causing gradient problems on backpropagation as all inputs are normalized and hence reducing the scale of weight update impact on backpropagation. In some cases, the utilization of batch normalization layer regularizes the network and reduces, even eliminates, the need for dropout, which in turn results in even faster training process since dropout slows down training by 2-3 times. However, it was reported that batch normalization may not be beneficial when small batch sizes are used.

    Formally, batch normalization is represented below:
    \(\hat{x} \:=\: \frac{x \:-\: \mu_{batch}}{\sqrt{\sigma^2_{batch} \:+\: \epsilon}}\),
    where \(\hat{x}\) is the normalized input, \(\mu_{batch}\) and \(\sigma^2_{batch}\) respectively denote the mean and variance of a batch, and \(\epsilon\) (epsilon) is a constant for numerical stability. The scale and shift operation can be formally defined as follows:
    \(y \:=\: \gamma\hat{x} \:+\: \beta\),
    where \(\gamma\) is the scale factor and \(\beta\) is the shift factor.

    See Also:
    The D2L chapter on batch normalization
    • Method Detail

      • getOutputShapes

        public Shape[] getOutputShapes​(Shape[] inputShapes)
        Returns the expected output shapes of the block for the specified input shapes.
        Parameters:
        inputShapes - the shapes of the inputs
        Returns:
        the expected output shapes of the block
      • beforeInitialize

        protected void beforeInitialize​(Shape... inputShapes)
        Performs any action necessary before initialization. For example, keep the input information or verify the layout.
        Overrides:
        beforeInitialize in class AbstractBaseBlock
        Parameters:
        inputShapes - the expected shapes of the input
      • saveMetadata

        protected void saveMetadata​(java.io.DataOutputStream os)
                             throws java.io.IOException
        Override this method to save additional data apart from parameter values.

        This default implementation saves the currently set input shapes.

        Overrides:
        saveMetadata in class AbstractBaseBlock
        Parameters:
        os - the non-null output stream the parameter values and metadata are written to
        Throws:
        java.io.IOException - saving failed
      • loadMetadata

        public void loadMetadata​(byte loadVersion,
                                 java.io.DataInputStream is)
                          throws java.io.IOException,
                                 MalformedModelException
        Overwrite this to load additional metadata with the parameter values.

        If you overwrite AbstractBaseBlock.saveMetadata(DataOutputStream) or need to provide backward compatibility to older binary formats, you prabably need to overwrite this. This default implementation checks if the version number fits, if not it throws an MalformedModelException. After that it restores the input shapes.

        Overrides:
        loadMetadata in class AbstractBaseBlock
        Parameters:
        loadVersion - the version used for loading this metadata.
        is - the input stream we are loading from
        Throws:
        java.io.IOException - loading failed
        MalformedModelException - data can be loaded but has wrong format
      • batchNorm

        public static NDList batchNorm​(NDArray input,
                                       NDArray runningMean,
                                       NDArray runningVar)
        Applies Batch Normalization for each channel across a batch of data.
        Parameters:
        input - the input NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
        runningMean - runningMean NDArray
        runningVar - runningVar NDArray
        Returns:
        the output NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
      • batchNorm

        public static NDList batchNorm​(NDArray input,
                                       NDArray runningMean,
                                       NDArray runningVar,
                                       NDArray gamma,
                                       NDArray beta)
        Applies Batch Normalization for each channel across a batch of data.
        Parameters:
        input - the input NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
        runningMean - runningMean NDArray
        runningVar - runningVar NDArray
        gamma - gamma weight NDArray
        beta - beta weight NDArray
        Returns:
        the output NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
      • batchNorm

        public static NDList batchNorm​(NDArray input,
                                       NDArray runningMean,
                                       NDArray runningVar,
                                       NDArray gamma,
                                       NDArray beta,
                                       int axis)
        Applies Batch Normalization for each channel across a batch of data.
        Parameters:
        input - the input NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
        runningMean - runningMean NDArray
        runningVar - runningVar NDArray
        gamma - gamma weight NDArray
        beta - beta weight NDArray
        axis - the axis that should be normalized
        Returns:
        the output NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
      • batchNorm

        public static NDList batchNorm​(NDArray input,
                                       NDArray runningMean,
                                       NDArray runningVar,
                                       NDArray gamma,
                                       NDArray beta,
                                       int axis,
                                       float momentum,
                                       float eps,
                                       boolean training)
        Applies Batch Normalization for each channel across a batch of data.
        Parameters:
        input - the input NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
        runningMean - runningMean NDArray
        runningVar - runningVar NDArray
        gamma - gamma weight NDArray
        beta - beta weight NDArray
        axis - the axis that should be normalized
        momentum - the value used for the runningMean and runningVar computation.
        eps - a value added to the denominator for numerical stability
        training - indicate the training mode if true
        Returns:
        the output NDArray of shape (batchSize, inputChannel, *), * could be empty, width, (height, width), (depth, height, width)
      • builder

        public static BatchNorm.BaseBuilder<?> builder()
        Creates a builder to build a BatchNorm.
        Returns:
        a new builder