Repository: WenjieDu/PyPOTS (language: python)

Issue:
Bug in RevIN for POTS data
### 1. System Info

PyPOTS v0.6

### 2. Information

- [X] The official example scripts
- [ ] My own created scripts

### 3. Reproduction

As shown below, for POTS data case, `x = x / self.stdev` is not applied.

https://github.com/WenjieDu/PyPOTS/blob/ac3318faa08a7eaf337597daf72bfc337f645997/pypots/nn/modules/revin/layers.py#L65-L74



### 4. Expected behavior

Normalization process has not been finished for POTS data case.
Update Imputeformer
<!-- 🚨Please create your PR to merge code from your branch to `dev` branch here, rather than `main`. -->

# What does this PR do?

<!--
Congrats! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution 😉.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, I will review your PR shortly. I may suggest changes to make the code even better 🤝.
-->

1. fixing #451;

## Before submitting

<!-- You can remove checks that are not relevant to this PR. -->

- [x] I have commented my code, particularly in hard-to-understand areas.
- [x] I have written necessary tests and already run them locally.



Discussion / hints:

Fix patch:
diff --git a/README.md b/README.md
index 4638a070..1a0d81de 100644
--- a/README.md
+++ b/README.md
@@ -112,6 +112,7 @@ The paper references and links are all listed at the bottom of this file.
 
 | **Type**      | **Algo**                    | **IMPU** | **FORE** | **CLAS** | **CLUS** | **ANOD** | **Year - Venue**   |
 |:--------------|:----------------------------|:--------:|:--------:|:--------:|:--------:|:--------:|:-------------------|
+| Neural Net    | ImputeFormer🧑‍🔧[^34]      |    ✅     |          |          |          |          | `2024 - KDD`       |
 | Neural Net    | iTransformer🧑‍🔧[^24]      |    ✅     |          |          |          |          | `2024 - ICLR`      |
 | Neural Net    | SAITS[^1]                   |    ✅     |          |          |          |          | `2023 - ESWA`      |
 | Neural Net    | FreTS🧑‍🔧[^23]             |    ✅     |          |          |          |          | `2023 - NeurIPS`   |
