Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
91455804
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看板
未验证
提交
91455804
编写于
10月 13, 2020
作者:
H
Huihuang Zheng
提交者:
GitHub
10月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine ProgramTranslator API English Doc for 2.0rc (#27849)
As the title
上级
04be37c5
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
68 deletion
+65
-68
python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py
...dle/fluid/dygraph/dygraph_to_static/program_translator.py
+65
-68
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py
浏览文件 @
91455804
...
...
@@ -702,11 +702,11 @@ class ProgramTranslator(object):
Examples:
.. code-block:: python
import paddle.fluid as fluid
import paddle
# Two methods get same object because ProgramTranslator is a singleton
fluid.dygraph
.ProgramTranslator()
fluid.dygraph
.ProgramTranslator.get_instance()
# Two methods get same object because ProgramTranslator is a singleton
paddle.jit
.ProgramTranslator()
paddle.jit
.ProgramTranslator.get_instance()
"""
...
...
@@ -743,11 +743,11 @@ class ProgramTranslator(object):
def
enable
(
self
,
enable_to_static
):
"""
Enable or disable the converting from imperative to
declarative
by
Enable or disable the converting from imperative to
static graph
by
ProgramTranslator globally.
Args:
enable_to_static (bool): True or False to enable or disable
declarative
.
enable_to_static (bool): True or False to enable or disable
converting to static
.
Returns:
None.
...
...
@@ -755,25 +755,24 @@ class ProgramTranslator(object):
Examples:
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
import paddle
@fluid.dygraph.jit.declarative
def func(x):
x = fluid.dygraph.to_variable(x)
if fluid.layers.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph.ProgramTranslator()
prog_trans.enable(False)
@paddle.jit.to_static
def func(x):
if paddle.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
x = np.ones([1, 2])
# The declarative is disabled so the func is run in dygraph
with fluid.dygraph.guard():
print(func(x).numpy()) # [[2. 2.]]
prog_trans = paddle.jit.ProgramTranslator()
prog_trans.enable(False)
x = paddle.ones([1, 2])
# ProgramTranslator is disabled so the func is run in dygraph
print(func(x).numpy()) # [[0. 0.]]
"""
check_type
(
enable_to_static
,
"enable_to_static"
,
bool
,
...
...
@@ -782,38 +781,37 @@ class ProgramTranslator(object):
def
get_output
(
self
,
dygraph_func
,
*
args
,
**
kwargs
):
"""
Returns the output dygraph
VarBase
for dygraph function. The dygraph
Returns the output dygraph
Tensor
for dygraph function. The dygraph
function will be translated into static graph function so the under
beneath numerical result will be calculated by
declarative
mode.
beneath numerical result will be calculated by
static graph
mode.
Args:
dygraph_func (callable): the dygraph function.
*args, **kwargs : the input argument of dygraph_func.
*args (tuple): the input argument of dygraph_func.
**kwargs (dict): the input argument of dygraph_func.
Returns:
VarBase or tuple of VarBase: the dygraph VarBase containing digital
result.
Tensor or tuple of Tensors: the dygraph Tensor containing digital result.
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import numpy as np
import paddle
def func(x):
x = fluid.dygraph.to_variable(x)
if fluid.layers.mean(x) > 0:
if paddle.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph.ProgramTranslator()
with fluid.dygraph.guard():
x = np.ones([1, 2])
x_v = prog_trans.get_output(func, x)
print(x_v.numpy()) # [[0. 0.]]
prog_trans = paddle.jit.ProgramTranslator()
x = paddle.ones([1, 2])
x_v = prog_trans.get_output(func, x)
print(x_v.numpy()) # [[0. 0.]]
"""
assert
callable
(
...
...
@@ -875,19 +873,18 @@ class ProgramTranslator(object):
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import numpy as np
import paddle
def func(x):
x = fluid.dygraph.to_variable(x)
if fluid.layers.mean(x) > 0:
if paddle.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph.ProgramTranslator()
prog_trans = paddle.jit.ProgramTranslator()
static_func = prog_trans.get_func(func)
print(callable(static_func)) # True
...
...
@@ -908,43 +905,43 @@ class ProgramTranslator(object):
def
get_program
(
self
,
dygraph_func
,
*
args
,
**
kwargs
):
"""
Returns the translated static program and input/output
variable
s from
Returns the translated static program and input/output
Tensor
s from
dygraph function. The users can use the program to run by executor.
Args:
dygraph_func (callable): the dygraph function.
*args, **kwargs : the input argument of dygraph_func.
*args (tuple): the input argument of dygraph_func.
**kwargs (dict): the input argument of dygraph_func.
Returns:
tuple of (main_program, startup_program, inputs, outputs) whose
types are (Program, Program, list of
Variable, list of Variable
).
types are (Program, Program, list of
Tensors, list of Tensors
).
main_program: the converted main program.
startup_program: the converted startup program.
inputs: list of input
Variable
s which need to be fed.
outputs: list of output
Variable
s which users can fetch.
inputs: list of input
Tensor
s which need to be fed.
outputs: list of output
Tensor
s which users can fetch.
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import numpy as np
import paddle
def func(x):
x = fluid.dygraph.to_variable(x)
if fluid.layers.mean(x) > 0:
if paddle.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph.ProgramTranslator()
x = np.ones([1, 2])
prog_trans = paddle.jit.ProgramTranslator()
x = paddle.ones([1, 2])
main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x)
print([i.name for i in inputs])
# [
'feed_0'] the feed input variable
name representing x
# [
u'generated_tensor_0'] the feed input Tensor
name representing x
print([o.name for o in outputs])
# [
'_generated_var_4'] the fetch output variable
name representing x_v
# [
u'_generated_var_4'] the fetch output Tensor
name representing x_v
"""
assert
callable
(
...
...
@@ -993,21 +990,21 @@ class ProgramTranslator(object):
Examples:
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
import paddle
def func(x):
if paddle.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
def func(x):
x = fluid.dygraph.to_variable(x)
if fluid.layers.mean(x) > 0:
x_v = x - 1
else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph
.ProgramTranslator()
prog_trans = paddle.jit
.ProgramTranslator()
code = prog_trans.get_code(func)
print(type(code)) # <class 'str'>
code = prog_trans.get_code(func)
print(type(code)) # <class 'str'>
"""
assert
callable
(
...
...
@@ -1040,9 +1037,9 @@ class ProgramTranslator(object):
Examples:
.. code-block:: python
import paddle
.fluid as fluid
import paddle
prog_trans =
fluid.dygraph
.ProgramTranslator()
prog_trans =
paddle.jit
.ProgramTranslator()
prog_cache = prog_trans.get_program_cache()
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录