Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7c880522
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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,发现更多精彩内容 >>
提交
7c880522
编写于
2月 08, 2018
作者:
Y
Yancey1989
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize return all opt ops
上级
ca5dc46a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
39 addition
and
35 deletion
+39
-35
python/paddle/v2/fluid/framework.py
python/paddle/v2/fluid/framework.py
+3
-0
python/paddle/v2/fluid/optimizer.py
python/paddle/v2/fluid/optimizer.py
+6
-9
python/paddle/v2/fluid/tests/test_optimizer.py
python/paddle/v2/fluid/tests/test_optimizer.py
+30
-26
未找到文件。
python/paddle/v2/fluid/framework.py
浏览文件 @
7c880522
...
...
@@ -740,6 +740,9 @@ class Block(object):
raise
e
self
.
desc
.
remove_op
(
start
,
end
+
1
)
def
slice_ops
(
self
,
start
,
end
):
return
list
(
self
.
ops
)[
start
:
end
]
def
prepend_op
(
self
,
*
args
,
**
kwargs
):
op_desc
=
self
.
desc
.
prepend_op
()
op
=
Operator
(
self
,
op_desc
,
*
args
,
**
kwargs
)
...
...
python/paddle/v2/fluid/optimizer.py
浏览文件 @
7c880522
...
...
@@ -190,6 +190,8 @@ class Optimizer(object):
# Create any accumulators
program
=
loss
.
block
.
program
with
program_guard
(
program
,
startup_program
):
global_block
=
framework
.
default_main_program
().
global_block
()
start
=
len
(
global_block
.
ops
)
self
.
helper
=
LayerHelper
(
self
.
__class__
.
__name__
)
self
.
_create_accumulators
(
loss
.
block
,
[
p
[
0
]
for
p
in
parameters_and_grads
])
...
...
@@ -203,19 +205,14 @@ class Optimizer(object):
param_and_grad
)
optimize_ops
.
append
(
optimize_op
)
# Returned list of ops can include more ops in addition
# to optimization ops
return_ops
=
optimize_ops
# Get custom finish ops for subclasses
# FIXME: Need to fix this once we figure out how to handle dependencies
finish_ops
=
self
.
_finish_update
(
loss
.
block
)
if
finish_ops
is
not
None
:
return_ops
+=
finish_ops
self
.
_finish_update
(
loss
.
block
)
if
self
.
_global_step
is
not
None
:
return_ops
.
append
(
self
.
_increment_global_step
(
loss
.
block
))
return
return_ops
self
.
_increment_global_step
(
loss
.
block
)
end
=
len
(
global_block
.
ops
)
return
global_block
.
slice_ops
(
start
,
end
)
def
minimize
(
self
,
loss
,
...
...
python/paddle/v2/fluid/tests/test_optimizer.py
浏览文件 @
7c880522
...
...
@@ -42,9 +42,9 @@ class TestOptimizer(unittest.TestCase):
type
=
"mean"
,
inputs
=
{
"X"
:
mul_out
},
outputs
=
{
"Out"
:
mean_out
})
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.01
)
opts
,
_
=
sgd_optimizer
.
minimize
(
mean_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
s
gd_op
=
opts
[
0
]
self
.
assertEqual
(
sgd_op
.
type
,
"sgd"
)
self
.
assertEqual
(
len
(
opts
),
3
)
s
elf
.
assertEqual
([
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"sgd"
]
)
def
test_sgd_optimizer_with_global_step
(
self
):
init_program
=
framework
.
Program
()
...
...
@@ -72,11 +72,10 @@ class TestOptimizer(unittest.TestCase):
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
learning_rate
,
global_step
=
global_step
)
opts
,
_
=
sgd_optimizer
.
minimize
(
mean_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
2
)
sgd_op
=
opts
[
0
]
self
.
assertEqual
(
sgd_op
.
type
,
"sgd"
)
increment_op
=
opts
[
1
]
self
.
assertEqual
(
increment_op
.
type
,
"increment"
)
self
.
assertEqual
(
len
(
opts
),
4
)
self
.
assertEqual
(
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"sgd"
,
"increment"
])
# Check init_program
init_ops
=
init_program
.
global_block
().
ops
...
...
@@ -121,9 +120,10 @@ class TestMomentumOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
momentum_optimizer
.
get_accumulators
()),
0
)
opts
=
momentum_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
sgd_op
=
opts
[
0
]
self
.
assertEqual
(
sgd_op
.
type
,
"momentum"
)
self
.
assertEqual
(
len
(
opts
),
3
)
sgd_op
=
opts
[
-
1
]
self
.
assertEqual
([
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"momentum"
])
self
.
assertFalse
(
sgd_op
.
attr
(
'use_nesterov'
))
# Check accumulators
...
...
@@ -170,9 +170,10 @@ class TestMomentumOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
momentum_optimizer
.
get_accumulators
()),
0
)
opts
=
momentum_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
sgd_op
=
opts
[
0
]
self
.
assertEqual
(
sgd_op
.
type
,
"momentum"
)
self
.
assertEqual
(
len
(
opts
),
3
)
sgd_op
=
opts
[
-
1
]
self
.
assertEqual
([
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"momentum"
])
self
.
assertTrue
(
sgd_op
.
attr
(
'use_nesterov'
))
# Check accumulators
...
...
@@ -228,9 +229,9 @@ class TestAdagradOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
adagrad_optimizer
.
get_accumulators
()),
0
)
opts
=
adagrad_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
adagrad_op
=
opts
[
0
]
self
.
assertEqual
(
adagrad_op
.
type
,
"adagrad"
)
self
.
assertEqual
(
len
(
opts
),
3
)
self
.
assertEqual
([
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"adagrad"
]
)
# Check accumulators
accumulators
=
adagrad_optimizer
.
get_accumulators
()
...
...
@@ -288,9 +289,10 @@ class TestAdamOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
adam_optimizer
.
get_accumulators
()),
0
)
opts
=
adam_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
3
)
adam_op
=
opts
[
0
]
self
.
assertEqual
(
adam_op
.
type
,
"adam"
)
self
.
assertEqual
(
len
(
opts
),
5
)
self
.
assertEqual
(
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"adam"
,
"scale"
,
"scale"
])
# Check accumulators
accumulators
=
adam_optimizer
.
get_accumulators
()
...
...
@@ -350,9 +352,10 @@ class TestAdamaxOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
adamax_optimizer
.
get_accumulators
()),
0
)
opts
=
adamax_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
2
)
adam_op
=
opts
[
0
]
self
.
assertEqual
(
adam_op
.
type
,
"adamax"
)
self
.
assertEqual
(
len
(
opts
),
4
)
self
.
assertEqual
(
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"adamax"
,
"scale"
])
# Check accumulators
accumulators
=
adamax_optimizer
.
get_accumulators
()
...
...
@@ -409,9 +412,10 @@ class TestDecayedAdagradOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
decayed_adagrad_optimizer
.
get_accumulators
()),
0
)
opts
=
decayed_adagrad_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
decayed_adagrad_op
=
opts
[
0
]
self
.
assertEqual
(
decayed_adagrad_op
.
type
,
"decayed_adagrad"
)
self
.
assertEqual
(
len
(
opts
),
3
)
self
.
assertEqual
(
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"decayed_adagrad"
])
# Check accumulators
accumulators
=
decayed_adagrad_optimizer
.
get_accumulators
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录