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

Trigger RuntimeError for when normalization is requested but not utilized #76

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions matsciml/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,26 @@ def _compute_losses(
targets = self._get_targets(batch)
predictions = self(batch)
losses = {}
used_norm = []
for key in self.task_keys:
target_val = targets[key]
if self.uses_normalizers:
target_val = self.normalizers[key].norm(target_val)
used_norm.append(key)
losses[key] = self.loss_func(predictions[key], target_val)
total_loss: torch.Tensor = sum(losses.values())
# trigger warning for when we infer normalization intent but
# not actually executed
if len(used_norm) == 0 and self.uses_normalizers:
raise RuntimeError(
"Target normalization was intended but not used."
f"Please check your config - expected: {self.task_keys}"
)
if len(used_norm) != len(self.normalizers):
raise RuntimeError(
"Normalization was performed, but number of keys do not match."
f"Expected {len(self.normalizers)} keys, but only used {len(used_norm)}."
)
return {"loss": total_loss, "log": losses}

def configure_optimizers(self) -> torch.optim.AdamW:
Expand Down