forked from QData/spacetimeformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecaster.py
286 lines (242 loc) · 8.91 KB
/
forecaster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from abc import ABC, abstractmethod
from typing import Tuple
import torch
import torch.nn.functional as F
import pytorch_lightning as pl
import numpy as np
from revin import RevIN, SeriesDecomposition
from eval_stats import mape, mae, mse, smape
class Forecaster(pl.LightningModule, ABC):
def __init__(
self,
d_x: int,
d_yc: int,
d_yt: int,
learning_rate: float = 1e-3,
l2_coeff: float = 0,
loss: str = "mse",
linear_window: int = 0,
linear_shared_weights: bool = False,
use_revin: bool = False,
use_seasonal_decomp: bool = False,
verbose: int = True,
):
super().__init__()
qprint = lambda _msg_: print(_msg_) if verbose else None
qprint("Forecaster")
qprint(f"\tL2: {l2_coeff}")
qprint(f"\tLinear Window: {linear_window}")
qprint(f"\tLinear Shared Weights: {linear_shared_weights}")
qprint(f"\tRevIN: {use_revin}")
qprint(f"\tDecomposition: {use_seasonal_decomp}")
self._inv_scaler = lambda x: x
self.l2_coeff = l2_coeff
self.learning_rate = learning_rate
self.time_masked_idx = None
self.null_value = None
self.loss = loss
self.linear_model = lambda x, *args, **kwargs: 0.0
self.use_revin = use_revin
if use_revin:
assert d_yc == d_yt, "TODO: figure out exo case for revin"
self.revin = RevIN(num_features=d_yc)
else:
self.revin = lambda x, *args, **kwargs: x
self.use_seasonal_decomp = use_seasonal_decomp
if use_seasonal_decomp:
self.seasonal_decomp = SeriesDecomposition(kernel_size=25)
else:
self.seasonal_decomp = lambda x: (x, x.clone())
self.d_x = d_x
self.d_yc = d_yc
self.d_yt = d_yt
self.save_hyperparameters()
def set_null_value(self, val: float) -> None:
self.null_value = val
@property
@abstractmethod
def train_step_forward_kwargs(self):
return {}
@property
@abstractmethod
def eval_step_forward_kwargs(self):
return {}
def loss_fn(
self, true: torch.Tensor, preds: torch.Tensor, mask: torch.Tensor
) -> torch.Tensor:
true = torch.nan_to_num(true)
if self.loss == "mse":
loss = (mask * (true - preds)).square().sum() / max(mask.sum(), 1)
elif self.loss == "mae":
loss = torch.abs(mask * (true - preds)).sum() / max(mask.sum(), 1)
elif self.loss == "smape":
num = 2.0 * abs(preds - true)
den = abs(preds.detach()) + abs(true) + 1e-5
loss = 100.0 * (mask * (num / den)).sum() / max(mask.sum(), 1)
else:
raise ValueError(f"Unrecognized Loss Function : {self.loss}")
return loss
def forecasting_loss(
self, outputs: torch.Tensor, y_t: torch.Tensor, time_mask: int
) -> Tuple[torch.Tensor]:
if self.null_value is not None:
null_mask_mat = y_t != self.null_value
else:
null_mask_mat = torch.ones_like(y_t)
# genuine NaN failsafe
null_mask_mat *= ~torch.isnan(y_t)
time_mask_mat = torch.ones_like(y_t)
if time_mask is not None:
time_mask_mat[:, time_mask:] = False
full_mask = time_mask_mat * null_mask_mat
forecasting_loss = self.loss_fn(y_t, outputs, full_mask)
return forecasting_loss, full_mask
def compute_loss(
self,
batch: Tuple[torch.Tensor],
time_mask: int = None,
forward_kwargs: dict = {},
) -> Tuple[torch.Tensor]:
x_c, y_c, x_t, y_t = batch
outputs, *_ = self(x_c, y_c, x_t, y_t, **forward_kwargs)
loss, mask = self.forecasting_loss(
outputs=outputs, y_t=y_t, time_mask=time_mask
)
return loss, outputs, mask
def predict(
self,
x_c: torch.Tensor,
y_c: torch.Tensor,
x_t: torch.Tensor,
sample_preds: bool = False,
) -> torch.Tensor:
og_device = y_c.device
# move to model device
x_c = x_c.to(self.device).float()
x_t = x_t.to(self.device).float()
# move y_c to cpu if it isn't already there, scale, and then move back to the model device
y_c = torch.from_numpy(y_c.cpu().numpy()).to(self.device).float()
# create dummy y_t of zeros
y_t = (
torch.zeros((x_t.shape[0], x_t.shape[1], self.d_yt, self.d_yt)).to(self.device).float()
)
with torch.no_grad():
# gradient-free prediction
normalized_preds, *_ = self.forward(
x_c, y_c, x_t, y_t, **self.eval_step_forward_kwargs
)
# preds --> cpu --> inverse scale to original units --> original device of y_c
preds = (
torch.from_numpy(self._inv_scaler(normalized_preds.cpu().numpy()))
.to(og_device)
.float()
)
return preds
@abstractmethod
def forward_model_pass(
self,
x_c: torch.Tensor,
y_c: torch.Tensor,
x_t: torch.Tensor,
y_t: torch.Tensor,
**forward_kwargs,
) -> Tuple[torch.Tensor]:
return NotImplemented
def nan_to_num(self, *inps):
return (torch.nan_to_num(i) for i in inps)
def forward(
self,
x_c: torch.Tensor,
y_c: torch.Tensor,
x_t: torch.Tensor,
y_t: torch.Tensor,
**forward_kwargs,
) -> Tuple[torch.Tensor]:
x_c, y_c, x_t, y_t = self.nan_to_num(x_c, y_c, x_t, y_t)
_, pred_len, map, d_yt = y_t.shape
y_c = self.revin(y_c, mode="norm") # does nothing if use_revin = False
seasonal_yc, trend_yc = self.seasonal_decomp(
y_c
) # both are the original if use_seasonal_decomp = False
preds, *extra = self.forward_model_pass(
x_c, seasonal_yc, x_t, y_t, **forward_kwargs
)
baseline = self.linear_model(trend_yc, pred_len=pred_len, map=map, d_yt=d_yt)
output = self.revin(preds + baseline, mode="denorm")
if extra:
return (output,) + tuple(extra)
return (output,)
def _compute_stats(
self, pred: torch.Tensor, true: torch.Tensor, mask: torch.Tensor
):
pred = pred * mask
true = torch.nan_to_num(true) * mask
adj = mask.mean().cpu().numpy() + 1e-5
pred = pred.detach().cpu().numpy()
true = true.detach().cpu().numpy()
scaled_pred = self._inv_scaler(pred)
scaled_true = self._inv_scaler(true)
stats = {
"mape": mape(scaled_true, scaled_pred) / adj,
"mae": mae(scaled_true, scaled_pred) / adj,
"mse": mse(scaled_true, scaled_pred) / adj,
"smape": smape(scaled_true, scaled_pred) / adj,
"norm_mae": mae(true, pred) / adj,
"norm_mse": mse(true, pred) / adj,
}
return stats
def step(self, batch: Tuple[torch.Tensor], train: bool = False):
kwargs = (
self.train_step_forward_kwargs if train else self.eval_step_forward_kwargs
)
time_mask = self.time_masked_idx if train else None
loss, output, mask = self.compute_loss(
batch=batch,
time_mask=time_mask,
forward_kwargs=kwargs,
)
*_, y_t = batch
stats = self._compute_stats(output, y_t, mask)
stats["loss"] = loss
return stats
def training_step(self, batch, batch_idx):
return self.step(batch, train=True)
def validation_step(self, batch, batch_idx):
stats = self.step(batch, train=False)
self.current_val_stats = stats
return stats
def test_step(self, batch, batch_idx):
return self.step(batch, train=False)
def _log_stats(self, section, outs):
for key in outs.keys():
stat = outs[key]
if isinstance(stat, np.ndarray) or isinstance(stat, torch.Tensor):
stat = stat.mean()
self.log(f"{section}/{key}", stat, sync_dist=True)
def training_step_end(self, outs):
self._log_stats("train", outs)
return {"loss": outs["loss"].mean()}
def validation_step_end(self, outs):
self._log_stats("val", outs)
return outs
def test_step_end(self, outs):
self._log_stats("test", outs)
return {"loss": outs["loss"].mean()}
def predict_step(self, batch, batch_idx):
return self(*batch, **self.eval_step_forward_kwargs)
def configure_optimizers(self):
optimizer = torch.optim.Adam(
self.parameters(), lr=self.learning_rate, weight_decay=self.l2_coeff
)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
patience=3,
factor=0.2,
)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": scheduler,
"monitor": "val/loss",
},
}