@@ -293,9 +294,9 @@ year={2023},
 }
 ```
 or
-> Wenjie Du. (2023).
+> Wenjie Du.
 > PyPOTS: a Python toolbox for data mining on Partially-Observed Time Series.
-> arXiv, abs/2305.18811. https://arxiv.org/abs/2305.18811
+> arXiv, abs/2305.18811, 2023.
 
 
 ## ❖ Contribution
@@ -380,7 +381,7 @@ PyPOTS community is open, transparent, and surely friendly. Let's work together
 [^31]: Kim, T., Kim, J., Tae, Y., Park, C., Choi, J. H., & Choo, J. (2022). [Reversible Instance Normalization for Accurate Time-Series Forecasting against Distribution Shift](https://openreview.net/forum?id=cGDAkQo1C0p). *ICLR 2022*.
 [^32]: Kitaev, N., Kaiser, Ł., & Levskaya, A. (2020). [Reformer: The Efficient Transformer](https://openreview.net/forum?id=0EXmFzUn5I). *ICLR 2020*.
 [^33]: Cao, D., Wang, Y., Duan, J., Zhang, C., Zhu, X., Huang, C., Tong, Y., Xu, B., Bai, J., Tong, J., & Zhang, Q. (2020). [Spectral Temporal Graph Neural Network for Multivariate Time-series Forecasting](https://proceedings.neurips.cc/paper/2020/hash/cdf6581cb7aca4b7e19ef136c6e601a5-Abstract.html). *NeurIPS 2020*.
-
+[^34]: Nie, T., Qin, G., Mei, Y., & Sun, J. (2024). [ImputeFormer: Low Rankness-Induced Transformers for Generalizable Spatiotemporal Imputation](https://arxiv.org/abs/2312.01728). *KDD 2024*.
 
 <details>
 <summary>🏠 Visits</summary>
diff --git a/README_zh.md b/README_zh.md
index f9ab4913..4ddee27b 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -98,6 +98,7 @@ PyPOTS当前支持多变量POTS数据的插补，预测，分类，聚类以及
 
 | **类型**      | **算法**                      | **插补** | **预测** | **分类** | **聚类** | **异常检测** | **年份 - 刊物**      |
 |:--------------|:----------------------------|:------:|:------:|:------:|:------:|:--------:|:-----------------|
+| Neural Net    | ImputeFormer🧑‍🔧[^34]      |    ✅     |          |          |          |          | `2024 - KDD`       |
 | Neural Net    | iTransformer🧑‍🔧[^24]      |    ✅     |          |          |          |          | `2024 - ICLR`      |
 | Neural Net    | SAITS[^1]                   |    ✅     |          |          |          |          | `2023 - ESWA`      |
 | Neural Net    | FreTS🧑‍🔧[^23]             |    ✅     |          |          |          |          | `2023 - NeurIPS`   |
@@ -145,7 +146,7 @@ PyPOTS当前支持多变量POTS数据的插补，预测，分类，聚类以及
 
 👈 在PyPOTS中，数据可以被看作是咖啡豆，而写的携带缺失值的POTS数据则是不完整的咖啡豆。
 为了让用户能够轻松使用各种开源的时间序列数据集，我们创建了开源时间序列数据集的仓库 Time Series Data Beans (TSDB)（可以将其视为咖啡豆仓库），
-TSDB让加载开源时序数据集变得超级简单！访问 [TSDB](https://github.com/WenjieDu/TSDB)，了解更多关于TSDB的信息，目前总共支持170个开源数据集！
+TSDB让加载开源时序数据集变得超级简单！访问 [TSDB](https://github.com/WenjieDu/TSDB)，了解更多关于TSDB的信息，目前总共支持172个开源数据集！
 
 <a href="https://github.com/WenjieDu/PyGrinder">
     <img src="https://pypots.com/figs/pypots_logos/PyGrinder/logo_FFBG.svg" align="right" width="140" alt="PyGrinder logo"/>
@@ -351,6 +352,7 @@ PyPOTS社区是一个开放、透明、友好的社区，让我们共同努力
 [^31]: Kim, T., Kim, J., Tae, Y., Park, C., Choi, J. H., & Choo, J. (2022). [Reversible Instance Normalization for Accurate Time-Series Forecasting against Distribution Shift](https://openreview.net/forum?id=cGDAkQo1C0p). *ICLR 2022*.
 [^32]: Kitaev, N., Kaiser, Ł., & Levskaya, A. (2020). [Reformer: The Efficient Transformer](https://openreview.net/forum?id=0EXmFzUn5I). *ICLR 2020*.
 [^33]: Cao, D., Wang, Y., Duan, J., Zhang, C., Zhu, X., Huang, C., Tong, Y., Xu, B., Bai, J., Tong, J., & Zhang, Q. (2020). [Spectral Temporal Graph Neural Network for Multivariate Time-series Forecasting](https://proceedings.neurips.cc/paper/2020/hash/cdf6581cb7aca4b7e19ef136c6e601a5-Abstract.html). *NeurIPS 2020*.
+[^34]: Nie, T., Qin, G., Mei, Y., & Sun, J. (2024). [ImputeFormer: Low Rankness-Induced Transformers for Generalizable Spatiotemporal Imputation](https://arxiv.org/abs/2312.01728). *KDD 2024*.
 
 
 <details>
diff --git a/docs/index.rst b/docs/index.rst
index 77049665..a72c0374 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -12,7 +12,7 @@ Welcome to PyPOTS docs!
 
 **A Python Toolbox for Machine Learning on Partially-Observed Time Series**
 
-.. image:: https://img.shields.io/badge/Python-v3.7+-E97040?logo=python&logoColor=white
+.. image:: https://img.shields.io/badge/Python-v3.8+-E97040?logo=python&logoColor=white
    :alt: Python version
    :target: https://docs.pypots.com/en/latest/install.html#reasons-of-version-limitations-on-dependencies
 
@@ -306,9 +306,9 @@ or
 
 ..
 
-   Wenjie Du. (2023).
+   Wenjie Du.
    PyPOTS: a Python toolbox for data mining on Partially-Observed Time Series.
-   arXiv, abs/2305.18811. https://doi.org/10.48550/arXiv.2305.18811
+   arXiv, abs/2305.18811, 2023.
 
 
 ❖ Contribution
diff --git a/docs/references.bib b/docs/references.bib
index 73c5c08f..7ef5bdd8 100644
--- a/docs/references.bib
+++ b/docs/references.bib
@@ -745,3 +745,14 @@ @inproceedings{xu2024fits
 year={2024},
 url={https://openreview.net/forum?id=bWcnvZ3qMb}
 }
+
+@article{nie2024imputeformer,
+title={ImputeFormer: Low Rankness-Induced Transformers for Generalizable Spatiotemporal Imputation},
+author={Nie, Tong and Qin, Guoyang and Ma, Wei and Mei, Yuewen and Sun, Jian},
+booktitle = {Proceedings of the 30th ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
+publisher = {Association for Computing Machinery},
+year={2024},
+series = {KDD '24},
+doi = {10.1145/3637528.3671751},
+url = {https://doi.org/10.1145/3637528.3671751},
+}
diff --git a/pypots/imputation/__init__.py b/pypots/imputation/__init__.py
index ed0fbd84..dbf168e3 100644
--- a/pypots/imputation/__init__.py
+++ b/pypots/imputation/__init__.py
@@ -34,6 +34,7 @@
 from .tide import TiDE
 from .grud import GRUD
 from .stemgnn import StemGNN
+from .imputeformer import ImputeFormer
 
 # naive imputation methods
 from .locf import LOCF
@@ -70,6 +71,7 @@
     "TiDE",
     "GRUD",
     "StemGNN",
+    "ImputeFormer",
     # naive imputation methods
     "LOCF",
     "Mean",
diff --git a/pypots/imputation/imputeformer/__init__.py b/pypots/imputation/imputeformer/__init__.py
new file mode 100644
index 00000000..a078d0e6
--- /dev/null
+++ b/pypots/imputation/imputeformer/__init__.py
@@ -0,0 +1,20 @@
+"""
+The package of the partially-observed time-series imputation model ImputeFormer.
+
+Refer to the papers
+`Tong Nie, Guoyang Qin, Wei Ma, Yuewen Mei, Jian Sun.
+"ImputeFormer: Low Rankness-Induced Transformers for Generalizable Spatiotemporal Imputation"
+KDD 2024.
+<https://doi.org/10.48550/arXiv.2312.01728>`_
+
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+
+from .model import ImputeFormer
+
+__all__ = [
+    "ImputeFormer",
+]
diff --git a/pypots/imputation/imputeformer/core.py b/pypots/imputation/imputeformer/core.py
new file mode 100644
index 00000000..a7ed10e5
--- /dev/null
+++ b/pypots/imputation/imputeformer/core.py
@@ -0,0 +1,151 @@
+"""
+The core wrapper assembles the submodules of ImputeFormer imputation model
+and takes over the forward progress of the algorithm.
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+import torch
+import torch.nn as nn
+
+from ...nn.modules.imputeformer import (
+    EmbeddedAttentionLayer,
+    ProjectedAttentionLayer,
+    MLP,
+)
+from ...nn.modules.saits import SaitsLoss
+
+
+class _ImputeFormer(nn.Module):
+    """
+    Spatiotemporal Imputation Transformer induced by low-rank factorization, KDD'24.
+    Note:
+        This is a simplified implementation under the SAITS framework (ORT+MIT).
+        The timestamp encoding is also removed for ease of implementation.
+    """
+
+    def __init__(
+        self,
+        n_steps: int,
+        n_features: int,
+        n_layers: int,
+        d_input_embed: int,
+        d_learnable_embed: int,
+        d_proj: int,
+        d_ffn: int,
+        n_temporal_heads: int,
+        dropout: float = 0.0,
+        input_dim: int = 1,
+        output_dim: int = 1,
+        ORT_weight: float = 1,
+        MIT_weight: float = 1,
+    ):
+        super().__init__()
+
+        self.n_nodes = n_features
+        self.in_steps = n_steps
+        self.out_steps = n_steps
+        self.input_dim = input_dim
+        self.output_dim = output_dim
+        self.input_embedding_dim = d_input_embed
+        self.learnable_embedding_dim = d_learnable_embed
+        self.model_dim = d_input_embed + d_learnable_embed
+
+        self.n_temporal_heads = n_temporal_heads
+        self.num_layers = n_layers
+        self.input_proj = nn.Linear(input_dim, self.input_embedding_dim)
+        self.d_proj = d_proj
+        self.d_ffn = d_ffn
+
+        self.learnable_embedding = nn.init.xavier_uniform_(
+            nn.Parameter(
+                torch.empty(self.in_steps, self.n_nodes, self.learnable_embedding_dim)
+            )
+        )
+
+        self.readout = MLP(self.model_dim, self.model_dim, output_dim, n_layers=2)
+
+        self.attn_layers_t = nn.ModuleList(
+            [
+                ProjectedAttentionLayer(
+                    self.n_nodes,
+                    self.d_proj,
+                    self.model_dim,
+                    self.n_temporal_heads,
+                    self.model_dim,
+                    dropout,
+                )
+                for _ in range(self.num_layers)
+            ]
+        )
+
+        self.attn_layers_s = nn.ModuleList(
+            [
+                EmbeddedAttentionLayer(
+                    self.model_dim,
+                    self.learnable_embedding_dim,
+                    self.d_ffn,
+                )
+                for _ in range(self.num_layers)
+            ]
+        )
+
+        # apply SAITS loss function to Transformer on the imputation task
+        self.saits_loss_func = SaitsLoss(ORT_weight, MIT_weight)
+
+    def forward(self, inputs: dict, training: bool = True) -> dict:
+        x, missing_mask = inputs["X"], inputs["missing_mask"]
+
+        # x: (batch_size, in_steps, num_nodes)
+        # Note that ImputeFormer is designed for Spatial-Temporal data that has the format [B, S, N, C],
+        # where N is the number of nodes and C is an additional feature dimension,
+        # We simply add an extra axis here for implementation.
+        x = x.unsqueeze(-1)  # [b s n c]
+        missing_mask = missing_mask.unsqueeze(-1)  # [b s n c]
+        batch_size = x.shape[0]
+        # Whiten missing values
+        x = x * missing_mask
+        x = self.input_proj(x)  # (batch_size, in_steps, num_nodes, input_embedding_dim)
+
+        # Learnable node embedding
+        node_emb = self.learnable_embedding.expand(
+            batch_size, *self.learnable_embedding.shape
+        )
+        x = torch.cat(
+            [x, node_emb], dim=-1
+        )  # (batch_size, in_steps, num_nodes, model_dim)
+
+        # Spatial and temporal processing with customized attention layers
+        x = x.permute(0, 2, 1, 3)  # [b n s c]
+        for att_t, att_s in zip(self.attn_layers_t, self.attn_layers_s):
+            x = att_t(x)
+            x = att_s(x, self.learnable_embedding, dim=1)
+
+        # Readout
+        x = x.permute(0, 2, 1, 3)  # [b s n c]
+        reconstruction = self.readout(x)
+        reconstruction = reconstruction.squeeze(-1)  # [b s n]
+        missing_mask = missing_mask.squeeze(-1)  # [b s n]
+
+        # Below is the SAITS processing pipeline:
+        # replace the observed part with values from X
+        imputed_data = missing_mask * inputs["X"] + (1 - missing_mask) * reconstruction
+
+        # ensemble the results as a dictionary for return
+        results = {
+            "imputed_data": imputed_data,
+        }
+
+        # if in training mode, return results with losses
+        if training:
+            X_ori, indicating_mask = inputs["X_ori"], inputs["indicating_mask"]
+            loss, ORT_loss, MIT_loss = self.saits_loss_func(
+                reconstruction, X_ori, missing_mask, indicating_mask
+            )
+            results["ORT_loss"] = ORT_loss
+            results["MIT_loss"] = MIT_loss
+            # `loss` is always the item for backward propagating to update the model
+            results["loss"] = loss
+
+        return results
diff --git a/pypots/imputation/imputeformer/data.py b/pypots/imputation/imputeformer/data.py
new file mode 100644
index 00000000..b13a77f7
--- /dev/null
+++ b/pypots/imputation/imputeformer/data.py
@@ -0,0 +1,22 @@
+"""
+Dataset class for the imputation model ImputeFormer.
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+from typing import Union
+
+from ..saits.data import DatasetForSAITS
+
+
+class DatasetForImputeFormer(DatasetForSAITS):
+    def __init__(
+        self,
+        data: Union[dict, str],
+        return_X_ori: bool,
+        return_y: bool,
+        file_type: str = "hdf5",
+        rate: float = 0.2,
+    ):
+        super().__init__(data, return_X_ori, return_y, file_type, rate)
diff --git a/pypots/imputation/imputeformer/model.py b/pypots/imputation/imputeformer/model.py
new file mode 100644
index 00000000..04a267bb
--- /dev/null
+++ b/pypots/imputation/imputeformer/model.py
@@ -0,0 +1,325 @@
+"""
+The package of the partially-observed time-series imputation model ImputeFormer.
+
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+from typing import Union, Optional
+
+import numpy as np
+import torch
+from torch.utils.data import DataLoader
+
+from .core import _ImputeFormer
+from .data import DatasetForImputeFormer
+from ..base import BaseNNImputer
+from ...data.checking import key_in_data_set
+from ...data.dataset import BaseDataset
+from ...optim.adam import Adam
+from ...optim.base import Optimizer
+from ...utils.logging import logger
+
+
+class ImputeFormer(BaseNNImputer):
+    """The PyTorch implementation of the ImputeFormer model.
+    ImputeFormer is originally proposed by Nie et al. in KDD'24: cite:`nie2024imputeformer`.
+
+
+    Parameters
+    ----------
+    n_steps :
+        The number of time steps in the time-series data sample.
+
+    n_features :
+        The number of features in the time-series data sample.
+
+    n_layers :
+        The number of layers in the 1st and 2nd DMSA blocks in the SAITS model.
+
+    d_input_embed :
+        The dimension of the input embedding.
+        It is the input dimension of the input embedding layer.
+
+    d_learnable_embed :
+        The dimension of the learnable node embedding.
+        It is the dimension of the learnable node embedding (spatial positional embedding)
+        used in spatial attention layers.
+
+    d_proj :
+        The dimension of the learnable projector.
+        It is the dimension of the learnable projector
+        used in temporal attention layers.
+
+    d_ffn :
+        The dimension of the layer in the Feed-Forward Networks (FFN).
+
+    dropout :
+        The dropout rate for all fully-connected layers in the model.
+
+    n_temporal_heads :
+        The number of attention heads in temporal attention layers.
+
+    input_dim :
+        The dimension of the input feature dimension, default is 1.
+
+    output_dim :
+        The dimension of the output feature dimension, default is 1.
+
+    ORT_weight :
+        The weight for the ORT loss.
+
+    MIT_weight :
+        The weight for the MIT loss.
+
+    batch_size :
+        The batch size for training and evaluating the model.
+
+    epochs :
+        The number of epochs for training the model.
+
+    patience :
+        The patience for the early-stopping mechanism. Given a positive integer, the training process will be
+        stopped when the model does not perform better after that number of epochs.
+        Leaving it default as None will disable the early-stopping.
+
+    optimizer :
+        The optimizer for model training.
+        If not given, will use a default Adam optimizer.
+
+    num_workers :
+        The number of subprocesses to use for data loading.
+        `0` means data loading will be in the main process, i.e. there won't be subprocesses.
+
+    device :
+        The device for the model to run on. It can be a string, a :class:`torch.device` object, or a list of them.
+        If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple),
+        then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models.
+        If given a list of devices, e.g. ['cuda:0', 'cuda:1'], or [torch.device('cuda:0'), torch.device('cuda:1')] , the
+        model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices).
+        Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.
+
+    saving_path :
+        The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during
+        training into a tensorboard file). Will not save if not given.
+
+    model_saving_strategy :
+        The strategy to save model checkpoints. It has to be one of [None, "best", "better", "all"].
+        No model will be saved when it is set as None.
+        The "best" strategy will only automatically save the best model after the training finished.
+        The "better" strategy will automatically save the model during training whenever the model performs
+        better than in previous epochs.
+        The "all" strategy will save every model after each epoch training.
+
+    verbose :
+        Whether to print out the training logs during the training process.
+    """
+
+    def __init__(
+        self,
+        n_steps: int,
+        n_features: int,
+        n_layers: int,
+        d_input_embed: int,
+        d_learnable_embed: int,
+        d_proj: int,
+        d_ffn: int,
+        n_temporal_heads: int,
+        dropout: float = 0.0,
+        input_dim: int = 1,
+        output_dim: int = 1,
+        ORT_weight: float = 1,
+        MIT_weight: float = 1,
+        batch_size: int = 32,
+        epochs: int = 100,
+        patience: Optional[int] = None,
+        optimizer: Optional[Optimizer] = Adam(),
+        num_workers: int = 0,
+        device: Optional[Union[str, torch.device, list]] = None,
+        saving_path: str = None,
+        model_saving_strategy: Optional[str] = "best",
+        verbose: bool = True,
+    ):
+        super().__init__(
+            batch_size,
+            epochs,
+            patience,
+            num_workers,
+            device,
+            saving_path,
+            model_saving_strategy,
+            verbose,
+        )
+
+        self.n_steps = n_steps
+        self.n_features = n_features
+        # model hype-parameters
+        self.n_layers = n_layers
+        self.d_input_embed = d_input_embed
+        self.d_learnable_embed = d_learnable_embed
+        self.d_proj = d_proj
+        self.d_ffn = d_ffn
+        self.n_temporal_heads = n_temporal_heads
+        self.dropout = dropout
+        self.input_dim = input_dim
+        self.output_dim = output_dim
+        self.ORT_weight = ORT_weight
+        self.MIT_weight = MIT_weight
+
+        # set up the model
+        self.model = _ImputeFormer(
+            self.n_steps,
+            self.n_features,
+            self.n_layers,
+            self.d_input_embed,
+            self.d_learnable_embed,
+            self.d_proj,
+            self.d_ffn,
+            self.n_temporal_heads,
+            self.dropout,
+            self.input_dim,
+            self.output_dim,
+            self.ORT_weight,
+            self.MIT_weight,
+        )
+        self._send_model_to_given_device()
+        self._print_model_size()
+
+        # set up the optimizer
+        self.optimizer = optimizer
+        self.optimizer.init_optimizer(self.model.parameters())
+
+    def _assemble_input_for_training(self, data: list) -> dict:
+        (
+            indices,
+            X,
+            missing_mask,
+            X_ori,
+            indicating_mask,
+        ) = self._send_data_to_given_device(data)
+
+        inputs = {
+            "X": X,
+            "missing_mask": missing_mask,
+            "X_ori": X_ori,
+            "indicating_mask": indicating_mask,
+        }
+
+        return inputs
+
+    def _assemble_input_for_validating(self, data: list) -> dict:
+        return self._assemble_input_for_training(data)
+
+    def _assemble_input_for_testing(self, data: list) -> dict:
+        indices, X, missing_mask = self._send_data_to_given_device(data)
+
+        inputs = {
+            "X": X,
+            "missing_mask": missing_mask,
+        }
+
+        return inputs
+
+    def fit(
+        self,
+        train_set: Union[dict, str],
+        val_set: Optional[Union[dict, str]] = None,
+        file_type: str = "hdf5",
+    ) -> None:
+        # Step 1: wrap the input data with classes Dataset and DataLoader
+        training_set = DatasetForImputeFormer(
+            train_set, return_X_ori=False, return_y=False, file_type=file_type
+        )
+        training_loader = DataLoader(
+            training_set,
+            batch_size=self.batch_size,
+            shuffle=True,
+            num_workers=self.num_workers,
+        )
+        val_loader = None
+        if val_set is not None:
+            if not key_in_data_set("X_ori", val_set):
+                raise ValueError("val_set must contain 'X_ori' for model validation.")
+            val_set = DatasetForImputeFormer(
+                val_set, return_X_ori=True, return_y=False, file_type=file_type
+            )
+            val_loader = DataLoader(
+                val_set,
+                batch_size=self.batch_size,
+                shuffle=False,
+                num_workers=self.num_workers,
+            )
+
+        # Step 2: train the model and freeze it
+        self._train_model(training_loader, val_loader)
+        self.model.load_state_dict(self.best_model_dict)
+        self.model.eval()  # set the model as eval status to freeze it.
+
+        # Step 3: save the model if necessary
+        self._auto_save_model_if_necessary(confirm_saving=True)
+
+    def predict(
+        self,
+        test_set: Union[dict, str],
+        file_type: str = "hdf5",
+    ) -> dict:
+        self.model.eval()  # set the model as eval status to freeze it.
+        test_set = BaseDataset(
+            test_set,
+            return_X_ori=False,
+            return_X_pred=False,
+            return_y=False,
+            file_type=file_type,
+        )
+        test_loader = DataLoader(
+            test_set,
+            batch_size=self.batch_size,
+            shuffle=False,
+            num_workers=self.num_workers,
+        )
+        imputation_collector = []
+
+        with torch.no_grad():
+            for idx, data in enumerate(test_loader):
+                inputs = self._assemble_input_for_testing(data)
+                results = self.model.forward(inputs, training=False)
+                imputed_data = results["imputed_data"]
+                imputation_collector.append(imputed_data)
+
+        imputation = torch.cat(imputation_collector).cpu().detach().numpy()
+        result_dict = {
+            "imputation": imputation,
+        }
+        return result_dict
+
+    def impute(
+        self,
+        X: Union[dict, str],
+        file_type: str = "hdf5",
+    ) -> np.ndarray:
+        """Impute missing values in the given data with the trained model.
+
+        Warnings
+        --------
+        The method impute is deprecated. Please use `predict()` instead.
+
+        Parameters
+        ----------
+        X :
+            The data samples for testing, should be array-like of shape [n_samples, sequence length (time steps),
+            n_features], or a path string locating a data file, e.g. h5 file.
+
+        file_type :
+            The type of the given file if X is a path string.
+
+        Returns
+        -------
+        array-like, shape [n_samples, sequence length (time steps), n_features],
+            Imputed data.
+        """
+        logger.warning(
+            "🚨DeprecationWarning: The method impute is deprecated. Please use `predict` instead."
+        )
+        results_dict = self.predict(X, file_type=file_type)
+        return results_dict["imputation"]
diff --git a/pypots/nn/modules/imputeformer/__init__.py b/pypots/nn/modules/imputeformer/__init__.py
new file mode 100644
index 00000000..d4ee589c
--- /dev/null
+++ b/pypots/nn/modules/imputeformer/__init__.py
@@ -0,0 +1,27 @@
+"""
+The package including the modules of ImputeFormer.
+
+Refer to the paper
+`Tong Nie, Guoyang Qin, Wei Ma, Yuewen Mei, Jian Sun.
+ImputeFormer: Low Rankness-Induced Transformers for Generalizable Spatiotemporal Imputation.
+KDD, 2024.
+<https://doi.org/10.48550/arXiv.2312.01728>`_
+
+Notes
+-----
+This implementation is inspired by the official one https://github.com/WenjieDu/SAITS
+
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+
+from .attention import EmbeddedAttentionLayer, ProjectedAttentionLayer
+from .mlp import MLP
+
+__all__ = [
+    "EmbeddedAttentionLayer",
+    "ProjectedAttentionLayer",
+    "MLP",
+]
diff --git a/pypots/nn/modules/imputeformer/attention.py b/pypots/nn/modules/imputeformer/attention.py
new file mode 100644
index 00000000..b9e982c8
--- /dev/null
+++ b/pypots/nn/modules/imputeformer/attention.py
@@ -0,0 +1,216 @@
+"""
+The implementation of the customized spatia-temporal modules for ImputeFormer :cite:`nie2024imputeformer`
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+import torch
+import torch.nn as nn
+from einops import repeat
+
+
+class AttentionLayer(nn.Module):
+    """Perform attention across the -2 dim (the -1 dim is `model_dim`).
+
+    Make sure the tensor is permuted to correct shape before attention.
+
+    E.g.
+    - Input shape (batch_size, in_steps, num_nodes, model_dim).
+    - Then the attention will be performed across the nodes.
+
+    Also, it supports different src and tgt length.
+
+    But must `src length == K length == V length`.
+
+    """
+
+    def __init__(self, model_dim, num_heads=8, mask=False):
+        super().__init__()
+
+        self.model_dim = model_dim
+        self.num_heads = num_heads
+        self.mask = mask
+        self.head_dim = model_dim // num_heads
+
+        self.FC_Q = nn.Linear(model_dim, model_dim)
+        self.FC_K = nn.Linear(model_dim, model_dim)
+        self.FC_V = nn.Linear(model_dim, model_dim)
+
+        self.out_proj = nn.Linear(model_dim, model_dim)
+
+    def forward(self, query, key, value):
+        # Q    (batch_size, ..., tgt_length, model_dim)
+        # K, V (batch_size, ..., src_length, model_dim)
+        batch_size = query.shape[0]
+        tgt_length = query.shape[-2]
+        src_length = key.shape[-2]
+
+        query = self.FC_Q(query)
+        key = self.FC_K(key)
+        value = self.FC_V(value)
+
+        # Qhead, Khead, Vhead (num_heads * batch_size, ..., length, head_dim)
+        query = torch.cat(torch.split(query, self.head_dim, dim=-1), dim=0)
+        key = torch.cat(torch.split(key, self.head_dim, dim=-1), dim=0)
+        value = torch.cat(torch.split(value, self.head_dim, dim=-1), dim=0)
+
+        key = key.transpose(
+            -1, -2
+        )  # (num_heads * batch_size, ..., head_dim, src_length)
+
+        attn_score = (
+            query @ key
+        ) / self.head_dim**0.5  # (num_heads * batch_size, ..., tgt_length, src_length)
+
+        if self.mask:
+            mask = torch.ones(
+                tgt_length, src_length, dtype=torch.bool, device=query.device
+            ).tril()  # lower triangular part of the matrix
+            attn_score.masked_fill_(~mask, -torch.inf)  # fill in-place
+
+        attn_score = torch.softmax(attn_score, dim=-1)
+        out = attn_score @ value  # (num_heads * batch_size, ..., tgt_length, head_dim)
+        out = torch.cat(
+            torch.split(out, batch_size, dim=0), dim=-1
+        )  # (batch_size, ..., tgt_length, head_dim * num_heads = model_dim)
+
+        out = self.out_proj(out)
+
+        return out
+
+
+class ProjectedAttentionLayer(nn.Module):
+    """
+    Temporal projected attention layer.
+    A low-rank factorization is achieved in the temporal attention matrix.
+    """
+
+    def __init__(
+        self,
+        seq_len,
+        dim_proj,
+        d_model,
+        n_heads,
+        d_ff=None,
+        dropout=0.1,
+    ):
+        super().__init__()
+        d_ff = d_ff or 4 * d_model
+        self.out_attn = AttentionLayer(d_model, n_heads, mask=None)
+        self.in_attn = AttentionLayer(d_model, n_heads, mask=None)
+        self.projector = nn.Parameter(torch.randn(seq_len, dim_proj, d_model))
+        # self.projector = nn.Parameter(torch.randn(dim_proj, d_model))
+
+        self.dropout = nn.Dropout(dropout)
+        self.norm1 = nn.LayerNorm(d_model)
+        self.norm2 = nn.LayerNorm(d_model)
+        self.MLP = nn.Sequential(
+            nn.Linear(d_model, d_ff), nn.GELU(), nn.Linear(d_ff, d_model)
+        )
+        self.seq_len = seq_len
+
+    def forward(self, x):
+        # x: [b s n d]
+        batch = x.shape[0]
+        projector = repeat(
+            self.projector,
+            "seq_len dim_proj d_model -> repeat seq_len dim_proj d_model",
+            repeat=batch,
+        )  # [b, s, c, d]
+        # projector = repeat(self.projector, 'dim_proj d_model -> repeat seq_len dim_proj d_model',
+        #                       repeat=batch, seq_len=self.seq_len)  # [b, s, c, d]
+
+        message_out = self.out_attn(
+            projector, x, x
+        )  # [b, s, c, d] <-> [b s n d] -> [b s c d]
+        message_in = self.in_attn(
+            x, projector, message_out
+        )  # [b s n d] <-> [b, s, c, d] -> [b s n d]
+        message = x + self.dropout(message_in)
+        message = self.norm1(message)
+        message = message + self.dropout(self.MLP(message))
+        message = self.norm2(message)
+
+        return message
+
+
+class EmbeddedAttention(nn.Module):
+    """
+    Spatial embedded attention layer.
+    The node embedding serves as the query and key matrices for attentive aggregation on graphs.
+    """
+
+    def __init__(self, model_dim, node_embedding_dim):
+        super().__init__()
+
+        self.model_dim = model_dim
+        self.FC_Q_K = nn.Linear(node_embedding_dim, model_dim)
+        self.FC_V = nn.Linear(model_dim, model_dim)
+        self.out_proj = nn.Linear(model_dim, model_dim)
+
+    def forward(self, value, emb):
+        # V (batch_size, ..., seq_length, model_dim)
+        # emb (..., length, model_dim)
+        batch_size = value.shape[0]
+        query = self.FC_Q_K(emb)
+        key = self.FC_Q_K(emb)
+        value = self.FC_V(value)
+
+        # Q, K (..., length, model_dim)
+        # V (batch_size, ..., length, model_dim)
+        key = key.transpose(-1, -2)  # (..., model_dim, src_length)
+        # attn_score = query @ key  # (..., tgt_length, src_length)
+        # attn_score = torch.softmax(attn_score, dim=-1)
+        # attn_score = repeat(attn_score, 'n s1 s2 -> b n s1 s2', b=batch_size)
+
+        # re-normalization
+        query = torch.softmax(query, dim=-1)
+        key = torch.softmax(key, dim=-1)
+        query = repeat(query, "n s1 s2 -> b n s1 s2", b=batch_size)
+        key = repeat(key, "n s2 s1 -> b n s2 s1", b=batch_size)
+
+        # out = attn_score @ value  # (batch_size, ..., tgt_length, model_dim)
+        out = key @ value  # (batch_size, ..., tgt_length, model_dim)
+        out = query @ out  # (batch_size, ..., tgt_length, model_dim)
+
+        return out
+
+
+class EmbeddedAttentionLayer(nn.Module):
+    def __init__(
+        self,
+        model_dim,
+        node_embedding_dim,
+        feed_forward_dim=2048,
+        dropout=0,
+    ):
+        super().__init__()
+
+        self.attn = EmbeddedAttention(model_dim, node_embedding_dim)
+        self.feed_forward = nn.Sequential(
+            nn.Linear(model_dim, feed_forward_dim),
+            nn.ReLU(inplace=True),
+            nn.Linear(feed_forward_dim, model_dim),
+        )
+        self.ln1 = nn.LayerNorm(model_dim)
+        self.ln2 = nn.LayerNorm(model_dim)
+        self.dropout1 = nn.Dropout(dropout)
+        self.dropout2 = nn.Dropout(dropout)
+
+    def forward(self, x, emb, dim=-2):
+        x = x.transpose(dim, -2)
+        # x: (batch_size, ..., length, model_dim)
+        # emb: (..., length, model_dim)
+        residual = x
+        out = self.attn(x, emb)  # (batch_size, ..., length, model_dim)
+        out = self.dropout1(out)
+        out = self.ln1(residual + out)
+
+        residual = out
+        out = self.feed_forward(out)  # (batch_size, ..., length, model_dim)
+        out = self.dropout2(out)
+        out = self.ln2(residual + out)
+
+        out = out.transpose(dim, -2)
+        return out
diff --git a/pypots/nn/modules/imputeformer/mlp.py b/pypots/nn/modules/imputeformer/mlp.py
new file mode 100644
index 00000000..eb8d6288
--- /dev/null
+++ b/pypots/nn/modules/imputeformer/mlp.py
@@ -0,0 +1,56 @@
+"""
+The implementation of the MLPs for ImputeFormer :cite:`nie2024imputeformer`
+"""
+
+# Created by Tong Nie <nietong@tongji.edu.cn> and Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+import torch.nn as nn
+
+
+class Dense(nn.Module):
+    """A simple fully-connected layer."""
+
+    def __init__(self, input_size, output_size, dropout=0.0, bias=True):
+        super(Dense, self).__init__()
+        self.layer = nn.Sequential(
+            nn.Linear(input_size, output_size, bias=bias),
+            nn.ReLU(),
+            nn.Dropout(dropout) if dropout > 0.0 else nn.Identity(),
+        )
+
+    def forward(self, x):
+        return self.layer(x)
+
+
+class MLP(nn.Module):
+    """
+    Simple Multi-layer Perceptron encoder with optional linear readout.
+    """
+
+    def __init__(
+        self, input_size, hidden_size, output_size=None, n_layers=1, dropout=0.0
+    ):
+        super(MLP, self).__init__()
+
+        layers = [
+            Dense(
+                input_size=input_size if i == 0 else hidden_size,
+                output_size=hidden_size,
+                dropout=dropout,
+            )
+            for i in range(n_layers)
+        ]
+        self.mlp = nn.Sequential(*layers)
+
+        if output_size is not None:
+            self.readout = nn.Linear(hidden_size, output_size)
+        else:
+            self.register_parameter("readout", None)
+
+    def forward(self, x, u=None):
+        """"""
+        out = self.mlp(x)
+        if self.readout is not None:
+            return self.readout(out)
+        return out
diff --git a/pypots/nn/modules/revin/layers.py b/pypots/nn/modules/revin/layers.py
index f7f54368..21719830 100644
--- a/pypots/nn/modules/revin/layers.py
+++ b/pypots/nn/modules/revin/layers.py
@@ -11,25 +11,27 @@
 
 
 class RevIN(nn.Module):
+    """RevIN: Reversible Inference Network.
+
+    Parameters
+    ----------
+    n_features :
+        the number of features or channels
+
+    eps :
+        a value added for numerical stability
+
+    affine :
+        if True, RevIN has learnable affine parameters
+
+    """
+
     def __init__(
         self,
         n_features: int,
-        eps: float = 1e-5,
+        eps: float = 1e-9,
         affine: bool = True,
     ):
-        """
-        Parameters
-        ----------
-        n_features :
-            the number of features or channels
-
-        eps :
-            a value added for numerical stability
-
-        affine :
-            if True, RevIN has learnable affine parameters
-
-        """
         super().__init__()
         self.n_features = n_features
         self.eps = eps
@@ -54,34 +56,42 @@ def _init_params(self):
     def _normalize(self, x, missing_mask=None):
         dim2reduce = tuple(range(1, x.ndim - 1))
 
+        # calculate mean and stdev
         if missing_mask is None:
             # original implementation
-            self.mean = torch.mean(x, dim=dim2reduce, keepdim=True).detach()
-            self.stdev = torch.sqrt(
+            mean = torch.mean(x, dim=dim2reduce, keepdim=True)
+            stdev = torch.sqrt(
                 torch.var(x, dim=dim2reduce, keepdim=True, unbiased=False) + self.eps
-            ).detach()
-            x = x - self.mean
-            x = x / self.stdev
+            )
         else:
             # pypots implementation for POTS data
             missing_sum = (
-                torch.sum(missing_mask == 1, dim=dim2reduce, keepdim=True) + 1e-9
+                torch.sum(missing_mask == 1, dim=dim2reduce, keepdim=True) + self.eps
             )
-            self.mean = torch.sum(x, dim=dim2reduce, keepdim=True) / missing_sum
-            x = x - self.mean
+            mean = torch.sum(x, dim=dim2reduce, keepdim=True) / missing_sum
             x_enc = x.masked_fill(missing_mask == 0, 0)
-            variance = torch.sum(x_enc * x_enc, dim=dim2reduce, keepdim=True) + 1e-9
-            self.stdev = torch.sqrt(variance / missing_sum)
+            variance = torch.sum(x_enc * x_enc, dim=dim2reduce, keepdim=True) + self.eps
+            stdev = torch.sqrt(variance / missing_sum)
+
+        # detach mean and stdev to avoid backpropagation
+        self.mean = mean.detach()
+        self.stdev = stdev.detach()
+        # normalize the input
+        x = x - self.mean
+        x = x / self.stdev
 
         if self.affine:
+            # apply affine transformation
             x = x * self.affine_weight
             x = x + self.affine_bias
         return x
 
     def _denormalize(self, x):
+        # reverse affine transformation
         if self.affine:
             x = x - self.affine_bias
-            x = x / (self.affine_weight + self.eps * self.eps)
+            x = x / (self.affine_weight + self.eps)
+        # denormalize the input
         x = x * self.stdev
         x = x + self.mean
         return x


Test patch:
diff --git a/tests/imputation/imputeformer.py b/tests/imputation/imputeformer.py
new file mode 100644
index 00000000..0257805d
--- /dev/null
+++ b/tests/imputation/imputeformer.py
@@ -0,0 +1,132 @@
+"""
+Test cases for ImputeFormer imputation model.
+"""
+
+# Created by Wenjie Du <wenjay.du@gmail.com>
+# License: BSD-3-Clause
+
+
+import os.path
+import unittest
+
+import numpy as np
+import pytest
+
+from pypots.imputation import ImputeFormer
+from pypots.optim import Adam
+from pypots.utils.logging import logger
+from pypots.utils.metrics import calc_mse
+from tests.global_test_config import (
+    DATA,
+    EPOCHS,
+    DEVICE,
+    TRAIN_SET,
+    VAL_SET,
+    TEST_SET,
+    GENERAL_H5_TRAIN_SET_PATH,
+    GENERAL_H5_VAL_SET_PATH,
+    GENERAL_H5_TEST_SET_PATH,
+    RESULT_SAVING_DIR_FOR_IMPUTATION,
+    check_tb_and_model_checkpoints_existence,
+)
+
+
+class TestImputeFormer(unittest.TestCase):
+    logger.info("Running tests for an imputation model ImputeFormer...")
+
+    # set the log and model saving path
+    saving_path = os.path.join(RESULT_SAVING_DIR_FOR_IMPUTATION, "ImputeFormer")
+    model_save_name = "saved_imputeformer_model.pypots"
+
+    # initialize an Adam optimizer
+    optimizer = Adam(lr=0.001, weight_decay=1e-5)
+
+    # initialize a ImputeFormer model
+    imputeformer = ImputeFormer(
+        DATA["n_steps"],
+        DATA["n_features"],
+        n_layers=2,
+        d_input_embed=32,
+        d_learnable_embed=32,
+        d_proj=32,
+        d_ffn=32,
+        n_temporal_heads=2,
+        dropout=0,
+        epochs=EPOCHS,
+        saving_path=saving_path,
+        optimizer=optimizer,
+        device=DEVICE,
+    )
+
+    @pytest.mark.xdist_group(name="imputation-imputeformer")
+    def test_0_fit(self):
+        self.imputeformer.fit(TRAIN_SET, VAL_SET)
+
+    @pytest.mark.xdist_group(name="imputation-imputeformer")
+    def test_1_impute(self):
+        imputation_results = self.imputeformer.predict(TEST_SET)
+        assert not np.isnan(
+            imputation_results["imputation"]
+        ).any(), "Output still has missing values after running impute()."
+
+        test_MSE = calc_mse(
+            imputation_results["imputation"],
+            DATA["test_X_ori"],
+            DATA["test_X_indicating_mask"],
+        )
+        logger.info(f"ImputeFormer test_MSE: {test_MSE}")
+
+    @pytest.mark.xdist_group(name="imputation-imputeformer")
+    def test_2_parameters(self):
+        assert (
+            hasattr(self.imputeformer, "model") and self.imputeformer.model is not None
+        )
+
+        assert (
+            hasattr(self.imputeformer, "optimizer")
+            and self.imputeformer.optimizer is not None
+        )
+
+        assert hasattr(self.imputeformer, "best_loss")
+        self.assertNotEqual(self.imputeformer.best_loss, float("inf"))
+
+        assert (
+            hasattr(self.imputeformer, "best_model_dict")
+            and self.imputeformer.best_model_dict is not None
+        )
+
+    @pytest.mark.xdist_group(name="imputation-imputeformer")
+    def test_3_saving_path(self):
+        # whether the root saving dir exists, which should be created by save_log_into_tb_file
+        assert os.path.exists(
+            self.saving_path
+        ), f"file {self.saving_path} does not exist"
+
+        # check if the tensorboard file and model checkpoints exist
+        check_tb_and_model_checkpoints_existence(self.imputeformer)
+
+        # save the trained model into file, and check if the path exists
+        saved_model_path = os.path.join(self.saving_path, self.model_save_name)
+        self.imputeformer.save(saved_model_path)
+
+        # test loading the saved model, not necessary, but need to test
+        self.imputeformer.load(saved_model_path)
+
+    @pytest.mark.xdist_group(name="imputation-imputeformer")
+    def test_4_lazy_loading(self):
+        self.imputeformer.fit(GENERAL_H5_TRAIN_SET_PATH, GENERAL_H5_VAL_SET_PATH)
+        imputation_results = self.imputeformer.predict(GENERAL_H5_TEST_SET_PATH)
+        assert not np.isnan(
+            imputation_results["imputation"]
+        ).any(), "Output still has missing values after running impute()."
+
+        test_MSE = calc_mse(
+            imputation_results["imputation"],
+            DATA["test_X_ori"],
+            DATA["test_X_indicating_mask"],
+        )
+        logger.info(f"Lazy-loading ImputeFormer test_MSE: {test_MSE}")
+
+
+if __name__ == "__main__":
+    unittest.main()