-
Notifications
You must be signed in to change notification settings - Fork 5.9k
【Hackathon 6th No.29】为 paddle.nn.functional.kl_div 进行功能增强 -part #63860
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56c3269
udpate kldiv_loss
NKNaN 2292301
update xpu
NKNaN 96c3e43
fix xpu test
NKNaN 976a2d7
add docs code example and add test case
NKNaN 53dee58
fix code example
NKNaN 6e89da8
fix code example
NKNaN cb0555b
fix code example
NKNaN 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1034,8 +1034,14 @@ class KLDivLoss(Layer): | |
|
|
||
| KL divergence loss is calculated as follows: | ||
|
|
||
| If `log_target` is False: | ||
|
|
||
| $$l(x, y) = y * (\log(y) - x)$$ | ||
|
|
||
| If `log_target` is True: | ||
|
|
||
| $$l(x, y) = \exp(y) * (y - x)$$ | ||
|
|
||
| Here :math:`x` is input and :math:`y` is label. | ||
|
|
||
| If `reduction` is ``'none'``, the output loss is the same shape as the input, and the loss at each point is calculated separately. There is no reduction to the result. | ||
|
|
@@ -1054,6 +1060,7 @@ class KLDivLoss(Layer): | |
| if `reduction` is ``'sum'``, the reduced sum loss is returned; | ||
| if `reduction` is ``'none'``, no reduction will be applied. | ||
| Default is ``'mean'``. | ||
| log_target (bool, optional): Indicate whether `label` is passed in log space. Default is False. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下面的示例代码,可以加一个log_target=True时的代码
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改 |
||
|
|
||
| Shape: | ||
|
|
||
|
|
@@ -1097,14 +1104,25 @@ class KLDivLoss(Layer): | |
| >>> print(pred_loss.shape) | ||
| [5, 20] | ||
|
|
||
| >>> # if label is in the log space, set log_target = True | ||
| >>> target = paddle.uniform(shape, min=0, max=10).astype('float32') | ||
| >>> log_target = paddle.log(target) | ||
| >>> kldiv_criterion_1 = nn.KLDivLoss(reduction='none') | ||
| >>> kldiv_criterion_2 = nn.KLDivLoss(reduction='none', log_target=True) | ||
| >>> pred_loss_1 = kldiv_criterion_1(x, target) | ||
| >>> pred_loss_2 = kldiv_criterion_2(x, log_target) | ||
| >>> print(paddle.allclose(pred_loss_1, pred_loss_2)) | ||
| Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| True) | ||
| """ | ||
|
|
||
| def __init__(self, reduction='mean'): | ||
| def __init__(self, reduction='mean', log_target=False): | ||
| super().__init__() | ||
| self.reduction = reduction | ||
| self.log_target = log_target | ||
|
|
||
| def forward(self, input, label): | ||
| out = F.kl_div(input, label, self.reduction) | ||
| out = F.kl_div(input, label, self.reduction, self.log_target) | ||
| return out | ||
|
|
||
|
|
||
|
|
||
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.
下面的示例代码,可以加一个log_target=True时的代码
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.
已修改