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
4 changes: 2 additions & 2 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3912,10 +3912,10 @@ def triplet_margin_with_distance_loss(

if (
not isinstance(positive_dist, paddle.pir.Value)
and not paddle.all(positive_dist > 0)
and not paddle.all(positive_dist >= 0)
) or (
not isinstance(negative_dist, paddle.pir.Value)
and not paddle.all(negative_dist > 0)
and not paddle.all(negative_dist >= 0)
):
raise ValueError(
"The positive distance or negative distance should be greater than 0, "
Expand Down
32 changes: 32 additions & 0 deletions test/legacy_test/test_lrn_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,37 @@ def test_static_fp16_gpu(self):
np.testing.assert_array_equal(res[0].shape, input.shape)


class TestLocalResponseNormAPI_ZeroSize(unittest.TestCase):
def setUp(self):
np.random.seed(123)
self.places = get_places()

def check_dygraph(self, place):
with base.dygraph.guard(place):
in_np1 = np.random.random([0, 40, 40]).astype("float32")
in_np2 = np.transpose(in_np1, (0, 2, 1))

in1 = paddle.to_tensor(in_np1)
in1.stop_gradient = False
in2 = paddle.to_tensor(in_np2)

res1 = paddle.nn.functional.local_response_norm(
x=in1, size=5, data_format='NCL'
)
res2 = paddle.nn.functional.local_response_norm(
x=in2, size=5, data_format='NLC'
)

res2_tran = np.transpose(res2.numpy(), (0, 2, 1))
np.testing.assert_allclose(res1.numpy(), res2_tran, rtol=1e-05)

res1.sum().backward()
np.testing.assert_allclose(in1.grad.shape, in1.shape)

def test_dygraph(self):
for place in self.places:
self.check_dygraph(place)


if __name__ == "__main__":
unittest.main()
60 changes: 60 additions & 0 deletions test/legacy_test/test_triplet_margin_with_distance_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,5 +489,65 @@ def test_TripletMarginWithDistanceLoss_margin(self):
paddle.enable_static()


class TestTripletMarginWithDistanceLoss_ZeroSize(unittest.TestCase):
def _test_dygraph(
self,
place,
input,
positive,
negative,
distance_function=None,
margin=0.3,
swap=False,
reduction='mean',
expected=None,
):
paddle.disable_static(place)
input = paddle.to_tensor(input)
input.stop_gradient = False
positive = paddle.to_tensor(positive)
negative = paddle.to_tensor(negative)

dy_res = call_TripletMaginDistanceLoss_functional(
input=input,
positive=positive,
negative=negative,
distance_function=distance_function,
margin=margin,
swap=swap,
reduction=reduction,
)
dy_result = dy_res.numpy()
np.testing.assert_allclose(dy_result, expected, rtol=1e-5, atol=1e-8)
dy_res.sum().backward()
np.testing.assert_allclose(input.grad.shape, input.shape)
paddle.enable_static()

def test_TripletMarginDistanceLoss(self):
shape = (5, 0)
np.random.seed(1234)
input = np.random.uniform(0.1, 0.8, size=shape).astype(np.float64)
positive = np.random.uniform(0, 2, size=shape).astype(np.float64)
negative = np.random.uniform(0, 2, size=shape).astype(np.float64)

places = get_places()
reduction = 'sum'
for place in places:
expected = calc_triplet_margin_distance_loss(
input=input,
positive=positive,
negative=negative,
reduction=reduction,
)
self._test_dygraph(
place=place,
input=input,
positive=positive,
negative=negative,
reduction=reduction,
expected=expected,
)


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