Set how abnormal conditions are handled by the default eager context. Example: ```python tfe.seterr(inf_or_nan=ExecutionCallback.RAISE) a = tf.constant(10.0) b = tf.constant(0.0) try: c = a / b # <-- Raises InfOrNanError. except Exception as e: print("Caught Exception: %s" %
(inf_or_nan=None)
| 290 | |
| 291 | |
| 292 | def seterr(inf_or_nan=None): |
| 293 | """Set how abnormal conditions are handled by the default eager context. |
| 294 | |
| 295 | Example: |
| 296 | ```python |
| 297 | tfe.seterr(inf_or_nan=ExecutionCallback.RAISE) |
| 298 | a = tf.constant(10.0) |
| 299 | b = tf.constant(0.0) |
| 300 | try: |
| 301 | c = a / b # <-- Raises InfOrNanError. |
| 302 | except Exception as e: |
| 303 | print("Caught Exception: %s" % e) |
| 304 | |
| 305 | tfe.seterr(inf_or_nan=ExecutionCallback.IGNORE) |
| 306 | c = a / b # <-- Does NOT raise exception anymore. |
| 307 | ``` |
| 308 | |
| 309 | Args: |
| 310 | inf_or_nan: An `ExecutionCallback` determining the action for infinity |
| 311 | (`inf`) and NaN (`nan`) values. A value of `None` leads to no change in |
| 312 | the action of the condition. |
| 313 | |
| 314 | Returns: |
| 315 | A dictionary of old actions. |
| 316 | |
| 317 | Raises: |
| 318 | ValueError: If the value of any keyword arguments is invalid. |
| 319 | """ |
| 320 | inf_or_nan = ExecutionCallback(inf_or_nan) if inf_or_nan is not None else None |
| 321 | old_settings = {"inf_or_nan": ExecutionCallback.IGNORE} |
| 322 | default_context = context.context() |
| 323 | |
| 324 | carryover_callbacks = [] |
| 325 | for callback in default_context.post_execution_callbacks: |
| 326 | # Check whether the callback is inf_nan_callback or a partial object of |
| 327 | # inf_nan_callback. |
| 328 | if (callback == inf_nan_callback or |
| 329 | isinstance(callback, functools.partial) and |
| 330 | callback.func == inf_nan_callback): |
| 331 | if callback == inf_nan_callback: |
| 332 | old_settings["inf_or_nan"] = _DEFAULT_CALLBACK_ACTION |
| 333 | else: |
| 334 | old_settings["inf_or_nan"] = callback.keywords.get( |
| 335 | "action", _DEFAULT_CALLBACK_ACTION) |
| 336 | elif inf_or_nan is not None: |
| 337 | carryover_callbacks.append(callback) |
| 338 | |
| 339 | if inf_or_nan is not None: |
| 340 | default_context.clear_post_execution_callbacks() |
| 341 | for callback in carryover_callbacks: |
| 342 | default_context.add_post_execution_callback(callback) |
| 343 | if inf_or_nan != ExecutionCallback.IGNORE: |
| 344 | default_context.add_post_execution_callback( |
| 345 | functools.partial(inf_nan_callback, action=inf_or_nan)) |
| 346 | |
| 347 | return old_settings |
| 348 | |
| 349 |
no test coverage detected