Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
78f4c803
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
78f4c803
编写于
10月 05, 2017
作者:
K
Kexin Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change learning rate and fix format
上级
d1de7ec6
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
97 addition
and
61 deletion
+97
-61
paddle/operators/adagrad_op.cc
paddle/operators/adagrad_op.cc
+35
-32
paddle/operators/adagrad_op.h
paddle/operators/adagrad_op.h
+19
-24
python/paddle/v2/framework/tests/test_adagrad_op.py
python/paddle/v2/framework/tests/test_adagrad_op.py
+43
-5
未找到文件。
paddle/operators/adagrad_op.cc
浏览文件 @
78f4c803
...
...
@@ -23,33 +23,33 @@ class AdagradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
p
aram"
),
"Input(
p
aram) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
g
rad"
),
"Input(
g
rad) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
m
oment"
),
"Input(
m
oment) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
learning_r
ate"
),
"Input(
learning_r
ate) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
param_o
ut"
),
"Output(
param_o
ut) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
moment_o
ut"
),
"Output(
moment_o
ut) of AdagradOp should not be null."
);
auto
lr_dims
=
ctx
->
GetInputDim
(
"
learning_r
ate"
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
P
aram"
),
"Input(
P
aram) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
G
rad"
),
"Input(
G
rad) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
M
oment"
),
"Input(
M
oment) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
LearningR
ate"
),
"Input(
LearningR
ate) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
ParamO
ut"
),
"Output(
ParamO
ut) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
MomentO
ut"
),
"Output(
MomentO
ut) of AdagradOp should not be null."
);
auto
lr_dims
=
ctx
->
GetInputDim
(
"
LearningR
ate"
);
PADDLE_ENFORCE_EQ
(
framework
::
product
(
lr_dims
),
1
,
"
learning_r
ate should have one element"
);
auto
param_dim
=
ctx
->
GetInputDim
(
"p
aram"
);
"
LearningR
ate should have one element"
);
auto
param_dim
s
=
ctx
->
GetInputDim
(
"P
aram"
);
PADDLE_ENFORCE_EQ
(
param_dim
,
ctx
->
GetInputDim
(
"g
rad"
),
"Param and
g
rad input of AdagradOp should have the same dimension."
);
param_dim
s
,
ctx
->
GetInputDim
(
"G
rad"
),
"Param and
G
rad input of AdagradOp should have the same dimension."
);
PADDLE_ENFORCE_EQ
(
param_dim
,
ctx
->
GetInputDim
(
"m
oment"
),
"Param and
m
oment input of AdagradOp should have the same dimension."
);
param_dim
s
,
ctx
->
GetInputDim
(
"M
oment"
),
"Param and
M
oment input of AdagradOp should have the same dimension."
);
ctx
->
SetOutputDim
(
"
param_out"
,
param_dim
);
ctx
->
SetOutputDim
(
"
moment_out"
,
param_dim
);
ctx
->
SetOutputDim
(
"
ParamOut"
,
param_dims
);
ctx
->
SetOutputDim
(
"
MomentOut"
,
param_dims
);
}
};
...
...
@@ -58,15 +58,18 @@ class AdagradOpMaker : public framework::OpProtoAndCheckerMaker {
AdagradOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"param"
,
"Input parameter"
);
AddInput
(
"grad"
,
"Input gradient"
);
AddInput
(
"moment"
,
"Second moment"
);
AddInput
(
"learning_rate"
,
"learning rate of adagrad"
);
AddOutput
(
"param_out"
,
"Output parameter"
);
AddOutput
(
"moment_out"
,
"Output second moment"
);
AddAttr
<
float
>
(
"epsilon"
,
"Constant for numerical stability"
);
AddInput
(
"Param"
,
"(Tensor) Input parameter"
);
AddInput
(
"Grad"
,
"(Tensor) Input gradient"
);
AddInput
(
"Moment"
,
"(Tensor) Second moment"
);
AddInput
(
"LearningRate"
,
"(Tensor) Learning rate"
);
AddOutput
(
"ParamOut"
,
"(Tensor) Output parameter"
);
AddOutput
(
"MomentOut"
,
"(Tensor) Output second moment"
);
AddAttr
<
float
>
(
"epsilon"
,
"(float, default 1.0e-6) "
"Constant for numerical stability"
)
.
SetDefault
(
1.0e-6
f
);
AddComment
(
R"DOC(
Adaptive Gradient Algorithm (Adagrad).
...
...
paddle/operators/adagrad_op.h
浏览文件 @
78f4c803
...
...
@@ -19,40 +19,35 @@ limitations under the License. */
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenScalar
=
framework
::
EigenScalar
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenVector
=
framework
::
EigenVector
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
class
AdagradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
param_out
=
ctx
.
Output
<
Tensor
>
(
"param_o
ut"
);
auto
moment_out
=
ctx
.
Output
<
Tensor
>
(
"moment_o
ut"
);
auto
param_out
_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"ParamO
ut"
);
auto
moment_out
_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"MomentO
ut"
);
param_out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
moment_out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
param_out
_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
moment_out
_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
float
lr
=
ctx
.
Input
<
Tensor
>
(
"learning_rate"
)
->
data
<
float
>
()[
0
];
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
p
=
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
Tensor
>
(
"param"
));
auto
g
=
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
Tensor
>
(
"grad"
));
auto
m
=
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
Tensor
>
(
"moment"
));
auto
lr
=
EigenScalar
<
T
>::
From
(
*
ctx
.
Input
<
Tensor
>
(
"learning_rate"
));
auto
p_out
=
EigenVector
<
T
>::
Flatten
(
*
param_out
);
auto
m_out
=
EigenVector
<
T
>::
Flatten
(
*
moment_out
);
auto
param
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Param"
));
auto
grad
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Grad"
));
auto
moment
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Moment"
));
auto
lr
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"LearningRate"
));
auto
param_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
param_out_tensor
);
auto
moment_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
moment_out_tensor
);
auto
place
=
ctx
.
GetEigenDevice
<
Place
>
();
m_out
.
device
(
place
)
=
m
+
g
*
g
;
p_out
.
device
(
place
)
=
p
-
lr
*
g
/
(
m_out
.
sqrt
()
+
epsilon
);
moment_out
.
device
(
place
)
=
moment
+
grad
*
grad
;
Eigen
::
DSizes
<
int
,
1
>
m_dsize
(
moment_out_tensor
->
numel
());
param_out
.
device
(
place
)
=
param
-
lr
.
broadcast
(
m_dsize
)
*
grad
/
(
moment_out
.
sqrt
()
+
epsilon
);
}
};
...
...
python/paddle/v2/framework/tests/test_adagrad_op.py
浏览文件 @
78f4c803
...
...
@@ -3,25 +3,63 @@ import numpy as np
from
op_test
import
OpTest
class
TestAdagradOp
(
OpTest
):
class
TestAdagradOp1
(
OpTest
):
''' Test Adagrad operator with explicit attributes
'''
def
setUp
(
self
):
self
.
op_type
=
"adagrad"
param
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
moment
=
np
.
zeros
((
123
,
321
)).
astype
(
"float32"
)
lr
=
0.01
epsilon
=
1e-8
self
.
inputs
=
{
'Param'
:
param
,
'Grad'
:
grad
,
'Moment'
:
moment
,
'LearningRate'
:
np
.
array
([
lr
]).
astype
(
"float32"
)
}
self
.
attrs
=
{
'epsilon'
:
epsilon
}
moment_out
=
moment
+
grad
*
grad
param_out
=
param
-
lr
*
grad
/
(
np
.
sqrt
(
moment_out
)
+
epsilon
)
self
.
outputs
=
{
'ParamOut'
:
param_out
,
'MomentOut'
:
moment_out
}
def
test_check_output
(
self
):
self
.
check_output
()
class
TestAdagradOp2
(
OpTest
):
''' Test Adagrad operator with default attributes
'''
lr
=
np
.
array
([
0.01
]).
astype
(
"float32"
)
def
setUp
(
self
):
self
.
op_type
=
"adagrad"
param
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
moment
=
np
.
zeros
((
123
,
321
)).
astype
(
"float32"
)
lr
=
0.01
epsilon
=
1e-6
self
.
inputs
=
{
'param'
:
param
,
'grad'
:
grad
,
'moment'
:
moment
}
self
.
inputs
=
{
'Param'
:
param
,
'Grad'
:
grad
,
'Moment'
:
moment
,
'LearningRate'
:
np
.
array
([
lr
]).
astype
(
"float32"
)
}
self
.
attrs
=
{
'
learning_rate'
:
learning_rate
,
'
epsilon'
:
epsilon
}
self
.
attrs
=
{
'epsilon'
:
epsilon
}
moment_out
=
moment
+
grad
*
grad
param_out
=
param
-
lr
*
grad
/
(
np
.
sqrt
(
moment_out
)
+
epsilon
)
self
.
outputs
=
{
'
param_out'
:
param_out
,
'moment_o
ut'
:
moment_out
}
self
.
outputs
=
{
'
ParamOut'
:
param_out
,
'MomentO
ut'
:
moment_out
}
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录