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

【Hackathon No.16】add PoissonNLLLoss API #51117

Merged
merged 8 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
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
248 changes: 248 additions & 0 deletions python/paddle/fluid/tests/unittests/test_poisson_nll_loss.py
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):
Copy link
Contributor

Choose a reason for hiding this comment

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

不同case的测试分成多个test class吧,方便后续定位具体是哪个case异常

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已经对单元测试进行了拆分

Copy link
Contributor

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

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()
2 changes: 2 additions & 0 deletions python/paddle/nn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
from .layer.loss import MSELoss # noqa: F401
from .layer.loss import L1Loss # noqa: F401
from .layer.loss import NLLLoss # noqa: F401
from .layer.loss import PoissonNLLLoss # noqa: F401
from .layer.loss import BCELoss # noqa: F401
from .layer.loss import KLDivLoss # noqa: F401
from .layer.loss import MarginRankingLoss # noqa: F401
Expand Down Expand Up @@ -268,6 +269,7 @@ def weight_norm(*args):
'AdaptiveAvgPool3D',
'AdaptiveMaxPool3D',
'NLLLoss',
'PoissonNLLLoss',
'Conv1D',
'Sequential',
'Hardswish',
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

should also add poisson_nll_loss in list of __all__ below in this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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',
Expand Down
Loading