pub struct BatchNorm2D<const C: usize> {
    pub scale: Tensor1D<C>,
    pub bias: Tensor1D<C>,
    pub running_mean: Tensor1D<C>,
    pub running_var: Tensor1D<C>,
    pub epsilon: f32,
    pub momentum: f32,
}
Expand description

Batch normalization for images as described in Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift

Generics:

  • C the size of the spatial dimension to reduce. For 3d tensors this is the 0th dimension. For 4d tensors, this is the 1st dimension.

Training vs Inference

BatchNorm2D supports the following cases (see sections below for more details):

  1. Training: ModuleMut and OwnedTape on the input tensor
  2. Inference: Module and NoneTape on the input tensor.

NOTE: ModuleMut/NoneTape, and Module/OwnedTape will fail to compile.

Examples:

let bn: BatchNorm2D<3> = Default::default();
let _ = bn.forward(Tensor3D::<3, 2, 2>::zeros());
let _ = bn.forward(Tensor4D::<4, 3, 2, 2>::zeros());

Training

  • Running statistics: updated with momentum
  • Normalization: calculated using batch stats

Inference

  • Running statistics: not updated
  • Normalization: calculated using running stats

Fields

scale: Tensor1D<C>

Scale for affine transform. Defaults to 1.0

bias: Tensor1D<C>

Bias for affine transform. Defaults to 0.0

running_mean: Tensor1D<C>

Spatial mean that is updated during training. Defaults to 0.0

running_var: Tensor1D<C>

Spatial variance that is updated during training. Defaults to 1.0

epsilon: f32

Added to variance before taking sqrt for numerical stability. Defaults to 1e-5

momentum: f32

Controls exponential moving average of running stats.Defaults to 0.1

running_stat * (1.0 - momentum) + stat * momentum.

Trait Implementations

Updates self given the GradientProvider. When any parameters that are NOT present in G, then this function should add the tensor’s UniqueId to UnusedTensors. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Reads this object from a ZipArchive. r with a base filename of filename_prefix. Read more
Loads data from a .npz zip archive at the specified path. Read more

Inference 3d forward - does not update Self::running_mean and Self::running_var

The type that this unit produces given Input.

Inference 4d forward - does not update Self::running_mean and Self::running_var

The type that this unit produces given Input.

Training 3d forward - updates Self::running_mean and Self::running_var

The type that this unit produces given Input.

Training 4d forward - updates Self::running_mean and Self::running_var

The type that this unit produces given Input.
Mutate the unit’s parameters using rand::Rng. Each implementor of this trait decides how the parameters are initialized. In fact, some impls may not even use the rng. Read more
Write this object into ZipWriter w with a base filename of filename_prefix. Read more
Save this object into the .npz file determined located at path. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.