未验证 提交 3b0d31ab 编写于 作者: H Huihuang Zheng 提交者: GitHub

Modify ProgramTranslator and TracedLayer Doc for API 2.0 (#28509)

Modify ProgramTranslator and TracedLayer Doc for API 2.0
上级 75196cda
......@@ -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(
......
......@@ -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,32 +1063,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)
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var])
layer = ExampleLayer()
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])
# run the static graph model using Executor inside
out_static_graph = static_layer([in_var])
print(len(out_static_graph)) # 1
print(out_static_graph[0].shape) # (2, 10)
print(len(out_static_graph)) # 1
print(out_static_graph[0].shape) # (2, 10)
# save the static graph model for inference
static_layer.save_inference_model(dirname='./saved_infer_model')
# 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)
layer = ExampleLayer()
in_var = paddle.uniform(shape=[2, 3], dtype='float32')
out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var])
build_strategy = paddle.static.BuildStrategy()
build_strategy.enable_inplace = True
build_strategy = fluid.BuildStrategy()
build_strategy.enable_inplace = True
exec_strategy = paddle.static.ExecutionStrategy()
exec_strategy.num_threads = 2
exec_strategy = fluid.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])
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')
in_var = paddle.to_tensor(in_np)
layer = ExampleLayer()
with fluid.dygraph.guard():
layer = ExampleLayer()
in_var = to_variable(in_np)
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var])
static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0])
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.
先完成此消息的编辑!
想要评论请 注册