Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion baselines/DeepAR/arch/deepar_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,9 @@ def forward(self, history_data: torch.Tensor, future_data: torch.Tensor, train:
mus = torch.concat(mus, dim=1)
sigmas = torch.concat(sigmas, dim=1)
reals = input_feat_full[:, -preds.shape[1]:, :, :]

if not train:
preds = preds[:, -len_out:, :, :]
reals = reals[:, -len_out:, :, :]
mus = mus[:, -len_out:, :, :]
sigmas = sigmas[:, -len_out:, :, :]
return {"prediction": preds, "target": reals, "mus": mus, "sigmas": sigmas}
49 changes: 0 additions & 49 deletions baselines/DeepAR/runner/deepar_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,55 +36,6 @@ def postprocessing(self, input_data: Dict) -> Dict:
# TODO: add more postprocessing steps as needed.
return input_data

@torch.no_grad()
@master_only
def test(self, train_epoch: Optional[int] = None, save_metrics: bool = False, save_results: bool = False) -> Dict:
"""Test process.

Args:
train_epoch (Optional[int]): Current epoch if in training process.
save_metrics (bool): Save the test metrics. Defaults to False.
save_results (bool): Save the test results. Defaults to False.
"""

prediction, target, inputs = [], [], []

for data in tqdm(self.test_data_loader):
forward_return = self.forward(data, epoch=None, iter_num=None, train=False)
loss = self.metric_forward(self.loss, forward_return)
self.update_epoch_meter('test/loss', loss.item())

if not self.if_evaluate_on_gpu:
forward_return['prediction'] = forward_return['prediction'].detach().cpu()
forward_return['target'] = forward_return['target'].detach().cpu()
forward_return['inputs'] = forward_return['inputs'].detach().cpu()

prediction.append(forward_return['prediction'])
target.append(forward_return['target'])
inputs.append(forward_return['inputs'])

prediction = torch.cat(prediction, dim=0)
target = torch.cat(target, dim=0)
inputs = torch.cat(inputs, dim=0)

returns_all = {'prediction': prediction[:, -self.output_seq_len:, :, :],
'target': target[:, -self.output_seq_len:, :, :],
'inputs': inputs}
metrics_results = self.compute_evaluation_metrics(returns_all)

# save
if save_results:
# save returns_all to self.ckpt_save_dir/test_results.npz
test_results = {k: v.cpu().numpy() for k, v in returns_all.items()}
np.savez(os.path.join(self.ckpt_save_dir, 'test_results.npz'), **test_results)

if save_metrics:
# save metrics_results to self.ckpt_save_dir/test_metrics.json
with open(os.path.join(self.ckpt_save_dir, 'test_metrics.json'), 'w') as f:
json.dump(metrics_results, f, indent=4)

return returns_all

def forward(self, data: tuple, epoch:int = None, iter_num: int = None, train:bool = True, **kwargs) -> tuple:
"""feed forward process for train, val, and test. Note that the outputs are NOT re-scaled.

Expand Down