Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
0dce16a6
P
Paddle
项目概览
机器未来
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0dce16a6
编写于
9月 22, 2017
作者:
D
dangqingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use bool type for attr in cross_entropy_op.
上级
58e3ad0a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
17 addition
and
22 deletion
+17
-22
paddle/operators/cross_entropy_op.cc
paddle/operators/cross_entropy_op.cc
+10
-15
paddle/operators/cross_entropy_op.cu
paddle/operators/cross_entropy_op.cu
+2
-2
paddle/operators/cross_entropy_op.h
paddle/operators/cross_entropy_op.h
+2
-2
python/paddle/v2/framework/tests/test_cross_entropy_op.py
python/paddle/v2/framework/tests/test_cross_entropy_op.py
+3
-3
未找到文件。
paddle/operators/cross_entropy_op.cc
浏览文件 @
0dce16a6
...
...
@@ -35,19 +35,16 @@ class CrossEntropyOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
x
->
dims
().
size
(),
2
,
"Input(X)'s rank must be 2."
);
PADDLE_ENFORCE_EQ
(
label
->
dims
().
size
(),
2
,
"Input(Label)'s rank must be 2."
);
// TODO(xinghai-sun): remove this check after swtiching to bool
PADDLE_ENFORCE
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
0
||
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
);
PADDLE_ENFORCE_EQ
(
x
->
dims
()[
0
],
label
->
dims
()[
0
],
"The 1st dimension of Input(X) and Input(Label) must "
"be equal."
);
if
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
)
{
if
(
ctx
.
Attr
<
bool
>
(
"soft_label"
)
)
{
PADDLE_ENFORCE_EQ
(
x
->
dims
()[
1
],
label
->
dims
()[
1
],
"If Attr(soft_label) ==
1
, The 2nd dimension of "
"If Attr(soft_label) ==
true
, The 2nd dimension of "
"Input(X) and Input(Label) must be equal."
);
}
else
{
PADDLE_ENFORCE_EQ
(
label
->
dims
()[
1
],
1
,
"If Attr(soft_label) ==
0
, The 2nd dimension of "
"If Attr(soft_label) ==
false
, The 2nd dimension of "
"Input(Label) must be 1."
);
}
...
...
@@ -74,9 +71,6 @@ class CrossEntropyGradientOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
dy
->
dims
().
size
(),
2
,
"Input(Y@Grad)'s rank must be 2."
);
PADDLE_ENFORCE_EQ
(
label
->
dims
().
size
(),
2
,
"Input(Label)'s rank must be 2."
);
// TODO(xinghai-sun): remove this check after swtiching to bool
PADDLE_ENFORCE
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
0
||
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
);
PADDLE_ENFORCE_EQ
(
x
->
dims
()[
0
],
label
->
dims
()[
0
],
"The 1st dimension of Input(X) and Input(Label) must "
"be equal."
);
...
...
@@ -85,13 +79,13 @@ class CrossEntropyGradientOp : public framework::OperatorWithKernel {
"be equal."
);
PADDLE_ENFORCE_EQ
(
dy
->
dims
()[
1
],
1
,
"The 2nd dimension of Input(Y@Grad) must be 1."
);
if
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
)
{
if
(
ctx
.
Attr
<
bool
>
(
"soft_label"
)
)
{
PADDLE_ENFORCE_EQ
(
x
->
dims
()[
1
],
label
->
dims
()[
1
],
"If Attr(soft_label) ==
1
, The 2nd dimension of "
"If Attr(soft_label) ==
true
, The 2nd dimension of "
"Input(X) and Input(Label) must be equal."
);
}
else
{
PADDLE_ENFORCE_EQ
(
label
->
dims
()[
1
],
1
,
"If Attr(soft_label) ==
0
, The 2nd dimension of "
"If Attr(soft_label) ==
false
, The 2nd dimension of "
"Input(Label) must be 1."
);
}
...
...
@@ -108,7 +102,8 @@ class CrossEntropyOpMaker : public framework::OpProtoAndCheckerMaker {
AddInput
(
"X"
,
"The first input of CrossEntropyOp"
);
AddInput
(
"Label"
,
"The second input of CrossEntropyOp"
);
AddOutput
(
"Y"
,
"The output of CrossEntropyOp"
);
AddAttr
<
int
>
(
"soft_label"
,
"Is soft label. Default zero."
).
SetDefault
(
0
);
AddAttr
<
bool
>
(
"soft_label"
,
"Is soft label. Default zero."
)
.
SetDefault
(
false
);
AddComment
(
R"DOC(
CrossEntropy Operator.
...
...
@@ -116,12 +111,12 @@ CrossEntropy Operator.
It supports both standard cross-entropy and soft-label cross-entropy loss
computation.
1) One-hot cross-entropy:
soft_label =
0
, Label[i, 0] indicates the class index for sample i:
soft_label =
False
, Label[i, 0] indicates the class index for sample i:
Y[i] = -log(X[i, Label[i]])
2) Soft-label cross-entropy:
soft_label =
1
, Label[i, j] indicates the soft label of class j
soft_label =
True
, Label[i, j] indicates the soft label of class j
for sample i:
Y[i] = \sum_j{-Label[i, j] * log(X[i, j])}
...
...
paddle/operators/cross_entropy_op.cu
浏览文件 @
0dce16a6
...
...
@@ -102,7 +102,7 @@ class CrossEntropyOpCUDAKernel : public framework::OpKernel {
int
grid
=
(
n
+
block
-
1
)
/
block
;
// TODO(qingqing) launch kernel on specified stream
// base on ExecutionContext.
if
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
)
{
if
(
ctx
.
Attr
<
bool
>
(
"soft_label"
)
)
{
auto
*
label_data
=
ctx
.
Input
<
Tensor
>
(
"Label"
)
->
data
<
T
>
();
SoftCrossEntropyKernel
<
T
><<<
grid
,
block
>>>
(
y_data
,
x_data
,
label_data
,
n
,
d
);
...
...
@@ -137,7 +137,7 @@ class CrossEntropyGradientOpCUDAKernel : public framework::OpKernel {
grid
=
(
n
+
block
-
1
)
/
block
;
// TODO(qingqing): launch kernel on specified stream
// base on ExecutionContext.
if
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
)
{
if
(
ctx
.
Attr
<
bool
>
(
"soft_label"
)
)
{
auto
*
label_data
=
label
->
data
<
T
>
();
SoftCrossEntropyGradientKernel
<
T
><<<
grid
,
block
>>>
(
dx_data
,
dy_data
,
x_data
,
label_data
,
n
,
d
);
...
...
paddle/operators/cross_entropy_op.h
浏览文件 @
0dce16a6
...
...
@@ -51,7 +51,7 @@ class CrossEntropyOpKernel : public framework::OpKernel {
int
batch_size
=
x
->
dims
()[
0
];
int
class_num
=
x
->
dims
()[
1
];
if
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
)
{
if
(
ctx
.
Attr
<
bool
>
(
"soft_label"
)
)
{
auto
*
label_data
=
ctx
.
Input
<
Tensor
>
(
"Label"
)
->
data
<
T
>
();
int
index
=
0
;
for
(
int
i
=
0
;
i
<
batch_size
;
++
i
)
{
...
...
@@ -92,7 +92,7 @@ class CrossEntropyGradientOpKernel : public framework::OpKernel {
int
class_num
=
x
->
dims
()[
1
];
// TODO(qingqing): make zero setting an common function.
if
(
ctx
.
Attr
<
int
>
(
"soft_label"
)
==
1
)
{
if
(
ctx
.
Attr
<
bool
>
(
"soft_label"
)
)
{
auto
*
label_data
=
ctx
.
Input
<
Tensor
>
(
"Label"
)
->
data
<
T
>
();
int
index
=
0
;
for
(
int
i
=
0
;
i
<
batch_size
;
++
i
)
{
...
...
python/paddle/v2/framework/tests/test_cross_entropy_op.py
浏览文件 @
0dce16a6
...
...
@@ -19,7 +19,7 @@ class TestCrossEntropyOp1(OpTest):
dtype
=
"float32"
)
self
.
inputs
=
{
"X"
:
X
,
"Label"
:
label
}
self
.
outputs
=
{
"Y"
:
cross_entropy
}
self
.
attrs
=
{
'soft_label'
:
0
}
self
.
attrs
=
{
'soft_label'
:
False
}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
@@ -45,7 +45,7 @@ class TestCrossEntropyOp2(OpTest):
axis
=
1
,
keepdims
=
True
).
astype
(
"float32"
)
self
.
inputs
=
{
'X'
:
X
,
'Label'
:
label
}
self
.
outputs
=
{
'Y'
:
cross_entropy
}
self
.
attrs
=
{
'soft_label'
:
1
}
self
.
attrs
=
{
'soft_label'
:
True
}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
@@ -76,7 +76,7 @@ class TestCrossEntropyOp3(OpTest):
axis
=
1
,
keepdims
=
True
).
astype
(
"float32"
)
self
.
inputs
=
{
'X'
:
X
,
'Label'
:
label
}
self
.
outputs
=
{
'Y'
:
cross_entropy
}
self
.
attrs
=
{
'soft_label'
:
1
}
self
.
attrs
=
{
'soft_label'
:
True
}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录