Skip to content

Commit c542d57

Browse files
authored
Modify paddle.static.nn.cond doc (PaddlePaddle#36694) (PaddlePaddle#36767)
Update `cond` English document
1 parent b080d98 commit c542d57

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

python/paddle/fluid/layers/control_flow.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,10 +2316,13 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
23162316
the same shape because of dataflow model of PaddlePaddle while the
23172317
tensors in the tuples or the lists can have different shapes.
23182318
2319-
2. Any tensors or operations created outside of ``true_fn`` and
2320-
``false_fn`` will be executed regardless of which branch is selected at
2321-
runtime. This has frequently surprised users who expected a lazy
2322-
semantics. For example:
2319+
2. This API could be used under both static mode or dygraph mode. If it
2320+
is in dygraph mode, the API only runs one branch based on condition.
2321+
2322+
3. If it is in static mode, any tensors or operations created outside
2323+
or inside of ``true_fn`` and ``false_fn`` will be in net building
2324+
regardless of which branch is selected at runtime. This has frequently
2325+
surprised users who expected a lazy semantics. For example:
23232326
23242327
.. code-block:: python
23252328
@@ -2328,9 +2331,11 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
23282331
a = paddle.zeros((1, 1))
23292332
b = paddle.zeros((1, 1))
23302333
c = a * b
2331-
out = paddle.nn.cond(a < b, lambda: a + c, lambda: b * b)
2334+
out = paddle.static.nn.cond(a < b, lambda: a + c, lambda: b * b)
23322335
2333-
No matter whether ``a < b`` , ``c = a * b`` will run.
2336+
No matter whether ``a < b`` , ``c = a * b`` will be in net building and
2337+
run. ``a + c`` and ``b * b`` will be in net building, but only one
2338+
branch will be executed during runtime.
23342339
23352340
Args:
23362341
pred(Tensor): A boolean tensor whose numel should be 1. The boolean
@@ -2366,24 +2371,24 @@ def cond(pred, true_fn=None, false_fn=None, name=None):
23662371
# return 3, 2
23672372
#
23682373
2369-
23702374
def true_func():
2371-
return paddle.fill_constant(shape=[1, 2], dtype='int32',
2372-
value=1), paddle.fill_constant(shape=[2, 3],
2373-
dtype='bool',
2374-
value=True)
2375+
return paddle.full(shape=[1, 2], dtype='int32',
2376+
fill_value=1), paddle.full(shape=[2, 3],
2377+
dtype='bool',
2378+
fill_value=True)
23752379
23762380
23772381
def false_func():
2378-
return paddle.fill_constant(shape=[3, 4], dtype='float32',
2379-
value=3), paddle.fill_constant(shape=[4, 5],
2380-
dtype='int64',
2381-
value=2)
2382+
return paddle.full(shape=[3, 4], dtype='float32',
2383+
fill_value=3), paddle.full(shape=[4, 5],
2384+
dtype='int64',
2385+
fill_value=2)
2386+
23822387
2383-
x = paddle.fill_constant(shape=[1], dtype='float32', value=0.1)
2384-
y = paddle.fill_constant(shape=[1], dtype='float32', value=0.23)
2388+
x = paddle.full(shape=[1], dtype='float32', fill_value=0.1)
2389+
y = paddle.full(shape=[1], dtype='float32', fill_value=0.23)
23852390
pred = paddle.less_than(x=x, y=y, name=None)
2386-
ret = paddle.nn.cond(pred, true_func, false_func)
2391+
ret = paddle.static.nn.cond(pred, true_func, false_func)
23872392
# ret is a tuple containing 2 tensors
23882393
# ret[0] = [[1 1]]
23892394
# ret[1] = [[ True True True]

0 commit comments

Comments
 (0)