Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
78f4c803
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
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 {
...
@@ -23,33 +23,33 @@ class AdagradOp : public framework::OperatorWithKernel {
protected:
protected:
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
p
aram"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
P
aram"
),
"Input(
p
aram) of AdagradOp should not be null."
);
"Input(
P
aram) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
g
rad"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
G
rad"
),
"Input(
g
rad) of AdagradOp should not be null."
);
"Input(
G
rad) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
m
oment"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
M
oment"
),
"Input(
m
oment) of AdagradOp should not be null."
);
"Input(
M
oment) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
learning_r
ate"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"
LearningR
ate"
),
"Input(
learning_r
ate) of AdagradOp should not be null."
);
"Input(
LearningR
ate) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
param_o
ut"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
ParamO
ut"
),
"Output(
param_o
ut) of AdagradOp should not be null."
);
"Output(
ParamO
ut) of AdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
moment_o
ut"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"
MomentO
ut"
),
"Output(
moment_o
ut) of AdagradOp should not be null."
);
"Output(
MomentO
ut) of AdagradOp should not be null."
);
auto
lr_dims
=
ctx
->
GetInputDim
(
"
learning_r
ate"
);
auto
lr_dims
=
ctx
->
GetInputDim
(
"
LearningR
ate"
);
PADDLE_ENFORCE_EQ
(
framework
::
product
(
lr_dims
),
1
,
PADDLE_ENFORCE_EQ
(
framework
::
product
(
lr_dims
),
1
,
"
learning_r
ate should have one element"
);
"
LearningR
ate should have one element"
);
auto
param_dim
=
ctx
->
GetInputDim
(
"p
aram"
);
auto
param_dim
s
=
ctx
->
GetInputDim
(
"P
aram"
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
param_dim
,
ctx
->
GetInputDim
(
"g
rad"
),
param_dim
s
,
ctx
->
GetInputDim
(
"G
rad"
),
"Param and
g
rad input of AdagradOp should have the same dimension."
);
"Param and
G
rad input of AdagradOp should have the same dimension."
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
param_dim
,
ctx
->
GetInputDim
(
"m
oment"
),
param_dim
s
,
ctx
->
GetInputDim
(
"M
oment"
),
"Param and
m
oment input of AdagradOp should have the same dimension."
);
"Param and
M
oment input of AdagradOp should have the same dimension."
);
ctx
->
SetOutputDim
(
"
param_out"
,
param_dim
);
ctx
->
SetOutputDim
(
"
ParamOut"
,
param_dims
);
ctx
->
SetOutputDim
(
"
moment_out"
,
param_dim
);
ctx
->
SetOutputDim
(
"
MomentOut"
,
param_dims
);
}
}
};
};
...
@@ -58,15 +58,18 @@ class AdagradOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -58,15 +58,18 @@ class AdagradOpMaker : public framework::OpProtoAndCheckerMaker {
AdagradOpMaker
(
framework
::
OpProto
*
proto
,
AdagradOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"param"
,
"Input parameter"
);
AddInput
(
"Param"
,
"(Tensor) Input parameter"
);
AddInput
(
"grad"
,
"Input gradient"
);
AddInput
(
"Grad"
,
"(Tensor) Input gradient"
);
AddInput
(
"moment"
,
"Second moment"
);
AddInput
(
"Moment"
,
"(Tensor) Second moment"
);
AddInput
(
"learning_rate"
,
"learning rate of adagrad"
);
AddInput
(
"LearningRate"
,
"(Tensor) Learning rate"
);
AddOutput
(
"param_out"
,
"Output parameter"
);
AddOutput
(
"ParamOut"
,
"(Tensor) Output parameter"
);
AddOutput
(
"moment_out"
,
"Output second moment"
);
AddOutput
(
"MomentOut"
,
"(Tensor) Output second moment"
);
AddAttr
<
float
>
(
"epsilon"
,
"Constant for numerical stability"
);
AddAttr
<
float
>
(
"epsilon"
,
"(float, default 1.0e-6) "
"Constant for numerical stability"
)
.
SetDefault
(
1.0e-6
f
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Adaptive Gradient Algorithm (Adagrad).
Adaptive Gradient Algorithm (Adagrad).
...
...
paddle/operators/adagrad_op.h
浏览文件 @
78f4c803
...
@@ -19,40 +19,35 @@ limitations under the License. */
...
@@ -19,40 +19,35 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
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
>
template
<
typename
Place
,
typename
T
>
class
AdagradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
AdagradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
param_out
=
ctx
.
Output
<
Tensor
>
(
"param_o
ut"
);
auto
param_out
_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"ParamO
ut"
);
auto
moment_out
=
ctx
.
Output
<
Tensor
>
(
"moment_o
ut"
);
auto
moment_out
_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"MomentO
ut"
);
param_out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
param_out
_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
moment_out
->
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"
);
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
p
=
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
Tensor
>
(
"param"
));
auto
param
=
framework
::
EigenVector
<
T
>::
Flatten
(
auto
g
=
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
Tensor
>
(
"grad"
));
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Param"
));
auto
m
=
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
Tensor
>
(
"moment"
));
auto
grad
=
framework
::
EigenVector
<
T
>::
Flatten
(
auto
lr
=
EigenScalar
<
T
>::
From
(
*
ctx
.
Input
<
Tensor
>
(
"learning_rate"
));
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Grad"
));
auto
moment
=
framework
::
EigenVector
<
T
>::
Flatten
(
auto
p_out
=
EigenVector
<
T
>::
Flatten
(
*
param_out
);
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Moment"
));
auto
m_out
=
EigenVector
<
T
>::
Flatten
(
*
moment_out
);
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
>
();
auto
place
=
ctx
.
GetEigenDevice
<
Place
>
();
m_out
.
device
(
place
)
=
m
+
g
*
g
;
moment_out
.
device
(
place
)
=
moment
+
grad
*
grad
;
p_out
.
device
(
place
)
=
p
-
lr
*
g
/
(
m_out
.
sqrt
()
+
epsilon
);
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
...
@@ -3,25 +3,63 @@ import numpy as np
from
op_test
import
OpTest
from
op_test
import
OpTest
class
TestAdagradOp
(
OpTest
):
class
TestAdagradOp1
(
OpTest
):
''' Test Adagrad operator with explicit attributes
'''
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"adagrad"
self
.
op_type
=
"adagrad"
param
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
param
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
moment
=
np
.
zeros
((
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
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
moment_out
=
moment
+
grad
*
grad
param_out
=
param
-
lr
*
grad
/
(
np
.
sqrt
(
moment_out
)
+
epsilon
)
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
):
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录