Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
1f6df878
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
1f6df878
编写于
8月 31, 2020
作者:
Z
Zhong Hui
提交者:
GitHub
8月 31, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix doc, use to_tensor
fix doc, use to_tensor for the loss ops
上级
7ee70a47
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
23 addition
and
30 deletion
+23
-30
python/paddle/nn/functional/loss.py
python/paddle/nn/functional/loss.py
+8
-12
python/paddle/nn/layer/distance.py
python/paddle/nn/layer/distance.py
+5
-5
python/paddle/nn/layer/loss.py
python/paddle/nn/layer/loss.py
+10
-13
未找到文件。
python/paddle/nn/functional/loss.py
浏览文件 @
1f6df878
...
...
@@ -147,7 +147,6 @@ def binary_cross_entropy(input, label, weight=None, reduction='mean',
label = paddle.to_tensor(label_data)
output = paddle.nn.functional.binary_cross_entropy(input, label)
print(output.numpy()) # [0.65537095]
paddle.enable_static()
"""
if
reduction
not
in
[
'sum'
,
'mean'
,
'none'
]:
...
...
@@ -165,8 +164,7 @@ def binary_cross_entropy(input, label, weight=None, reduction='mean',
return
core
.
ops
.
reduce_sum
(
out
,
'dim'
,
[
0
],
'keep_dim'
,
False
,
"reduce_all"
,
True
)
elif
reduction
==
'mean'
:
return
core
.
ops
.
reduce_mean
(
out
,
'dim'
,
[
0
],
'keep_dim'
,
False
,
"reduce_all"
,
True
)
return
core
.
ops
.
mean
(
out
)
else
:
return
out
...
...
@@ -467,14 +465,12 @@ def margin_ranking_loss(input,
.. code-block:: python
import numpy as np
import paddle
paddle.disable_static()
input = paddle.to_
variable(np.array([[1, 2], [3, 4]]).astype('float32')
)
other = paddle.to_
variable(np.array([[2, 1], [2, 4]]).astype('float32')
)
label = paddle.to_
variable(np.array([[1, -1], [-1, -1]]).astype('float32')
)
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.numpy()) # [0.75]
"""
...
...
@@ -578,8 +574,8 @@ def l1_loss(input, label, reduction='mean', name=None):
paddle.disable_static()
input_data = np.array([[1.5, 0.8], [0.2, 1.3]]).astype("float32")
label_data = np.array([[1.7, 1], [0.4, 0.5]]).astype("float32")
input = paddle.to_
variable
(input_data)
label = paddle.to_
variable
(label_data)
input = paddle.to_
tensor
(input_data)
label = paddle.to_
tensor
(label_data)
l1_loss = paddle.nn.functional.l1_loss(input, label)
print(l1_loss.numpy())
...
...
@@ -675,9 +671,9 @@ def nll_loss(input,
place = paddle.CPUPlace()
paddle.disable_static(place)
input = paddle.to_
variable
(input_np)
input = paddle.to_
tensor
(input_np)
log_out = log_softmax(input)
label = paddle.to_
variable
(label_np)
label = paddle.to_
tensor
(label_np)
result = nll_loss(log_out, label)
print(result.numpy()) # [1.0720209]
"""
...
...
python/paddle/nn/layer/distance.py
浏览文件 @
1f6df878
...
...
@@ -44,10 +44,10 @@ class PairwiseDistance(layers.Layer):
For more information, please refer to :ref:`api_guide_Name`.
Shape:
x: :math:`
(N, D)
` where `D` is the dimension of vector, available dtype
x: :math:`
[N, D]
` where `D` is the dimension of vector, available dtype
is float32, float64.
y: :math:`
(N, D)
`, y have the same shape and dtype as x.
out: :math:`
(N)`. If :attr:`keepdim` is ``True``, the out shape is :math:`(N, 1)
`.
y: :math:`
[N, D]
`, y have the same shape and dtype as x.
out: :math:`
[N]`. If :attr:`keepdim` is ``True``, the out shape is :math:`[N, 1]
`.
The same dtype as input tensor.
Examples:
...
...
@@ -58,8 +58,8 @@ class PairwiseDistance(layers.Layer):
paddle.disable_static()
x_np = np.array([[1., 3.], [3., 5.]]).astype(np.float64)
y_np = np.array([[5., 6.], [7., 8.]]).astype(np.float64)
x = paddle.to_
variable
(x_np)
y = paddle.to_
variable
(y_np)
x = paddle.to_
tensor
(x_np)
y = paddle.to_
tensor
(y_np)
dist = paddle.nn.PairwiseDistance()
distance = dist(x, y)
print(distance.numpy()) # [5. 5.]
...
...
python/paddle/nn/layer/loss.py
浏览文件 @
1f6df878
...
...
@@ -376,8 +376,8 @@ class L1Loss(fluid.dygraph.Layer):
paddle.disable_static()
input_data = np.array([[1.5, 0.8], [0.2, 1.3]]).astype("float32")
label_data = np.array([[1.7, 1], [0.4, 0.5]]).astype("float32")
input = paddle.to_
variable
(input_data)
label = paddle.to_
variable
(label_data)
input = paddle.to_
tensor
(input_data)
label = paddle.to_
tensor
(label_data)
l1_loss = paddle.nn.loss.L1Loss()
output = l1_loss(input, label)
...
...
@@ -455,7 +455,7 @@ class BCELoss(fluid.dygraph.Layer):
For more information, please refer to :ref:`api_guide_Name`.
Shape:
input (Tensor): 2-D tensor with shape:
(N, *)
, N is batch_size, `*` means
input (Tensor): 2-D tensor with shape:
[N, *]
, N is batch_size, `*` means
number of additional dimensions. The input ``input`` should always
be the output of sigmod. Available dtype is float32, float64.
label (Tensor): 2-D tensor with the same shape as ``input``. The target
...
...
@@ -476,12 +476,11 @@ class BCELoss(fluid.dygraph.Layer):
label_data = np.array([1.0, 0.0, 1.0]).astype("float32")
paddle.disable_static()
input = paddle.to_
variable
(input_data)
label = paddle.to_
variable
(label_data)
input = paddle.to_
tensor
(input_data)
label = paddle.to_
tensor
(label_data)
bce_loss = paddle.nn.loss.BCELoss()
output = bce_loss(input, label)
print(output.numpy()) # [0.65537095]
paddle.enable_static()
"""
...
...
@@ -584,9 +583,9 @@ class NLLLoss(fluid.dygraph.Layer):
place = paddle.CPUPlace()
paddle.disable_static(place)
input = paddle.to_
variable
(input_np)
input = paddle.to_
tensor
(input_np)
log_out = log_softmax(input)
label = paddle.to_
variable
(label_np)
label = paddle.to_
tensor
(label_np)
result = nll_loss(log_out, label)
print(result.numpy()) # [1.0720209]
...
...
@@ -729,14 +728,12 @@ class MarginRankingLoss(fluid.dygraph.Layer):
.. code-block:: python
import numpy as np
import paddle
paddle.disable_static()
input = paddle.to_
variable(np.array([[1, 2], [3, 4]]).astype("float32")
)
other = paddle.to_
variable(np.array([[2, 1], [2, 4]]).astype("float32")
)
label = paddle.to_
variable(np.array([[1, -1], [-1, -1]]).astype("float32")
)
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"
)
margin_rank_loss = paddle.nn.MarginRankingLoss()
loss = margin_rank_loss(input, other, label)
print(loss.numpy()) # [0.75]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录