-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[WIP v2 - deprecated] Unlikelihood token loss #2011
Draft
funboarder13920
wants to merge
4
commits into
OpenNMT:master
Choose a base branch
from
funboarder13920:unlikelihood_loss_and_ppl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6d93dcc
implement unlikelihood token loss and fix ppl to always be the ppl
c1787a8
address PR comments + commit missing test file
770f60d
mutually exclusive label_smoothing and unlikelihood_coeff
118a43f
merge LossComputeBase and CommonLossComputeBase
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import unittest | ||
from onmt.utils.loss import UnlikelihoodTokenLoss | ||
import torch | ||
import math | ||
|
||
|
||
class TestUnlikelihoodLossCriterion(unittest.TestCase): | ||
def test_compute_previous_context_tokens(self): | ||
criterion = UnlikelihoodTokenLoss(1, 7) | ||
target = torch.tensor([[2, 3, 4, 3, 5], [1, 1, 5, 6, 7]]).permute(1, 0) | ||
previous_context_tokens = criterion.compute_previous_context_tokens( | ||
target | ||
) | ||
|
||
self.assertEqual( | ||
previous_context_tokens.permute(1, 0, 2).tolist(), | ||
torch.tensor( | ||
[ | ||
[ | ||
[7, 7, 7, 7, 7], | ||
[2, 7, 7, 7, 7], | ||
[2, 3, 7, 7, 7], | ||
[2, 7, 4, 7, 7], | ||
[2, 3, 4, 3, 7], | ||
], | ||
[ | ||
[7, 7, 7, 7, 7], | ||
[7, 7, 7, 7, 7], | ||
[1, 1, 7, 7, 7], | ||
[1, 1, 5, 7, 7], | ||
[7, 7, 7, 7, 7], | ||
], | ||
] | ||
).tolist(), | ||
) | ||
|
||
def test_loss_perfect_pred_should_be_zero(self): | ||
criterion = UnlikelihoodTokenLoss(1, 7) | ||
n_prob = -10e6 | ||
target = torch.tensor([[2, 3, 4, 3, 5], [1, 1, 5, 6, 7]]).permute(1, 0) | ||
perfect_probs = [ | ||
[[n_prob if i != t else 1 for i in range(8)] for t in ex_target] | ||
for ex_target in target | ||
] | ||
|
||
# check padded seq is removed | ||
perfect_probs[-1][-1][-1] = n_prob | ||
perfect_probs[-1][-1][1] = 0.1 | ||
|
||
output = torch.tensor(perfect_probs).view(-1, 8) | ||
|
||
unlikelihood_loss = criterion.compute_unlikelihood_loss(output, target) | ||
|
||
self.assertEqual(unlikelihood_loss.sum().item(), 0) | ||
|
||
def test_loss_value(self): | ||
criterion = UnlikelihoodTokenLoss(1, 7) | ||
n_prob = -10e6 | ||
target = torch.tensor([[2, 3, 4, 3, 5], [1, 1, 5, 6, 7]]).permute(1, 0) | ||
perfect_probs = [ | ||
[[n_prob if i != t else 1 for i in range(8)] for t in ex_target] | ||
for ex_target in target | ||
] | ||
|
||
# check padded seq is removed | ||
perfect_probs[-1][-1][-1] = n_prob | ||
perfect_probs[-1][-1][1] = 0.1 | ||
|
||
# set prob at 0.5 on 1 after softmax | ||
perfect_probs[2][-1][1] = 1 | ||
|
||
output = torch.tensor(perfect_probs).view(-1, 8) | ||
|
||
unlikelihood_loss = criterion.compute_unlikelihood_loss(output, target) | ||
|
||
self.assertAlmostEqual( | ||
unlikelihood_loss.view(5, 2, 8)[2, -1, 1].item(), -math.log(0.5) | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to grasp the whole rationale behind the
CommonLossCompute
/LossComputeBase
refactoring. Is the last big remaining difference only the log_ppl computation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Underlying question is: do we really need both
CommonLossCompute
andLossComputeBase
anymore?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _compute_loss, _make_shard_state and the way to use the generator are different between CopyGeneratorLoss and the other classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do it in one class, the code is already not very clear, it's not going to be worse. If we do that CopyGenerator will override _compute_loss, _compute_log_ppl and _compute_alignement_loss will only be used in the compute_loss of the main class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think this might be a bit better to explicitly override this method instead of having a full class that we don't really know what it's for unless we look at this specific CopyGeneratorLoss.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I merged it, the ppl part is not nice. Also there is a normalization args that was not used anywhere, I will investigate to see if the normalization process disappeared by mistake
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalization was already not used a year ago
OpenNMT-py/onmt/utils/loss.py
Line 228 in 7835130