Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d3323268
P
Paddle
项目概览
PaddlePaddle
/
Paddle
11 个月 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d3323268
编写于
11月 08, 2018
作者:
J
Jacek Czaja
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
- Added unit tests for softmax is_test=True op
test=develop
上级
c1fccc29
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
49 addition
and
45 deletion
+49
-45
paddle/fluid/operators/math/softmax.cc
paddle/fluid/operators/math/softmax.cc
+4
-4
paddle/fluid/operators/math/softmax_impl.h
paddle/fluid/operators/math/softmax_impl.h
+32
-35
paddle/fluid/operators/softmax_op.h
paddle/fluid/operators/softmax_op.h
+3
-3
paddle/fluid/operators/softmax_with_cross_entropy_op.h
paddle/fluid/operators/softmax_with_cross_entropy_op.h
+2
-2
python/paddle/fluid/tests/unittests/test_softmax_op.py
python/paddle/fluid/tests/unittests/test_softmax_op.py
+8
-1
未找到文件。
paddle/fluid/operators/math/softmax.cc
浏览文件 @
d3323268
...
@@ -19,10 +19,10 @@ namespace paddle {
...
@@ -19,10 +19,10 @@ namespace paddle {
namespace
operators
{
namespace
operators
{
namespace
math
{
namespace
math
{
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
float
,
true
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
float
,
true
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
float
,
false
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
float
,
false
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
double
,
true
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
double
,
true
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
double
,
false
>;
template
class
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
double
,
false
>;
template
class
SoftmaxGradFunctor
<
platform
::
CPUDeviceContext
,
float
>;
template
class
SoftmaxGradFunctor
<
platform
::
CPUDeviceContext
,
float
>;
template
class
SoftmaxGradFunctor
<
platform
::
CPUDeviceContext
,
double
>;
template
class
SoftmaxGradFunctor
<
platform
::
CPUDeviceContext
,
double
>;
...
...
paddle/fluid/operators/math/softmax_impl.h
浏览文件 @
d3323268
...
@@ -33,9 +33,9 @@ struct ValueClip {
...
@@ -33,9 +33,9 @@ struct ValueClip {
};
};
template
<
typename
DeviceContext
,
typename
T
,
bool
is_test
>
template
<
typename
DeviceContext
,
typename
T
,
bool
is_test
>
void
SoftmaxFunctor
<
DeviceContext
,
T
,
is_test
>::
operator
()(
const
DeviceContext
&
context
,
void
SoftmaxFunctor
<
DeviceContext
,
T
,
is_test
>::
operator
()(
const
framework
::
Tensor
*
X
,
const
DeviceContext
&
context
,
const
framework
::
Tensor
*
X
,
framework
::
Tensor
*
Y
)
{
framework
::
Tensor
*
Y
)
{
auto
logits
=
EigenMatrix
<
T
>::
From
(
*
X
);
auto
logits
=
EigenMatrix
<
T
>::
From
(
*
X
);
auto
softmax
=
EigenMatrix
<
T
>::
From
(
*
Y
);
auto
softmax
=
EigenMatrix
<
T
>::
From
(
*
Y
);
...
@@ -67,40 +67,37 @@ void SoftmaxFunctor<DeviceContext, T, is_test>::operator()(const DeviceContext&
...
@@ -67,40 +67,37 @@ void SoftmaxFunctor<DeviceContext, T, is_test>::operator()(const DeviceContext&
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SoftmaxFunctor
<
DeviceContext
,
T
,
true
>
{
class
SoftmaxFunctor
<
DeviceContext
,
T
,
true
>
{
void
operator
()(
const
DeviceContext
&
context
,
void
operator
()(
const
DeviceContext
&
context
,
const
framework
::
Tensor
*
X
,
const
framework
::
Tensor
*
X
,
framework
::
Tensor
*
Y
)
{
framework
::
Tensor
*
Y
)
{
auto
logits
=
EigenMatrix
<
T
>::
From
(
*
X
);
auto
logits
=
EigenMatrix
<
T
>::
From
(
*
X
);
auto
softmax
=
EigenMatrix
<
T
>::
From
(
*
Y
);
auto
softmax
=
EigenMatrix
<
T
>::
From
(
*
Y
);
const
int
kBatchDim
=
0
;
const
int
kBatchDim
=
0
;
const
int
kClassDim
=
1
;
const
int
kClassDim
=
1
;
const
int
batch_size
=
logits
.
dimension
(
kBatchDim
);
const
int
batch_size
=
logits
.
dimension
(
kBatchDim
);
const
int
num_classes
=
logits
.
dimension
(
kClassDim
);
const
int
num_classes
=
logits
.
dimension
(
kClassDim
);
Eigen
::
DSizes
<
int
,
1
>
along_class
(
kClassDim
);
Eigen
::
DSizes
<
int
,
1
>
along_class
(
kClassDim
);
Eigen
::
DSizes
<
int
,
2
>
batch_by_one
(
batch_size
,
1
);
Eigen
::
DSizes
<
int
,
2
>
batch_by_one
(
batch_size
,
1
);
Eigen
::
DSizes
<
int
,
2
>
one_by_class
(
1
,
num_classes
);
Eigen
::
DSizes
<
int
,
2
>
one_by_class
(
1
,
num_classes
);
auto
shifted_logits
=
(
logits
-
auto
shifted_logits
=
(
logits
-
logits
.
maximum
(
along_class
)
logits
.
maximum
(
along_class
)
.
eval
()
.
eval
()
.
reshape
(
batch_by_one
)
.
reshape
(
batch_by_one
)
.
broadcast
(
one_by_class
));
.
broadcast
(
one_by_class
));
softmax
.
device
(
*
context
.
eigen_device
())
=
shifted_logits
.
exp
();
softmax
.
device
(
*
context
.
eigen_device
())
=
shifted_logits
.
exp
();
softmax
.
device
(
*
context
.
eigen_device
())
=
(
softmax
*
softmax
.
device
(
*
context
.
eigen_device
())
=
(
softmax
*
softmax
.
sum
(
along_class
)
softmax
.
sum
(
along_class
)
.
inverse
()
.
inverse
()
.
eval
()
.
eval
()
.
reshape
(
batch_by_one
)
.
reshape
(
batch_by_one
)
.
broadcast
(
one_by_class
));
.
broadcast
(
one_by_class
));
}
}
};
};
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
void
SoftmaxGradFunctor
<
DeviceContext
,
T
>::
operator
()(
void
SoftmaxGradFunctor
<
DeviceContext
,
T
>::
operator
()(
const
DeviceContext
&
context
,
const
framework
::
Tensor
*
y
,
const
DeviceContext
&
context
,
const
framework
::
Tensor
*
y
,
...
...
paddle/fluid/operators/softmax_op.h
浏览文件 @
d3323268
...
@@ -36,11 +36,11 @@ class SoftmaxKernel : public framework::OpKernel<T> {
...
@@ -36,11 +36,11 @@ class SoftmaxKernel : public framework::OpKernel<T> {
Tensor
Out_2d
=
framework
::
ReshapeToMatrix
(
*
Out
,
rank
-
1
);
Tensor
Out_2d
=
framework
::
ReshapeToMatrix
(
*
Out
,
rank
-
1
);
const
bool
is_test
=
context
.
Attr
<
bool
>
(
"is_test"
);
const
bool
is_test
=
context
.
Attr
<
bool
>
(
"is_test"
);
if
(
is_test
==
true
)
{
if
(
is_test
==
true
)
{
math
::
SoftmaxFunctor
<
DeviceContext
,
T
,
true
>
()(
math
::
SoftmaxFunctor
<
DeviceContext
,
T
,
true
>
()(
context
.
template
device_context
<
DeviceContext
>(),
&
X_2d
,
&
Out_2d
);
context
.
template
device_context
<
DeviceContext
>(),
&
X_2d
,
&
Out_2d
);
}
else
{
}
else
{
math
::
SoftmaxFunctor
<
DeviceContext
,
T
,
false
>
()(
math
::
SoftmaxFunctor
<
DeviceContext
,
T
,
false
>
()(
context
.
template
device_context
<
DeviceContext
>(),
&
X_2d
,
&
Out_2d
);
context
.
template
device_context
<
DeviceContext
>(),
&
X_2d
,
&
Out_2d
);
}
}
}
}
...
...
paddle/fluid/operators/softmax_with_cross_entropy_op.h
浏览文件 @
d3323268
...
@@ -42,8 +42,8 @@ class SoftmaxWithCrossEntropyKernel : public framework::OpKernel<T> {
...
@@ -42,8 +42,8 @@ class SoftmaxWithCrossEntropyKernel : public framework::OpKernel<T> {
auto
&
dev_ctx
=
auto
&
dev_ctx
=
context
.
template
device_context
<
platform
::
CPUDeviceContext
>();
context
.
template
device_context
<
platform
::
CPUDeviceContext
>();
math
::
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
T
,
false
>
()(
dev_ctx
,
logits
,
math
::
SoftmaxFunctor
<
platform
::
CPUDeviceContext
,
T
,
false
>
()(
softmax
);
dev_ctx
,
logits
,
softmax
);
math
::
CrossEntropyFunctor
<
platform
::
CPUDeviceContext
,
T
>
()(
math
::
CrossEntropyFunctor
<
platform
::
CPUDeviceContext
,
T
>
()(
dev_ctx
,
loss
,
softmax
,
labels
,
context
.
Attr
<
bool
>
(
"soft_label"
),
dev_ctx
,
loss
,
softmax
,
labels
,
context
.
Attr
<
bool
>
(
"soft_label"
),
context
.
Attr
<
int
>
(
"ignore_index"
));
context
.
Attr
<
int
>
(
"ignore_index"
));
...
...
python/paddle/fluid/tests/unittests/test_softmax_op.py
浏览文件 @
d3323268
...
@@ -35,6 +35,7 @@ class TestSoftmaxOp(OpTest):
...
@@ -35,6 +35,7 @@ class TestSoftmaxOp(OpTest):
self
.
op_type
=
"softmax"
self
.
op_type
=
"softmax"
self
.
use_cudnn
=
False
self
.
use_cudnn
=
False
self
.
use_mkldnn
=
False
self
.
use_mkldnn
=
False
self
.
is_test
=
False
self
.
dtype
=
np
.
float32
self
.
dtype
=
np
.
float32
self
.
init_kernel_type
()
self
.
init_kernel_type
()
self
.
shape
=
self
.
get_x_shape
()
self
.
shape
=
self
.
get_x_shape
()
...
@@ -48,7 +49,8 @@ class TestSoftmaxOp(OpTest):
...
@@ -48,7 +49,8 @@ class TestSoftmaxOp(OpTest):
self
.
outputs
=
{
'Out'
:
out
}
self
.
outputs
=
{
'Out'
:
out
}
self
.
attrs
=
{
self
.
attrs
=
{
'use_cudnn'
:
self
.
use_cudnn
,
'use_cudnn'
:
self
.
use_cudnn
,
'use_mkldnn'
:
self
.
use_mkldnn
'use_mkldnn'
:
self
.
use_mkldnn
,
'is_test'
:
self
.
is_test
}
}
def
init_kernel_type
(
self
):
def
init_kernel_type
(
self
):
...
@@ -144,6 +146,11 @@ class TestSoftmaxFP16CUDNNOp2(TestSoftmaxFP16CUDNNOp):
...
@@ -144,6 +146,11 @@ class TestSoftmaxFP16CUDNNOp2(TestSoftmaxFP16CUDNNOp):
return
[
2
,
3
,
4
,
5
]
return
[
2
,
3
,
4
,
5
]
class
TestSoftmaxInference
(
TestSoftmaxOp
):
def
init_kernel_type
(
self
):
self
.
is_test
=
True
class
TestSoftmaxMKLDNNOp
(
TestSoftmaxOp
):
class
TestSoftmaxMKLDNNOp
(
TestSoftmaxOp
):
def
init_kernel_type
(
self
):
def
init_kernel_type
(
self
):
self
.
use_mkldnn
=
True
self
.
use_mkldnn
=
True
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录