diff --git a/README.md b/README.md index 7df93dfe..86e52e6f 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ This functionality is implemented with the [Microsoft NNI](https://github.com/mi | Neural Net | DLinear | Are Transformers Effective for Time Series Forecasting? [^17] | 2023 | | Neural Net | ETSformer | Exponential Smoothing Transformers for Time-series Forecasting [^19] | 2023 | | Neural Net | FEDformer | Frequency Enhanced Decomposed Transformer for Long-term Series Forecasting [^20] | 2022 | +| Neural Net | Informer | Beyond Efficient Transformer for Long Sequence Time-Series Forecasting [^21] | 2021 | | Neural Net | Autoformer | Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting [^15] | 2021 | | Neural Net | CSDI | Conditional Score-based Diffusion Models for Probabilistic Time Series Imputation [^12] | 2021 | | Neural Net | US-GAN | Unsupervised GAN for Multivariate Time Series Imputation [^10] | 2021 | @@ -332,6 +333,8 @@ PyPOTS community is open, transparent, and surely friendly. Let's work together [^18]: Nie, Y., Nguyen, N. H., Sinthong, P., & Kalagnanam, J. (2023). [A time series is worth 64 words: Long-term forecasting with transformers](https://openreview.net/forum?id=Jbdc0vTOcol). *ICLR 2023* [^19]: Woo, G., Liu, C., Sahoo, D., Kumar, A., & Hoi, S. (2023). [ETSformer: Exponential Smoothing Transformers for Time-series Forecasting](https://openreview.net/forum?id=5m_3whfo483). *ICLR 2023* [^20]: Zhou, T., Ma, Z., Wen, Q., Wang, X., Sun, L., & Jin, R. (2022). [FEDformer: Frequency enhanced decomposed transformer for long-term series forecasting](https://proceedings.mlr.press/v162/zhou22g.html). *ICML 2022*. +[^21]: Zhou, H., Zhang, S., Peng, J., Zhang, S., Li, J., Xiong, H., & Zhang, W. (2021). [Informer: Beyond efficient transformer for long sequence time-series forecasting](https://ojs.aaai.org/index.php/AAAI/article/view/17325). *AAAI 2021*. +
diff --git a/docs/index.rst b/docs/index.rst index e600ebb2..265c97b1 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -181,6 +181,7 @@ Imputation Neural Net PatchTST (A Time Series is Worth Imputation Neural Net DLinear (Are transformers effective for time series forecasting?) 2023 :cite:`zeng2023dlinear` Imputation Neural Net ETSformer (Exponential Smoothing Transformers for Time-series Forecasting) 2023 :cite:`woo2023etsformer` Imputation Neural Net FEDformer (Frequency Enhanced Decomposed Transformer for Long-term Series Forecasting) 2022 :cite:`zhou2022fedformer` +Imputation Neural Net Informer (Beyond Efficient Transformer for Long Sequence Time-Series Forecasting) 2021 :cite:`zhou2021informer` Imputation Neural Net Autoformer (Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting) 2021 :cite:`wu2021autoformer` Imputation Neural Net US-GAN (Unsupervised GAN for Multivariate Time Series Imputation) 2021 :cite:`miao2021SSGAN` Imputation Neural Net CSDI (Conditional Score-based Diffusion Models for Probabilistic Time Series Imputation) 2021 :cite:`tashiro2021csdi` diff --git a/docs/pypots.imputation.rst b/docs/pypots.imputation.rst index cd4d156a..1893591b 100644 --- a/docs/pypots.imputation.rst +++ b/docs/pypots.imputation.rst @@ -73,6 +73,15 @@ pypots.imputation.fedformer :show-inheritance: :inherited-members: +pypots.imputation.informer +------------------------------ + +.. automodule:: pypots.imputation.informer + :members: + :undoc-members: + :show-inheritance: + :inherited-members: + pypots.imputation.autoformer ------------------------------ diff --git a/docs/references.bib b/docs/references.bib index e4624d06..0d6eeabd 100644 --- a/docs/references.bib +++ b/docs/references.bib @@ -544,3 +544,13 @@ @inproceedings{zhou2022fedformer pdf = {https://proceedings.mlr.press/v162/zhou22g/zhou22g.pdf}, url = {https://proceedings.mlr.press/v162/zhou22g.html}, } + +@inproceedings{zhou2021informer, +title={Informer: Beyond efficient transformer for long sequence time-series forecasting}, +author={Zhou, Haoyi and Zhang, Shanghang and Peng, Jieqi and Zhang, Shuai and Li, Jianxin and Xiong, Hui and Zhang, Wancai}, +booktitle={Proceedings of the AAAI conference on artificial intelligence}, +volume={35}, +number={12}, +pages={11106--11115}, +year={2021} +} diff --git a/pypots/imputation/__init__.py b/pypots/imputation/__init__.py index a7052dcc..0d4e2184 100644 --- a/pypots/imputation/__init__.py +++ b/pypots/imputation/__init__.py @@ -16,6 +16,7 @@ from .etsformer import ETSformer from .fedformer import FEDformer from .crossformer import Crossformer +from .informer import Informer from .autoformer import Autoformer from .dlinear import DLinear from .patchtst import PatchTST @@ -36,6 +37,7 @@ "TimesNet", "PatchTST", "DLinear", + "Informer", "Autoformer", "BRITS", "MRNN", diff --git a/pypots/imputation/autoformer/modules/core.py b/pypots/imputation/autoformer/modules/core.py index d2d19f23..c3747fde 100644 --- a/pypots/imputation/autoformer/modules/core.py +++ b/pypots/imputation/autoformer/modules/core.py @@ -10,10 +10,10 @@ from .submodules import ( SeasonalLayerNorm, AutoformerEncoderLayer, - AutoformerEncoder, AutoCorrelation, AutoCorrelationLayer, ) +from ...informer.modules.submodules import InformerEncoder from ....nn.modules.transformer.embedding import DataEmbedding from ....utils.metrics import calc_mse @@ -43,7 +43,7 @@ def __init__( dropout=dropout, with_pos=False, ) - self.encoder = AutoformerEncoder( + self.encoder = InformerEncoder( [ AutoformerEncoderLayer( AutoCorrelationLayer( diff --git a/pypots/imputation/autoformer/modules/submodules.py b/pypots/imputation/autoformer/modules/submodules.py index 40665d05..6eb3d9e2 100644 --- a/pypots/imputation/autoformer/modules/submodules.py +++ b/pypots/imputation/autoformer/modules/submodules.py @@ -285,35 +285,6 @@ def forward(self, x, attn_mask=None): return res, attn -class AutoformerEncoder(nn.Module): - def __init__(self, attn_layers, conv_layers=None, norm_layer=None): - super().__init__() - self.attn_layers = nn.ModuleList(attn_layers) - self.conv_layers = ( - nn.ModuleList(conv_layers) if conv_layers is not None else None - ) - self.norm = norm_layer - - def forward(self, x, attn_mask=None): - attns = [] - if self.conv_layers is not None: - for attn_layer, conv_layer in zip(self.attn_layers, self.conv_layers): - x, attn = attn_layer(x, attn_mask=attn_mask) - x = conv_layer(x) - attns.append(attn) - x, attn = self.attn_layers[-1](x) - attns.append(attn) - else: - for attn_layer in self.attn_layers: - x, attn = attn_layer(x, attn_mask=attn_mask) - attns.append(attn) - - if self.norm is not None: - x = self.norm(x) - - return x, attns - - class AutoformerDecoderLayer(nn.Module): """ Autoformer decoder layer with the progressive decomposition architecture @@ -372,23 +343,3 @@ def forward(self, x, cross, x_mask=None, cross_mask=None): 1, 2 ) return x, residual_trend - - -class AutoformerDecoder(nn.Module): - def __init__(self, layers, norm_layer=None, projection=None): - super().__init__() - self.layers = nn.ModuleList(layers) - self.norm = norm_layer - self.projection = projection - - def forward(self, x, cross, x_mask=None, cross_mask=None, trend=None): - for layer in self.layers: - x, residual_trend = layer(x, cross, x_mask=x_mask, cross_mask=cross_mask) - trend = trend + residual_trend - - if self.norm is not None: - x = self.norm(x) - - if self.projection is not None: - x = self.projection(x) - return x, trend diff --git a/pypots/imputation/fedformer/modules/core.py b/pypots/imputation/fedformer/modules/core.py index 0be8b14f..895cf8d4 100644 --- a/pypots/imputation/fedformer/modules/core.py +++ b/pypots/imputation/fedformer/modules/core.py @@ -9,11 +9,11 @@ from .submodules import MultiWaveletTransform, FourierBlock from ...autoformer.modules.submodules import ( - AutoformerEncoder, AutoformerEncoderLayer, AutoCorrelationLayer, SeasonalLayerNorm, ) +from ...informer.modules.submodules import InformerEncoder from ....nn.modules.transformer.embedding import DataEmbedding from ....utils.metrics import calc_mse @@ -57,7 +57,7 @@ def __init__( f"Unsupported version: {version}. Please choose from ['Wavelets', 'Fourier']." ) - self.encoder = AutoformerEncoder( + self.encoder = InformerEncoder( [ AutoformerEncoderLayer( AutoCorrelationLayer( diff --git a/pypots/imputation/informer/__init__.py b/pypots/imputation/informer/__init__.py new file mode 100644 index 00000000..df52eeb2 --- /dev/null +++ b/pypots/imputation/informer/__init__.py @@ -0,0 +1,17 @@ +""" +The package of the partially-observed time-series imputation model Informer. + +Refer to the paper "Wu, H., Xu, J., Wang, J., & Long, M. (2021). +Informer: Decomposition transformers with auto-correlation for long-term series forecasting. NeurIPS 2021.". + +""" + +# Created by Wenjie Du +# License: BSD-3-Clause + + +from .model import Informer + +__all__ = [ + "Informer", +] diff --git a/pypots/imputation/informer/data.py b/pypots/imputation/informer/data.py new file mode 100644 index 00000000..bf6a146d --- /dev/null +++ b/pypots/imputation/informer/data.py @@ -0,0 +1,24 @@ +""" +Dataset class for Informer. +""" + +# Created by Wenjie Du +# License: BSD-3-Clause + +from typing import Union + +from ..saits.data import DatasetForSAITS + + +class DatasetForInformer(DatasetForSAITS): + """Actually Informer uses the same data strategy as SAITS, needs MIT for training.""" + + def __init__( + self, + data: Union[dict, str], + return_X_ori: bool, + return_labels: bool, + file_type: str = "h5py", + rate: float = 0.2, + ): + super().__init__(data, return_X_ori, return_labels, file_type, rate) diff --git a/pypots/imputation/informer/model.py b/pypots/imputation/informer/model.py new file mode 100644 index 00000000..a1e70da8 --- /dev/null +++ b/pypots/imputation/informer/model.py @@ -0,0 +1,323 @@ +""" +The implementation of Informer for the partially-observed time-series imputation task. + +Refer to the paper "Zhou, H., Zhang, S., Peng, J., Zhang, S., Li, J., Xiong, H., & Zhang, W. (2021). +Informer: Beyond efficient transformer for long sequence time-series forecasting. AAAI 2021". + +Notes +----- +Partial implementation uses code from https://github.com/zhouhaoyi/Informer2020 + +""" + +# Created by Wenjie Du +# License: BSD-3-Clause + +from typing import Union, Optional + +import numpy as np +import torch +from torch.utils.data import DataLoader + +from .data import DatasetForInformer +from .modules.core import _Informer +from ..base import BaseNNImputer +from ...data.base import BaseDataset +from ...data.checking import check_X_ori_in_val_set +from ...optim.adam import Adam +from ...optim.base import Optimizer +from ...utils.logging import logger + + +class Informer(BaseNNImputer): + """The PyTorch implementation of the Informer model. + Informer is originally proposed by Wu et al. in :cite:`zhou2021informer`. + + 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 Informer model. + + n_heads : + The number of heads in each layer of Informer. + + d_model : + The dimension of the model. + + d_ffn : + The dimension of the feed-forward network. + + factor : + The factor of the auto correlation mechanism for the Informer model. + + moving_avg_window_size : + The window size of moving average. + + dropout : + The dropout rate for the model. + + 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. + + References + ---------- + .. [1] `Zhou, Haoyi, Shanghang Zhang, Jieqi Peng, Shuai Zhang, Jianxin Li, Hui Xiong, and Wancai Zhang. + "Informer: Beyond efficient transformer for long sequence time-series forecasting." + In Proceedings of the AAAI conference on artificial intelligence, vol. 35, no. 12, pp. 11106-11115. 2021. + `_ + + """ + + def __init__( + self, + n_steps: int, + n_features: int, + n_layers: int, + n_heads: int, + d_model: int, + d_ffn: int, + factor: int, + dropout: float = 0, + batch_size: int = 32, + epochs: int = 100, + patience: 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", + ): + super().__init__( + batch_size, + epochs, + patience, + num_workers, + device, + saving_path, + model_saving_strategy, + ) + + self.n_steps = n_steps + self.n_features = n_features + # model hype-parameters + self.n_heads = n_heads + self.n_layers = n_layers + self.d_model = d_model + self.d_ffn = d_ffn + self.factor = factor + self.dropout = dropout + + # set up the model + self.model = _Informer( + self.n_steps, + self.n_features, + self.n_layers, + self.n_heads, + self.d_model, + self.d_ffn, + self.factor, + self.dropout, + ) + 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 = "h5py", + ) -> None: + # Step 1: wrap the input data with classes Dataset and DataLoader + training_set = DatasetForInformer( + train_set, return_X_ori=False, return_labels=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 check_X_ori_in_val_set(val_set): + raise ValueError("val_set must contain 'X_ori' for model validation.") + val_set = DatasetForInformer( + val_set, return_X_ori=True, return_labels=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 = "h5py", + ) -> dict: + """Make predictions for the input data with the trained model. + + Parameters + ---------- + test_set : dict or str + The dataset for model validating, should be a dictionary including keys as 'X', + or a path string locating a data file supported by PyPOTS (e.g. h5 file). + If it is a dict, X should be array-like of shape [n_samples, sequence length (time steps), n_features], + which is time-series data for validating, can contain missing values, and y should be array-like of shape + [n_samples], which is classification labels of X. + If it is a path string, the path should point to a data file, e.g. a h5 file, which contains + key-value pairs like a dict, and it has to include keys as 'X' and 'y'. + + file_type : str + The type of the given file if test_set is a path string. + + Returns + ------- + result_dict : dict, + The dictionary containing the clustering results and latent variables if necessary. + + """ + # Step 1: wrap the input data with classes Dataset and DataLoader + self.model.eval() # set the model as eval status to freeze it. + test_set = BaseDataset( + test_set, return_X_ori=False, return_labels=False, file_type=file_type + ) + test_loader = DataLoader( + test_set, + batch_size=self.batch_size, + shuffle=False, + num_workers=self.num_workers, + ) + imputation_collector = [] + + # Step 2: process the data with the model + 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) + imputation_collector.append(results["imputed_data"]) + + # Step 3: output collection and return + imputation = torch.cat(imputation_collector).cpu().detach().numpy() + result_dict = { + "imputation": imputation, + } + return result_dict + + def impute( + self, + X: Union[dict, str], + file_type="h5py", + ) -> 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/imputation/informer/modules/__init__.py b/pypots/imputation/informer/modules/__init__.py new file mode 100644 index 00000000..ceaa7ee3 --- /dev/null +++ b/pypots/imputation/informer/modules/__init__.py @@ -0,0 +1,6 @@ +""" + +""" + +# Created by Wenjie Du +# License: BSD-3-Clause diff --git a/pypots/imputation/informer/modules/core.py b/pypots/imputation/informer/modules/core.py new file mode 100644 index 00000000..455a7b1a --- /dev/null +++ b/pypots/imputation/informer/modules/core.py @@ -0,0 +1,86 @@ +""" + +""" + +# Created by Wenjie Du +# License: BSD-3-Clause + +import torch.nn as nn + +from .submodules import ProbAttention, ConvLayer, InformerEncoderLayer, InformerEncoder +from ....nn.modules.transformer.embedding import DataEmbedding +from ....nn.modules.transformer import MultiHeadAttention +from ....utils.metrics import calc_mse + + +class _Informer(nn.Module): + def __init__( + self, + n_steps, + n_features, + n_layers, + n_heads, + d_model, + d_ffn, + factor, + dropout, + distil=False, + activation="relu", + output_attention=False, + ): + super().__init__() + + self.seq_len = n_steps + self.n_layers = n_layers + self.enc_embedding = DataEmbedding( + n_features, + d_model, + dropout=dropout, + ) + self.encoder = InformerEncoder( + [ + InformerEncoderLayer( + MultiHeadAttention( + n_heads, + d_model, + d_model // n_heads, + d_model // n_heads, + ProbAttention(False, factor, dropout, output_attention), + ), + d_model, + d_ffn, + dropout, + activation, + ) + for _ in range(n_layers) + ], + [ConvLayer(d_model) for _ in range(n_layers - 1)] if distil else None, + norm_layer=nn.LayerNorm(d_model), + ) + + # for the imputation task, the output dim is the same as input dim + self.projection = nn.Linear(d_model, n_features) + + def forward(self, inputs: dict, training: bool = True) -> dict: + X, masks = inputs["X"], inputs["missing_mask"] + + # embedding + enc_out = self.enc_embedding(X) + + # Informer encoder processing + enc_out, attns = self.encoder(enc_out) + + # project back the original data space + dec_out = self.projection(enc_out) + + imputed_data = masks * X + (1 - masks) * dec_out + results = { + "imputed_data": imputed_data, + } + + if training: + # `loss` is always the item for backward propagating to update the model + loss = calc_mse(dec_out, inputs["X_ori"], inputs["indicating_mask"]) + results["loss"] = loss + + return results diff --git a/pypots/imputation/informer/modules/submodules.py b/pypots/imputation/informer/modules/submodules.py new file mode 100644 index 00000000..8465feeb --- /dev/null +++ b/pypots/imputation/informer/modules/submodules.py @@ -0,0 +1,240 @@ +""" + +""" + +# Created by Wenjie Du +# License: BSD-3-Clause + +from math import sqrt +from typing import Optional + +import numpy as np +import torch +import torch.fft +import torch.nn as nn +import torch.nn.functional as F + +from ....nn.modules.transformer.attention import AttentionOperator + + +class ProbMask: + def __init__(self, B, H, L, index, scores, device="cpu"): + _mask = torch.ones(L, scores.shape[-1], dtype=torch.bool).to(device).triu(1) + _mask_ex = _mask[None, None, :].expand(B, H, L, scores.shape[-1]) + indicator = _mask_ex[ + torch.arange(B)[:, None, None], torch.arange(H)[None, :, None], index, : + ].to(device) + self._mask = indicator.view(scores.shape).to(device) + + @property + def mask(self): + return self._mask + + +class ConvLayer(nn.Module): + def __init__(self, c_in): + super().__init__() + padding = 1 if torch.__version__ >= "1.5.0" else 2 + self.downConv = nn.Conv1d( + in_channels=c_in, + out_channels=c_in, + kernel_size=3, + padding=padding, + padding_mode="circular", + ) + self.norm = nn.BatchNorm1d(c_in) + self.activation = nn.ELU() + self.maxPool = nn.MaxPool1d(kernel_size=3, stride=2, padding=1) + + def forward(self, x): + x = self.downConv(x.permute(0, 2, 1)) + x = self.norm(x) + x = self.activation(x) + x = self.maxPool(x) + x = x.transpose(1, 2) + return x + + +class ProbAttention(AttentionOperator): + def __init__( + self, + mask_flag=True, + factor=5, + scale=None, + attention_dropout=0.1, + output_attention=False, + ): + super().__init__() + self.factor = factor + self.scale = scale + self.mask_flag = mask_flag + self.output_attention = output_attention + self.dropout = nn.Dropout(attention_dropout) + + def _prob_QK(self, Q, K, sample_k, n_top): # n_top: c*ln(L_q) + # Q [B, H, L, D] + B, H, L_K, E = K.shape + _, _, L_Q, _ = Q.shape + + # calculate the sampled Q_K + K_expand = K.unsqueeze(-3).expand(B, H, L_Q, L_K, E) + index_sample = torch.randint( + L_K, (L_Q, sample_k) + ) # real U = U_part(factor*ln(L_k))*L_q + K_sample = K_expand[:, :, torch.arange(L_Q).unsqueeze(1), index_sample, :] + Q_K_sample = torch.matmul(Q.unsqueeze(-2), K_sample.transpose(-2, -1)).squeeze( + -2 + ) + + # find the Top_k query with sparisty measurement + M = Q_K_sample.max(-1)[0] - torch.div(Q_K_sample.sum(-1), L_K) + M_top = M.topk(n_top, sorted=False)[1] + + # use the reduced Q to calculate Q_K + Q_reduce = Q[ + torch.arange(B)[:, None, None], torch.arange(H)[None, :, None], M_top, : + ] # factor*ln(L_q) + Q_K = torch.matmul(Q_reduce, K.transpose(-2, -1)) # factor*ln(L_q)*L_k + + return Q_K, M_top + + def _get_initial_context(self, V, L_Q): + B, H, L_V, D = V.shape + if not self.mask_flag: + # V_sum = V.sum(dim=-2) + V_sum = V.mean(dim=-2) + contex = V_sum.unsqueeze(-2).expand(B, H, L_Q, V_sum.shape[-1]).clone() + else: # use mask + assert L_Q == L_V # requires that L_Q == L_V, i.e. for self-attention only + contex = V.cumsum(dim=-2) + return contex + + def _update_context(self, context_in, V, scores, index, L_Q, attn_mask): + B, H, L_V, D = V.shape + + if self.mask_flag: + attn_mask = ProbMask(B, H, L_Q, index, scores, device=V.device) + scores.masked_fill_(attn_mask.mask, -np.inf) + + attn = torch.softmax(scores, dim=-1) # nn.Softmax(dim=-1)(scores) + + context_in[ + torch.arange(B)[:, None, None], torch.arange(H)[None, :, None], index, : + ] = torch.matmul(attn, V).type_as(context_in) + if self.output_attention: + attns = (torch.ones([B, H, L_V, L_V]) / L_V).type_as(attn).to(attn.device) + attns[ + torch.arange(B)[:, None, None], torch.arange(H)[None, :, None], index, : + ] = attn + return (context_in, attns) + else: + return (context_in, None) + + def forward( + self, + q: torch.Tensor, + k: torch.Tensor, + v: torch.Tensor, + attn_mask: Optional[torch.Tensor] = None, + **kwargs, + ): + + B, L_Q, H, D = q.shape + _, L_K, _, _ = k.shape + + q = q.transpose(2, 1) + k = k.transpose(2, 1) + v = v.transpose(2, 1) + + U_part = self.factor * np.ceil(np.log(L_K)).astype("int").item() # c*ln(L_k) + u = self.factor * np.ceil(np.log(L_Q)).astype("int").item() # c*ln(L_q) + + U_part = U_part if U_part < L_K else L_K + u = u if u < L_Q else L_Q + + scores_top, index = self._prob_QK(q, k, sample_k=U_part, n_top=u) + + # add scale factor + scale = self.scale or 1.0 / sqrt(D) + if scale is not None: + scores_top = scores_top * scale + # get the context + context = self._get_initial_context(v, L_Q) + # update the context with selected top_k queries + context, attn = self._update_context( + context, v, scores_top, index, L_Q, attn_mask + ) + + return context.transpose(2, 1).contiguous(), attn + + +class InformerEncoderLayer(nn.Module): + def __init__(self, attention, d_model, d_ff=None, dropout=0.1, activation="relu"): + super().__init__() + d_ff = d_ff or 4 * d_model + self.attention = attention + self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1) + self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1) + self.norm1 = nn.LayerNorm(d_model) + self.norm2 = nn.LayerNorm(d_model) + self.dropout = nn.Dropout(dropout) + self.activation = F.relu if activation == "relu" else F.gelu + + def forward(self, x, attn_mask=None): + new_x, attn = self.attention(x, x, x, attn_mask=attn_mask) + x = x + self.dropout(new_x) + + y = x = self.norm1(x) + y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1)))) + y = self.dropout(self.conv2(y).transpose(-1, 1)) + + return self.norm2(x + y), attn + + +class InformerEncoder(nn.Module): + def __init__(self, attn_layers, conv_layers=None, norm_layer=None): + super().__init__() + self.attn_layers = nn.ModuleList(attn_layers) + self.conv_layers = ( + nn.ModuleList(conv_layers) if conv_layers is not None else None + ) + self.norm = norm_layer + + def forward(self, x, attn_mask=None): + attns = [] + if self.conv_layers is not None: + for attn_layer, conv_layer in zip(self.attn_layers, self.conv_layers): + x, attn = attn_layer(x, attn_mask=attn_mask) + x = conv_layer(x) + attns.append(attn) + x, attn = self.attn_layers[-1](x) + attns.append(attn) + else: + for attn_layer in self.attn_layers: + x, attn = attn_layer(x, attn_mask=attn_mask) + attns.append(attn) + + if self.norm is not None: + x = self.norm(x) + + return x, attns + + +class InformerDecoder(nn.Module): + def __init__(self, layers, norm_layer=None, projection=None): + super().__init__() + self.layers = nn.ModuleList(layers) + self.norm = norm_layer + self.projection = projection + + def forward(self, x, cross, x_mask=None, cross_mask=None, trend=None): + for layer in self.layers: + x, residual_trend = layer(x, cross, x_mask=x_mask, cross_mask=cross_mask) + trend = trend + residual_trend + + if self.norm is not None: + x = self.norm(x) + + if self.projection is not None: + x = self.projection(x) + return x, trend diff --git a/tests/imputation/informer.py b/tests/imputation/informer.py new file mode 100644 index 00000000..ede0a15e --- /dev/null +++ b/tests/imputation/informer.py @@ -0,0 +1,128 @@ +""" +Test cases for Informer imputation model. +""" + +# Created by Wenjie Du +# License: BSD-3-Clause + + +import os.path +import unittest + +import numpy as np +import pytest + +from pypots.imputation import Informer +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, + H5_TRAIN_SET_PATH, + H5_VAL_SET_PATH, + H5_TEST_SET_PATH, + RESULT_SAVING_DIR_FOR_IMPUTATION, + check_tb_and_model_checkpoints_existence, +) + + +class TestInformer(unittest.TestCase): + logger.info("Running tests for an imputation model Informer...") + + # set the log and model saving path + saving_path = os.path.join(RESULT_SAVING_DIR_FOR_IMPUTATION, "Informer") + model_save_name = "saved_informer_model.pypots" + + # initialize an Adam optimizer + optimizer = Adam(lr=0.001, weight_decay=1e-5) + + # initialize a Informer model + informer = Informer( + DATA["n_steps"], + DATA["n_features"], + n_layers=2, + n_heads=2, + d_model=128, + d_ffn=256, + factor=3, + dropout=0, + epochs=EPOCHS, + saving_path=saving_path, + optimizer=optimizer, + device=DEVICE, + ) + + @pytest.mark.xdist_group(name="imputation-informer") + def test_0_fit(self): + self.informer.fit(TRAIN_SET, VAL_SET) + + @pytest.mark.xdist_group(name="imputation-informer") + def test_1_impute(self): + imputation_results = self.informer.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"Informer test_MSE: {test_MSE}") + + @pytest.mark.xdist_group(name="imputation-informer") + def test_2_parameters(self): + assert hasattr(self.informer, "model") and self.informer.model is not None + + assert ( + hasattr(self.informer, "optimizer") and self.informer.optimizer is not None + ) + + assert hasattr(self.informer, "best_loss") + self.assertNotEqual(self.informer.best_loss, float("inf")) + + assert ( + hasattr(self.informer, "best_model_dict") + and self.informer.best_model_dict is not None + ) + + @pytest.mark.xdist_group(name="imputation-informer") + 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.informer) + + # 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.informer.save(saved_model_path) + + # test loading the saved model, not necessary, but need to test + self.informer.load(saved_model_path) + + @pytest.mark.xdist_group(name="imputation-informer") + def test_4_lazy_loading(self): + self.informer.fit(H5_TRAIN_SET_PATH, H5_VAL_SET_PATH) + imputation_results = self.informer.predict(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 Informer test_MSE: {test_MSE}") + + +if __name__ == "__main__": + unittest.main()