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,
strict=False,
name=None,
fn1=None,
fn2=None)
| 1097 | None, "fn1/fn2 are deprecated in favor of the true_fn/false_fn arguments.", |
| 1098 | "fn1", "fn2") |
| 1099 | def cond(pred, |
| 1100 | true_fn=None, |
| 1101 | false_fn=None, |
| 1102 | strict=False, |
| 1103 | name=None, |
| 1104 | fn1=None, |
| 1105 | fn2=None): |
| 1106 | """Return `true_fn()` if the predicate `pred` is true else `false_fn()`. |
| 1107 | |
| 1108 | `true_fn` and `false_fn` both return lists of output tensors. `true_fn` and |
| 1109 | `false_fn` must have the same non-zero number and type of outputs. |
| 1110 | |
| 1111 | **WARNING**: Any Tensors or Operations created outside of `true_fn` and |
| 1112 | `false_fn` will be executed regardless of which branch is selected at runtime. |
| 1113 | |
| 1114 | Although this behavior is consistent with the dataflow model of TensorFlow, |
| 1115 | it has frequently surprised users who expected a lazier semantics. |
| 1116 | Consider the following simple program: |
| 1117 | |
| 1118 | ```python |
| 1119 | z = tf.multiply(a, b) |
| 1120 | result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y)) |
| 1121 | ``` |
| 1122 | |
| 1123 | If `x < y`, the `tf.add` operation will be executed and `tf.square` |
| 1124 | operation will not be executed. Since `z` is needed for at least one |
| 1125 | branch of the `cond`, the `tf.multiply` operation is always executed, |
| 1126 | unconditionally. |
| 1127 | |
| 1128 | Note that `cond` calls `true_fn` and `false_fn` *exactly once* (inside the |
| 1129 | call to `cond`, and not at all during `Session.run()`). `cond` |
| 1130 | stitches together the graph fragments created during the `true_fn` and |
| 1131 | `false_fn` calls with some additional graph nodes to ensure that the right |
| 1132 | branch gets executed depending on the value of `pred`. |
| 1133 | |
| 1134 | `tf.cond` supports nested structures as implemented in |
| 1135 | `tensorflow.python.util.nest`. Both `true_fn` and `false_fn` must return the |
| 1136 | same (possibly nested) value structure of lists, tuples, and/or named tuples. |
| 1137 | Singleton lists and tuples form the only exceptions to this: when returned by |
| 1138 | `true_fn` and/or `false_fn`, they are implicitly unpacked to single values. |
| 1139 | This behavior is disabled by passing `strict=True`. |
| 1140 | |
| 1141 | Args: |
| 1142 | pred: A scalar determining whether to return the result of `true_fn` or |
| 1143 | `false_fn`. |
| 1144 | true_fn: The callable to be performed if pred is true. |
| 1145 | false_fn: The callable to be performed if pred is false. |
| 1146 | strict: A boolean that enables/disables 'strict' mode; see above. |
| 1147 | name: Optional name prefix for the returned tensors. |
| 1148 | |
| 1149 | Returns: |
| 1150 | Tensors returned by the call to either `true_fn` or `false_fn`. If the |
| 1151 | callables return a singleton list, the element is extracted from the list. |
| 1152 | |
| 1153 | Raises: |
| 1154 | TypeError: if `true_fn` or `false_fn` is not callable. |
| 1155 | ValueError: if `true_fn` and `false_fn` do not return the same number of |
| 1156 | tensors, or return tensors of different types. |
no test coverage detected