Create a case operation. See also `tf.switch_case`. The `pred_fn_pairs` parameter is a list of pairs of size N. Each pair contains a boolean scalar tensor and a python callable that creates the tensors to be returned if the boolean evaluates to True. `default` is a callable generating a
(pred_fn_pairs,
default=None,
exclusive=False,
strict=False,
name="case")
| 3280 | |
| 3281 | @tf_export("case", v1=[]) |
| 3282 | def case_v2(pred_fn_pairs, |
| 3283 | default=None, |
| 3284 | exclusive=False, |
| 3285 | strict=False, |
| 3286 | name="case"): |
| 3287 | """Create a case operation. |
| 3288 | |
| 3289 | See also `tf.switch_case`. |
| 3290 | |
| 3291 | The `pred_fn_pairs` parameter is a list of pairs of size N. |
| 3292 | Each pair contains a boolean scalar tensor and a python callable that |
| 3293 | creates the tensors to be returned if the boolean evaluates to True. |
| 3294 | `default` is a callable generating a list of tensors. All the callables |
| 3295 | in `pred_fn_pairs` as well as `default` (if provided) should return the same |
| 3296 | number and types of tensors. |
| 3297 | |
| 3298 | If `exclusive==True`, all predicates are evaluated, and an exception is |
| 3299 | thrown if more than one of the predicates evaluates to `True`. |
| 3300 | If `exclusive==False`, execution stops at the first predicate which |
| 3301 | evaluates to True, and the tensors generated by the corresponding function |
| 3302 | are returned immediately. If none of the predicates evaluate to True, this |
| 3303 | operation returns the tensors generated by `default`. |
| 3304 | |
| 3305 | `tf.case` supports nested structures as implemented in |
| 3306 | `tf.contrib.framework.nest`. All of the callables must return the same |
| 3307 | (possibly nested) value structure of lists, tuples, and/or named tuples. |
| 3308 | Singleton lists and tuples form the only exceptions to this: when returned by |
| 3309 | a callable, they are implicitly unpacked to single values. This |
| 3310 | behavior is disabled by passing `strict=True`. |
| 3311 | |
| 3312 | @compatibility(v2) |
| 3313 | `pred_fn_pairs` could be a dictionary in v1. However, tf.Tensor and |
| 3314 | tf.Variable are no longer hashable in v2, so cannot be used as a key for a |
| 3315 | dictionary. Please use a list or a tuple instead. |
| 3316 | @end_compatibility |
| 3317 | |
| 3318 | |
| 3319 | **Example 1:** |
| 3320 | |
| 3321 | Pseudocode: |
| 3322 | |
| 3323 | ``` |
| 3324 | if (x < y) return 17; |
| 3325 | else return 23; |
| 3326 | ``` |
| 3327 | |
| 3328 | Expressions: |
| 3329 | |
| 3330 | ```python |
| 3331 | f1 = lambda: tf.constant(17) |
| 3332 | f2 = lambda: tf.constant(23) |
| 3333 | r = tf.case([(tf.less(x, y), f1)], default=f2) |
| 3334 | ``` |
| 3335 | |
| 3336 | **Example 2:** |
| 3337 | |
| 3338 | Pseudocode: |
| 3339 |
nothing calls this directly
no test coverage detected