Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
7d16fe87
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7d16fe87
编写于
9月 06, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine softmax operator.
上级
b3afe30d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
47 addition
and
35 deletion
+47
-35
paddle/operators/scale_op.cc
paddle/operators/scale_op.cc
+11
-7
paddle/operators/softmax_op.cc
paddle/operators/softmax_op.cc
+15
-12
paddle/operators/softmax_op.h
paddle/operators/softmax_op.h
+8
-8
python/paddle/v2/framework/tests/test_softmax_op.py
python/paddle/v2/framework/tests/test_softmax_op.py
+13
-8
未找到文件。
paddle/operators/scale_op.cc
浏览文件 @
7d16fe87
...
...
@@ -44,11 +44,12 @@ class ScaleOpMaker : public framework::OpProtoAndCheckerMaker {
The equation is: Out = scale*X
)DOC"
);
AddAttr
<
AttrType
>
(
"scale"
,
"scale of scale operator."
).
SetDefault
(
1.0
);
AddAttr
<
AttrType
>
(
"scale"
,
"The scaling factor of the scale operator."
)
.
SetDefault
(
1.0
);
}
};
// Identity
Op's gradient is identity o
p, too.
// Identity
Op's gradient is IdentityO
p, too.
// Grad(Out=scale(X)) => Grad(X) = scale(Grad(Out))
template
<
typename
AttrType
>
class
ScaleGradOp
:
public
NetOp
{
...
...
@@ -65,17 +66,20 @@ class ScaleGradOp : public NetOp {
}
};
//
identity is a alias of scale op. This is also a example for creating a alias
// operator.
//
IdentityOp is an alias of the ScaleOp. This is also an example for creating
//
an alias of an existing
operator.
template
<
typename
AttrType
>
class
IdentityOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
IdentityOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"input tensor of identity op"
);
AddOutput
(
"Out"
,
"output tensor of identity op"
);
AddComment
(
"identity operator. Just a alias of scale op which scale = 1.0"
);
AddInput
(
"X"
,
"The input tensor of identity op."
);
AddOutput
(
"Out"
,
"The output tensor of identity op."
);
AddComment
(
R"DOC(
The identity operator is just an alias of the scale operator with the
attribute scale is fixed to 1.0.
)DOC"
);
}
};
...
...
paddle/operators/softmax_op.cc
浏览文件 @
7d16fe87
...
...
@@ -23,9 +23,9 @@ class SoftmaxOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"
X
"
)
->
dims
().
size
()
==
2UL
,
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"
logits
"
)
->
dims
().
size
()
==
2UL
,
"The input of softmax op must be a matrix."
);
ctx
.
Output
<
Tensor
>
(
"
Y"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X
"
)
->
dims
());
ctx
.
Output
<
Tensor
>
(
"
softmax"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"logits
"
)
->
dims
());
}
};
...
...
@@ -34,10 +34,10 @@ class SoftmaxOpMaker : public framework::OpProtoAndCheckerMaker {
SoftmaxOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"
X
"
,
AddInput
(
"
logits
"
,
"The input tensor of softmax. "
"2-D with shape [batch_size, input_feature_dimensions]."
);
AddOutput
(
"
Y
"
,
"The normalized values with the same shape as X."
);
AddOutput
(
"
softmax
"
,
"The normalized values with the same shape as X."
);
AddComment
(
R"DOC(
The input of softmax operator is a 2-D tensor with shape N x K (N is the
batch_size, K is the dimension of input feature). The output tensor has the
...
...
@@ -64,14 +64,17 @@ class SoftmaxOpGrad : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
.
InputVar
(
"Y"
)
!=
nullptr
,
"Input(Y) should not be null"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Y"
)),
"Input(Y@GRAD) should not be null"
);
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
()
==
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
))
->
dims
(),
"the shape of Input(0) and Input(1) should be the same"
);
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
))
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
());
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"softmax"
),
"Input(softmax) should be not null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"softmax"
)),
"Input(softmax@GRAD) should be not null."
);
PADDLE_ENFORCE_EQ
(
ctx
.
Input
<
Tensor
>
(
"softmax"
)
->
dims
(),
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"softmax"
))
->
dims
(),
"Input(softmax) and its gradients should have a same shape."
);
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"logits"
))
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"logits"
)
->
dims
());
}
};
...
...
paddle/operators/softmax_op.h
浏览文件 @
7d16fe87
...
...
@@ -28,12 +28,12 @@ template <typename Place, typename T>
class
SoftmaxKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
input
=
context
.
Input
<
Tensor
>
(
"X
"
);
auto
output
=
context
.
Output
<
Tensor
>
(
"Y
"
);
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
X
=
context
.
Input
<
Tensor
>
(
"logits
"
);
auto
Y
=
context
.
Output
<
Tensor
>
(
"softmax
"
);
Y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
logits
=
EigenMatrix
<
T
>::
From
(
*
input
);
auto
softmax
=
EigenMatrix
<
T
>::
From
(
*
output
);
auto
logits
=
EigenMatrix
<
T
>::
From
(
*
X
);
auto
softmax
=
EigenMatrix
<
T
>::
From
(
*
Y
);
const
int
kBatchDim
=
0
;
const
int
kClassDim
=
1
;
...
...
@@ -69,9 +69,9 @@ class SoftmaxGradKernel : public framework::OpKernel {
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
std
::
shared_ptr
<
Tensor
>
scale_
=
std
::
make_shared
<
Tensor
>
();
auto
Y
=
context
.
Input
<
Tensor
>
(
"
Y
"
);
auto
dY
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"
Y
"
));
auto
dX
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"
X
"
));
auto
Y
=
context
.
Input
<
Tensor
>
(
"
softmax
"
);
auto
dY
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"
softmax
"
));
auto
dX
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"
logits
"
));
dX
->
mutable_data
<
T
>
(
context
.
GetPlace
());
const
int
batch_size
=
Y
->
dims
()[
0
];
...
...
python/paddle/v2/framework/tests/test_softmax_op.py
浏览文件 @
7d16fe87
...
...
@@ -18,18 +18,23 @@ class TestSoftmaxOp(unittest.TestCase):
def
setUp
(
self
):
self
.
type
=
"softmax"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
32
,
10
0
)).
astype
(
"float32"
)}
self
.
inputs
=
{
"logits"
:
np
.
random
.
random
((
10
,
1
0
)).
astype
(
"float32"
)}
self
.
outputs
=
{
'Y'
:
np
.
apply_along_axis
(
stable_softmax
,
1
,
self
.
inputs
[
'X'
])
"softmax"
:
np
.
apply_along_axis
(
stable_softmax
,
1
,
self
.
inputs
[
"logits"
])
}
class
SoftmaxGradOpTest
(
GradientChecker
):
def
test_softmax
(
self
):
op
=
create_op
(
"softmax"
)
inputs
=
{
"X"
:
np
.
random
.
uniform
(
0.1
,
1
,
[
10
,
10
]).
astype
(
"float32"
)}
self
.
check_grad
(
op
,
inputs
,
set
(
"X"
),
"Y"
)
class
TestSoftmaxGradOp
(
GradientChecker
):
def
setUp
(
self
):
self
.
op
=
create_op
(
"softmax"
)
self
.
inputs
=
{
"logits"
:
np
.
random
.
uniform
(
0.1
,
1
,
[
10
,
10
]).
astype
(
"float32"
)
}
def
test_softmax_grad
(
self
):
self
.
check_grad
(
self
.
op
,
self
.
inputs
,
[
"logits"
],
"softmax"
)
if
__name__
==
'__main__'
:
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录