-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
【Hackathon No.16】add PoissonNLLLoss API #51117
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fef43dc
add PoissonNLLLoss API
LyndonKong 2115a6a
update unittests
LyndonKong 5b07236
Fix poisson_nll_loss init and update data type support
LyndonKong 390d583
remove type comment
LyndonKong 527189c
Update doc string
LyndonKong 2068bb7
Fix doc string erro
LyndonKong c80cf60
Fix doc string math equation format
LyndonKong c49854e
Add float16 and bfloat16 support
LyndonKong 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
248 changes: 248 additions & 0 deletions
248
python/paddle/fluid/tests/unittests/test_poisson_nll_loss.py
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,248 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
import paddle.nn.functional as F | ||
from paddle.fluid import core | ||
|
||
np.random.seed(100) | ||
|
||
|
||
def ref_poisson_nll_loss( | ||
input, | ||
label, | ||
log_input=True, | ||
full=False, | ||
epsilon=1e-8, | ||
reduction="mean", | ||
): | ||
if epsilon <= 0: | ||
raise ValueError( | ||
"The value of `epsilon` in PoissonNLLLoss should be positve, but received %f, which is not allowed" | ||
% epsilon | ||
) | ||
|
||
if reduction not in ['sum', 'mean', 'none']: | ||
raise ValueError( | ||
"The value of 'reduction' in SoftMarginLoss should be 'sum', 'mean' or 'none', but " | ||
"received %s, which is not allowed." % reduction | ||
) | ||
loss_out = 0 | ||
if log_input: | ||
loss_out = np.exp(input) - label * input | ||
else: | ||
loss_out = input - label * np.log(input + epsilon) | ||
if full: | ||
stirling_approx = ( | ||
label * np.log(label) - label + 0.5 * np.log(2 * np.pi * label) | ||
) | ||
loss_out += np.where(stirling_approx <= 1, 0, stirling_approx) | ||
|
||
if reduction == 'none': | ||
return loss_out | ||
elif reduction == 'sum': | ||
return [np.sum(loss_out)] | ||
elif reduction == 'mean': | ||
return [np.mean(loss_out)] | ||
|
||
|
||
class TestPoissonNLLLossBasicCase(unittest.TestCase): | ||
def setUp(self, dtype="float32"): | ||
self.shape = [10, 2] | ||
self.dtype = dtype | ||
self.input_np = np.random.random(self.shape).astype(self.dtype) | ||
self.label_np = np.random.random(self.shape).astype(self.dtype) | ||
self.place = ( | ||
paddle.CUDAPlace(0) | ||
if core.is_compiled_with_cuda() | ||
else paddle.CPUPlace() | ||
) | ||
|
||
def test_static_case( | ||
self, | ||
dtype="float32", | ||
log_input=True, | ||
full=False, | ||
epsilon=1e-8, | ||
reduction="mean", | ||
): | ||
self.setUp(dtype) | ||
paddle.enable_static() | ||
prog = paddle.static.Program() | ||
startup_prog = paddle.static.Program() | ||
with paddle.static.program_guard(prog, startup_prog): | ||
input = paddle.static.data('input', self.shape, dtype) | ||
label = paddle.static.data('label', self.shape, dtype) | ||
input.desc.set_need_check_feed(False) | ||
label.desc.set_need_check_feed(False) | ||
out1 = F.poisson_nll_loss( | ||
input, | ||
label, | ||
log_input=log_input, | ||
full=full, | ||
epsilon=epsilon, | ||
reduction=reduction, | ||
) | ||
poisson_nll_loss = paddle.nn.PoissonNLLLoss( | ||
log_input=log_input, | ||
full=full, | ||
epsilon=epsilon, | ||
reduction=reduction, | ||
) | ||
out2 = poisson_nll_loss(input, label) | ||
exe = paddle.static.Executor(self.place) | ||
exe.run(startup_prog) | ||
res = exe.run( | ||
prog, | ||
feed={'input': self.input_np, 'label': self.label_np}, | ||
fetch_list=[out1, out2], | ||
) | ||
out_ref = ref_poisson_nll_loss( | ||
self.input_np, | ||
self.label_np, | ||
log_input=log_input, | ||
full=full, | ||
epsilon=epsilon, | ||
reduction=reduction, | ||
) | ||
for r in res: | ||
np.allclose(out_ref, r, rtol=1e-5) | ||
|
||
def test_dynamic_case( | ||
self, | ||
dtype="float32", | ||
log_input=True, | ||
full=False, | ||
epsilon=1e-8, | ||
reduction="mean", | ||
type=None, | ||
): | ||
self.setUp(dtype) | ||
paddle.disable_static(self.place) | ||
|
||
input_x = paddle.to_tensor(self.input_np) | ||
label = paddle.to_tensor(self.label_np) | ||
out_ref = ref_poisson_nll_loss( | ||
self.input_np, | ||
self.label_np, | ||
log_input=log_input, | ||
full=full, | ||
epsilon=epsilon, | ||
reduction=reduction, | ||
) | ||
out1 = F.poisson_nll_loss( | ||
input_x, | ||
label, | ||
log_input=log_input, | ||
full=full, | ||
epsilon=epsilon, | ||
reduction=reduction, | ||
) | ||
if type == 'test_err_reduction': | ||
self.assertRaises( | ||
ValueError, | ||
paddle.nn.functional.poisson_nll_loss, | ||
input=input_x, | ||
label=label, | ||
log_input=log_input, | ||
full=full, | ||
epsilon=epsilon, | ||
reduction="unsupport reduction", | ||
) | ||
elif type == 'test_err_epsilon': | ||
self.assertRaises( | ||
ValueError, | ||
paddle.nn.functional.poisson_nll_loss, | ||
input=input_x, | ||
label=label, | ||
log_input=log_input, | ||
full=full, | ||
epsilon=-1, | ||
reduction="mean", | ||
) | ||
poisson_nll_loss = paddle.nn.PoissonNLLLoss( | ||
log_input=log_input, full=full, epsilon=epsilon, reduction=reduction | ||
) | ||
out2 = poisson_nll_loss(input_x, label) | ||
|
||
for r in [out1, out2]: | ||
np.allclose(out_ref, r.numpy(), rtol=1e-5) | ||
paddle.enable_static() | ||
|
||
def test_api(self): | ||
pass | ||
|
||
|
||
class TestPoissonNLLLossErrCase(TestPoissonNLLLossBasicCase): | ||
def test_err_reduction(self): | ||
self.test_dynamic_case(type="test_err_reduction") | ||
|
||
def test_err_epsilon(self): | ||
self.test_dynamic_case(type="test_err_epsilon") | ||
|
||
def test_api(self): | ||
self.test_err_reduction() | ||
self.test_err_epsilon() | ||
|
||
|
||
class TestPoissonNLLLossFloat16Case(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
if core.is_compiled_with_cuda(): | ||
self.test_static_case(dtype="float16") | ||
self.test_dynamic_case(dtype="float16") | ||
|
||
|
||
class TestPoissonNLLLossBfloat16Case(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
if core.is_compiled_with_cuda(): | ||
self.test_static_case(dtype="uint16") | ||
self.test_dynamic_case(dtype="uint16") | ||
|
||
|
||
class TestPoissonNLLLossFloat32Case(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
self.test_static_case(dtype="float32") | ||
self.test_dynamic_case(dtype="float32") | ||
|
||
|
||
class TestPoissonNLLLossFloat64Case(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
self.test_static_case(dtype="float64") | ||
self.test_dynamic_case(dtype="float64") | ||
|
||
|
||
class TestPoissonNLLLossNoLoginputCase(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
self.test_static_case(log_input=False) | ||
self.test_dynamic_case(log_input=False) | ||
|
||
|
||
class TestPoissonNLLLossFulllossCase(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
self.test_static_case(full=True) | ||
self.test_dynamic_case(full=True) | ||
|
||
|
||
class TestPoissonNLLLossSumReductionCase(TestPoissonNLLLossBasicCase): | ||
def test_api(self): | ||
self.test_static_case(reduction="sum") | ||
self.test_dynamic_case(reduction="sum") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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 |
---|---|---|
|
@@ -83,6 +83,7 @@ | |
from .loss import margin_ranking_loss # noqa: F401 | ||
from .loss import mse_loss # noqa: F401 | ||
from .loss import nll_loss # noqa: F401 | ||
from .loss import poisson_nll_loss # noqa: F401 | ||
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. should also add 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. Done in new commit. |
||
from .loss import npair_loss # noqa: F401 | ||
from .loss import sigmoid_focal_loss # noqa: F401 | ||
from .loss import smooth_l1_loss # noqa: F401 | ||
|
@@ -214,6 +215,7 @@ | |
'margin_ranking_loss', | ||
'multi_label_soft_margin_loss', | ||
'nll_loss', | ||
'poisson_nll_loss', | ||
'npair_loss', | ||
'sigmoid_focal_loss', | ||
'smooth_l1_loss', | ||
|
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.
不同case的测试分成多个test class吧,方便后续定位具体是哪个case异常
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.
已经对单元测试进行了拆分
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.
这个单测有点分的太细了,动态图和静态图可以整合到一个class下面的两个方法,然后再不同的case分成不同的class, test error的也可以整合到一个class,不同的方法测试不同的error