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] fix AttributeError: 'ExperimentWriter' object has no attribute 'add_figure' #1694

Merged
merged 3 commits into from
Nov 11, 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
19 changes: 18 additions & 1 deletion pytorch_forecasting/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,18 @@ def log_interval(self) -> float:
else:
return self.hparams.log_val_interval

def _logger_supports(self, method: str) -> bool:
"""Whether logger supports method.

Returns
-------
supports_method : bool
True if attribute self.logger.experiment.method exists, False otherwise.
"""
if not hasattr(self, "logger") or not hasattr(self.logger, "experiment"):
return False
return hasattr(self.logger.experiment, method)

def log_prediction(
self, x: Dict[str, torch.Tensor], out: Dict[str, torch.Tensor], batch_idx: int, **kwargs
) -> None:
Expand All @@ -976,6 +988,10 @@ def log_prediction(
if not mpl_available:
return None # don't log matplotlib plots if not available

# Don't log figures if add_figure is not available
if not self._logger_supports("add_figure"):
return None

for idx in log_indices:
fig = self.plot_prediction(x, out, idx=idx, add_loss_to_title=True, **kwargs)
tag = f"{self.current_stage} prediction"
Expand Down Expand Up @@ -1146,7 +1162,8 @@ def log_gradient_flow(self, named_parameters: Dict[str, torch.Tensor]) -> None:

mpl_available = _check_matplotlib("log_gradient_flow", raise_error=False)

if not mpl_available:
# Don't log figures if matplotlib or add_figure is not available
if not mpl_available or not self._logger_supports("add_figure"):
return None

import matplotlib.pyplot as plt
Expand Down
3 changes: 2 additions & 1 deletion pytorch_forecasting/models/nbeats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ def log_interpretation(self, x, out, batch_idx):
"""
mpl_available = _check_matplotlib("log_interpretation", raise_error=False)

if not mpl_available:
# Don't log figures if matplotlib or add_figure is not available
if not mpl_available or not self._logger_supports("add_figure"):
return None

label = ["val", "train"][self.training]
Expand Down
3 changes: 2 additions & 1 deletion pytorch_forecasting/models/nhits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ def log_interpretation(self, x, out, batch_idx):
"""
mpl_available = _check_matplotlib("log_interpretation", raise_error=False)

if not mpl_available:
# Don't log figures if matplotlib or add_figure is not available
if not mpl_available or not self._logger_supports("add_figure"):
return None

label = ["val", "train"][self.training]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ def log_interpretation(self, outputs):

mpl_available = _check_matplotlib("log_interpretation", raise_error=False)

if not mpl_available:
# Don't log figures if matplotlib or add_figure is not available
if not mpl_available or not self._logger_supports("add_figure"):
return None

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -857,6 +858,11 @@ def log_embeddings(self):
"""
Log embeddings to tensorboard
"""

# Don't log embeddings if add_embedding is not available
if not self._logger_supports("add_embedding"):
return None

for name, emb in self.input_embeddings.items():
labels = self.hparams.embedding_labels[name]
self.logger.experiment.add_embedding(
Expand Down
Loading