tf.debugging.enable_check_numerics
Stay organized with collections
Save and categorize content based on your preferences.
Enable tensor numerics checking in an eager/graph unified fashion.
tf.debugging.enable_check_numerics(
stack_height_limit=30, path_length_limit=50
)
The numerics checking mechanism will cause any TensorFlow eager execution or
graph execution to error out as soon as an op's output tensor contains
infinity or NaN.
This method is idempotent. Calling it multiple times has the same effect
as calling it once.
This method takes effect only on the thread in which it is called.
When a op's float-type output tensor contains any Infinity or NaN, an
tf.errors.InvalidArgumentError
will be thrown, with an error message that
reveals the following information:
- The type of the op that generated the tensor with bad numerics.
- Data type (dtype) of the tensor.
- Shape of the tensor (to the extent known at the time of eager execution
or graph construction).
- Name of the containing graph (if available).
- (Graph mode only): The stack trace of the intra-graph op's creation,
with a stack-height limit and a path-length limit for visual clarity.
The stack frames that belong to the user's code (as opposed to
tensorflow's internal code) are highlighted with a text arrow ("->").
- (Eager mode only): How many of the offending tensor's elements are
Infinity
and NaN
, respectively.
Once enabled, the check-numerics mechanism can be disabled by using
tf.debugging.disable_check_numerics()
.
Example usage:
Catching infinity during the execution of a tf.function
graph:
import tensorflow as tf
tf.debugging.enable_check_numerics()
@tf.function
def square_log_x_plus_1(x):
v = tf.math.log(x + 1)
return tf.math.square(v)
x = -1.0
# When the following line runs, a function graph will be compiled
# from the Python function `square_log_x_plus_1()`. Due to the
# `enable_check_numerics()` call above, the graph will contain
# numerics checking ops that will run during the function graph's
# execution. The function call generates an -infinity when the Log
# (logarithm) op operates on the output tensor of the Add op.
# The program errors out at this line, printing an error message.
y = square_log_x_plus_1(x)
z = -y
Catching NaN during eager execution:
import numpy as np
import tensorflow as tf
tf.debugging.enable_check_numerics()
x = np.array([[0.0, -1.0], [4.0, 3.0]])
# The following line executes the Sqrt op eagerly. Due to the negative
# element in the input array, a NaN is generated. Due to the
# `enable_check_numerics()` call above, the program errors immediately
# at this line, printing an error message.
y = tf.math.sqrt(x)
z = tf.matmul(y, y)
tf.config.set_soft_device_placement(True)
tf.debugging.enable_check_numerics()
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')
strategy = tf.distribute.TPUStrategy(resolver)
with strategy.scope():
# ...
Args |
stack_height_limit
|
Limit to the height of the printed stack trace.
Applicable only to ops in tf.function s (graphs).
|
path_length_limit
|
Limit to the file path included in the printed stack
trace. Applicable only to ops in tf.function s (graphs).
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.debugging.enable_check_numerics\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/debug/lib/check_numerics_callback.py#L336-L442) |\n\nEnable tensor numerics checking in an eager/graph unified fashion.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.debugging.enable_check_numerics`](https://www.tensorflow.org/api_docs/python/tf/debugging/enable_check_numerics)\n\n\u003cbr /\u003e\n\n tf.debugging.enable_check_numerics(\n stack_height_limit=30, path_length_limit=50\n )\n\nThe numerics checking mechanism will cause any TensorFlow eager execution or\ngraph execution to error out as soon as an op's output tensor contains\ninfinity or NaN.\n\nThis method is idempotent. Calling it multiple times has the same effect\nas calling it once.\n\nThis method takes effect only on the thread in which it is called.\n\nWhen a op's float-type output tensor contains any Infinity or NaN, an\n[`tf.errors.InvalidArgumentError`](../../tf/errors/InvalidArgumentError) will be thrown, with an error message that\nreveals the following information:\n\n- The type of the op that generated the tensor with bad numerics.\n- Data type (dtype) of the tensor.\n- Shape of the tensor (to the extent known at the time of eager execution or graph construction).\n- Name of the containing graph (if available).\n- (Graph mode only): The stack trace of the intra-graph op's creation, with a stack-height limit and a path-length limit for visual clarity. The stack frames that belong to the user's code (as opposed to tensorflow's internal code) are highlighted with a text arrow (\"-\\\u003e\").\n- (Eager mode only): How many of the offending tensor's elements are `Infinity` and `NaN`, respectively.\n\nOnce enabled, the check-numerics mechanism can be disabled by using\n[`tf.debugging.disable_check_numerics()`](../../tf/debugging/disable_check_numerics).\n\n#### Example usage:\n\n1. Catching infinity during the execution of a [`tf.function`](../../tf/function) graph:\n\n import tensorflow as tf\n\n tf.debugging.enable_check_numerics()\n\n @tf.function\n def square_log_x_plus_1(x):\n v = tf.math.log(x + 1)\n return tf.math.square(v)\n\n x = -1.0\n\n # When the following line runs, a function graph will be compiled\n # from the Python function `square_log_x_plus_1()`. Due to the\n # `enable_check_numerics()` call above, the graph will contain\n # numerics checking ops that will run during the function graph's\n # execution. The function call generates an -infinity when the Log\n # (logarithm) op operates on the output tensor of the Add op.\n # The program errors out at this line, printing an error message.\n y = square_log_x_plus_1(x)\n z = -y\n\n2. Catching NaN during eager execution:\n\n import numpy as np\n import tensorflow as tf\n\n tf.debugging.enable_check_numerics()\n\n x = np.array([[0.0, -1.0], [4.0, 3.0]])\n\n # The following line executes the Sqrt op eagerly. Due to the negative\n # element in the input array, a NaN is generated. Due to the\n # `enable_check_numerics()` call above, the program errors immediately\n # at this line, printing an error message.\n y = tf.math.sqrt(x)\n z = tf.matmul(y, y)\n\n**Note:** If your code is running on TPUs, be sure to call [`tf.config.set_soft_device_placement(True)`](../../tf/config/set_soft_device_placement) before calling [`tf.debugging.enable_check_numerics()`](../../tf/debugging/enable_check_numerics) as this API uses automatic outside compilation on TPUs. For example: \n\n tf.config.set_soft_device_placement(True)\n tf.debugging.enable_check_numerics()\n\n resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')\n strategy = tf.distribute.TPUStrategy(resolver)\n with strategy.scope():\n # ...\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|\n| `stack_height_limit` | Limit to the height of the printed stack trace. Applicable only to ops in [`tf.function`](../../tf/function)s (graphs). |\n| `path_length_limit` | Limit to the file path included in the printed stack trace. Applicable only to ops in [`tf.function`](../../tf/function)s (graphs). |\n\n\u003cbr /\u003e"]]