Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Fix regloss logging #4449

Merged
merged 14 commits into from
Jul 10, 2020
Merged

Fix regloss logging #4449

merged 14 commits into from
Jul 10, 2020

Conversation

AkshitaB
Copy link
Contributor

@AkshitaB AkshitaB commented Jul 7, 2020

Fixes #4436

reg_loss is only displayed if the model being trained is configured to have a regularization penalty.

@AkshitaB AkshitaB requested a review from dirkgr July 7, 2020 23:43
Copy link
Member

@dirkgr dirkgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unintentionally a bigger change. The old code would display all metrics that are configured. This one will only show loss and reg_loss, or just loss.

@@ -409,6 +409,11 @@ def __init__(
self._tensorboard.get_batch_num_total = lambda: self._batch_num_total
self._tensorboard.enable_activation_logging(self.model)

# Only display reg_loss if the model's configuration has regularization.
self._show_metrics = ["loss", "reg_loss"]
if self.model.get_regularization_penalty() == 0.0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's likely that we have to detect whether a regularization loss is configured by looking whether the number is 0. But it would be better if we make that decision based on whether it's configured, not based on whether the result is zero. That way we can show it, for example, when it's misconfigured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the get_regularization_penalty() function returns a 0 if the model is configured to not have regularization. This is what the docstring for that function says: Returns 0 if the model was not configured to use regularization.

@dirkgr dirkgr linked an issue Jul 8, 2020 that may be closed by this pull request
@AkshitaB AkshitaB requested a review from dirkgr July 8, 2020 16:35
Copy link
Member

@dirkgr dirkgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a perfectly fine PR and we could merge it like this.

But I have a suggestion. Feel free to roll your eyes at me. We could expand the type of reg_loss to be Optional[float] (or Optional[FloatTensor]). In other words, Model.get_regularization_penalty() would return None when there is no regularizer, and all the places in trainer.py where this is read have to handle this case properly. It's not a lot of places where this happens, so it shouldn't be too much work. Finally, there is a call like Trainer.get_metrics_from_dict() or something like that that takes a whole bunch of parameters, including total_reg_loss. That parameter becomes Optional[float] instead of the current float, and if total_reg_loss is None, then it wouldn't add the "reg_loss" key to the metrics at all.

This is a slightly larger scope for this change than originally intended, but I think it makes the whole thing cleaner. We no longer have to do the hasattr() stuff, and our metrics dict doesn't carry useless zeros. @matt-gardner, what do you think?

@matt-gardner
Copy link
Contributor

I agree that what @dirkgr suggests sounds like a cleaner change.

@AkshitaB AkshitaB requested a review from dirkgr July 9, 2020 21:54
Copy link
Member

@dirkgr dirkgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, though I have an open question about the regularizer returning float. Does that ever happen? I think that's a bug in the regularizer if that happens.

@@ -574,19 +578,20 @@ def _train_epoch(self, epoch: int) -> Dict[str, float]:
for batch in batch_group:
batch_outputs = self.batch_outputs(batch, for_training=True)
batch_group_outputs.append(batch_outputs)
loss = batch_outputs["loss"]
reg_loss = batch_outputs["reg_loss"]
loss = batch_outputs.get("loss")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "loss" is always there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was more for uniformity's sake.

allennlp/models/model.py Outdated Show resolved Hide resolved
Copy link
Member

@dirkgr dirkgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎿

@AkshitaB AkshitaB requested a review from dirkgr July 10, 2020 17:28
@dirkgr dirkgr merged commit 6acf205 into allenai:master Jul 10, 2020
@AkshitaB AkshitaB deleted the fix-regloss-logging branch July 10, 2020 19:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop logging regularization loss of 0
3 participants