Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine cond API English Doc for 2.0RC #27708

Merged
merged 5 commits into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 26 additions & 37 deletions python/paddle/fluid/layers/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2297,11 +2297,6 @@ def copy_var_to_parent_block(var, layer_helper):

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
``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
Expand All @@ -2323,17 +2318,18 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
semantics. For example:

.. code-block:: python

import paddle.fluid as fluid
a = fluid.data(name='a', shape=[-1, 1], dtype='float32')
b = fluid.data(name='b', shape=[-1, 1], dtype='float32')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2300~2303有关alias的内容都可以删掉了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

import paddle

a = paddle.zeros((1, 1))
b = paddle.zeros((1, 1))
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.

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
``false_fn`` .
true_fn(callable, optional): A callable to be performed if ``pred`` is
Expand All @@ -2345,7 +2341,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
refer to :ref:`api_guide_Name` .

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()`` .

Raises:
Expand All @@ -2356,10 +2352,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
Examples:
.. code-block:: python

import paddle.fluid as fluid
import paddle.fluid.layers as layers
from paddle.fluid.executor import Executor
from paddle.fluid.framework import Program, program_guard
import paddle

#
# pseudocode:
Expand All @@ -2369,32 +2362,28 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
# return 3, 2
#


def true_func():
return layers.fill_constant(
shape=[1, 2], dtype='int32', value=1), layers.fill_constant(
shape=[2, 3], dtype='bool', value=True)
return paddle.fill_constant(shape=[1, 2], dtype='int32',
value=1), paddle.fill_constant(shape=[2, 3],
dtype='bool',
value=True)


def false_func():
return layers.fill_constant(
shape=[3, 4], dtype='float32', value=3), layers.fill_constant(
shape=[4, 5], dtype='int64', value=2)

main_program = Program()
startup_program = Program()
with program_guard(main_program, startup_program):
x = layers.fill_constant(shape=[1], dtype='float32', value=0.1)
y = layers.fill_constant(shape=[1], dtype='float32', value=0.23)
pred = layers.less_than(x, y)
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)
return paddle.fill_constant(shape=[3, 4], dtype='float32',
value=3), paddle.fill_constant(shape=[4, 5],
dtype='int64',
value=2)

x = paddle.fill_constant(shape=[1], dtype='float32', value=0.1)
y = paddle.fill_constant(shape=[1], dtype='float32', value=0.23)
pred = paddle.less_than(x=x, y=y, name=None)
ret = paddle.nn.cond(pred, true_func, false_func)
# ret is a tuple containing 2 tensors
# ret[0] = [[1 1]]
# ret[1] = [[ True True True]
# [ True True True]]
# [ True True True]]

"""
if in_dygraph_mode():
Expand Down