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

[BUG] PLForecastingModule._calculate_metrics should call log_dict with batch_size #2389

Closed
youurayy opened this issue May 20, 2024 · 2 comments · Fixed by #2391
Closed

[BUG] PLForecastingModule._calculate_metrics should call log_dict with batch_size #2389

youurayy opened this issue May 20, 2024 · 2 comments · Fixed by #2391
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@youurayy
Copy link

youurayy commented May 20, 2024

Describe the bug
When using additional metrics with PyTorch Lightning forecasting models, due to PLForecastingModule._calculate_metrics (called from train_step() or validation_step()) not calling self.log_dict(...) with the batch_size= parameter, the Trainer (_ResultCollection.log() is complaining about ambiguity (when show_warnings=True is enabled on the model):

/opt/homebrew/Caskroom/miniconda/base/envs/tf/lib/python3.12/site-packages/pytorch_lightning/utilities/data.py:77: Trying to infer the batch_size from an ambiguous collection. The batch size we found is 32. To avoid any miscalculations, use self.log(..., batch_size=batch_size).

To Reproduce
Add a custom metric via torch_metrics=... model parameter (e.g. BlockRNNModel), and run .fit().

Expected behavior
The warnings should be gone.

System (please complete the following information):

  • Python version: 3.12.3
  • darts version: 0.29.0

Additional context
suggested code modification (tested):

def _calculate_metrics(self, output, target, metrics):
    if not len(metrics):
        return

    if self.likelihood:
        _metric = metrics(self.likelihood.sample(output), target)
    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.
        _metric = metrics(output.squeeze(dim=-1), target)

    self.log_dict(
        _metric,
        on_epoch=True,
        on_step=False,
        logger=True,
        prog_bar=True,
        sync_dist=True,
        batch_size=target.shape[0],  # ADD THIS LINE
    )
@youurayy youurayy added bug Something isn't working triage Issue waiting for triaging labels May 20, 2024
@madtoinou madtoinou added good first issue Good for newcomers and removed triage Issue waiting for triaging labels May 21, 2024
@dennisbader
Copy link
Collaborator

Hi @youurayy and thanks for raising this issue.

Your suggested modification looks great! Would you like to contribute this?

@youurayy
Copy link
Author

Hi @youurayy and thanks for raising this issue.

Your suggested modification looks great! Would you like to contribute this?

ah sorry @dennisbader I didn't check this in time. Thanks for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants