MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / cond_for_tf_v2

Function cond_for_tf_v2

tensorflow/python/ops/control_flow_ops.py:1319–1389  ·  view source on GitHub ↗

Return `true_fn()` if the predicate `pred` is true else `false_fn()`. `true_fn` and `false_fn` both return lists of output tensors. `true_fn` and `false_fn` must have the same non-zero number and type of outputs. **WARNING**: Any Tensors or Operations created outside of `true_fn` and `fals

(pred, true_fn=None, false_fn=None, name=None)

Source from the content-addressed store, hash-verified

1317
1318@tf_export("cond", v1=[])
1319def cond_for_tf_v2(pred, true_fn=None, false_fn=None, name=None):
1320 """Return `true_fn()` if the predicate `pred` is true else `false_fn()`.
1321
1322 `true_fn` and `false_fn` both return lists of output tensors. `true_fn` and
1323 `false_fn` must have the same non-zero number and type of outputs.
1324
1325 **WARNING**: Any Tensors or Operations created outside of `true_fn` and
1326 `false_fn` will be executed regardless of which branch is selected at runtime.
1327
1328 Although this behavior is consistent with the dataflow model of TensorFlow,
1329 it has frequently surprised users who expected a lazier semantics.
1330 Consider the following simple program:
1331
1332 ```python
1333 z = tf.multiply(a, b)
1334 result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))
1335 ```
1336
1337 If `x < y`, the `tf.add` operation will be executed and `tf.square`
1338 operation will not be executed. Since `z` is needed for at least one
1339 branch of the `cond`, the `tf.multiply` operation is always executed,
1340 unconditionally.
1341
1342 Note that `cond` calls `true_fn` and `false_fn` *exactly once* (inside the
1343 call to `cond`, and not at all during `Session.run()`). `cond`
1344 stitches together the graph fragments created during the `true_fn` and
1345 `false_fn` calls with some additional graph nodes to ensure that the right
1346 branch gets executed depending on the value of `pred`.
1347
1348 `tf.cond` supports nested structures as implemented in
1349 `tensorflow.python.util.nest`. Both `true_fn` and `false_fn` must return the
1350 same (possibly nested) value structure of lists, tuples, and/or named tuples.
1351 Singleton lists and tuples form the only exceptions to this: when returned by
1352 `true_fn` and/or `false_fn`, they are implicitly unpacked to single values.
1353
1354 Note: It is illegal to "directly" use tensors created inside a cond branch
1355 outside it, e.g. by storing a reference to a branch tensor in the python
1356 state. If you need to use a tensor created in a branch function you should
1357 return it as an output of the branch function and use the output from
1358 `tf.cond` instead.
1359
1360 Args:
1361 pred: A scalar determining whether to return the result of `true_fn` or
1362 `false_fn`.
1363 true_fn: The callable to be performed if pred is true.
1364 false_fn: The callable to be performed if pred is false.
1365 name: Optional name prefix for the returned tensors.
1366
1367 Returns:
1368 Tensors returned by the call to either `true_fn` or `false_fn`. If the
1369 callables return a singleton list, the element is extracted from the list.
1370
1371 Raises:
1372 TypeError: if `true_fn` or `false_fn` is not callable.
1373 ValueError: if `true_fn` and `false_fn` do not return the same number of
1374 tensors, or return tensors of different types.
1375
1376 Example:

Callers

nothing calls this directly

Calls 1

condFunction · 0.70

Tested by

no test coverage detected