Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7a3a05cc
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看板
未验证
提交
7a3a05cc
编写于
10月 26, 2020
作者:
A
Aurelius84
提交者:
GitHub
10月 26, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Stat]Support to save model with nested output (#28224)
上级
4671d85a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
42 addition
and
1 deletion
+42
-1
python/paddle/fluid/dygraph/jit.py
python/paddle/fluid/dygraph/jit.py
+2
-1
python/paddle/fluid/tests/unittests/test_jit_save_load.py
python/paddle/fluid/tests/unittests/test_jit_save_load.py
+40
-0
未找到文件。
python/paddle/fluid/dygraph/jit.py
浏览文件 @
7a3a05cc
...
...
@@ -25,6 +25,7 @@ import paddle
from
paddle.fluid
import
core
from
paddle.fluid.compiler
import
BuildStrategy
,
CompiledProgram
,
ExecutionStrategy
from
paddle.fluid.data_feeder
import
check_type
from
paddle.fluid.layers.utils
import
flatten
from
paddle.fluid.dygraph.base
import
program_desc_tracing_guard
,
switch_to_static_graph
from
paddle.fluid.dygraph.dygraph_to_static
import
logging_utils
from
paddle.fluid.dygraph.dygraph_to_static.logging_utils
import
set_code_level
,
set_verbosity
...
...
@@ -397,7 +398,7 @@ def _get_output_vars(outputs, output_spec):
"Layer.forward method."
result_list
=
[]
output_vars_dict
=
OrderedDict
()
for
var
in
outputs
:
for
var
in
flatten
(
outputs
)
:
if
isinstance
(
var
,
Variable
):
output_vars_dict
[
var
.
name
]
=
var
if
output_spec
is
None
:
...
...
python/paddle/fluid/tests/unittests/test_jit_save_load.py
浏览文件 @
7a3a05cc
...
...
@@ -21,6 +21,7 @@ import numpy as np
import
paddle
from
paddle.static
import
InputSpec
import
paddle.fluid
as
fluid
from
paddle.fluid.layers.utils
import
flatten
from
paddle.fluid.dygraph
import
Linear
from
paddle.fluid.dygraph
import
declarative
,
ProgramTranslator
from
paddle.fluid.dygraph.io
import
INFER_MODEL_SUFFIX
,
INFER_PARAMS_SUFFIX
,
INFER_PARAMS_INFO_SUFFIX
...
...
@@ -153,6 +154,21 @@ class LinearNetReturnHidden(fluid.dygraph.Layer):
return
y
,
loss
class
LinearNetWithNestOut
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
in_size
,
out_size
):
super
(
LinearNetWithNestOut
,
self
).
__init__
()
self
.
_linear_1
=
Linear
(
in_size
,
out_size
)
self
.
_linear_2
=
Linear
(
in_size
,
out_size
)
@
declarative
def
forward
(
self
,
x
):
y
=
self
.
_linear_1
(
x
)
z
=
self
.
_linear_2
(
y
)
out
=
y
+
z
loss
=
fluid
.
layers
.
mean
(
out
)
return
y
,
[(
z
,
loss
),
out
]
class
EmptyLayer
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
):
super
(
EmptyLayer
,
self
).
__init__
()
...
...
@@ -299,6 +315,30 @@ class TestJitSaveLoad(unittest.TestCase):
loaded_layer
=
paddle
.
jit
.
load
(
path
)
class
TestSaveLoadWithNestOut
(
unittest
.
TestCase
):
def
setUp
(
self
):
# enable dygraph mode
fluid
.
enable_dygraph
()
def
test_nest_output
(
self
):
x
=
fluid
.
dygraph
.
to_variable
(
np
.
random
.
random
((
4
,
8
)).
astype
(
'float32'
))
net
=
LinearNetWithNestOut
(
8
,
8
)
dy_outs
=
flatten
(
net
(
x
))
net
=
declarative
(
net
,
input_spec
=
[
InputSpec
([
None
,
8
],
name
=
'x'
)])
model_path
=
"net_with_nest_out/model"
paddle
.
jit
.
save
(
net
,
model_path
)
load_net
=
paddle
.
jit
.
load
(
model_path
)
load_outs
=
flatten
(
load_net
(
x
))
self
.
assertTrue
(
len
(
dy_outs
)
==
4
)
for
dy_out
,
load_out
in
zip
(
dy_outs
,
load_outs
):
self
.
assertTrue
(
np
.
allclose
(
dy_out
.
numpy
(),
load_out
.
numpy
()))
class
TestSaveLoadWithInputSpec
(
unittest
.
TestCase
):
def
setUp
(
self
):
# enable dygraph mode
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录