tf.debugging.enable_check_numerics  |  TensorFlow v2.16.1 (original) (raw)

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.

View aliases

Compat aliases for migration

SeeMigration guide for more details.

tf.compat.v1.debugging.enable_check_numerics

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, antf.errors.InvalidArgumentError will be thrown, with an error message that reveals the following information:

Once enabled, the check-numerics mechanism can be disabled by usingtf.debugging.disable_check_numerics().

Example usage:

  1. 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  
  1. 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.functions (graphs).
path_length_limit Limit to the file path included in the printed stack trace. Applicable only to ops in tf.functions (graphs).