cond¶
- api_attr
declarative programming (static graph)
-
paddle.fluid.layers.
cond
(pred, true_fn=None, false_fn=None, name=None)[source] This API returns
true_fn()
if the predicatepred
is true elsefalse_fn()
. Users could also settrue_fn
orfalse_fn
toNone
if do nothing and this API will treat the callable simply returnsNone
in this case.true_fn
andfalse_fn
should return same nest structure of tensors or both returnNone
if user doens’t like to return anything. A nest structure of tensors in PaddlePaddle is tensor(s), or tuple of tensors, or list of tensors.Note
1. The tuples or lists returned by
true_fn
andfalse_fn
must have the same shape because of dataflow model of PaddlePaddle while the tensors in the tuples or the lists can have different shapes.2. Any tensors or operations created outside of
true_fn
andfalse_fn
will be executed regardless of which branch is selected at runtime. This has frequently surprised users who expected a lazy semantics. For example: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') c = a * b out = fluid.layers.cond(a < b, lambda: a + c, lambda: b * b)
No matter whether
a < b
,c = a * b
will run.- Parameters
pred (Variable) – A boolean tensor whose numel should be 1. The boolean value determines whether to return the result of
true_fn
orfalse_fn
.true_fn (callable, optional) – A callable to be performed if
pred
is true. The default value isNone
.false_fn (callable, optional) – A callable to be performed if
pred
is false. The default value isNone
.name (str, optional) – The default value is
None
. Normally users don’t have to set this parameter. For more information, please refer to Name .
- Returns
returns
true_fn()
if the predicatepred
is true elsefalse_fn()
.- Return type
Variable|list(Variable)|tuple(Variable)
- Raises
TypeError
– iftrue_fn
orfalse_fn
is not callable.ValueError
– iftrue_fn
andfalse_fn
don’t return the same nest structure of tensors.
Examples
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 # # pseudocode: # if 0.1 < 0.23: # return 1, True # else: # 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) 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) # ret[0] = [[1 1]] # ret[1] = [[ True True True] # [ True True True]]