Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e48091db
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
e48091db
编写于
5月 14, 2021
作者:
A
Aurelius84
提交者:
GitHub
5月 14, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Static]Add param_guard in ParameterList to support @to_static
上级
b035c8b0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
100 addition
and
2 deletion
+100
-2
python/paddle/fluid/dygraph/container.py
python/paddle/fluid/dygraph/container.py
+5
-2
python/paddle/fluid/tests/unittests/dygraph_to_static/test_param_guard.py
...uid/tests/unittests/dygraph_to_static/test_param_guard.py
+95
-0
未找到文件。
python/paddle/fluid/dygraph/container.py
浏览文件 @
e48091db
...
...
@@ -15,6 +15,7 @@
from
collections
import
OrderedDict
from
..framework
import
Parameter
from
.layers
import
Layer
from
.base
import
param_guard
__all__
=
[
'Sequential'
,
...
...
@@ -159,7 +160,8 @@ class ParameterList(Layer):
self
.
add_parameter
(
str
(
idx
),
param
)
def
__getitem__
(
self
,
idx
):
return
self
.
_parameters
[
str
(
idx
)]
with
param_guard
(
self
.
_parameters
):
return
self
.
_parameters
[
str
(
idx
)]
def
__setitem__
(
self
,
idx
,
param
):
assert
isinstance
(
param
,
Parameter
)
...
...
@@ -169,7 +171,8 @@ class ParameterList(Layer):
return
len
(
self
.
_parameters
)
def
__iter__
(
self
):
return
iter
(
self
.
_parameters
.
values
())
with
param_guard
(
self
.
_parameters
):
return
iter
(
self
.
_parameters
.
values
())
def
append
(
self
,
parameter
):
"""Appends a given parameter at the end of the list.
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_param_guard.py
0 → 100644
浏览文件 @
e48091db
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
paddle
import
numpy
as
np
import
unittest
from
paddle.jit
import
to_static
,
ProgramTranslator
class
NetWithParameterList
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
,
in_size
,
out_size
):
super
(
NetWithParameterList
,
self
).
__init__
()
weight
=
self
.
create_parameter
([
in_size
,
out_size
])
bias
=
self
.
create_parameter
([
out_size
],
is_bias
=
True
)
self
.
params
=
paddle
.
nn
.
ParameterList
([
weight
,
bias
])
@
to_static
def
forward
(
self
,
x
):
out
=
paddle
.
matmul
(
x
,
self
.
params
[
0
])
out
=
paddle
.
add
(
out
,
self
.
params
[
1
])
out
=
paddle
.
tanh
(
out
)
return
out
class
NetWithParameterListIter
(
NetWithParameterList
):
def
__init__
(
self
,
in_size
,
out_size
):
super
(
NetWithParameterListIter
,
self
).
__init__
(
in_size
,
out_size
)
@
to_static
def
forward
(
self
,
x
):
# NOTE: manually trigger `__iter__` logic.
params
=
list
(
self
.
params
.
__iter__
())
out
=
paddle
.
matmul
(
x
,
params
[
0
])
out
=
paddle
.
add
(
out
,
params
[
1
])
out
=
paddle
.
tanh
(
out
)
return
out
class
TestParameterList
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
seed
=
2021
self
.
iter_num
=
5
self
.
prog_trans
=
ProgramTranslator
()
def
train
(
self
,
is_iter
,
to_static
):
paddle
.
seed
(
self
.
seed
)
np
.
random
.
seed
(
self
.
seed
)
self
.
prog_trans
.
enable
(
to_static
)
if
is_iter
:
net
=
NetWithParameterList
(
10
,
3
)
else
:
net
=
NetWithParameterListIter
(
10
,
3
)
sgd
=
paddle
.
optimizer
.
SGD
(
0.1
,
parameters
=
net
.
parameters
())
for
batch_id
in
range
(
self
.
iter_num
):
x
=
paddle
.
rand
([
4
,
10
],
dtype
=
'float32'
)
out
=
net
(
x
)
loss
=
paddle
.
mean
(
out
)
loss
.
backward
()
sgd
.
step
()
sgd
.
clear_grad
()
return
loss
def
test_parameter_list
(
self
):
static_loss
=
self
.
train
(
False
,
to_static
=
True
)
dygraph_loss
=
self
.
train
(
False
,
to_static
=
False
)
self
.
assertTrue
(
np
.
allclose
(
dygraph_loss
,
static_loss
),
msg
=
'dygraph result is {}
\n
static result is {}'
.
format
(
dygraph_loss
,
static_loss
))
def
test_parameter_list_iter
(
self
):
static_loss
=
self
.
train
(
True
,
to_static
=
True
)
dygraph_loss
=
self
.
train
(
True
,
to_static
=
False
)
self
.
assertTrue
(
np
.
allclose
(
dygraph_loss
,
static_loss
),
msg
=
'dygraph result is {}
\n
static result is {}'
.
format
(
dygraph_loss
,
static_loss
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录