Return the bool value for `pred`, or None if `pred` had a dynamic value. Arguments: pred: A scalar, either a Python bool or tensor. Returns: True or False if `pred` has a constant boolean value, None otherwise. Raises: TypeError: If `pred` is not a Tensor or bool.
(pred)
| 60 | |
| 61 | |
| 62 | def smart_constant_value(pred): |
| 63 | """Return the bool value for `pred`, or None if `pred` had a dynamic value. |
| 64 | |
| 65 | Arguments: |
| 66 | pred: A scalar, either a Python bool or tensor. |
| 67 | |
| 68 | Returns: |
| 69 | True or False if `pred` has a constant boolean value, None otherwise. |
| 70 | |
| 71 | Raises: |
| 72 | TypeError: If `pred` is not a Tensor or bool. |
| 73 | """ |
| 74 | if isinstance(pred, ops.Tensor): |
| 75 | pred_value = tensor_util.constant_value(pred) |
| 76 | # TODO(skyewm): consider folding this into tensor_util.constant_value. |
| 77 | # pylint: disable=protected-access |
| 78 | if pred_value is None: |
| 79 | pred_value = c_api.TF_TryEvaluateConstant_wrapper(pred.graph._c_graph, |
| 80 | pred._as_tf_output()) |
| 81 | # pylint: enable=protected-access |
| 82 | elif pred in {0, 1}: # Accept 1/0 as valid boolean values |
| 83 | pred_value = bool(pred) |
| 84 | elif isinstance(pred, bool): |
| 85 | pred_value = pred |
| 86 | else: |
| 87 | raise TypeError("`pred` must be a Tensor, or a Python bool, or 1 or 0. " |
| 88 | "Found instead: %s" % type(pred)) |
| 89 | |
| 90 | return pred_value |
| 91 | |
| 92 | |
| 93 | def smart_case(pred_fn_pairs, default=None, exclusive=False, name="smart_case"): |
no test coverage detected