Implementation:Sktime Pytorch forecasting RevIN
| Knowledge Sources | |
|---|---|
| Domains | Time_Series, Forecasting, Deep_Learning |
| Last Updated | 2026-02-08 08:00 GMT |
Overview
RevIN implements Reversible Instance Normalization, a normalization layer that can both normalize and denormalize time series data to handle distribution shift between training and inference.
Description
The RevIN class provides two operating modes controlled by a mode parameter in the forward pass: "norm" for normalization and "denorm" for denormalization. During normalization, it computes per-instance mean and standard deviation statistics (or subtracts the last value when subtract_last is enabled), normalizes the input, and optionally applies learnable affine parameters. During denormalization, it reverses these transformations in the correct order, restoring the original scale and location. The statistics are stored between the norm and denorm calls, enabling paired usage in encoder-decoder architectures.
Usage
Use RevIN when building time series forecasting models that suffer from distribution shift or non-stationarity. Apply "norm" mode before feeding data into the model and "denorm" mode on the model output to restore the original data scale. It is widely used in models such as iTransformer and PatchTST to improve forecasting accuracy on data with shifting means and variances.
Code Reference
Source Location
- Repository: Sktime_Pytorch_forecasting
- File: pytorch_forecasting/layers/_normalization/_revin.py
- Lines: 1-80
Signature
class RevIN(nn.Module):
def __init__(self, num_features, eps=1e-5, affine=True, subtract_last=False):
...
def forward(self, x, mode: str):
...
Import
from pytorch_forecasting.layers import RevIN
I/O Contract
Inputs
__init__ Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| num_features | int | Yes | Number of input features (channels) in the time series. |
| eps | float | No | Small value added to the denominator for numerical stability. Default: 1e-5. |
| affine | bool | No | If True, the layer has learnable affine parameters (weight and bias). Default: True. |
| subtract_last | bool | No | If True, subtracts the last time step value instead of the mean for centering. Default: False. |
forward Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| x | torch.Tensor | Yes | Input tensor of shape (batch_size, seq_len, num_features). |
| mode | str | Yes | Operating mode: "norm" to normalize, "denorm" to denormalize. Must call "norm" before "denorm" to compute statistics. |
Outputs
| Name | Type | Description |
|---|---|---|
| x | torch.Tensor | Normalized or denormalized tensor of the same shape as input: (batch_size, seq_len, num_features). |
Usage Examples
import torch
from pytorch_forecasting.layers import RevIN
# Create RevIN layer for 7 features with learnable affine parameters
revin = RevIN(num_features=7, eps=1e-5, affine=True, subtract_last=False)
# Input: batch of 16 samples, 96 time steps, 7 features
x = torch.randn(16, 96, 7) * 10 + 50 # non-zero mean, large scale
# Normalize the input before passing to the model
x_norm = revin(x, mode="norm")
print(x_norm.mean(dim=1).abs().max()) # close to 0
# ... pass x_norm through the forecasting model to get predictions ...
# Simulate model output
pred_norm = torch.randn(16, 24, 7)
# Denormalize predictions to restore original scale
pred = revin(pred_norm, mode="denorm")
print(pred.shape) # torch.Size([16, 24, 7])