Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
5b077214
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看板
未验证
提交
5b077214
编写于
11月 06, 2017
作者:
Q
qingqing01
提交者:
GitHub
11月 06, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5394 from lcy-seso/fix_softamx_with_cross_entropy
fix bugs of softmax_with_cross_entropy op.
上级
7c551fba
6f4bf505
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
69 addition
and
72 deletion
+69
-72
paddle/operators/cross_entropy_op.cc
paddle/operators/cross_entropy_op.cc
+10
-14
paddle/operators/softmax_with_cross_entropy_op.cc
paddle/operators/softmax_with_cross_entropy_op.cc
+15
-15
paddle/operators/softmax_with_cross_entropy_op.cu
paddle/operators/softmax_with_cross_entropy_op.cu
+13
-11
paddle/operators/softmax_with_cross_entropy_op.h
paddle/operators/softmax_with_cross_entropy_op.h
+18
-18
python/paddle/v2/framework/tests/test_softmax_with_cross_entropy_op.py
.../v2/framework/tests/test_softmax_with_cross_entropy_op.py
+13
-14
未找到文件。
paddle/operators/cross_entropy_op.cc
浏览文件 @
5b077214
...
...
@@ -114,21 +114,17 @@ class CrossEntropyOpMaker : public framework::OpProtoAndCheckerMaker {
"where N is the batch size and D is the number of classes. "
"This input is a probability computed by the previous operator, "
"which is almost always the result of a softmax operator."
);
AddInput
(
"Label"
,
"(Tensor, default Tensor<int>), the ground truth which is "
"a 2-D tensor. "
"When soft_label is set to false, Label is a Tensor<int> with shape "
"[N x 1]. "
"When soft_label is set to true, Label is a Tensor<float/double> "
"with shape [N x K]."
);
AddInput
(
"Label"
,
"(Tensor), the ground truth which is a 2-D tensor. When "
"soft_label is set to false, Label is a Tensor<int64> with shape "
"[N x 1]. When soft_label is set to true, Label is a "
"Tensor<float/double> with shape [N x K]."
);
AddOutput
(
"Y"
,
"(Tensor, default Tensor<float>), a 2-D tensor "
"with shape [N x 1]. The cross entropy loss."
);
AddAttr
<
bool
>
(
"soft_label"
,
"(bool, default false), a flag to indicate whether to interpretate "
"the given labels as soft labels."
)
"(Tensor, default Tensor<float>), a 2-D tensor with shape "
"[N x 1]. The cross entropy loss."
);
AddAttr
<
bool
>
(
"soft_label"
,
"(bool, default false), a flag indicating whether to "
"interpretate the given labels as soft labels."
)
.
SetDefault
(
false
);
AddComment
(
R"DOC(
CrossEntropy Operator.
...
...
paddle/operators/softmax_with_cross_entropy_op.cc
浏览文件 @
5b077214
...
...
@@ -4,13 +4,13 @@
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/softmax_with_cross_entropy_op.h"
#include <paddle/function/TensorType.h>
...
...
@@ -30,12 +30,10 @@ class SoftmaxWithCrossEntropyOpMaker
"which is a 2-D tensor with shape [N x K]. N is the batch_size, "
"and K is the class number."
);
AddInput
(
"Label"
,
"(Tensor, default: Tensor<int>), The ground truth which is a 2-D "
"tensor. "
"If softLabel is set to false, Label is a Tensor<int> with shape "
"[N x 1]."
"If softLabel is set to true, Label is a Tensor<float/double> "
"with shape [N x K]."
);
"(Tensor) The ground truth which is a 2-D tensor. If soft_label "
"is set to false, Label is a Tensor<int64> with shape [N x 1]. If "
"soft_label is set to true, Label is a Tensor<float/double> with "
"shape [N x K]."
);
AddOutput
(
"Softmax"
,
"(Tensor, default: Tensor<float>), A 2-D tensor with shape [N x K]. "
...
...
@@ -62,7 +60,7 @@ Because this operator performs a softmax on logits internally, it expects
unscaled logits. This operator should not be used with the output of
softmax operator since that would produce incorrect results.
When the attribute soft
L
abel is set false, this operators expects mutually
When the attribute soft
_l
abel is set false, this operators expects mutually
exclusive hard labels, each sample in a batch is in exactly one class with a
probability of 1.0. Each sample in the batch will have a single label.
...
...
@@ -198,6 +196,8 @@ REGISTER_OPERATOR(softmax_with_cross_entropy, ops::SoftmaxWithCrossEntropyOp,
REGISTER_OPERATOR
(
softmax_with_cross_entropy_grad
,
ops
::
SoftmaxWithCrossEntropyOpGrad
);
REGISTER_OP_CPU_KERNEL
(
softmax_with_cross_entropy
,
ops
::
SoftmaxWithCrossEntropyKernel
<
float
>
);
ops
::
SoftmaxWithCrossEntropyKernel
<
float
>
,
ops
::
SoftmaxWithCrossEntropyKernel
<
double
>
);
REGISTER_OP_CPU_KERNEL
(
softmax_with_cross_entropy_grad
,
ops
::
SoftmaxWithCrossEntropyGradKernel
<
float
>
);
ops
::
SoftmaxWithCrossEntropyGradKernel
<
float
>
,
ops
::
SoftmaxWithCrossEntropyGradKernel
<
double
>
);
paddle/operators/softmax_with_cross_entropy_op.cu
浏览文件 @
5b077214
...
...
@@ -4,13 +4,13 @@
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#define EIGEN_USE_GPU
...
...
@@ -24,7 +24,7 @@ using Tensor = framework::Tensor;
namespace
{
template
<
typename
T
>
__global__
void
CrossEntropyGrad
(
T
*
logit_grad
,
const
T
*
loss_grad
,
const
int
*
labels
,
const
int
batch_size
,
const
int
64_t
*
labels
,
const
int
batch_size
,
const
int
class_num
)
{
int
tid
=
blockIdx
.
x
*
blockDim
.
x
+
threadIdx
.
x
;
int
sample_idx
=
tid
/
class_num
;
...
...
@@ -50,7 +50,7 @@ __global__ void SoftCrossEntropyGradientKernel(T* logit_grad,
int
ids
=
blockIdx
.
x
*
blockDim
.
x
+
threadIdx
.
x
;
if
(
ids
<
batch_size
*
class_num
)
{
int
row_ids
=
ids
/
class_num
;
logit_grad
[
ids
]
=
lo
git_grad
[
ids
]
*
(
loss_grad
[
row_
ids
]
-
labels
[
ids
]);
logit_grad
[
ids
]
=
lo
ss_grad
[
row_ids
]
*
(
logit_grad
[
ids
]
-
labels
[
ids
]);
}
}
}
// namespace
...
...
@@ -104,7 +104,7 @@ class SoftmaxWithCrossEntropyGradCUDAKernel : public framework::OpKernel<T> {
.
stream
()
>>>
(
logit_grad_data
,
loss_grad_data
,
label_data
,
batch_size
,
class_num
);
}
else
{
const
int
*
label_data
=
labels
->
data
<
in
t
>
();
const
int
64_t
*
label_data
=
labels
->
data
<
int64_
t
>
();
CrossEntropyGrad
<
T
><<<
grid
,
block
,
0
,
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
context
.
device_context
())
...
...
@@ -119,6 +119,8 @@ class SoftmaxWithCrossEntropyGradCUDAKernel : public framework::OpKernel<T> {
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
softmax_with_cross_entropy
,
ops
::
SoftmaxWithCrossEntropyCUDAKernel
<
float
>
);
ops
::
SoftmaxWithCrossEntropyCUDAKernel
<
float
>
,
ops
::
SoftmaxWithCrossEntropyCUDAKernel
<
double
>
);
REGISTER_OP_GPU_KERNEL
(
softmax_with_cross_entropy_grad
,
ops
::
SoftmaxWithCrossEntropyGradCUDAKernel
<
float
>
);
ops
::
SoftmaxWithCrossEntropyGradCUDAKernel
<
float
>
,
ops
::
SoftmaxWithCrossEntropyGradCUDAKernel
<
double
>
);
paddle/operators/softmax_with_cross_entropy_op.h
浏览文件 @
5b077214
...
...
@@ -4,13 +4,13 @@
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
...
...
@@ -60,25 +60,25 @@ class SoftmaxWithCrossEntropyGradKernel : public framework::OpKernel<T> {
logit_grad
->
ShareDataWith
(
*
context
.
Input
<
Tensor
>
(
"Softmax"
));
const
int
class_num
=
logit_grad
->
dims
()[
1
];
auto
out_grad_mat
=
EigenMatrix
<
T
>::
From
(
*
out_grad
);
auto
logit_grad_mat
=
EigenMatrix
<
T
>::
From
(
*
logit_grad
);
if
(
context
.
Attr
<
bool
>
(
"soft_label"
))
{
auto
out_grad_mat
=
EigenMatrix
<
T
>::
From
(
*
out_grad
);
auto
logit_grad_mat
=
EigenMatrix
<
T
>::
From
(
*
logit_grad
);
auto
lbl_mat
=
EigenMatrix
<
T
>::
From
(
*
labels
);
logit_grad_mat
.
device
(
context
.
GetEigenDevice
<
platform
::
CPUPlace
>
())
=
logit_grad_mat
*
(
out_grad_mat
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
class_num
))
-
lbl_mat
);
out_grad_mat
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
class_num
))
*
(
logit_grad_mat
-
lbl_mat
);
}
else
{
logit_grad_mat
.
device
(
context
.
GetEigenDevice
<
platform
::
CPUPlace
>
())
=
logit_grad_mat
*
out_grad_mat
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
class_num
));
const
int
batch_size
=
logit_grad
->
dims
()[
0
];
const
int
*
label_data
=
labels
->
data
<
int
>
();
const
T
*
out_grad_data
=
out_grad
->
data
<
T
>
();
const
int64_t
*
label_data
=
labels
->
data
<
int64_t
>
();
T
*
logit_grad_data
=
logit_grad
->
data
<
T
>
();
const
T
*
out_grad_data
=
out_grad
->
data
<
T
>
();
for
(
int
i
=
0
;
i
<
batch_size
;
++
i
)
{
int
index
=
i
*
class_num
+
label_data
[
i
];
logit_grad_data
[
index
]
=
out_grad_data
[
i
]
*
(
logit_grad_data
[
index
]
-
1.
);
logit_grad_data
[
i
*
class_num
+
label_data
[
i
]]
-=
out_grad_data
[
i
];
}
}
}
...
...
python/paddle/v2/framework/tests/test_softmax_with_cross_entropy_op.py
浏览文件 @
5b077214
...
...
@@ -12,30 +12,30 @@ class TestSoftmaxWithCrossEntropyOp(OpTest):
def
setUp
(
self
):
self
.
op_type
=
"softmax_with_cross_entropy"
batch_size
=
3
batch_size
=
2
class_num
=
37
logits
=
np
.
random
.
uniform
(
0.1
,
1.0
,
[
batch_size
,
class_num
]).
astype
(
"float
32
"
)
[
batch_size
,
class_num
]).
astype
(
"float
64
"
)
softmax
=
np
.
apply_along_axis
(
stable_softmax
,
1
,
logits
)
labels
=
np
.
random
.
randint
(
0
,
class_num
,
[
batch_size
,
1
],
dtype
=
"int
32
"
)
labels
=
np
.
random
.
randint
(
0
,
class_num
,
[
batch_size
,
1
],
dtype
=
"int
64
"
)
cross_entropy
=
np
.
asmatrix
(
[[
-
np
.
log
(
softmax
[
i
][
labels
[
i
][
0
]])]
for
i
in
range
(
softmax
.
shape
[
0
])],
dtype
=
"float
32
"
)
dtype
=
"float
64
"
)
self
.
inputs
=
{
"Logits"
:
logits
,
"Label"
:
labels
}
self
.
outputs
=
{
"Softmax"
:
softmax
.
astype
(
'float32'
),
"Loss"
:
cross_entropy
.
astype
(
'float32'
)
"Softmax"
:
softmax
.
astype
(
"float64"
),
"Loss"
:
cross_entropy
.
astype
(
"float64"
)
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"Logits"
],
"Loss"
,
max_relative_error
=
0.05
)
self
.
check_grad
([
"Logits"
],
"Loss"
)
class
TestSoftmaxWithCrossEntropyOp2
(
OpTest
):
...
...
@@ -49,19 +49,19 @@ class TestSoftmaxWithCrossEntropyOp2(OpTest):
class_num
=
37
logits
=
np
.
random
.
uniform
(
0.1
,
1.0
,
[
batch_size
,
class_num
]).
astype
(
"float
32
"
)
[
batch_size
,
class_num
]).
astype
(
"float
64
"
)
softmax
=
np
.
apply_along_axis
(
stable_softmax
,
1
,
logits
)
labels
=
np
.
random
.
uniform
(
0.1
,
1.0
,
[
batch_size
,
class_num
]).
astype
(
"float
32
"
)
[
batch_size
,
class_num
]).
astype
(
"float
64
"
)
labels
/=
np
.
sum
(
labels
,
axis
=
1
,
keepdims
=
True
)
cross_entropy
=
(
-
labels
*
np
.
log
(
softmax
)).
sum
(
axis
=
1
,
keepdims
=
True
).
astype
(
"float
32
"
)
axis
=
1
,
keepdims
=
True
).
astype
(
"float
64
"
)
self
.
inputs
=
{
"Logits"
:
logits
,
"Label"
:
labels
}
self
.
outputs
=
{
"Softmax"
:
softmax
.
astype
(
'float32'
),
"Loss"
:
cross_entropy
.
astype
(
'float32'
)
"Softmax"
:
softmax
.
astype
(
"float64"
),
"Loss"
:
cross_entropy
.
astype
(
"float64"
)
}
self
.
attrs
=
{
"soft_label"
:
True
}
...
...
@@ -69,9 +69,8 @@ class TestSoftmaxWithCrossEntropyOp2(OpTest):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"Logits"
],
"Loss"
,
max_relative_error
=
0.05
)
self
.
check_grad
([
"Logits"
],
"Loss"
)
if
__name__
==
"__main__"
:
exit
(
0
)
# FIXME: xe has bug
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录