Skip to content

standard_scale

StandardScaleLayer ¤

StandardScaleLayer(
    mean,
    variance,
    name=None,
    input_dtype=None,
    output_dtype=None,
    axis=-1,
    mask_value=None,
    **kwargs
)

Bases: NormalizeLayer

Performs the standard scaling of the input.

This layer will shift and scale inputs into a distribution centered around 0 with standard deviation 1. It accomplishes this by precomputing the mean and variance of the data, and calling (input - mean) / sqrt(var) at runtime. mask_value is used to ignore certain values in the standard scaling process. They will remain the same value in the output value as they were in the input value.

Initialise the StandardScaleLayer layer.

Parameters:

Name Type Description Default
mean Union[List[float], array]

The mean value(s) to use during normalization. The passed value(s) will be broadcast to the shape of the kept axes above; if the value(s) cannot be broadcast, an error will be raised when this layer's build() method is called.

required
variance Union[List[float], array]

The variance value(s) to use during normalization. The passed value(s) will be broadcast to the shape of the kept axes above; if the value(s) cannot be broadcast, an error will be raised when this layer's build() method is called.

required
name Optional[str]

The name of the layer. Defaults to None.

None
input_dtype Optional[str]

The dtype to cast the input to. Defaults to None.

None
output_dtype Optional[str]

The dtype to cast the output to. Defaults to None.

None
axis Optional[Union[int, tuple[int]]]

Integer, tuple of integers, or None. The axis or axes that should have a separate mean and variance for each index in the shape. For example, if shape is (None, 5) and axis=1, the layer will track 5 separate mean and variance values for the last axis. If axis is set to None, the layer will normalize all elements in the input by a scalar mean and variance. Defaults to -1, where the last axis of the input is assumed to be a feature dimension and is normalized per index. Note that in the specific case of batched scalar inputs where the only axis is the batch axis, the default will normalize each index in the batch separately. In this case, consider passing axis=None.

-1
mask_value Optional[float]

Value which should be ignored in the standard scaling process and left unchanged.

None
Source code in src/kamae/keras/core/layers/standard_scale.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def __init__(
    self,
    mean: Union[List[float], np.array],
    variance: Union[List[float], np.array],
    name: Optional[str] = None,
    input_dtype: Optional[str] = None,
    output_dtype: Optional[str] = None,
    axis: Optional[Union[int, tuple[int]]] = -1,
    mask_value: Optional[float] = None,
    **kwargs: Any,
) -> None:
    """
    Initialise the StandardScaleLayer layer.

    :param mean: The mean value(s) to use during normalization. The passed value(s)
    will be broadcast to the shape of the kept axes above; if the value(s)
    cannot be broadcast, an error will be raised when this layer's
    `build()` method is called.
    :param variance: The variance value(s) to use during normalization. The passed
    value(s) will be broadcast to the shape of the kept axes above; if the
    value(s) cannot be broadcast, an error will be raised when this
    layer's `build()` method is called.
    :param name: The name of the layer. Defaults to `None`.
    :param input_dtype: The dtype to cast the input to. Defaults to `None`.
    :param output_dtype: The dtype to cast the output to. Defaults to `None`.
    :param axis: Integer, tuple of integers, or None. The axis or axes that should
    have a separate mean and variance for each index in the shape. For
    example, if shape is `(None, 5)` and `axis=1`, the layer will track 5
    separate mean and variance values for the last axis. If `axis` is set
    to `None`, the layer will normalize all elements in the input by a
    scalar mean and variance. Defaults to -1, where the last axis of the
    input is assumed to be a feature dimension and is normalized per
    index. Note that in the specific case of batched scalar inputs where
    the only axis is the batch axis, the default will normalize each index
    in the batch separately. In this case, consider passing `axis=None`.
    :param mask_value: Value which should be ignored in the standard scaling
    process and left unchanged.
    """
    super().__init__(
        name=name,
        mean=mean,
        variance=variance,
        axis=axis,
        input_dtype=input_dtype,
        output_dtype=output_dtype,
        **kwargs,
    )
    self.mask_value = mask_value

get_config ¤

get_config()

Gets the configuration of the StandardScaleLayer layer. Used for saving and loading from a model. Specifically adds additional parameters to the base configuration.

Returns:

Type Description
Dict[str, Any]

Dictionary of the configuration of the layer.

Source code in src/kamae/keras/core/layers/standard_scale.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def get_config(self) -> Dict[str, Any]:
    """
    Gets the configuration of the StandardScaleLayer layer.
    Used for saving and loading from a model.
    Specifically adds additional parameters to the base configuration.
    :returns: Dictionary of the configuration of the layer.
    """
    config = super().get_config()
    config.update(
        {
            "mask_value": self.mask_value,
        }
    )
    return config