Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e388e603
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看板
未验证
提交
e388e603
编写于
10月 12, 2020
作者:
H
Huihuang Zheng
提交者:
GitHub
10月 12, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine cond API English Doc for 2.0RC (#27708)
As the title
上级
2e1bca99
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
37 deletion
+26
-37
python/paddle/fluid/layers/control_flow.py
python/paddle/fluid/layers/control_flow.py
+26
-37
未找到文件。
python/paddle/fluid/layers/control_flow.py
浏览文件 @
e388e603
...
@@ -2297,11 +2297,6 @@ def copy_var_to_parent_block(var, layer_helper):
...
@@ -2297,11 +2297,6 @@ def copy_var_to_parent_block(var, layer_helper):
def
cond
(
pred
,
true_fn
=
None
,
false_fn
=
None
,
name
=
None
):
def
cond
(
pred
,
true_fn
=
None
,
false_fn
=
None
,
name
=
None
):
"""
"""
:api_attr: Static Graph
:alias_main: paddle.nn.cond
:alias: paddle.nn.cond,paddle.nn.control_flow.cond
:old_api: paddle.fluid.layers.cond
This API returns ``true_fn()`` if the predicate ``pred`` is true else
This API returns ``true_fn()`` if the predicate ``pred`` is true else
``false_fn()`` . Users could also set ``true_fn`` or ``false_fn`` to
``false_fn()`` . Users could also set ``true_fn`` or ``false_fn`` to
``None`` if do nothing and this API will treat the callable simply returns
``None`` if do nothing and this API will treat the callable simply returns
...
@@ -2323,17 +2318,18 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
...
@@ -2323,17 +2318,18 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
semantics. For example:
semantics. For example:
.. code-block:: python
.. code-block:: python
import paddle.fluid as fluid
import paddle
a = fluid.data(name='a', shape=[-1, 1], dtype='float32')
b = fluid.data(name='b', shape=[-1, 1], dtype='float32')
a = paddle.zeros((1, 1))
b = paddle.zeros((1, 1))
c = a * b
c = a * b
out =
fluid.layers
.cond(a < b, lambda: a + c, lambda: b * b)
out =
paddle.nn
.cond(a < b, lambda: a + c, lambda: b * b)
No matter whether ``a < b`` , ``c = a * b`` will run.
No matter whether ``a < b`` , ``c = a * b`` will run.
Args:
Args:
pred(
Variable
): A boolean tensor whose numel should be 1. The boolean
pred(
Tensor
): A boolean tensor whose numel should be 1. The boolean
value determines whether to return the result of ``true_fn`` or
value determines whether to return the result of ``true_fn`` or
``false_fn`` .
``false_fn`` .
true_fn(callable, optional): A callable to be performed if ``pred`` is
true_fn(callable, optional): A callable to be performed if ``pred`` is
...
@@ -2345,7 +2341,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
...
@@ -2345,7 +2341,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
refer to :ref:`api_guide_Name` .
refer to :ref:`api_guide_Name` .
Returns:
Returns:
Variable|list(Variable)|tuple(Variable
): returns ``true_fn()`` if the
Tensor|list(Tensor)|tuple(Tensor
): returns ``true_fn()`` if the
predicate ``pred`` is true else ``false_fn()`` .
predicate ``pred`` is true else ``false_fn()`` .
Raises:
Raises:
...
@@ -2356,10 +2352,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
...
@@ -2356,10 +2352,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle.fluid as fluid
import paddle
import paddle.fluid.layers as layers
from paddle.fluid.executor import Executor
from paddle.fluid.framework import Program, program_guard
#
#
# pseudocode:
# pseudocode:
...
@@ -2369,32 +2362,28 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
...
@@ -2369,32 +2362,28 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
# return 3, 2
# return 3, 2
#
#
def true_func():
def true_func():
return layers.fill_constant(
return paddle.fill_constant(shape=[1, 2], dtype='int32',
shape=[1, 2], dtype='int32', value=1), layers.fill_constant(
value=1), paddle.fill_constant(shape=[2, 3],
shape=[2, 3], dtype='bool', value=True)
dtype='bool',
value=True)
def false_func():
def false_func():
return layers.fill_constant(
return paddle.fill_constant(shape=[3, 4], dtype='float32',
shape=[3, 4], dtype='float32', value=3), layers.fill_constant(
value=3), paddle.fill_constant(shape=[4, 5],
shape=[4, 5], dtype='int64', value=2)
dtype='int64',
value=2)
main_program = Program()
startup_program = Program()
x = paddle.fill_constant(shape=[1], dtype='float32', value=0.1)
with program_guard(main_program, startup_program):
y = paddle.fill_constant(shape=[1], dtype='float32', value=0.23)
x = layers.fill_constant(shape=[1], dtype='float32', value=0.1)
pred = paddle.less_than(x=x, y=y, name=None)
y = layers.fill_constant(shape=[1], dtype='float32', value=0.23)
ret = paddle.nn.cond(pred, true_func, false_func)
pred = layers.less_than(x, y)
# ret is a tuple containing 2 tensors
out = layers.cond(pred, true_func, false_func)
# out is a tuple containing 2 tensors
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
ret = exe.run(main_program, fetch_list=out)
# ret[0] = [[1 1]]
# ret[0] = [[1 1]]
# ret[1] = [[ True True True]
# ret[1] = [[ True True True]
# [ True True True]]
# [ True True True]]
"""
"""
if
in_dygraph_mode
():
if
in_dygraph_mode
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录