# BatchNorm1D¶

## Descripition¶

Info

Parent class: BatchNormND

Derived classes: -

This module implements the batch normalization operation for three-dimensional tensors of shape (batchsize, maps, insize), for example, after one-dimensional convolutional layers. For more detailed theoretical information see BatchNormND.

## Initializing¶

def __init__(self, maps, epsilon=1e-5, initFactor=1.0, minFactor=0.1, sscale=0.01, affine=True, name=None, empty=False, inplace=False):


Parameters

Parameter Allowed types Descripition Default
size int Number of input features -
epsilon float float Stabilizing constant 1e-5
initFactor float Initial factor value in the moving average 1.0
minFactor float Minimal factor value in the moving average 0.1
sscale float Dispersion of the Gaussian distribution for the scale parameter of batch normalization 0.01
affine bool If True, the layer will have trainable affine parameters scale and bias True
name str Layer name None
empty bool If True the tensors of the parameters of the module will not be initialized. False
inplace bool If True, the output tensor will be written in memory in the place of the input tensor. False

Explanations

Note

In order to understand how initFactor and minFactor are used, it should be mentioned that the principle of the moving average is used in the calculation of statistical parameters in the module, i.e.: $$\hat{\mu} = \alpha\hat{\mu} + (1 - \alpha)\mu$$ $$\hat{\sigma}^2 = \alpha\hat{\sigma}^2 + (1 - \alpha)\sigma^2$$

where

$\hat{\mu}$, $\mu$ - moving average and average on the batch, respectively;
$\hat{\sigma^2}$, $\sigma^2$ - moving variance and variance on the batch, respectively; $\alpha$ - conservation factor.

$\alpha$ in the module is calculated as follows: $$\alpha = max(\frac{IF}{n}, MF)$$

where

$IF$ - initFactor, $MF$ - minFactor, $n$ - batch number.

size - number of input features, i.e. the maps axis of the tensor of shape (batchsize, maps, insize);

epsilon - small number added to prevent division by zero during the normalization of features (see the theory in BatchNormND);

affine - flag that is responsible for whether the scale and bias parameters of the batch normalization layer will be trained, or they will be fixed (1 and 0, respectively), so that the layer will only perform normalization by the average and the variance;

inplace - flag showing whether additional memory resources should be allocated for the result. If True, then the output tensor will be written to the place of the input one in memory, which can negatively affect the network if the input tensor should take part in calculations on other branches of the graph.

## Examples¶

Necessary imports.

>>> import numpy as np
>>> from PuzzleLib.Backend import gpuarray
>>> from PuzzleLib.Modules import BatchNorm1D


Info

gpuarray is required to properly place the tensor in the GPU.

Let us create a synthetic data tensor that is convenient for demonstrating the module:

>>> batchsize, maps, size = 3, 2, 2
>>> data = gpuarray.to_gpu(np.arange(batchsize * maps * size).reshape(batchsize, maps, size).astype(np.float32))
>>> data
[[[ 0.  1.]
[ 2.  3.]]

[[ 4.  5.]
[ 6.  7.]]

[[ 8.  9.]
[10. 11.]]]


Let us initialize the class object with default parameters and apply it to the data:

>>> bn = BatchNorm1D(maps)
>>> bn(data)


We can see the calculated mean and variance:

>>> bn.mean
[[[[4.5]]

[[6.5]]]]

>>>  bn.var
[[[[13.100001]]

[[13.100001]]]]


Initialized default scales and shifts (the scales are randomly normally distributed, the shifts are zero):

>>> bn.scale
[[[[1.006838 ]]

[[1.0114421]]]]
>>> bn.bias
[[[[0.]]

[[0.]]]]

The final form of the data passed through the module:
>>> bn.data
[[[-1.3712823  -1.0665529 ]
[-1.377553   -1.0714301 ]]

[[-0.15236473  0.15236467]
[-0.15306139  0.15306151]]

[[ 1.0665529   1.3712823 ]
[ 1.0714302   1.3775531 ]]]