Asserts that the given condition is true. If `condition` evaluates to false, print the list of tensors in `data`. `summarize` determines how many entries of the tensors to print. NOTE: In graph mode, to ensure that Assert executes, one usually attaches a dependency: ```python # Ensure
(condition, data, summarize=None, name=None)
| 112 | @tf_export("debugging.Assert", "Assert") |
| 113 | @tf_should_use.should_use_result |
| 114 | def Assert(condition, data, summarize=None, name=None): |
| 115 | """Asserts that the given condition is true. |
| 116 | |
| 117 | If `condition` evaluates to false, print the list of tensors in `data`. |
| 118 | `summarize` determines how many entries of the tensors to print. |
| 119 | |
| 120 | NOTE: In graph mode, to ensure that Assert executes, one usually attaches |
| 121 | a dependency: |
| 122 | |
| 123 | ```python |
| 124 | # Ensure maximum element of x is smaller or equal to 1 |
| 125 | assert_op = tf.Assert(tf.less_equal(tf.reduce_max(x), 1.), [x]) |
| 126 | with tf.control_dependencies([assert_op]): |
| 127 | ... code using x ... |
| 128 | ``` |
| 129 | |
| 130 | Args: |
| 131 | condition: The condition to evaluate. |
| 132 | data: The tensors to print out when condition is false. |
| 133 | summarize: Print this many entries of each tensor. |
| 134 | name: A name for this operation (optional). |
| 135 | |
| 136 | Returns: |
| 137 | assert_op: An `Operation` that, when executed, raises a |
| 138 | `tf.errors.InvalidArgumentError` if `condition` is not true. |
| 139 | @compatibility(eager) |
| 140 | returns None |
| 141 | @end_compatibility |
| 142 | |
| 143 | Raises: |
| 144 | @compatibility(eager) |
| 145 | `tf.errors.InvalidArgumentError` if `condition` is not true |
| 146 | @end_compatibility |
| 147 | """ |
| 148 | if context.executing_eagerly(): |
| 149 | if not condition: |
| 150 | xs = ops.convert_n_to_tensor(data) |
| 151 | data_str = [_summarize_eager(x, summarize) for x in xs] |
| 152 | raise errors.InvalidArgumentError( |
| 153 | node_def=None, |
| 154 | op=None, |
| 155 | message="Expected '%s' to be true. Summarized data: %s" % |
| 156 | (condition, "\n".join(data_str))) |
| 157 | return |
| 158 | |
| 159 | with ops.name_scope(name, "Assert", [condition, data]) as name: |
| 160 | xs = ops.convert_n_to_tensor(data) |
| 161 | if all(x.dtype in {dtypes.string, dtypes.int32} for x in xs): |
| 162 | # As a simple heuristic, we assume that string and int32 are |
| 163 | # on host to avoid the need to use cond. If it is not case, |
| 164 | # we will pay the price copying the tensor to host memory. |
| 165 | return gen_logging_ops._assert(condition, data, summarize, name="Assert") |
| 166 | else: |
| 167 | condition = ops.convert_to_tensor(condition, name="Condition") |
| 168 | |
| 169 | def true_assert(): |
| 170 | return gen_logging_ops._assert( |
| 171 | condition, data, summarize, name="Assert") |
no test coverage detected