batch_norm#

ivy.batch_norm(x, mean, variance, /, *, offset=None, scale=None, training=False, eps=1e-05, momentum=0.1, data_format='NSC', out=None)[source]#

Apply batch normalization to the input array and returns the normalized input, running mean and running variance arrays as output. If training == False, the mean and variance arrays passed as input are used for normalization and the same arrays are returned as running mean and running variance respectively. However, when training ==True, this function computes the batch mean and batch variance which is then used for normalization.In this case, the function returns the running mean and running variance calculated using the following formula:

running_mean = (1 - momentum) * running_mean + momentum * batch_mean running_var = (1 - momentum) * running_var + momentum * frac{n}{n-1} * batch_var

Parameters:
  • x (Union[NativeArray, Array]) –

    Input array of default shape (N, *S, C), where N is the batch dimension, *S corresponds to any number of spatial dimensions and

    C corresponds to the channel dimension.

  • mean (Union[NativeArray, Array]) – Mean array used for input’s normalization. It can be of any shape braodcastable to (N,*S,C).

  • variance (Union[NativeArray, Array]) – Variance array used for input’s normalization. It can be of any shape braodcastable to (N,*S,C).

  • offset (Optional[Union[Array, NativeArray]], default: None) – An offset array. If present, will be added to the normalized input. It can be of any shape broadcastable to (N,*S,C).

  • scale (Optional[Union[Array, NativeArray]], default: None) – A scale array. If present, the scale is applied to the normalized input. It can be of any shape broadcastable to (N,*S,C).

  • training (Optional[bool], default: False) – If true, calculate and use the mean and variance of x. Otherwise, use the provided mean and variance.

  • eps (Optional[float], default: 1e-05) – A small float number to avoid dividing by 0.

  • momentum (Optional[float], default: 0.1) –

    the value used for the running_mean and running_var computation.

    Default value is 0.1.

  • data_format (Optional[str], default: 'NSC') – The ordering of the dimensions in the input, one of “NSC” or “NCS”, where N is the batch dimension, S represents any number of spatial dimensions and C is the channel dimension. Default is “NSC”.

  • out (Optional[Tuple[Array, Array, Array]], default: None) – optional output arrays, for writing the result to.

Return type:

Tuple[Array, Array, Array]

Returns:

ret

Tuple of arrays containing

the normalized input, running_mean, and running_variance.

Array.batch_norm(self, mean, variance, /, *, offset=None, scale=None, training=False, eps=1e-05, momentum=0.1, data_format='NSC', out=None)[source]#

ivy.Array instance method variant of ivy.batch_norm. This method simply wraps the function, and so the docstring for ivy.batch_norm also applies to this method with minimal changes.

Parameters:
  • self (Union[NativeArray, Array]) –

    Input array of default shape (N, *S, C), where N is the batch dimension, *S corresponds to any number of spatial dimensions and

    C corresponds to the channel dimension.

  • training (bool, default: False) – If true, calculate and use the mean and variance of x. Otherwise, use the provided mean and variance.

  • mean (Union[NativeArray, Array]) – Mean array used for input’s normalization. It can be of any shape braodcastable to (N,*S,C).

  • variance (Union[NativeArray, Array]) – Variance array used for input’s normalization. It can be of any shape braodcastable to (N,*S,C).

  • offset (Optional[Union[Array, NativeArray]], default: None) – An offset array. If present, will be added to the normalized input. It can be of any shape broadcastable to (N,*S,C).

  • scale (Optional[Union[Array, NativeArray]], default: None) – A scale array. If present, the scale is applied to the normalized input. It can be of any shape broadcastable to (N,*S,C).

  • eps (float, default: 1e-05) – A small float number to avoid dividing by 0.

  • momentum (float, default: 0.1) –

    the value used for the running_mean and running_var computation.

    Default value is 0.1.

  • data_format (str, default: 'NSC') – The ordering of the dimensions in the input, one of “NSC” or “NCS”, where N is the batch dimension, S represents any number of spatial dimensions and C is the channel dimension. Default is “NSC”.

  • out (Optional[Tuple[Array, Array, Array]], default: None) – optional output arrays, for writing the result to.

Return type:

Tuple[Array, Array, Array]

Returns:

ret – Tuple of arrays containing the normalized input, running mean, and running variance.

Container.batch_norm(self, mean, variance, /, *, offset=None, scale=None, training=False, eps=1e-05, momentum=0.1, data_format='NSC', out=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.batch_norm. This method simply wraps the function, and so the docstring for ivy.batch_norm also applies to this method with minimal changes.

Parameters:
  • x

    Input array of default shape (N, *S, C), where N is the batch dimension, *S corresponds to any number of spatial dimensions and

    C corresponds to the channel dimension.

  • mean (Union[NativeArray, Array, Container]) – Mean array used for input’s normalization. It can be of any shape braodcastable to (N,*S,C).

  • variance (Union[NativeArray, Array, Container]) – Variance array used for input’s normalization. It can be of any shape braodcastable to (N,*S,C).

  • offset (Optional[Union[Array, NativeArray, Container]], default: None) – An offset array. If present, will be added to the normalized input. It can be of any shape broadcastable to (N,*S,C).

  • scale (Optional[Union[Array, NativeArray, Container]], default: None) – A scale array. If present, the scale is applied to the normalized input. It can be of any shape broadcastable to (N,*S,C).

  • training (Union[bool, Container], default: False) – If true, calculate and use the mean and variance of x. Otherwise, use the provided mean and variance.

  • eps (Union[float, Container], default: 1e-05) – A small float number to avoid dividing by 0.

  • momentum (Union[float, Container], default: 0.1) –

    the value used for the running_mean and running_var computation.

    Default value is 0.1.

  • data_format (Union[str, Container], default: 'NSC') – The ordering of the dimensions in the input, one of “NSC” or “NCS”, where N is the batch dimension, S represents any number of spatial dimensions and C is the channel dimension. Default is “NSC”.

  • out (Optional[Tuple[Union[Array, Container], Union[Array, Container], Union[Array, Container]]], default: None) – optional output arrays, for writing the result to.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Tuple[Container, Container, Container]

Returns:

ret

Tuple of containers containing

the normalized input, running mean, and running variance.