Skip to content

normalization

Mean/Variance Normalization Layer API

This module provides classes to build fixed mean/variance normalization layers.

Classes:

Classes

Normalization1D

Normalization1D(mean: float | list[float] | tuple[float, ...], variance: float | list[float] | tuple[float, ...], epsilon: float = 1e-06, name: str | None = None, **kwargs)

Apply fixed mean/variance normalization to 1D inputs.

Parameters:

  • mean

    (float | list[float] | tuple[float, ...]) –

    Mean value(s) used for normalization.

  • variance

    (float | list[float] | tuple[float, ...]) –

    Variance value(s) used for normalization.

  • epsilon

    (float, default: 1e-06 ) –

    Small value to avoid division by zero.

  • name

    (str | None, default: None ) –

    Layer name.

Source code in helia_edge/layers/preprocessing/normalization.py
def __init__(
    self,
    mean: float | list[float] | tuple[float, ...],
    variance: float | list[float] | tuple[float, ...],
    epsilon: float = 1e-6,
    name: str | None = None,
    **kwargs,
):
    """Apply fixed mean/variance normalization to 1D inputs.

    Args:
        mean: Mean value(s) used for normalization.
        variance: Variance value(s) used for normalization.
        epsilon: Small value to avoid division by zero.
        name: Layer name.
    """
    super().__init__(name=name, **kwargs)
    self.mean = mean
    self.variance = variance
    self.epsilon = epsilon

Functions

augment_samples
augment_samples(inputs) -> keras.KerasTensor

Normalize a batch of samples.

Source code in helia_edge/layers/preprocessing/normalization.py
def augment_samples(self, inputs) -> keras.KerasTensor:
    """Normalize a batch of samples."""
    samples = inputs[self.SAMPLES]
    stats_shape = (1, -1, 1) if self.data_format == "channels_first" else (1, 1, -1)

    mean = keras.ops.reshape(self.backend.convert_to_tensor(self.mean, dtype=samples.dtype), stats_shape)
    variance = keras.ops.reshape(
        self.backend.convert_to_tensor(self.variance, dtype=samples.dtype), stats_shape
    )
    epsilon = keras.ops.cast(self.epsilon, samples.dtype)

    return (samples - mean) / keras.ops.sqrt(variance + epsilon)
compute_output_shape
compute_output_shape(input_shape, *args, **kwargs)

Compute output shape.

Source code in helia_edge/layers/preprocessing/normalization.py
def compute_output_shape(self, input_shape, *args, **kwargs):
    """Compute output shape."""
    return input_shape
get_config
get_config()

Serialize the configuration.

Source code in helia_edge/layers/preprocessing/normalization.py
def get_config(self):
    """Serialize the configuration."""
    config = super().get_config()
    config.update(mean=self.mean, variance=self.variance, epsilon=self.epsilon)
    return config

Normalization2D

Normalization2D(mean: float | list[float] | tuple[float, ...], variance: float | list[float] | tuple[float, ...], epsilon: float = 1e-06, name: str | None = None, **kwargs)

Apply fixed mean/variance normalization to 2D inputs.

Parameters:

  • mean

    (float | list[float] | tuple[float, ...]) –

    Mean value(s) used for normalization.

  • variance

    (float | list[float] | tuple[float, ...]) –

    Variance value(s) used for normalization.

  • epsilon

    (float, default: 1e-06 ) –

    Small value to avoid division by zero.

  • name

    (str | None, default: None ) –

    Layer name.

Source code in helia_edge/layers/preprocessing/normalization.py
def __init__(
    self,
    mean: float | list[float] | tuple[float, ...],
    variance: float | list[float] | tuple[float, ...],
    epsilon: float = 1e-6,
    name: str | None = None,
    **kwargs,
):
    """Apply fixed mean/variance normalization to 2D inputs.

    Args:
        mean: Mean value(s) used for normalization.
        variance: Variance value(s) used for normalization.
        epsilon: Small value to avoid division by zero.
        name: Layer name.
    """
    super().__init__(name=name, **kwargs)
    self.mean = mean
    self.variance = variance
    self.epsilon = epsilon

Functions

augment_samples
augment_samples(inputs) -> keras.KerasTensor

Normalize a batch of samples.

Source code in helia_edge/layers/preprocessing/normalization.py
def augment_samples(self, inputs) -> keras.KerasTensor:
    """Normalize a batch of samples."""
    samples = inputs[self.SAMPLES]
    stats_shape = (1, -1, 1, 1) if self.data_format == "channels_first" else (1, 1, 1, -1)

    mean = keras.ops.reshape(self.backend.convert_to_tensor(self.mean, dtype=samples.dtype), stats_shape)
    variance = keras.ops.reshape(
        self.backend.convert_to_tensor(self.variance, dtype=samples.dtype), stats_shape
    )
    epsilon = keras.ops.cast(self.epsilon, samples.dtype)

    return (samples - mean) / keras.ops.sqrt(variance + epsilon)
compute_output_shape
compute_output_shape(input_shape, *args, **kwargs)

Compute output shape.

Source code in helia_edge/layers/preprocessing/normalization.py
def compute_output_shape(self, input_shape, *args, **kwargs):
    """Compute output shape."""
    return input_shape
get_config
get_config()

Serialize the configuration.

Source code in helia_edge/layers/preprocessing/normalization.py
def get_config(self):
    """Serialize the configuration."""
    config = super().get_config()
    config.update(mean=self.mean, variance=self.variance, epsilon=self.epsilon)
    return config