Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3b0d31ab
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
3b0d31ab
编写于
11月 11, 2020
作者:
H
Huihuang Zheng
提交者:
GitHub
11月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modify ProgramTranslator and TracedLayer Doc for API 2.0 (#28509)
Modify ProgramTranslator and TracedLayer Doc for API 2.0
上级
75196cda
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
41 addition
and
47 deletion
+41
-47
python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py
...dle/fluid/dygraph/dygraph_to_static/program_translator.py
+2
-2
python/paddle/fluid/dygraph/jit.py
python/paddle/fluid/dygraph/jit.py
+39
-45
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py
浏览文件 @
3b0d31ab
...
...
@@ -789,7 +789,7 @@ class ProgramTranslator(object):
x = paddle.ones([1, 2])
# ProgramTranslator is disabled so the func is run in dygraph
print(func(x)
.numpy()
) # [[0. 0.]]
print(func(x)) # [[0. 0.]]
"""
check_type
(
enable_to_static
,
"enable_to_static"
,
bool
,
...
...
@@ -828,7 +828,7 @@ class ProgramTranslator(object):
x = paddle.ones([1, 2])
x_v = prog_trans.get_output(func, x)
print(x_v
.numpy()
) # [[0. 0.]]
print(x_v) # [[0. 0.]]
"""
assert
callable
(
...
...
python/paddle/fluid/dygraph/jit.py
浏览文件 @
3b0d31ab
...
...
@@ -1051,7 +1051,7 @@ class TracedLayer(object):
model and convert it into a static graph model.
Args:
layer (
dygraph
.Layer): the layer object to be traced.
layer (
paddle.nn
.Layer): the layer object to be traced.
inputs (list(Tensor)|tuple(Tensor)|Tensor): the input tensors of
the layer object.
...
...
@@ -1063,23 +1063,20 @@ class TracedLayer(object):
Examples:
.. code-block:: python:
import paddle.fluid as fluid
from paddle.fluid.dygraph import Linear, to_variable, TracedLayer
import numpy as np
import paddle
class ExampleLayer(
fluid.dygraph
.Layer):
class ExampleLayer(
paddle.nn
.Layer):
def __init__(self):
super(ExampleLayer, self).__init__()
self._fc = Linear(3, 10)
self._fc =
paddle.nn.
Linear(3, 10)
def forward(self, input):
return self._fc(input)
with fluid.dygraph.guard():
layer = ExampleLayer()
in_np = np.random.random([2, 3]).astype('float32')
in_var = to_variable(in_np)
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var])
in_var = paddle.uniform(shape=[2, 3], dtype='float32')
out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
# run the static graph model using Executor inside
out_static_graph = static_layer([in_var])
...
...
@@ -1089,6 +1086,7 @@ class TracedLayer(object):
# save the static graph model for inference
static_layer.save_inference_model(dirname='./saved_infer_model')
"""
assert
isinstance
(
layer
,
Layer
...
...
@@ -1114,33 +1112,30 @@ class TracedLayer(object):
Examples:
.. code-block:: python:
import paddle.fluid as fluid
from paddle.fluid.dygraph import Linear, to_variable, TracedLayer
import numpy as np
import paddle
class ExampleLayer(
fluid.dygraph
.Layer):
class ExampleLayer(
paddle.nn
.Layer):
def __init__(self):
super(ExampleLayer, self).__init__()
self._fc = Linear(3, 10)
self._fc =
paddle.nn.
Linear(3, 10)
def forward(self, input):
return self._fc(input)
with fluid.dygraph.guard():
layer = ExampleLayer()
in_np = np.random.random([2, 3]).astype('float32')
in_var = to_variable(in_np)
in_var = paddle.uniform(shape=[2, 3], dtype='float32')
out_dygraph, static_layer =
TracedLayer.trace(layer, inputs=[in_var])
out_dygraph, static_layer = paddle.jit.
TracedLayer.trace(layer, inputs=[in_var])
build_strategy = fluid
.BuildStrategy()
build_strategy = paddle.static
.BuildStrategy()
build_strategy.enable_inplace = True
exec_strategy = fluid
.ExecutionStrategy()
exec_strategy = paddle.static
.ExecutionStrategy()
exec_strategy.num_threads = 2
static_layer.set_strategy(build_strategy=build_strategy, exec_strategy=exec_strategy)
out_static_graph = static_layer([in_var])
"""
assert
self
.
_compiled_program
is
None
,
"Cannot set strategy after run"
assert
isinstance
(
...
...
@@ -1212,30 +1207,29 @@ class TracedLayer(object):
Examples:
.. code-block:: python:
import paddle.fluid as fluid
from paddle.fluid.dygraph import Linear, to_variable, TracedLayer
import numpy as np
import paddle
class ExampleLayer(
fluid.dygraph
.Layer):
class ExampleLayer(
paddle.nn
.Layer):
def __init__(self):
super(ExampleLayer, self).__init__()
self._fc = Linear(3, 10)
self._fc =
paddle.nn.
Linear(3, 10)
def forward(self, input):
return self._fc(input)
save_dirname = './saved_infer_model'
in_np = np.random.random([2, 3]).astype('float32')
with fluid.dygraph.guard():
in_var = paddle.to_tensor(in_np)
layer = ExampleLayer()
in_var = to_variable(in_np)
out_dygraph, static_layer =
TracedLayer.trace(layer, inputs=[in_var])
out_dygraph, static_layer = paddle.jit.
TracedLayer.trace(layer, inputs=[in_var])
static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0])
place = fluid.CPUPlace()
exe = fluid.Executor(place)
program, feed_vars, fetch_vars = fluid.io.load_inference_model(save_dirname,
paddle.enable_static()
place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
program, feed_vars, fetch_vars = paddle.static.load_inference_model(save_dirname,
exe)
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录