espnet2.asr.state_spaces.components.TSNormalization
espnet2.asr.state_spaces.components.TSNormalization
class espnet2.asr.state_spaces.components.TSNormalization(method, horizon)
Bases: Module
Time Series Normalization module.
This module normalizes time series data using specified methods over a defined horizon. It supports normalization by either the mean or the last value within the specified horizon. The input tensor is expected to have the shape (B, L, D), where B is the batch size, L is the sequence length, and D is the number of features.
method
The normalization method to apply. Can be “mean” or “last”.
- Type: str
horizon
The number of time steps to consider for normalization.
Type: int
Parameters:
- method (str) – The normalization method, either “mean” or “last”.
- horizon (int) – The number of time steps to use for normalization.
Returns: The normalized tensor with the same shape as the input.
Return type: Tensor
####### Examples
>>> normalization = TSNormalization(method="mean", horizon=3)
>>> input_tensor = torch.tensor([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]])
>>> normalized_tensor = normalization(input_tensor)
>>> print(normalized_tensor)
tensor([[[0.3333, 0.6667],
[1.0000, 1.3333],
[1.6667, 2.0000]]])
>>> normalization_last = TSNormalization(method="last", horizon=1)
>>> normalized_tensor_last = normalization_last(input_tensor)
>>> print(normalized_tensor_last)
tensor([[[1.0, 2.0],
[1.5, 2.0],
[2.5, 3.0]]])
NOTE
If the input tensor does not have the expected shape, the behavior of this module may be undefined.
- Raises:ValueError – If the input tensor does not have at least 2 dimensions.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
forward(x)
Time Series Normalization module.
This module normalizes time series data based on the specified method and horizon. The normalization is performed on the input tensor, which is expected to have a shape of (B, L, D), where B is the batch size, L is the length of the sequence, and D is the number of features.
method
The normalization method to use. Options are “mean” or “last”.
- Type: str
horizon
The number of timesteps to consider for normalization.
Type: int
Parameters:
- method (str) – The normalization method to use (“mean” or “last”).
- horizon (int) – The number of timesteps to consider for normalization.
Returns: The normalized tensor.
Return type: Tensor
####### Examples
>>> normalization = TSNormalization(method="mean", horizon=5)
>>> x = torch.randn(10, 20, 3) # A batch of 10 sequences, each of length 20 with 3 features
>>> normalized_x = normalization(x)
>>> print(normalized_x.shape) # Output: torch.Size([10, 20, 3])
>>> normalization_last = TSNormalization(method="last", horizon=5)
>>> normalized_last_x = normalization_last(x)
>>> print(normalized_last_x.shape) # Output: torch.Size([10, 20, 3])
NOTE
Ensure that the input tensor has the correct shape (B, L, D) before using this module.