Skip to content
Open
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
5 changes: 1 addition & 4 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -8388,7 +8388,7 @@
}
},
"torch.nn.functional.binary_cross_entropy": {
"Matcher": "SizeAverageMatcher",
"Matcher": "ChangePrefixMatcher",
"paddle_api": "paddle.nn.functional.binary_cross_entropy",
"args_list": [
"input",
Expand All @@ -8398,9 +8398,6 @@
"reduce",
"reduction"
],
"kwargs_change": {
"target": "label"
},
"min_input_args": 2
},
"torch.nn.functional.binary_cross_entropy_with_logits": {
Expand Down
155 changes: 140 additions & 15 deletions tests/test_nn_functional_binary_cross_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ def test_case_1():
result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,size_average=True)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
input, target, weight=weight, size_average=True
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


def test_case_2():
Expand All @@ -43,11 +55,22 @@ def test_case_2():
[ 0.9112, 0.7526, 0.4061]])
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]])
weight = torch.tensor([0.5,0.2,0.3])
weight = torch.tensor([0.5,0.2,0.3])
result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,size_average=False)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
input, target, weight=weight, size_average=False
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


def test_case_3():
Expand Down Expand Up @@ -107,7 +130,19 @@ def test_case_6():
result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduce=True)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
input, target, weight=weight, reduce=True
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


def test_case_7():
Expand All @@ -122,7 +157,19 @@ def test_case_7():
result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduce=False)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
input, target, weight=weight, reduce=False
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


def test_case_8():
Expand Down Expand Up @@ -151,10 +198,21 @@ def test_case_9():
result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight, size_average=None, reduce=False, reduction='mean')
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
input, target, weight=weight, size_average=None, reduce=False, reduction="mean"
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


# generated by validate_unittest autofix, based on test_case_9
def test_case_10():
pytorch_code = textwrap.dedent(
"""
Expand All @@ -163,14 +221,21 @@ def test_case_10():
input = torch.tensor([[0.2837, 0.0297, 0.0355],
[ 0.9112, 0.7526, 0.4061]])
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]])
weight = torch.tensor([0.5,0.2,0.3])
result = torch.nn.functional.binary_cross_entropy(input, target, weight, None, False, 'mean')
result = torch.nn.functional.binary_cross_entropy(input=input, target=target)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
result = paddle.nn.functional.binary_cross_entropy(input=input, target=target)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


# generated by validate_unittest autofix, based on test_case_9
def test_case_11():
pytorch_code = textwrap.dedent(
"""
Expand All @@ -180,14 +245,57 @@ def test_case_11():
[ 0.9112, 0.7526, 0.4061]])
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]])
weight = torch.tensor([0.5,0.2,0.3])
result = torch.nn.functional.binary_cross_entropy(input=input, target=target, weight=weight, size_average=None, reduce=False, reduction='mean')
result = torch.nn.functional.binary_cross_entropy(target=target, input=input, reduction='sum', weight=weight)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
target=target, input=input, reduction="sum", weight=weight
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


# generated by validate_unittest autofix, based on test_case_9
def test_case_12():
pytorch_code = textwrap.dedent(
"""
import torch
import torch.nn as nn
input = torch.tensor([[0.2837, 0.0297, 0.0355],
[ 0.9112, 0.7526, 0.4061]])
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]])
weight = torch.tensor([0.5,0.2,0.3])
result = torch.nn.functional.binary_cross_entropy(input=input, target=target, weight=weight, size_average=None, reduce=False, reduction='mean')
"""
)
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
input=input,
target=target,
weight=weight,
size_average=None,
reduce=False,
reduction="mean",
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)


def test_case_13():
pytorch_code = textwrap.dedent(
"""
import torch
Expand All @@ -199,4 +307,21 @@ def test_case_12():
result = torch.nn.functional.binary_cross_entropy(reduction='mean', reduce=False, size_average=None, weight=weight, target=target, input=input)
"""
)
obj.run(pytorch_code, ["result"])
expect_paddle_code = textwrap.dedent(
"""
import paddle

input = paddle.tensor([[0.2837, 0.0297, 0.0355], [0.9112, 0.7526, 0.4061]])
target = paddle.tensor([[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]])
weight = paddle.tensor([0.5, 0.2, 0.3])
result = paddle.nn.functional.binary_cross_entropy(
reduction="mean",
reduce=False,
size_average=None,
weight=weight,
target=target,
input=input,
)
"""
)
obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)