Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix failing torch test for torchmetrics when multi output #2573

Merged
merged 1 commit into from
Oct 27, 2024
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
11 changes: 9 additions & 2 deletions darts/models/forecasting/pl_forecasting_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,18 @@ def _update_metrics(self, output, target, metrics):
return

if self.likelihood:
metrics.update(self.likelihood.sample(output), target)
pred = self.likelihood.sample(output)
else:
# If there's no likelihood, nr_params=1, and we need to squeeze out the
# last dimension of model output, for properly computing the metric.
metrics.update(output.squeeze(dim=-1), target)
pred = output.squeeze(dim=-1)

# torch metrics require 2D targets of shape (batch size * ocl, num targets)
if self.n_targets > 1:
target = target.reshape(-1, self.n_targets)
pred = pred.reshape(-1, self.n_targets)

metrics.update(pred, target)

def _compute_metrics(self, metrics):
if not len(metrics):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ def test_metrics(self):
10,
10,
n_epochs=1,
torch_metrics=metric,
torch_metrics=metric_collection,
pl_trainer_kwargs=model_kwargs,
)
model.fit(self.multivariate_series)
Expand Down
Loading