Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
5fd4bee2
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5fd4bee2
编写于
10月 20, 2017
作者:
K
kexinzhao
提交者:
GitHub
10月 20, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4977 from kexinzhao/python_adagrad
Adding interface for the adagrad optimizer
上级
fa050941
7256cbe1
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
99 addition
and
1 deletion
+99
-1
python/paddle/v2/framework/optimizer.py
python/paddle/v2/framework/optimizer.py
+58
-1
python/paddle/v2/framework/tests/test_optimizer.py
python/paddle/v2/framework/tests/test_optimizer.py
+41
-0
未找到文件。
python/paddle/v2/framework/optimizer.py
浏览文件 @
5fd4bee2
import
paddle.v2.framework.framework
as
framework
from
collections
import
defaultdict
__all__
=
[
'SGDOptimizer'
,
'MomentumOptimizer'
]
__all__
=
[
'SGDOptimizer'
,
'MomentumOptimizer'
,
'AdagradOptimizer'
]
class
Optimizer
(
object
):
...
...
@@ -272,3 +272,60 @@ class MomentumOptimizer(Optimizer):
attrs
=
{
"mu"
:
self
.
_momentum
})
return
momentum_op
class
AdagradOptimizer
(
Optimizer
):
"""Simple Adagrad optimizer with moment state
"""
_moment_acc_str
=
"moment"
def
__init__
(
self
,
learning_rate
,
epsilon
=
1.0e-6
):
assert
learning_rate
is
not
None
assert
epsilon
is
not
None
super
(
AdagradOptimizer
,
self
).
__init__
()
self
.
type
=
"adagrad"
self
.
_learning_rate
=
learning_rate
self
.
_epsilon
=
epsilon
def
_initialize_tensors
(
self
,
block
):
assert
isinstance
(
block
,
framework
.
Block
)
lr_shape
=
[
1
]
# create a variable for learning_rate
self
.
_lr
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
lr_shape
,
lod_level
=
0
)
# create an op to init the learning_rate
# FIXME: Fix when Initialization design has been implemented
# https://github.com/PaddlePaddle/Paddle/pull/4852
block
.
append_op
(
type
=
"fill_constant"
,
outputs
=
{
"Out"
:
self
.
_lr
},
attrs
=
{
"shape"
:
lr_shape
,
"value"
:
self
.
_learning_rate
})
def
_create_accumulators
(
self
,
block
,
parameters
):
assert
isinstance
(
block
,
framework
.
Block
)
for
p
in
parameters
:
self
.
_add_accumulator
(
block
,
self
.
_moment_acc_str
,
p
,
'float32'
)
def
_append_optimize_op
(
self
,
block
,
param_and_grad
):
assert
isinstance
(
block
,
framework
.
Block
)
moment_acc
=
self
.
_get_accumulator
(
self
.
_moment_acc_str
,
param_and_grad
[
0
])
# create the adagrad optimizer op
adagrad_op
=
block
.
append_op
(
type
=
self
.
type
,
inputs
=
{
"Param"
:
param_and_grad
[
0
],
"Grad"
:
param_and_grad
[
1
],
"Moment"
:
moment_acc
,
"LearningRate"
:
self
.
_lr
},
outputs
=
{
"ParamOut"
:
param_and_grad
[
0
],
"MomentOut"
:
moment_acc
},
attrs
=
{
"epsilon"
:
self
.
_epsilon
})
return
adagrad_op
python/paddle/v2/framework/tests/test_optimizer.py
浏览文件 @
5fd4bee2
...
...
@@ -69,5 +69,46 @@ class TestMomentumOptimizer(unittest.TestCase):
self
.
assertTrue
(
mul_x
.
name
in
velocity_acc
)
class
TestAdagradOptimizer
(
unittest
.
TestCase
):
class
MockAdagrad
(
optimizer
.
AdagradOptimizer
):
def
get_accumulators
(
self
):
return
self
.
_accumulators
def
get_moment_str
(
self
):
return
self
.
_moment_acc_str
def
test_adagrad_optimizer
(
self
):
program
=
framework
.
Program
()
block
=
program
.
global_block
()
mul_x
=
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"mul.x"
)
mul_y
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
[
10
,
8
],
lod_level
=
0
,
name
=
"mul.y"
)
mul_out
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
[
5
,
8
],
lod_level
=
0
,
name
=
"mul.out"
)
block
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
mul_x
,
"Y"
:
mul_y
},
outputs
=
{
"Out"
:
mul_out
},
attrs
=
{
"x_num_col_dims"
:
1
})
adagrad_optimizer
=
self
.
MockAdagrad
(
learning_rate
=
0.01
,
epsilon
=
1.0e-6
)
params_grads
=
adagrad_optimizer
.
create_backward_pass
(
mul_out
)
self
.
assertEqual
(
len
(
params_grads
),
1
)
self
.
assertEqual
(
len
(
adagrad_optimizer
.
get_accumulators
()),
0
)
opts
=
adagrad_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
)
self
.
assertEqual
(
len
(
opts
),
1
)
adagrad_op
=
opts
[
0
]
self
.
assertEqual
(
adagrad_op
.
type
,
"adagrad"
)
# check accumulators
accumulators
=
adagrad_optimizer
.
get_accumulators
()
self
.
assertEqual
(
len
(
accumulators
),
1
)
self
.
assertTrue
(
adagrad_optimizer
.
get_moment_str
()
in
accumulators
)
moment_acc
=
accumulators
[
adagrad_optimizer
.
get_moment_str
()]
self
.
assertEqual
(
len
(
moment_acc
),
1
)
self
.
assertTrue
(
mul_x
.
name
in
moment_acc
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录