Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1fe5acb2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
1fe5acb2
编写于
12月 01, 2017
作者:
Y
Yu Yang
提交者:
GitHub
12月 01, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Expose sigmoid_cross_entropy_with_logits (#6147)
Also, change the `labels` to `label` for api consistency
上级
44e39144
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
41 addition
and
29 deletion
+41
-29
paddle/operators/sigmoid_cross_entropy_with_logits_op.cc
paddle/operators/sigmoid_cross_entropy_with_logits_op.cc
+11
-13
paddle/operators/sigmoid_cross_entropy_with_logits_op.h
paddle/operators/sigmoid_cross_entropy_with_logits_op.h
+2
-4
python/paddle/v2/fluid/layers.py
python/paddle/v2/fluid/layers.py
+1
-0
python/paddle/v2/fluid/tests/test_layers.py
python/paddle/v2/fluid/tests/test_layers.py
+10
-0
python/paddle/v2/fluid/tests/test_sigmoid_cross_entropy_with_logits_op.py
.../fluid/tests/test_sigmoid_cross_entropy_with_logits_op.py
+17
-12
未找到文件。
paddle/operators/sigmoid_cross_entropy_with_logits_op.cc
浏览文件 @
1fe5acb2
...
...
@@ -25,20 +25,19 @@ class SigmoidCrossEntropyWithLogitsOp : public framework::OperatorWithKernel {
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Labels"
),
"Input(Labels) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Label"
),
"Input(Label) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) should be not null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
labels_dims
=
ctx
->
GetInputDim
(
"Label
s
"
);
auto
labels_dims
=
ctx
->
GetInputDim
(
"Label"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
2
,
"Input(X)'s rank should be 2."
);
PADDLE_ENFORCE_EQ
(
labels_dims
.
size
(),
2
,
"Input(Label
s
)'s rank should be 2."
);
"Input(Label)'s rank should be 2."
);
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
labels_dims
[
0
],
"The 1st dimension of Input(X) and Input(Label
s
) should "
"The 1st dimension of Input(X) and Input(Label) should "
"be equal."
);
PADDLE_ENFORCE_EQ
(
x_dims
[
1
],
labels_dims
[
1
],
"The 2nd dimension of Input(X) and Input(Label
s
) should "
"The 2nd dimension of Input(X) and Input(Label) should "
"be equal."
);
ctx
->
SetOutputDim
(
"Out"
,
x_dims
);
...
...
@@ -53,26 +52,25 @@ class SigmoidCrossEntropyWithLogitsGradOp
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Labels"
),
"Input(Labels) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Label"
),
"Input(Label) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) shoudl be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"Output(X@GRAD) should be not null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
labels_dims
=
ctx
->
GetInputDim
(
"Label
s
"
);
auto
labels_dims
=
ctx
->
GetInputDim
(
"Label"
);
auto
dout_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Out"
));
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
2
,
"Input(X)'s rank should be 2."
);
PADDLE_ENFORCE_EQ
(
labels_dims
.
size
(),
2
,
"Input(Label
s
)'s rank should be 2."
);
"Input(Label)'s rank should be 2."
);
PADDLE_ENFORCE_EQ
(
dout_dims
.
size
(),
2
,
"Input(Out@Grad)'s rank should be 2."
);
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
labels_dims
[
0
],
"The 1st dimension of Input(X) and Input(Label
s
) should "
"The 1st dimension of Input(X) and Input(Label) should "
"be equal."
);
PADDLE_ENFORCE_EQ
(
x_dims
[
1
],
labels_dims
[
1
],
"The 2nd dimension of Input(X) and Input(Label
s
) should "
"The 2nd dimension of Input(X) and Input(Label) should "
"be equal."
);
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
dout_dims
[
0
],
"The 1st dimension of Input(X) and Input(Out@Grad) "
...
...
@@ -97,7 +95,7 @@ class SigmoidCrossEntropyWithLogitsOpMaker
"This input is a tensor of logits computed by the previous "
" operator. Logits are unscaled log probabilities given as "
"log(p/(1-p))."
);
AddInput
(
"Label
s
"
,
AddInput
(
"Label"
,
"(Tensor, default Tensor<float>), a 2-D tensor of the same type "
"and shape as X. This input is a tensor of probabalistic labels "
"for each logit"
);
...
...
paddle/operators/sigmoid_cross_entropy_with_logits_op.h
浏览文件 @
1fe5acb2
...
...
@@ -25,8 +25,7 @@ class SigmoidCrossEntropyWithLogitsKernel : public framework::OpKernel<T> {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
const
framework
::
Tensor
*
X
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
const
framework
::
Tensor
*
Labels
=
context
.
Input
<
framework
::
Tensor
>
(
"Labels"
);
const
framework
::
Tensor
*
Labels
=
context
.
Input
<
framework
::
Tensor
>
(
"Label"
);
framework
::
Tensor
*
Out
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
Out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
...
...
@@ -52,8 +51,7 @@ class SigmoidCrossEntropyWithLogitsGradKernel : public framework::OpKernel<T> {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
const
framework
::
Tensor
*
X
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
const
framework
::
Tensor
*
Labels
=
context
.
Input
<
framework
::
Tensor
>
(
"Labels"
);
const
framework
::
Tensor
*
Labels
=
context
.
Input
<
framework
::
Tensor
>
(
"Label"
);
const
framework
::
Tensor
*
dOut
=
context
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
framework
::
Tensor
*
dX
=
...
...
python/paddle/v2/fluid/layers.py
浏览文件 @
1fe5acb2
...
...
@@ -403,6 +403,7 @@ _create_op_func_('sigmoid')
_create_op_func_
(
'scale'
)
_create_op_func_
(
'reshape'
)
_create_op_func_
(
'transpose'
)
_create_op_func_
(
'sigmoid_cross_entropy_with_logits'
)
def
cast
(
x
,
dtype
,
main_program
=
None
):
...
...
python/paddle/v2/fluid/tests/test_layers.py
浏览文件 @
1fe5acb2
...
...
@@ -137,6 +137,16 @@ class TestBook(unittest.TestCase):
print
(
str
(
program
))
def
test_sigmoid_cross_entropy
(
self
):
program
=
Program
()
with
program_guard
(
program
):
dat
=
layers
.
data
(
name
=
'data'
,
shape
=
[
10
],
dtype
=
'float32'
)
lbl
=
layers
.
data
(
name
=
'label'
,
shape
=
[
10
],
dtype
=
'float32'
)
self
.
assertIsNotNone
(
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
dat
,
label
=
lbl
))
print
(
str
(
program
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/v2/fluid/tests/test_sigmoid_cross_entropy_with_logits_op.py
浏览文件 @
1fe5acb2
...
...
@@ -2,11 +2,12 @@ import numpy as np
from
op_test
import
OpTest
from
scipy.special
import
logit
from
scipy.special
import
expit
import
unittest
class
TestSigmoidCrossEntropyWithLogitsOp1
(
OpTest
):
'''Test sigmoid_cross_entropy_with_logit_op with binary labels
'''
"""Test sigmoid_cross_entropy_with_logit_op with binary label
"""
def
setUp
(
self
):
self
.
op_type
=
"sigmoid_cross_entropy_with_logits"
...
...
@@ -16,16 +17,16 @@ class TestSigmoidCrossEntropyWithLogitsOp1(OpTest):
'X'
:
logit
(
np
.
random
.
uniform
(
0
,
1
,
(
batch_size
,
num_classes
))
.
astype
(
"float32"
)),
'Label
s
'
:
np
.
random
.
randint
(
0
,
2
,
(
batch_size
,
num_classes
))
'Label'
:
np
.
random
.
randint
(
0
,
2
,
(
batch_size
,
num_classes
))
.
astype
(
"float32"
)
}
# Fw Pass is implemented as elementwise sigmoid followed by
# elementwise logistic loss
# Label
s * -log(sigmoid(X)) + (1 - labels
) * -log(1 - sigmoid(X))
# Label
* -log(sigmoid(X)) + (1 - label
) * -log(1 - sigmoid(X))
sigmoid_X
=
expit
(
self
.
inputs
[
'X'
])
term1
=
self
.
inputs
[
'Label
s
'
]
*
np
.
log
(
sigmoid_X
)
term2
=
(
1
-
self
.
inputs
[
'Label
s
'
])
*
np
.
log
(
1
-
sigmoid_X
)
term1
=
self
.
inputs
[
'Label'
]
*
np
.
log
(
sigmoid_X
)
term2
=
(
1
-
self
.
inputs
[
'Label'
])
*
np
.
log
(
1
-
sigmoid_X
)
self
.
outputs
=
{
'Out'
:
-
term1
-
term2
}
def
test_check_output
(
self
):
...
...
@@ -36,8 +37,8 @@ class TestSigmoidCrossEntropyWithLogitsOp1(OpTest):
class
TestSigmoidCrossEntropyWithLogitsOp2
(
OpTest
):
'''Test sigmoid_cross_entropy_with_logit_op with probabalistic labels
'''
"""Test sigmoid_cross_entropy_with_logit_op with probabalistic label
"""
def
setUp
(
self
):
self
.
op_type
=
"sigmoid_cross_entropy_with_logits"
...
...
@@ -47,16 +48,16 @@ class TestSigmoidCrossEntropyWithLogitsOp2(OpTest):
'X'
:
logit
(
np
.
random
.
uniform
(
0
,
1
,
(
batch_size
,
num_classes
))
.
astype
(
"float32"
)),
'Label
s
'
:
np
.
random
.
uniform
(
0
,
1
,
(
batch_size
,
num_classes
))
'Label'
:
np
.
random
.
uniform
(
0
,
1
,
(
batch_size
,
num_classes
))
.
astype
(
"float32"
)
}
# Fw Pass is implemented as elementwise sigmoid followed by
# elementwise logistic loss
# Label
s * -log(sigmoid(X)) + (1 - labels
) * -log(1 - sigmoid(X))
# Label
* -log(sigmoid(X)) + (1 - label
) * -log(1 - sigmoid(X))
sigmoid_X
=
expit
(
self
.
inputs
[
'X'
])
term1
=
self
.
inputs
[
'Label
s
'
]
*
np
.
log
(
sigmoid_X
)
term2
=
(
1
-
self
.
inputs
[
'Label
s
'
])
*
np
.
log
(
1
-
sigmoid_X
)
term1
=
self
.
inputs
[
'Label'
]
*
np
.
log
(
sigmoid_X
)
term2
=
(
1
-
self
.
inputs
[
'Label'
])
*
np
.
log
(
1
-
sigmoid_X
)
self
.
outputs
=
{
'Out'
:
-
term1
-
term2
}
def
test_check_output
(
self
):
...
...
@@ -64,3 +65,7 @@ class TestSigmoidCrossEntropyWithLogitsOp2(OpTest):
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录