Skip to content
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
6 changes: 0 additions & 6 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

from __future__ import annotations

import functools
import math
import operator
from typing import TYPE_CHECKING, Literal, overload

import paddle
Expand Down Expand Up @@ -110,10 +108,6 @@ def dice_loss(
assert (
input.shape[:-1] == label.shape[:-1]
), "All dimensions should be equal except the last one."
assert (
functools.reduce(operator.mul, input.shape) != 0
and functools.reduce(operator.mul, label.shape) != 0
), "Any dimension of input and label cannot be equal to 0."

label = paddle.squeeze(label, [-1])
label = paddle.nn.functional.one_hot(label, input.shape[-1])
Expand Down
19 changes: 19 additions & 0 deletions test/legacy_test/test_nn_dice_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,28 @@

import unittest

import numpy as np
from op_test import get_places

import paddle

num_classes = 4
eps = 1e-6


class TestDiceLossOpApi_ZeroSize(unittest.TestCase):
def test_api_with_dygraph(self):
for place in get_places():
paddle.disable_static(place)
input = paddle.randn([0, 2]).astype(paddle.float64)
input.stop_gradient = False
label = paddle.randn([0, 1]).astype(paddle.int64)
label.stop_gradient = False
out = paddle.nn.functional.dice_loss(input, label, 1e-5)
np.testing.assert_allclose(out.numpy(), paddle.nan)
out.sum().backward()
np.testing.assert_allclose(input.grad.shape, input.shape)


if __name__ == "__main__":
unittest.main()
Loading