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.
Although this behavior is consistent with the dataflow model of TensorFlow,
it has frequently surprised users who expected a lazier semantics.
Consider the following simple program:
If x < y, the tf.add operation will be executed and tf.square
operation will not be executed. Since z is needed for at least one
branch of the cond, the tf.multiply operation is always executed,
unconditionally.
Note that cond calls true_fn and false_fnexactly once (inside the
call to cond, and not at all during Session.run()). cond
stitches together the graph fragments created during the true_fn and
false_fn calls with some additional graph nodes to ensure that the right
branch gets executed depending on the value of pred.
tf.cond supports nested structures as implemented in
tensorflow.python.util.nest. Both true_fn and false_fn must return the
same (possibly nested) value structure of lists, tuples, and/or named tuples.
Singleton lists and tuples form the only exceptions to this: when returned by
true_fn and/or false_fn, they are implicitly unpacked to single values.
This behavior is disabled by passing strict=True.
Args
pred
A scalar determining whether to return the result of true_fn or
false_fn.
true_fn
The callable to be performed if pred is true.
false_fn
The callable to be performed if pred is false.
strict
A boolean that enables/disables 'strict' mode; see above.
name
Optional name prefix for the returned tensors.
Returns
Tensors returned by the call to either true_fn or false_fn. If the
callables return a singleton list, the element is extracted from the list.
Raises
TypeError
if true_fn or false_fn is not callable.
ValueError
if true_fn and false_fn do not return the same number of
tensors, or return tensors of different types.
Example:
x=tf.constant(2)y=tf.constant(5)deff1():returntf.multiply(x,17)deff2():returntf.add(y,23)r=tf.cond(tf.less(x,y),f1,f2)# r is set to f1().# Operations in f2 (e.g., tf.add) are not executed.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.cond\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/cond.py#L39-L240) |\n\nReturn `true_fn()` if the predicate `pred` is true else `false_fn()`. (deprecated arguments) \n\n tf.compat.v1.cond(\n pred,\n true_fn=None,\n false_fn=None,\n strict=False,\n name=None,\n fn1=None,\n fn2=None\n )\n\n| **Deprecated:** SOME ARGUMENTS ARE DEPRECATED: `(fn1, fn2)`. They will be removed in a future version. Instructions for updating: fn1/fn2 are deprecated in favor of the true_fn/false_fn arguments.\n\n`true_fn` and `false_fn` both return lists of output tensors. `true_fn` and\n`false_fn` must have the same non-zero number and type of outputs.\n| **Warning:** Any Tensors or Operations created outside of `true_fn` and `false_fn` will be executed regardless of which branch is selected at runtime.\n\nAlthough this behavior is consistent with the dataflow model of TensorFlow,\nit has frequently surprised users who expected a lazier semantics.\nConsider the following simple program: \n\n z = tf.multiply(a, b)\n result = tf.cond(x \u003c y, lambda: tf.add(x, z), lambda: tf.square(y))\n\nIf `x \u003c y`, the `tf.add` operation will be executed and `tf.square`\noperation will not be executed. Since `z` is needed for at least one\nbranch of the `cond`, the [`tf.multiply`](../../../tf/math/multiply) operation is always executed,\nunconditionally.\n\nNote that `cond` calls `true_fn` and `false_fn` *exactly once* (inside the\ncall to `cond`, and not at all during `Session.run()`). `cond`\nstitches together the graph fragments created during the `true_fn` and\n`false_fn` calls with some additional graph nodes to ensure that the right\nbranch gets executed depending on the value of `pred`.\n\n[`tf.cond`](../../../tf/cond) supports nested structures as implemented in\n`tensorflow.python.util.nest`. Both `true_fn` and `false_fn` must return the\nsame (possibly nested) value structure of lists, tuples, and/or named tuples.\nSingleton lists and tuples form the only exceptions to this: when returned by\n`true_fn` and/or `false_fn`, they are implicitly unpacked to single values.\nThis behavior is disabled by passing `strict=True`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|-------------------------------------------------------------------------------|\n| `pred` | A scalar determining whether to return the result of `true_fn` or `false_fn`. |\n| `true_fn` | The callable to be performed if pred is true. |\n| `false_fn` | The callable to be performed if pred is false. |\n| `strict` | A boolean that enables/disables 'strict' mode; see above. |\n| `name` | Optional name prefix for the returned tensors. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Tensors returned by the call to either `true_fn` or `false_fn`. If the callables return a singleton list, the element is extracted from the list. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------------------------------------------------------|\n| `TypeError` | if `true_fn` or `false_fn` is not callable. |\n| `ValueError` | if `true_fn` and `false_fn` do not return the same number of tensors, or return tensors of different types. |\n\n\u003cbr /\u003e\n\n#### Example:\n\n x = tf.constant(2)\n y = tf.constant(5)\n def f1(): return tf.multiply(x, 17)\n def f2(): return tf.add(y, 23)\n r = tf.cond(tf.less(x, y), f1, f2)\n # r is set to f1().\n # Operations in f2 (e.g., tf.add) are not executed."]]