|
| 1 | +""" |
| 2 | +The core wrapper assembles the submodules of TimeMixer forecasting model |
| 3 | +and takes over the forward progress of the algorithm. |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +# Created by Wenjie Du <[email protected]> |
| 8 | +# License: BSD-3-Clause |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | + |
| 13 | +from ...nn.functional import nonstationary_norm, nonstationary_denorm |
| 14 | +from ...nn.functional.error import calc_mse |
| 15 | +from ...nn.modules.timemixer import BackboneTimeMixer |
| 16 | + |
| 17 | + |
| 18 | +class _TimeMixer(nn.Module): |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + n_steps: int, |
| 22 | + n_features: int, |
| 23 | + n_pred_steps: int, |
| 24 | + n_pred_features: int, |
| 25 | + term: str, |
| 26 | + n_layers: int, |
| 27 | + d_model: int, |
| 28 | + d_ffn: int, |
| 29 | + dropout: float, |
| 30 | + top_k: int, |
| 31 | + channel_independence: bool, |
| 32 | + decomp_method: str, |
| 33 | + moving_avg: int, |
| 34 | + downsampling_layers: int, |
| 35 | + downsampling_window: int, |
| 36 | + apply_nonstationary_norm: bool = False, |
| 37 | + ): |
| 38 | + super().__init__() |
| 39 | + |
| 40 | + self.n_pred_steps = n_pred_steps |
| 41 | + self.n_pred_features = n_pred_features |
| 42 | + self.apply_nonstationary_norm = apply_nonstationary_norm |
| 43 | + |
| 44 | + assert term in ["long", "short"], "forecasting term should be either 'long' or 'short'" |
| 45 | + self.model = BackboneTimeMixer( |
| 46 | + task_name=term + "_term_forecast", |
| 47 | + n_steps=n_steps, |
| 48 | + n_features=n_features, |
| 49 | + n_pred_steps=n_pred_steps, |
| 50 | + n_pred_features=n_pred_features, |
| 51 | + n_layers=n_layers, |
| 52 | + d_model=d_model, |
| 53 | + d_ffn=d_ffn, |
| 54 | + dropout=dropout, |
| 55 | + channel_independence=channel_independence, |
| 56 | + decomp_method=decomp_method, |
| 57 | + top_k=top_k, |
| 58 | + moving_avg=moving_avg, |
| 59 | + downsampling_layers=downsampling_layers, |
| 60 | + downsampling_window=downsampling_window, |
| 61 | + downsampling_method="avg", |
| 62 | + use_future_temporal_feature=False, |
| 63 | + ) |
| 64 | + |
| 65 | + # for the imputation task, the output dim is the same as input dim |
| 66 | + self.output_projection = nn.Linear(n_features, n_pred_features) |
| 67 | + |
| 68 | + def forward(self, inputs: dict) -> dict: |
| 69 | + X, missing_mask = inputs["X"], inputs["missing_mask"] |
| 70 | + |
| 71 | + if self.training: |
| 72 | + X_pred, X_pred_missing_mask = inputs["X_pred"], inputs["X_pred_missing_mask"] |
| 73 | + else: |
| 74 | + batch_size = X.shape[0] |
| 75 | + X_pred, X_pred_missing_mask = ( |
| 76 | + torch.zeros(batch_size, self.n_pred_steps, self.n_pred_features), |
| 77 | + torch.ones(batch_size, self.n_pred_steps, self.n_pred_features), |
| 78 | + ) |
| 79 | + |
| 80 | + if self.apply_nonstationary_norm: |
| 81 | + # Normalization from Non-stationary Transformer |
| 82 | + X, means, stdev = nonstationary_norm(X, missing_mask) |
| 83 | + |
| 84 | + # TimesMixer processing |
| 85 | + enc_out = self.model.forecast(X, missing_mask) |
| 86 | + |
| 87 | + if self.apply_nonstationary_norm: |
| 88 | + # De-Normalization from Non-stationary Transformer |
| 89 | + enc_out = nonstationary_denorm(enc_out, means, stdev) |
| 90 | + |
| 91 | + # project back the original data space |
| 92 | + forecasting_result = self.output_projection(enc_out) |
| 93 | + # the raw output has length = n_steps+n_pred_steps, we only need the last n_pred_steps |
| 94 | + forecasting_result = forecasting_result[:, -self.n_pred_steps :] |
| 95 | + |
| 96 | + results = { |
| 97 | + "forecasting_data": forecasting_result, |
| 98 | + } |
| 99 | + |
| 100 | + # if in training mode, return results with losses |
| 101 | + if self.training: |
| 102 | + # `loss` is always the item for backward propagating to update the model |
| 103 | + results["loss"] = calc_mse(X_pred, forecasting_result, X_pred_missing_mask) |
| 104 | + |
| 105 | + return results |
0 commit comments