Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
橘蓝
Paddle
提交
7c880522
P
Paddle
项目概览
橘蓝
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
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):
...
@@ -740,6 +740,9 @@ class Block(object):
raise
e
raise
e
self
.
desc
.
remove_op
(
start
,
end
+
1
)
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
):
def
prepend_op
(
self
,
*
args
,
**
kwargs
):
op_desc
=
self
.
desc
.
prepend_op
()
op_desc
=
self
.
desc
.
prepend_op
()
op
=
Operator
(
self
,
op_desc
,
*
args
,
**
kwargs
)
op
=
Operator
(
self
,
op_desc
,
*
args
,
**
kwargs
)
...
...
python/paddle/v2/fluid/optimizer.py
浏览文件 @
7c880522
...
@@ -190,6 +190,8 @@ class Optimizer(object):
...
@@ -190,6 +190,8 @@ class Optimizer(object):
# Create any accumulators
# Create any accumulators
program
=
loss
.
block
.
program
program
=
loss
.
block
.
program
with
program_guard
(
program
,
startup_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
.
helper
=
LayerHelper
(
self
.
__class__
.
__name__
)
self
.
_create_accumulators
(
loss
.
block
,
self
.
_create_accumulators
(
loss
.
block
,
[
p
[
0
]
for
p
in
parameters_and_grads
])
[
p
[
0
]
for
p
in
parameters_and_grads
])
...
@@ -203,19 +205,14 @@ class Optimizer(object):
...
@@ -203,19 +205,14 @@ class Optimizer(object):
param_and_grad
)
param_and_grad
)
optimize_ops
.
append
(
optimize_op
)
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
# Get custom finish ops for subclasses
# FIXME: Need to fix this once we figure out how to handle dependencies
# FIXME: Need to fix this once we figure out how to handle dependencies
finish_ops
=
self
.
_finish_update
(
loss
.
block
)
self
.
_finish_update
(
loss
.
block
)
if
finish_ops
is
not
None
:
return_ops
+=
finish_ops
if
self
.
_global_step
is
not
None
:
if
self
.
_global_step
is
not
None
:
return_ops
.
append
(
self
.
_increment_global_step
(
loss
.
block
))
self
.
_increment_global_step
(
loss
.
block
)
return
return_ops
end
=
len
(
global_block
.
ops
)
return
global_block
.
slice_ops
(
start
,
end
)
def
minimize
(
self
,
def
minimize
(
self
,
loss
,
loss
,
...
...
python/paddle/v2/fluid/tests/test_optimizer.py
浏览文件 @
7c880522
...
@@ -42,9 +42,9 @@ class TestOptimizer(unittest.TestCase):
...
@@ -42,9 +42,9 @@ class TestOptimizer(unittest.TestCase):
type
=
"mean"
,
inputs
=
{
"X"
:
mul_out
},
outputs
=
{
"Out"
:
mean_out
})
type
=
"mean"
,
inputs
=
{
"X"
:
mul_out
},
outputs
=
{
"Out"
:
mean_out
})
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.01
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.01
)
opts
,
_
=
sgd_optimizer
.
minimize
(
mean_out
,
init_program
)
opts
,
_
=
sgd_optimizer
.
minimize
(
mean_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
self
.
assertEqual
(
len
(
opts
),
3
)
s
gd_op
=
opts
[
0
]
s
elf
.
assertEqual
([
op
.
type
for
op
in
opts
],
self
.
assertEqual
(
sgd_op
.
type
,
"sgd"
)
[
"fill_constant"
,
"elementwise_mul"
,
"sgd"
]
)
def
test_sgd_optimizer_with_global_step
(
self
):
def
test_sgd_optimizer_with_global_step
(
self
):
init_program
=
framework
.
Program
()
init_program
=
framework
.
Program
()
...
@@ -72,11 +72,10 @@ class TestOptimizer(unittest.TestCase):
...
@@ -72,11 +72,10 @@ class TestOptimizer(unittest.TestCase):
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
learning_rate
,
global_step
=
global_step
)
learning_rate
=
learning_rate
,
global_step
=
global_step
)
opts
,
_
=
sgd_optimizer
.
minimize
(
mean_out
,
init_program
)
opts
,
_
=
sgd_optimizer
.
minimize
(
mean_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
2
)
self
.
assertEqual
(
len
(
opts
),
4
)
sgd_op
=
opts
[
0
]
self
.
assertEqual
(
self
.
assertEqual
(
sgd_op
.
type
,
"sgd"
)
[
op
.
type
for
op
in
opts
],
increment_op
=
opts
[
1
]
[
"fill_constant"
,
"elementwise_mul"
,
"sgd"
,
"increment"
])
self
.
assertEqual
(
increment_op
.
type
,
"increment"
)
# Check init_program
# Check init_program
init_ops
=
init_program
.
global_block
().
ops
init_ops
=
init_program
.
global_block
().
ops
...
@@ -121,9 +120,10 @@ class TestMomentumOptimizer(unittest.TestCase):
...
@@ -121,9 +120,10 @@ class TestMomentumOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
momentum_optimizer
.
get_accumulators
()),
0
)
self
.
assertEqual
(
len
(
momentum_optimizer
.
get_accumulators
()),
0
)
opts
=
momentum_optimizer
.
create_optimization_pass
(
opts
=
momentum_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
self
.
assertEqual
(
len
(
opts
),
3
)
sgd_op
=
opts
[
0
]
sgd_op
=
opts
[
-
1
]
self
.
assertEqual
(
sgd_op
.
type
,
"momentum"
)
self
.
assertEqual
([
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"momentum"
])
self
.
assertFalse
(
sgd_op
.
attr
(
'use_nesterov'
))
self
.
assertFalse
(
sgd_op
.
attr
(
'use_nesterov'
))
# Check accumulators
# Check accumulators
...
@@ -170,9 +170,10 @@ class TestMomentumOptimizer(unittest.TestCase):
...
@@ -170,9 +170,10 @@ class TestMomentumOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
momentum_optimizer
.
get_accumulators
()),
0
)
self
.
assertEqual
(
len
(
momentum_optimizer
.
get_accumulators
()),
0
)
opts
=
momentum_optimizer
.
create_optimization_pass
(
opts
=
momentum_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
self
.
assertEqual
(
len
(
opts
),
3
)
sgd_op
=
opts
[
0
]
sgd_op
=
opts
[
-
1
]
self
.
assertEqual
(
sgd_op
.
type
,
"momentum"
)
self
.
assertEqual
([
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"momentum"
])
self
.
assertTrue
(
sgd_op
.
attr
(
'use_nesterov'
))
self
.
assertTrue
(
sgd_op
.
attr
(
'use_nesterov'
))
# Check accumulators
# Check accumulators
...
@@ -228,9 +229,9 @@ class TestAdagradOptimizer(unittest.TestCase):
...
@@ -228,9 +229,9 @@ class TestAdagradOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
adagrad_optimizer
.
get_accumulators
()),
0
)
self
.
assertEqual
(
len
(
adagrad_optimizer
.
get_accumulators
()),
0
)
opts
=
adagrad_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
opts
=
adagrad_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
self
.
assertEqual
(
len
(
opts
),
3
)
adagrad_op
=
opts
[
0
]
self
.
assertEqual
([
op
.
type
for
op
in
opts
],
self
.
assertEqual
(
adagrad_op
.
type
,
"adagrad"
)
[
"fill_constant"
,
"elementwise_mul"
,
"adagrad"
]
)
# Check accumulators
# Check accumulators
accumulators
=
adagrad_optimizer
.
get_accumulators
()
accumulators
=
adagrad_optimizer
.
get_accumulators
()
...
@@ -288,9 +289,10 @@ class TestAdamOptimizer(unittest.TestCase):
...
@@ -288,9 +289,10 @@ class TestAdamOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
adam_optimizer
.
get_accumulators
()),
0
)
self
.
assertEqual
(
len
(
adam_optimizer
.
get_accumulators
()),
0
)
opts
=
adam_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
opts
=
adam_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
init_program
)
self
.
assertEqual
(
len
(
opts
),
3
)
self
.
assertEqual
(
len
(
opts
),
5
)
adam_op
=
opts
[
0
]
self
.
assertEqual
(
self
.
assertEqual
(
adam_op
.
type
,
"adam"
)
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"adam"
,
"scale"
,
"scale"
])
# Check accumulators
# Check accumulators
accumulators
=
adam_optimizer
.
get_accumulators
()
accumulators
=
adam_optimizer
.
get_accumulators
()
...
@@ -350,9 +352,10 @@ class TestAdamaxOptimizer(unittest.TestCase):
...
@@ -350,9 +352,10 @@ class TestAdamaxOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
adamax_optimizer
.
get_accumulators
()),
0
)
self
.
assertEqual
(
len
(
adamax_optimizer
.
get_accumulators
()),
0
)
opts
=
adamax_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
opts
=
adamax_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
init_program
)
self
.
assertEqual
(
len
(
opts
),
2
)
self
.
assertEqual
(
len
(
opts
),
4
)
adam_op
=
opts
[
0
]
self
.
assertEqual
(
self
.
assertEqual
(
adam_op
.
type
,
"adamax"
)
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"adamax"
,
"scale"
])
# Check accumulators
# Check accumulators
accumulators
=
adamax_optimizer
.
get_accumulators
()
accumulators
=
adamax_optimizer
.
get_accumulators
()
...
@@ -409,9 +412,10 @@ class TestDecayedAdagradOptimizer(unittest.TestCase):
...
@@ -409,9 +412,10 @@ class TestDecayedAdagradOptimizer(unittest.TestCase):
self
.
assertEqual
(
len
(
decayed_adagrad_optimizer
.
get_accumulators
()),
0
)
self
.
assertEqual
(
len
(
decayed_adagrad_optimizer
.
get_accumulators
()),
0
)
opts
=
decayed_adagrad_optimizer
.
create_optimization_pass
(
opts
=
decayed_adagrad_optimizer
.
create_optimization_pass
(
params_grads
,
mul_out
,
init_program
)
params_grads
,
mul_out
,
init_program
)
self
.
assertEqual
(
len
(
opts
),
1
)
self
.
assertEqual
(
len
(
opts
),
3
)
decayed_adagrad_op
=
opts
[
0
]
self
.
assertEqual
(
self
.
assertEqual
(
decayed_adagrad_op
.
type
,
"decayed_adagrad"
)
[
op
.
type
for
op
in
opts
],
[
"fill_constant"
,
"elementwise_mul"
,
"decayed_adagrad"
])
# Check accumulators
# Check accumulators
accumulators
=
decayed_adagrad_optimizer
.
get_accumulators
()
accumulators
=
decayed_adagrad_optimizer
.
get_accumulators
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录