econometron.utils.data_preparation.scaler
RevINclass fromeconometron.utils.data_preparation.scaler
The RevIN class implements Reversible Instance Normalization, a normalization technique commonly used in deep learning for time series. It normalizes the input features while preserving the ability to reverse the transformation, which is especially useful for models where predictions must be transformed back to the original scale.
Overview
- Module:
torch.nn.Module - Purpose: Normalize input tensors across features and allow exact reversal of the transformation.
- Use Cases: Time series forecasting models, N-BEATS, or any network requiring reversible normalization for stability.
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
num_features | int | Number of input features (channels) to normalize. | — |
eps | float | Small constant added to standard deviation for numerical stability. | 1e-5 |
affine | bool | Whether to apply learnable scaling (gamma) and shifting (beta) after normalization. | True |
Methods
forward(x, mode: str)
Applies either normalization or denormalization to the input.
Parameters:
x(torch.Tensor): Input tensor of shape(batch_size, num_features, ...).mode(str):"norm"to normalize,"denorm"to reverse normalization.
Returns:
- For
"norm": Tuple(x_norm, (mean, std)), wherex_normis the normalized tensor. - For
"denorm": Tensor restored to original scale.
- For
Raises:
ValueErrorifmodeis not"norm"or"denorm".
_normalize(x) : mode norm
Internal method that normalizes the input tensor.
- Computes mean and standard deviation along the feature dimension.
- Optionally applies affine transformation (
gammaandbeta). - Returns normalized tensor and detached statistics
(mean, std)for denormalization.
_denormalize(x_tuple): mode denorm
Internal method that reverses normalization.
- Accepts a tuple
(x_norm, (mean, std)). - Reverts affine transformation if applied.
- Returns tensor in the original scale.
Features
- Reversible Normalization: Ensures that data can be restored to original scale after training or prediction.
- Affine Transformation: Learnable scaling and shifting for each feature.
- Numerical Stability: Small epsilon added to standard deviation prevents division by zero.
- Flexible Input: Works with multi-dimensional tensors, typically
(batch_size, features, sequence_length).
Notes
RevIN is particularly effective for non-stationary time series, where feature scaling per instance improves model training and convergence while retaining interpretability of outputs.
