未验证 提交 875a4ea9 编写于 作者: iSerendipity's avatar iSerendipity 提交者: GitHub

[xdoctest][task 79] reformat example code with google style in ...

[xdoctest][task 79] reformat example code with google style in  `python/paddle/nn/functional/loss.py` (#56901)

* [Doctest] fix No.79, test=docs_preview

* Apply suggestions from code review

* fix gpu

* fix

* fix

---------
Co-authored-by: NNyakku Shigure <sigure.qaq@gmail.com>
上级 4dbe441c
...@@ -66,13 +66,13 @@ def dice_loss(input, label, epsilon=0.00001, name=None): ...@@ -66,13 +66,13 @@ def dice_loss(input, label, epsilon=0.00001, name=None):
Example: Example:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
x = paddle.randn((3,224,224,2)) >>> x = paddle.randn((3,224,224,2))
label = paddle.randint(high=2, shape=(3,224,224,1)) >>> label = paddle.randint(high=2, shape=(3,224,224,1))
predictions = F.softmax(x) >>> predictions = F.softmax(x)
loss = F.dice_loss(input=predictions, label=label) >>> loss = F.dice_loss(input=predictions, label=label)
""" """
assert input.dtype in (paddle.float32, paddle.float64) assert input.dtype in (paddle.float32, paddle.float64)
assert label.dtype in (paddle.int32, paddle.int64) assert label.dtype in (paddle.int32, paddle.int64)
...@@ -136,12 +136,12 @@ def log_loss(input, label, epsilon=1e-4, name=None): ...@@ -136,12 +136,12 @@ def log_loss(input, label, epsilon=1e-4, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
label = paddle.randn((10,1)) >>> label = paddle.randn((10,1))
prob = paddle.randn((10,1)) >>> prob = paddle.randn((10,1))
cost = F.log_loss(input=prob, label=label) >>> cost = F.log_loss(input=prob, label=label)
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.log_loss(input, label, epsilon) return _C_ops.log_loss(input, label, epsilon)
...@@ -244,15 +244,16 @@ def fluid_softmax_with_cross_entropy( ...@@ -244,15 +244,16 @@ def fluid_softmax_with_cross_entropy(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> paddle.seed(2023)
logits = paddle.to_tensor([0.4, 0.6, 0.9]) >>> logits = paddle.to_tensor([0.4, 0.6, 0.9])
label = paddle.randint(high=2, shape=[1], dtype="int64") >>> label = paddle.randint(high=2, shape=[1], dtype="int64")
out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label) >>> out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label)
print(out) >>> print(out)
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.15328646]) [1.15328646])
""" """
input_dims = len(list(logits.shape)) input_dims = len(list(logits.shape))
if input_dims == 0: if input_dims == 0:
...@@ -331,18 +332,20 @@ def npair_loss(anchor, positive, labels, l2_reg=0.002): ...@@ -331,18 +332,20 @@ def npair_loss(anchor, positive, labels, l2_reg=0.002):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
DATATYPE = "float32" >>> import paddle
>>> DATATYPE = "float32"
>>> paddle.seed(2023)
anchor = paddle.rand(shape=(18, 6), dtype=DATATYPE) >>> anchor = paddle.rand(shape=(18, 6), dtype=DATATYPE)
positive = paddle.rand(shape=(18, 6), dtype=DATATYPE) >>> positive = paddle.rand(shape=(18, 6), dtype=DATATYPE)
labels = paddle.rand(shape=(18,), dtype=DATATYPE) >>> labels = paddle.rand(shape=(18,), dtype=DATATYPE)
npair_loss = paddle.nn.functional.npair_loss(anchor, positive, labels, l2_reg = 0.002) >>> npair_loss = paddle.nn.functional.npair_loss(anchor, positive, labels, l2_reg = 0.002)
print(npair_loss) >>> print(npair_loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.94269347)
""" """
if anchor.size == 0: if anchor.size == 0:
...@@ -410,12 +413,13 @@ def square_error_cost(input, label): ...@@ -410,12 +413,13 @@ def square_error_cost(input, label):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.to_tensor([1.1, 1.9]) >>> input = paddle.to_tensor([1.1, 1.9])
label = paddle.to_tensor([1.0, 2.0]) >>> label = paddle.to_tensor([1.0, 2.0])
output = paddle.nn.functional.square_error_cost(input, label) >>> output = paddle.nn.functional.square_error_cost(input, label)
print(output) >>> print(output)
# [0.01, 0.01] Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.01000000, 0.01000000])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -496,29 +500,35 @@ def edit_distance( ...@@ -496,29 +500,35 @@ def edit_distance(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
input = paddle.to_tensor([[1,2,3],[4,5,6],[4,4,4],[1,1,1]], dtype='int64') >>> input = paddle.to_tensor([[1,2,3],[4,5,6],[4,4,4],[1,1,1]], dtype='int64')
label = paddle.to_tensor([[1,3,4,1],[4,5,8,1],[7,7,7,1],[1,1,1,1]], dtype='int64') >>> label = paddle.to_tensor([[1,3,4,1],[4,5,8,1],[7,7,7,1],[1,1,1,1]], dtype='int64')
input_len = paddle.to_tensor([3,3,3,3], dtype='int64') >>> input_len = paddle.to_tensor([3,3,3,3], dtype='int64')
label_len = paddle.to_tensor([4,4,4,4], dtype='int64') >>> label_len = paddle.to_tensor([4,4,4,4], dtype='int64')
distance, sequence_num = F.loss.edit_distance(input=input, label=label, input_length=input_len, label_length=label_len, normalized=False) >>> distance, sequence_num = F.loss.edit_distance(input=input, label=label, input_length=input_len, label_length=label_len, normalized=False)
>>> print(distance)
# print(distance) Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3.] [4])
# [2.] >>> print(sequence_num)
# [4.] Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.]] [[3.],
# if set normalized to True [2.],
# [[0.75] [4.],
# [0.5 ] [1.]])
# [1. ]
# [0.25] >>> distance, sequence_num = F.loss.edit_distance(input=input, label=label, input_length=input_len, label_length=label_len, normalized=True)
# >>> print(distance)
# print(sequence_num) Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
# [4] [4])
>>> print(sequence_num)
Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.75000000],
[0.50000000],
[1. ],
[0.25000000]])
""" """
...@@ -629,12 +639,14 @@ def binary_cross_entropy( ...@@ -629,12 +639,14 @@ def binary_cross_entropy(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.to_tensor([0.5, 0.6, 0.7], 'float32') >>> input = paddle.to_tensor([0.5, 0.6, 0.7], 'float32')
label = paddle.to_tensor([1.0, 0.0, 1.0], 'float32') >>> label = paddle.to_tensor([1.0, 0.0, 1.0], 'float32')
output = paddle.nn.functional.binary_cross_entropy(input, label) >>> output = paddle.nn.functional.binary_cross_entropy(input, label)
print(output) # 0.65537095 >>> print(output)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.65537095)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -769,12 +781,14 @@ def binary_cross_entropy_with_logits( ...@@ -769,12 +781,14 @@ def binary_cross_entropy_with_logits(
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
logit = paddle.to_tensor([5.0, 1.0, 3.0]) >>> logit = paddle.to_tensor([5.0, 1.0, 3.0])
label = paddle.to_tensor([1.0, 0.0, 1.0]) >>> label = paddle.to_tensor([1.0, 0.0, 1.0])
output = paddle.nn.functional.binary_cross_entropy_with_logits(logit, label) >>> output = paddle.nn.functional.binary_cross_entropy_with_logits(logit, label)
print(output) # 0.45618808 >>> print(output)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.45618808)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -933,29 +947,36 @@ def hsigmoid_loss( ...@@ -933,29 +947,36 @@ def hsigmoid_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
paddle.set_device('cpu') >>> paddle.set_device('cpu')
>>> paddle.seed(2023)
input = paddle.uniform([4, 3])
# [[0.45424712 -0.77296764 0.82943869] # random >>> input = paddle.uniform([4, 3])
# [0.85062802 0.63303483 0.35312140] # random >>> print(input)
# [0.57170701 0.16627562 0.21588242] # random Tensor(shape=[4, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.27610803 -0.99303514 -0.17114788]] # random [[ 0.73167229, 0.04029441, -0.48078126],
label = paddle.to_tensor([0, 1, 4, 5]) [ 0.81050646, -0.15199822, -0.18717426],
num_classes = 5 [ 0.94041789, 0.48874724, 0.03570259],
weight=paddle.uniform([num_classes-1, 3]) [ 0.46585739, 0.95573163, -0.91368192]])
# [[-0.64477652 0.24821866 -0.17456549] # random >>> label = paddle.to_tensor([0, 1, 4, 5])
# [-0.04635394 0.07473493 -0.25081766] # random >>> num_classes = 5
# [ 0.05986035 -0.12185556 0.45153677] # random >>> weight = paddle.uniform([num_classes - 1, 3])
# [-0.66236806 0.91271877 -0.88088769]] # random >>> print(weight)
Tensor(shape=[4, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
out=F.hsigmoid_loss(input, label, num_classes, weight) [[-0.14721161, 0.43916738, -0.58377075],
# [[1.96709502] [-0.60536981, -0.23151302, -0.70793629],
# [2.40019274] [-0.54572451, -0.10784978, -0.56684279],
# [2.11009121] [ 0.35370791, -0.07079649, 0.84765708]])
# [1.92374969]] >>> out = F.hsigmoid_loss(input, label, num_classes, weight)
>>> print(out)
Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2.23681736],
[1.97140026],
[1.66425037],
[2.54727197]])
""" """
if num_classes < 2: if num_classes < 2:
raise ValueError(f'Expected num_classes >= 2 (got {num_classes})') raise ValueError(f'Expected num_classes >= 2 (got {num_classes})')
...@@ -1067,13 +1088,16 @@ def smooth_l1_loss(input, label, reduction='mean', delta=1.0, name=None): ...@@ -1067,13 +1088,16 @@ def smooth_l1_loss(input, label, reduction='mean', delta=1.0, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> paddle.seed(2023)
>>> input = paddle.rand([3, 3]).astype('float32')
>>> label = paddle.rand([3, 3]).astype('float32')
>>> output = paddle.nn.functional.smooth_l1_loss(input, label)
>>> print(output)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.08307374)
input = paddle.rand([3, 3]).astype('float32')
label = paddle.rand([3, 3]).astype('float32')
output = paddle.nn.functional.smooth_l1_loss(input, label)
print(output)
# 0.068004
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -1155,13 +1179,16 @@ def margin_ranking_loss( ...@@ -1155,13 +1179,16 @@ def margin_ranking_loss(
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> input = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32')
>>> other = paddle.to_tensor([[2, 1], [2, 4]], dtype='float32')
>>> label = paddle.to_tensor([[1, -1], [-1, -1]], dtype='float32')
>>> loss = paddle.nn.functional.margin_ranking_loss(input, other, label)
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.75000000)
input = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32')
other = paddle.to_tensor([[2, 1], [2, 4]], dtype='float32')
label = paddle.to_tensor([[1, -1], [-1, -1]], dtype='float32')
loss = paddle.nn.functional.margin_ranking_loss(input, other, label)
print(loss) # 0.75
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
raise ValueError( raise ValueError(
...@@ -1271,26 +1298,26 @@ def l1_loss(input, label, reduction='mean', name=None): ...@@ -1271,26 +1298,26 @@ def l1_loss(input, label, reduction='mean', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]]) >>> input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]])
label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]]) >>> label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]])
l1_loss = paddle.nn.functional.l1_loss(input, label) >>> l1_loss = paddle.nn.functional.l1_loss(input, label)
print(l1_loss) >>> print(l1_loss)
# Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 0.34999999) 0.34999999)
l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='none') >>> l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='none')
print(l1_loss) >>> print(l1_loss)
# Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.20000005, 0.19999999], [[0.20000005, 0.19999999],
# [0.20000000, 0.79999995]]) [0.20000000, 0.79999995]])
l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='sum') >>> l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='sum')
print(l1_loss) >>> print(l1_loss)
# Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 1.39999998) 1.39999998)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -1367,19 +1394,22 @@ def nll_loss( ...@@ -1367,19 +1394,22 @@ def nll_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.nn.functional import nll_loss >>> from paddle.nn.functional import nll_loss
log_softmax = paddle.nn.LogSoftmax(axis=1) >>> log_softmax = paddle.nn.LogSoftmax(axis=1)
input = paddle.to_tensor([[0.88103855, 0.9908683 , 0.6226845 ], >>> input = paddle.to_tensor([[0.88103855, 0.9908683 , 0.6226845 ],
[0.53331435, 0.07999352, 0.8549948 ], ... [0.53331435, 0.07999352, 0.8549948 ],
[0.25879037, 0.39530203, 0.698465 ], ... [0.25879037, 0.39530203, 0.698465 ],
[0.73427284, 0.63575995, 0.18827209], ... [0.73427284, 0.63575995, 0.18827209],
[0.05689114, 0.0862954 , 0.6325046 ]], "float32") ... [0.05689114, 0.0862954 , 0.6325046 ]], "float32")
log_out = log_softmax(input) >>> log_out = log_softmax(input)
label = paddle.to_tensor([0, 2, 1, 1, 0], "int64") >>> label = paddle.to_tensor([0, 2, 1, 1, 0], "int64")
result = nll_loss(log_out, label) >>> result = nll_loss(log_out, label)
print(result) # Tensor(shape=[], dtype=float32, place=CPUPlace, stop_gradient=True, 1.07202101) >>> print(result)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.07202101)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
raise ValueError( raise ValueError(
...@@ -1502,15 +1532,24 @@ def poisson_nll_loss( ...@@ -1502,15 +1532,24 @@ def poisson_nll_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
>>> paddle.seed(2023)
input = paddle.randn([5, 2], dtype=paddle.float32)
label = paddle.randn([5, 2], dtype=paddle.float32) >>> input = paddle.randn([5, 2], dtype=paddle.float32)
loss = F.poisson_nll_loss(input, label, log_input=True, reduction='none') >>> label = paddle.randn([5, 2], dtype=paddle.float32)
print(loss) >>> loss = F.poisson_nll_loss(input, label, log_input=True, reduction='none')
loss = F.poisson_nll_loss(input, label, reduction='mean') >>> print(loss)
print(loss) Tensor(shape=[5, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 1.09998012, 3.68829036],
[ 1.95291090, 0.69603068],
[-0.39289063, -2.03713036],
[ 4.52518702, 1.28625548],
[ 3.94454789, 0.53521496]])
>>> loss = F.poisson_nll_loss(input, label, reduction='mean')
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.52983975)
""" """
# check parameter values # check parameter values
...@@ -1606,31 +1645,36 @@ def kl_div(input, label, reduction='mean', name=None): ...@@ -1606,31 +1645,36 @@ def kl_div(input, label, reduction='mean', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
>>> paddle.seed(2023)
shape = (5, 20) >>> shape = (5, 20)
# input(x) should be a distribution in the log space >>> # input(x) should be a distribution in the log space
x = F.log_softmax(paddle.randn(shape), axis=1).astype('float32') >>> x = F.log_softmax(paddle.randn(shape), axis=1).astype('float32')
target = paddle.uniform(shape, min=-10, max=10).astype('float32') >>> target = paddle.uniform(shape, min=-10, max=10).astype('float32')
# 'batchmean' reduction, loss shape will be [], who is 0-D Tensor >>> # 'batchmean' reduction, loss shape will be [], who is 0-D Tensor
pred_loss = F.kl_div(x, target, reduction='batchmean') >>> pred_loss = F.kl_div(x, target, reduction='batchmean')
# shape=[] >>> print(pred_loss.shape)
[]
# 'mean' reduction, loss shape will be [], who is 0-D Tensor >>> # 'mean' reduction, loss shape will be [], who is 0-D Tensor
pred_loss = F.kl_div(x, target, reduction='mean') >>> pred_loss = F.kl_div(x, target, reduction='mean')
# shape=[] >>> print(pred_loss.shape)
[]
# 'sum' reduction, loss shape will be [], who is 0-D Tensor >>> # 'sum' reduction, loss shape will be [], who is 0-D Tensor
pred_loss = F.kl_div(x, target, reduction='sum') >>> pred_loss = F.kl_div(x, target, reduction='sum')
# shape=[] >>> print(pred_loss.shape)
[]
# 'none' reduction, loss shape is same with input shape >>> # 'none' reduction, loss shape is same with input shape
pred_loss = F.kl_div(x, target, reduction='none') >>> pred_loss = F.kl_div(x, target, reduction='none')
# shape=[5, 20] >>> print(pred_loss.shape)
[5, 20]
""" """
# ugly type promotion # ugly type promotion
...@@ -1723,13 +1767,14 @@ def mse_loss(input, label, reduction='mean', name=None): ...@@ -1723,13 +1767,14 @@ def mse_loss(input, label, reduction='mean', name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
mse_loss = paddle.nn.loss.MSELoss() >>> mse_loss = paddle.nn.loss.MSELoss()
input = paddle.to_tensor(1.5) >>> input = paddle.to_tensor(1.5)
label = paddle.to_tensor(1.7) >>> label = paddle.to_tensor(1.7)
output = mse_loss(input, label) >>> output = mse_loss(input, label)
print(output) >>> print(output)
# 0.04000002 Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.04000002)
""" """
...@@ -1791,56 +1836,54 @@ def ctc_loss( ...@@ -1791,56 +1836,54 @@ def ctc_loss(
.. code-block:: python .. code-block:: python
# declarative mode >>> # declarative mode
import paddle.nn.functional as F >>> import paddle.nn.functional as F
import paddle >>> import paddle
>>> import numpy as np
# length of the longest logit sequence
max_seq_length = 4 >>> # length of the longest logit sequence
#length of the longest label sequence >>> max_seq_length = 4
max_label_length = 3 >>> #length of the longest label sequence
# number of logit sequences >>> max_label_length = 3
batch_size = 2 >>> # number of logit sequences
# class num >>> batch_size = 2
class_num = 3 >>> # class num
>>> class_num = 3
log_probs = paddle.to_tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04],
[3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], >>> log_probs = paddle.to_tensor(np.array([
... [[4.17021990e-01, 7.20324516e-01, 1.14374816e-04],
[[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], ... [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]],
[5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], ... [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01],
... [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]],
[[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], ... [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02],
[6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], ... [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]],
... [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01],
[[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], ... [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]],
[9.68261600e-01, 3.13424170e-01, 6.92322612e-01]], ... [[8.76389146e-01, 8.94606650e-01, 8.50442126e-02],
... [3.90547849e-02, 1.69830427e-01, 8.78142476e-01]]
[[8.76389146e-01, 8.94606650e-01, 8.50442126e-02], ... ]), dtype="float32")
[3.90547849e-02, 1.69830427e-01, 8.78142476e-01]]], >>> labels = paddle.to_tensor([[1, 2, 2],
dtype="float32") ... [1, 2, 2]], dtype="int32")
labels = paddle.to_tensor([[1, 2, 2], >>> input_lengths = paddle.to_tensor([5, 5], dtype="int64")
[1, 2, 2]], dtype="int32") >>> label_lengths = paddle.to_tensor([3, 3], dtype="int64")
input_lengths = paddle.to_tensor([5, 5], dtype="int64")
label_lengths = paddle.to_tensor([3, 3], dtype="int64") >>> loss = F.ctc_loss(log_probs, labels,
... input_lengths,
loss = F.ctc_loss(log_probs, labels, ... label_lengths,
input_lengths, ... blank=0,
label_lengths, ... reduction='none')
blank=0, >>> print(loss)
reduction='none') Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
print(loss) [3.91798496, 2.90765190])
# Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [3.91798496, 2.90765190]) >>> loss = F.ctc_loss(log_probs, labels,
... input_lengths,
loss = F.ctc_loss(log_probs, labels, ... label_lengths,
input_lengths, ... blank=0,
label_lengths, ... reduction='mean')
blank=0, >>> print(loss)
reduction='mean') Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
print(loss) 1.13760614)
# Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# 1.13760614)
""" """
...@@ -1940,34 +1983,37 @@ def rnnt_loss( ...@@ -1940,34 +1983,37 @@ def rnnt_loss(
.. code-block:: python .. code-block:: python
# declarative mode >>> # declarative mode
import paddle.nn.functional as F >>> import paddle.nn.functional as F
import numpy as np >>> import numpy as np
import paddle >>> import paddle
import functools >>> import functools
fn = functools.partial(F.rnnt_loss, reduction='sum', fastemit_lambda=0.0, blank=0) >>> fn = functools.partial(F.rnnt_loss, reduction='sum', fastemit_lambda=0.0, blank=0)
acts = np.array([[[[0.1, 0.6, 0.1, 0.1, 0.1], >>> acts = np.array([[
[0.1, 0.1, 0.6, 0.1, 0.1], ... [[0.1, 0.6, 0.1, 0.1, 0.1],
[0.1, 0.1, 0.2, 0.8, 0.1]], ... [0.1, 0.1, 0.6, 0.1, 0.1],
[[0.1, 0.6, 0.1, 0.1, 0.1], ... [0.1, 0.1, 0.2, 0.8, 0.1]],
[0.1, 0.1, 0.2, 0.1, 0.1], ... [[0.1, 0.6, 0.1, 0.1, 0.1],
[0.7, 0.1, 0.2, 0.1, 0.1]]]]) ... [0.1, 0.1, 0.2, 0.1, 0.1],
labels = [[1, 2]] ... [0.7, 0.1, 0.2, 0.1, 0.1]]
... ]])
acts = paddle.to_tensor(acts, stop_gradient=False) >>> labels = [[1, 2]]
lengths = [acts.shape[1]] * acts.shape[0] >>> acts = paddle.to_tensor(acts, stop_gradient=False)
label_lengths = [len(l) for l in labels]
labels = paddle.to_tensor(labels, paddle.int32) >>> lengths = [acts.shape[1]] * acts.shape[0]
lengths = paddle.to_tensor(lengths, paddle.int32) >>> label_lengths = [len(l) for l in labels]
label_lengths = paddle.to_tensor(label_lengths, paddle.int32) >>> labels = paddle.to_tensor(labels, paddle.int32)
>>> lengths = paddle.to_tensor(lengths, paddle.int32)
costs = fn(acts, labels, lengths, label_lengths) >>> label_lengths = paddle.to_tensor(label_lengths, paddle.int32)
print(costs)
# Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=False, >>> costs = fn(acts, labels, lengths, label_lengths)
# 4.49566677) >>> print(costs)
Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
-2.85042444)
""" """
def warprnnt( def warprnnt(
...@@ -2087,144 +2133,148 @@ def margin_cross_entropy( ...@@ -2087,144 +2133,148 @@ def margin_cross_entropy(
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example1 :name: code-example1
# required: gpu >>> # doctest: +REQUIRES(env:GPU)
# Single GPU >>> import paddle
import paddle >>> paddle.seed(2023)
m1 = 1.0 >>> paddle.device.set_device('gpu')
m2 = 0.5 >>> m1 = 1.0
m3 = 0.0 >>> m2 = 0.5
s = 64.0 >>> m3 = 0.0
batch_size = 2 >>> s = 64.0
feature_length = 4 >>> batch_size = 2
num_classes = 4 >>> feature_length = 4
>>> num_classes = 4
label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64')
>>> label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64')
X = paddle.randn(
shape=[batch_size, feature_length], >>> X = paddle.randn(
dtype='float64') ... shape=[batch_size, feature_length],
X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True)) ... dtype='float64')
X = paddle.divide(X, X_l2) >>> X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True))
>>> X = paddle.divide(X, X_l2)
W = paddle.randn(
shape=[feature_length, num_classes], >>> W = paddle.randn(
dtype='float64') ... shape=[feature_length, num_classes],
W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True)) ... dtype='float64')
W = paddle.divide(W, W_l2) >>> W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True))
>>> W = paddle.divide(W, W_l2)
logits = paddle.matmul(X, W)
loss, softmax = paddle.nn.functional.margin_cross_entropy( >>> logits = paddle.matmul(X, W)
logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None) >>> loss, softmax = paddle.nn.functional.margin_cross_entropy(
... logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None)
print(logits) >>> print(logits)
print(label) Tensor(shape=[2, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True,
print(loss) [[-0.59561850, 0.32797505, 0.80279214, 0.00144975],
print(softmax) [-0.16265212, 0.84155098, 0.62008629, 0.79126072]])
>>> print(label)
#Tensor(shape=[2, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[2], dtype=int64, place=Place(gpu:0), stop_gradient=True,
# [[ 0.85204151, -0.55557678, 0.04994566, 0.71986042], [1, 0])
# [-0.20198586, -0.35270476, -0.55182702, 0.09749021]]) >>> print(loss)
#Tensor(shape=[2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[2, 1], dtype=float64, place=Place(gpu:0), stop_gradient=True,
# [2, 3]) [[61.94391901],
#Tensor(shape=[2, 1], dtype=float64, place=CUDAPlace(0), stop_gradient=True, [93.30853839]])
# [[82.37059586], >>> print(softmax)
# [12.13448420]]) Tensor(shape=[2, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True,
#Tensor(shape=[2, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, [[0.00000000, 0.00000000, 1. , 0.00000000],
# [[0.99978819, 0.00000000, 0.00000000, 0.00021181], [0.00000000, 0.96152676, 0.00000067, 0.03847257]])
# [0.99992995, 0.00006468, 0.00000000, 0.00000537]])
.. code-block:: python
.. code-block:: python :name: code-example2
:name: code-example2
>>> # doctest: +REQUIRES(env:DISTRIBUTED)
# required: distributed >>> # Multi GPU, test_margin_cross_entropy.py
# Multi GPU, test_margin_cross_entropy.py >>> import paddle
import paddle >>> import paddle.distributed as dist
import paddle.distributed as dist >>> paddle.seed(2023)
strategy = dist.fleet.DistributedStrategy() >>> strategy = dist.fleet.DistributedStrategy()
dist.fleet.init(is_collective=True, strategy=strategy) >>> dist.fleet.init(is_collective=True, strategy=strategy)
rank_id = dist.get_rank() >>> rank_id = dist.get_rank()
m1 = 1.0 >>> m1 = 1.0
m2 = 0.5 >>> m2 = 0.5
m3 = 0.0 >>> m3 = 0.0
s = 64.0 >>> s = 64.0
batch_size = 2 >>> batch_size = 2
feature_length = 4 >>> feature_length = 4
num_class_per_card = [4, 8] >>> num_class_per_card = [4, 8]
num_classes = paddle.sum(paddle.to_tensor(num_class_per_card)) >>> num_classes = paddle.sum(paddle.to_tensor(num_class_per_card))
label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64') >>> label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64')
label_list = [] >>> label_list = []
dist.all_gather(label_list, label) >>> dist.all_gather(label_list, label)
label = paddle.concat(label_list, axis=0) >>> label = paddle.concat(label_list, axis=0)
X = paddle.randn( >>> X = paddle.randn(
shape=[batch_size, feature_length], ... shape=[batch_size, feature_length],
dtype='float64') ... dtype='float64')
X_list = [] >>> X_list = []
dist.all_gather(X_list, X) >>> dist.all_gather(X_list, X)
X = paddle.concat(X_list, axis=0) >>> X = paddle.concat(X_list, axis=0)
X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True)) >>> X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True))
X = paddle.divide(X, X_l2) >>> X = paddle.divide(X, X_l2)
W = paddle.randn( >>> W = paddle.randn(
shape=[feature_length, num_class_per_card[rank_id]], ... shape=[feature_length, num_class_per_card[rank_id]],
dtype='float64') ... dtype='float64')
W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True)) >>> W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True))
W = paddle.divide(W, W_l2) >>> W = paddle.divide(W, W_l2)
logits = paddle.matmul(X, W) >>> logits = paddle.matmul(X, W)
loss, softmax = paddle.nn.functional.margin_cross_entropy( >>> loss, softmax = paddle.nn.functional.margin_cross_entropy(
logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None) ... logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None)
>>> print(logits)
print(logits) >>> print(label)
print(label) >>> print(loss)
print(loss) >>> print(softmax)
print(softmax)
>>> # python -m paddle.distributed.launch --gpus=0,1 --log_dir log test_margin_cross_entropy.py
# python -m paddle.distributed.launch --gpus=0,1 test_margin_cross_entropy.py >>> # cat log/workerlog.0
## for rank0 input >>> # Tensor(shape=[4, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True,
#Tensor(shape=[4, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, >>> # [[-0.59561850, 0.32797505, 0.80279214, 0.00144975],
# [[ 0.32888934, 0.02408748, -0.02763289, 0.18173063], >>> # [-0.16265212, 0.84155098, 0.62008629, 0.79126072],
# [-0.52893978, -0.10623845, -0.21596515, -0.06432517], >>> # [-0.59561850, 0.32797505, 0.80279214, 0.00144975],
# [-0.00536345, -0.03924667, 0.66735314, -0.28640926], >>> # [-0.16265212, 0.84155098, 0.62008629, 0.79126072]])
# [-0.09907366, -0.48534973, -0.10365338, -0.39472322]]) >>> # Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True,
#Tensor(shape=[4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, >>> # [5, 4, 5, 4])
# [11, 1 , 10, 11]) >>> # Tensor(shape=[4, 1], dtype=float64, place=Place(gpu:0), stop_gradient=True,
>>> # [[104.27437027],
## for rank1 input >>> # [113.40243782],
#Tensor(shape=[4, 8], dtype=float64, place=CUDAPlace(1), stop_gradient=True, >>> # [104.27437027],
# [[ 0.68654754, 0.28137170, 0.69694954, -0.60923933, -0.57077653, 0.54576703, -0.38709028, 0.56028204], >>> # [113.40243782]])
# [-0.80360371, -0.03042448, -0.45107338, 0.49559349, 0.69998950, -0.45411693, 0.61927630, -0.82808600], >>> # Tensor(shape=[4, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True,
# [ 0.11457570, -0.34785879, -0.68819499, -0.26189226, -0.48241491, -0.67685711, 0.06510185, 0.49660849], >>> # [[0.00000000, 0.00000000, 0.01210039, 0.00000000],
# [ 0.31604851, 0.52087884, 0.53124749, -0.86176582, -0.43426329, 0.34786144, -0.10850784, 0.51566383]]) >>> # [0.00000000, 0.96152674, 0.00000067, 0.03847257],
#Tensor(shape=[4], dtype=int64, place=CUDAPlace(1), stop_gradient=True, >>> # [0.00000000, 0.00000000, 0.01210039, 0.00000000],
# [11, 1 , 10, 11]) >>> # [0.00000000, 0.96152674, 0.00000067, 0.03847257]])
>>> # cat log/workerlog.1
## for rank0 output >>> # Tensor(shape=[4, 8], dtype=float64, place=Place(gpu:1), stop_gradient=True,
#Tensor(shape=[4, 1], dtype=float64, place=CUDAPlace(0), stop_gradient=True, >>> # [[-0.34913275, -0.35180883, -0.53976657, -0.75234331, 0.70534995,
# [[38.96608230], >>> # 0.87157838, 0.31064437, 0.19537700],
# [81.28152394], >>> # [-0.63941012, -0.05631600, -0.02561853, 0.09363013, 0.56571130,
# [69.67229865], >>> # 0.13611246, 0.08849565, 0.39219619],
# [31.74197251]]) >>> # [-0.34913275, -0.35180883, -0.53976657, -0.75234331, 0.70534995,
#Tensor(shape=[4, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, >>> # 0.87157838, 0.31064437, 0.19537700],
# [[0.00000000, 0.00000000, 0.00000000, 0.00000000], >>> # [-0.63941012, -0.05631600, -0.02561853, 0.09363013, 0.56571130,
# [0.00000000, 0.00000000, 0.00000000, 0.00000000], >>> # 0.13611246, 0.08849565, 0.39219619]])
# [0.00000000, 0.00000000, 0.99998205, 0.00000000], >>> # Tensor(shape=[4], dtype=int64, place=Place(gpu:1), stop_gradient=True,
# [0.00000000, 0.00000000, 0.00000000, 0.00000000]]) >>> # [5, 4, 5, 4])
## for rank1 output >>> # Tensor(shape=[4, 1], dtype=float64, place=Place(gpu:1), stop_gradient=True,
#Tensor(shape=[4, 1], dtype=float64, place=CUDAPlace(1), stop_gradient=True, >>> # [[104.27437027],
# [[38.96608230], >>> # [113.40243782],
# [81.28152394], >>> # [104.27437027],
# [69.67229865], >>> # [113.40243782]])
# [31.74197251]]) >>> # Tensor(shape=[4, 8], dtype=float64, place=Place(gpu:1), stop_gradient=True,
#Tensor(shape=[4, 8], dtype=float64, place=CUDAPlace(1), stop_gradient=True, >>> # [[0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00002368, 0.98787593,
# [[0.33943993, 0.00000000, 0.66051859, 0.00000000, 0.00000000, 0.00004148, 0.00000000, 0.00000000], >>> # 0.00000000, 0.00000000],
# [0.00000000, 0.00000000, 0.00000000, 0.00000207, 0.99432097, 0.00000000, 0.00567696, 0.00000000], >>> # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000002, 0.00000000,
# [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00001795], >>> # 0.00000000, 0.00000000],
# [0.00000069, 0.33993085, 0.66006319, 0.00000000, 0.00000000, 0.00000528, 0.00000000, 0.00000000]]) >>> # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00002368, 0.98787593,
>>> # 0.00000000, 0.00000000],
>>> # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000002, 0.00000000,
>>> # 0.00000000, 0.00000000]])
""" """
assert reduction in ['mean', 'sum', 'none', None] assert reduction in ['mean', 'sum', 'none', None]
...@@ -2422,15 +2472,16 @@ def softmax_with_cross_entropy( ...@@ -2422,15 +2472,16 @@ def softmax_with_cross_entropy(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> logits = paddle.to_tensor([0.4, 0.6, 0.9], dtype="float32")
>>> label = paddle.to_tensor([1], dtype="int64")
logits = paddle.to_tensor([0.4, 0.6, 0.9], dtype="float32") >>> out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label)
label = paddle.to_tensor([1], dtype="int64") >>> print(out)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.15328646])
out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label)
print(out)
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [1.15328646])
""" """
return fluid_softmax_with_cross_entropy( return fluid_softmax_with_cross_entropy(
logits, logits,
...@@ -2622,50 +2673,50 @@ def cross_entropy( ...@@ -2622,50 +2673,50 @@ def cross_entropy(
Examples: Examples:
.. code-block:: python .. code-block:: python
# hard labels >>> # hard labels
import paddle >>> import paddle
paddle.seed(99999) >>> paddle.seed(99999)
N=100 >>> N=100
C=200 >>> C=200
reduction='mean' >>> reduction='mean'
input = paddle.rand([N, C], dtype='float64') >>> input = paddle.rand([N, C], dtype='float64')
label = paddle.randint(0, C, shape=[N], dtype='int64') >>> label = paddle.randint(0, C, shape=[N], dtype='int64')
weight = paddle.rand([C], dtype='float64') >>> weight = paddle.rand([C], dtype='float64')
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( >>> cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(
weight=weight, reduction=reduction) ... weight=weight, reduction=reduction)
dy_ret = cross_entropy_loss( >>> dy_ret = cross_entropy_loss(
input, ... input,
label) ... label)
print(dy_ret) >>> print(dy_ret)
# Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
# 5.34043430) 5.35419278)
.. code-block:: python .. code-block:: python
# soft labels >>> # soft labels
import paddle >>> import paddle
paddle.seed(99999) >>> paddle.seed(99999)
axis = -1 >>> axis = -1
ignore_index = -100 >>> ignore_index = -100
N = 4 >>> N = 4
C = 3 >>> C = 3
shape = [N, C] >>> shape = [N, C]
reduction='mean' >>> reduction='mean'
weight = None >>> weight = None
logits = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0) >>> logits = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0)
labels = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0) >>> labels = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0)
labels /= paddle.sum(labels, axis=axis, keepdim=True) >>> labels /= paddle.sum(labels, axis=axis, keepdim=True)
paddle_loss_mean = paddle.nn.functional.cross_entropy( >>> paddle_loss_mean = paddle.nn.functional.cross_entropy(
logits, ... logits,
labels, ... labels,
soft_label=True, ... soft_label=True,
axis=axis, ... axis=axis,
weight=weight, ... weight=weight,
reduction=reduction) ... reduction=reduction)
print(paddle_loss_mean) >>> print(paddle_loss_mean)
# Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
# 1.11043464) 1.12801195)
""" """
...@@ -3022,15 +3073,17 @@ def sigmoid_focal_loss( ...@@ -3022,15 +3073,17 @@ def sigmoid_focal_loss(
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
logit = paddle.to_tensor([[0.97, 0.91, 0.03], [0.55, 0.43, 0.71]], dtype='float32') >>> logit = paddle.to_tensor([[0.97, 0.91, 0.03], [0.55, 0.43, 0.71]], dtype='float32')
label = paddle.to_tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], dtype='float32') >>> label = paddle.to_tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], dtype='float32')
one = paddle.to_tensor([1.], dtype='float32') >>> one = paddle.to_tensor([1.], dtype='float32')
fg_label = paddle.greater_equal(label, one) >>> fg_label = paddle.greater_equal(label, one)
fg_num = paddle.sum(paddle.cast(fg_label, dtype='float32')) >>> fg_num = paddle.sum(paddle.cast(fg_label, dtype='float32'))
output = paddle.nn.functional.sigmoid_focal_loss(logit, label, normalizer=fg_num) >>> output = paddle.nn.functional.sigmoid_focal_loss(logit, label, normalizer=fg_num)
print(output) # 0.65782464 >>> print(output)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.65782464)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -3177,17 +3230,20 @@ def multi_label_soft_margin_loss( ...@@ -3177,17 +3230,20 @@ def multi_label_soft_margin_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) >>> input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32)
# label elements in {1., -1.} >>> # label elements in {1., -1.}
label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) >>> label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32)
loss = F.multi_label_soft_margin_loss(input, label, reduction='none') >>> loss = F.multi_label_soft_margin_loss(input, label, reduction='none')
print(loss) >>> print(loss)
# Tensor([3.49625897, 0.71111226, 0.43989015]) Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
loss = F.multi_label_soft_margin_loss(input, label, reduction='mean') [3.49625897, 0.71111226, 0.43989015])
print(loss) >>> loss = F.multi_label_soft_margin_loss(input, label, reduction='mean')
# Tensor(1.54908717) >>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.54908717)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
raise ValueError( raise ValueError(
...@@ -3296,22 +3352,24 @@ def hinge_embedding_loss(input, label, margin=1.0, reduction='mean', name=None): ...@@ -3296,22 +3352,24 @@ def hinge_embedding_loss(input, label, margin=1.0, reduction='mean', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) >>> input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32)
# label elements in {1., -1.} >>> # label elements in {1., -1.}
label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) >>> label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32)
loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='none') >>> loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='none')
print(loss) >>> print(loss)
# Tensor([[0., -2., 0.], Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0., -1., 2.], [[ 0., -2., 0.],
# [1., 1., 1.]]) [ 0., -1., 2.],
[ 1., 1., 1.]])
>>> loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='mean')
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.22222222)
loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='mean')
print(loss)
# Tensor(0.22222222)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -3386,20 +3444,24 @@ def cosine_embedding_loss( ...@@ -3386,20 +3444,24 @@ def cosine_embedding_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input1 = paddle.to_tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]], 'float32') >>> input1 = paddle.to_tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]], 'float32')
input2 = paddle.to_tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]], 'float32') >>> input2 = paddle.to_tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]], 'float32')
label = paddle.to_tensor([1, -1], 'int64') >>> label = paddle.to_tensor([1, -1], 'int64')
output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='mean') >>> output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='mean')
print(output) # 0.21155193 >>> print(output) # 0.21155193
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='sum') 0.21155193)
print(output) # 0.42310387 >>> output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='sum')
>>> print(output) # 0.42310387
output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='none') Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
print(output) # [0.42310387, 0. ] 0.42310387)
>>> output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='none')
>>> print(output) # [0.42310387, 0. ]
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.42310387, 0. ])
""" """
if len(label.shape) != 1: if len(label.shape) != 1:
...@@ -3519,20 +3581,21 @@ def triplet_margin_with_distance_loss( ...@@ -3519,20 +3581,21 @@ def triplet_margin_with_distance_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32)
positive= paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32)
negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32)
loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='none')
print(loss)
# Tensor([0. , 0.57496738, 0. ])
>>> input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32)
>>> positive = paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32)
>>> negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32)
>>> loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='none')
>>> print(loss)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[0. , 0.57496595, 0. ])
loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='mean') >>> loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='mean')
print(loss) >>> print(loss)
# Tensor(0.19165580) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.19165532)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -3669,20 +3732,21 @@ def triplet_margin_loss( ...@@ -3669,20 +3732,21 @@ def triplet_margin_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32)
positive= paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32)
negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32)
loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='none')
print(loss)
# Tensor([0. , 0.57496738, 0. ])
>>> input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32)
>>> positive = paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32)
>>> negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32)
>>> loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='none')
>>> print(loss)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[0. , 0.57496595, 0. ])
loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='mean') >>> loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='mean')
print(loss) >>> print(loss)
# Tensor(0.19165580) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.19165532)
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -3789,13 +3853,15 @@ def multi_margin_loss( ...@@ -3789,13 +3853,15 @@ def multi_margin_loss(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) >>> input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32)
label = paddle.to_tensor([1, 2, 1], dtype=paddle.int32) >>> label = paddle.to_tensor([1, 2, 1], dtype=paddle.int32)
loss = F.multi_margin_loss(input, label, margin=1.0, reduction='none') >>> loss = F.multi_margin_loss(input, label, margin=1.0, reduction='none')
print(loss) >>> print(loss)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[0. , 0.66666663, 0. ])
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -3895,27 +3961,28 @@ def soft_margin_loss(input, label, reduction='mean', name=None): ...@@ -3895,27 +3961,28 @@ def soft_margin_loss(input, label, reduction='mean', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> paddle.seed(2023)
input = paddle.to_tensor([[0.5, 0.6, 0.7],[0.3, 0.5, 0.2]], 'float32')
label = paddle.to_tensor([[1.0, -1.0, 1.0],[-1.0, 1.0, 1.0]], 'float32') >>> input = paddle.to_tensor([[0.5, 0.6, 0.7],[0.3, 0.5, 0.2]], 'float32')
output = paddle.nn.functional.soft_margin_loss(input, label) >>> label = paddle.to_tensor([[1.0, -1.0, 1.0],[-1.0, 1.0, 1.0]], 'float32')
print(output) >>> output = paddle.nn.functional.soft_margin_loss(input, label)
# Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, >>> print(output)
# 0.64022040) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.64022040)
input = paddle.uniform(shape=(5, 5), dtype="float32", min=0.1, max=0.8)
label = paddle.randint(0, 2, shape=(5, 5), dtype="int64") >>> input = paddle.uniform(shape=(5, 5), dtype="float32", min=0.1, max=0.8)
label[label==0]=-1 >>> label = paddle.randint(0, 2, shape=(5, 5), dtype="int64")
>>> label[label==0] = -1
output = paddle.nn.functional.soft_margin_loss(input, label, reduction='none')
print(output) >>> output = paddle.nn.functional.soft_margin_loss(input, label, reduction='none')
# Tensor(shape=[5, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, >>> print(output)
# [[1.09917796, 0.52613139, 0.56263304, 0.82736146, 0.38776723], Tensor(shape=[5, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.07179427, 1.11924267, 0.49877715, 1.10026348, 0.46184641], [[1.10725629, 0.48778144, 0.56217247, 1.12581408, 0.51430041],
# [0.84367639, 0.74795729, 0.44629076, 0.55123353, 0.77659678], [0.90375793, 0.37761253, 0.43007556, 0.95089805, 0.43288314],
# [0.39465919, 0.76651484, 0.54485321, 0.76609844, 0.77166790], [1.16043591, 0.63015938, 0.51362717, 0.43617544, 0.57783306],
# [0.51283568, 0.84757161, 0.78913331, 1.05268764, 0.45318675]]) [0.81927848, 0.52558368, 0.59713912, 0.83100700, 0.50811619],
[0.82684207, 1.02064908, 0.50296998, 1.13461733, 0.93222517]])
""" """
if reduction not in ['sum', 'mean', 'none']: if reduction not in ['sum', 'mean', 'none']:
...@@ -4007,18 +4074,27 @@ def gaussian_nll_loss( ...@@ -4007,18 +4074,27 @@ def gaussian_nll_loss(
Examples:: Examples::
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn.functional as F >>> import paddle.nn.functional as F
>>> paddle.seed(2023)
input = paddle.randn([5, 2], dtype=paddle.float32)
label = paddle.randn([5, 2], dtype=paddle.float32) >>> input = paddle.randn([5, 2], dtype=paddle.float32)
variance = paddle.ones([5, 2], dtype=paddle.float32) >>> label = paddle.randn([5, 2], dtype=paddle.float32)
>>> variance = paddle.ones([5, 2], dtype=paddle.float32)
loss = F.gaussian_nll_loss(input, label, variance, reduction='none')
print(loss) >>> loss = F.gaussian_nll_loss(input, label, variance, reduction='none')
>>> print(loss)
loss = F.gaussian_nll_loss(input, label, variance, reduction='mean') Tensor(shape=[5, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
print(loss) [[0.21808575, 1.43013096],
[1.05245590, 0.00394560],
[1.20861185, 0.00000062],
[0.56946373, 0.73300570],
[0.37142906, 0.12038800]])
>>> loss = F.gaussian_nll_loss(input, label, variance, reduction='mean')
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.57075173)
Note: Note:
The clamping of ``variance`` is ignored with respect to autograd, and so the The clamping of ``variance`` is ignored with respect to autograd, and so the
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册