Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e7985729
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看板
未验证
提交
e7985729
编写于
3月 06, 2020
作者:
C
Chen Weihang
提交者:
GitHub
3月 06, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add all_parameters api for Program (#22180), test=release/1.7 (#22894)
上级
1ccdaa4d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
72 addition
and
0 deletion
+72
-0
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+59
-0
python/paddle/fluid/tests/unittests/test_program.py
python/paddle/fluid/tests/unittests/test_program.py
+13
-0
未找到文件。
python/paddle/fluid/framework.py
浏览文件 @
e7985729
...
@@ -4524,6 +4524,65 @@ class Program(object):
...
@@ -4524,6 +4524,65 @@ class Program(object):
for
each_var
in
list
(
each_block
.
vars
.
values
()):
for
each_var
in
list
(
each_block
.
vars
.
values
()):
yield
each_var
yield
each_var
@
dygraph_not_support
def
all_parameters
(
self
):
"""
Get all :ref:`api_guide_parameter_en` from this Program. A list object is returned.
Returns:
list[ :ref:`api_guide_parameter_en` ]: The list contians all parameters in this program.
Examples:
.. code-block:: python
import paddle.fluid as fluid
program = fluid.default_main_program()
data = fluid.data(name='x', shape=[None, 13], dtype='float32')
hidden = fluid.layers.fc(input=data, size=10)
loss = fluid.layers.mean(hidden)
fluid.optimizer.SGD(learning_rate=0.01).minimize(loss)
for param in program.all_parameters():
print(param)
# Here will print all parameters in current program, in this example,
# the result is like:
#
# name: "fc_0.w_0"
# type {
# type: LOD_TENSOR
# lod_tensor {
# tensor {
# data_type: FP32
# dims: 13
# dims: 10
# }
# }
# }
# persistable: true
#
# name: "fc_0.b_0"
# type {
# type: LOD_TENSOR
# lod_tensor {
# tensor {
# data_type: FP32
# dims: 10
# }
# }
# }
# persistable: true
#
# Here print(param) will print out all the properties of a parameter,
# including name, type and persistable, you can access to specific
# property of a parameter, such as param.name, param.type
"""
parameters
=
[]
for
each_block
in
self
.
blocks
:
parameters
.
extend
(
each_block
.
all_parameters
())
return
parameters
@
six
.
add_metaclass
(
ParameterMetaClass
)
@
six
.
add_metaclass
(
ParameterMetaClass
)
class
Parameter
(
Variable
):
class
Parameter
(
Variable
):
...
...
python/paddle/fluid/tests/unittests/test_program.py
浏览文件 @
e7985729
...
@@ -132,6 +132,19 @@ class TestProgram(unittest.TestCase):
...
@@ -132,6 +132,19 @@ class TestProgram(unittest.TestCase):
for
i
in
range
(
len
(
no_read_ops
)):
for
i
in
range
(
len
(
no_read_ops
)):
self
.
assertEqual
(
no_read_ops
[
i
].
type
,
keep_read_ops
[
i
+
2
].
type
)
self
.
assertEqual
(
no_read_ops
[
i
].
type
,
keep_read_ops
[
i
+
2
].
type
)
def
test_program_all_parameters
(
self
):
program
=
fluid
.
default_main_program
()
data
=
fluid
.
data
(
name
=
'x'
,
shape
=
[
None
,
13
],
dtype
=
'float32'
)
hidden
=
fluid
.
layers
.
fc
(
input
=
data
,
size
=
10
)
loss
=
fluid
.
layers
.
mean
(
hidden
)
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.01
).
minimize
(
loss
)
# NOTE: here the parameters are fc_0.w_0 and fc_0.b_0
param_list
=
program
.
all_parameters
()
self
.
assertEqual
(
len
(
param_list
),
2
)
self
.
assertEqual
(
param_list
[
0
].
name
,
"fc_0.w_0"
)
self
.
assertEqual
(
param_list
[
1
].
name
,
"fc_0.b_0"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录