importtensorflowastfimportpdbtf.data.experimental.enable_debug_mode()deffunc(x):# Python 3.7 and older requires `pdb.Pdb(nosigint=True).set_trace()`pdb.set_trace()x=x+1returnxdataset=tf.data.Dataset.from_tensor_slices([1,2,3])dataset=dataset.map(func)foritemindataset:print(item)
The effect of debug mode is two-fold:
1) Any transformations that would introduce asynchrony, parallelism, or
non-determinism to the input pipeline execution will be forced to execute
synchronously, sequentially, and deterministically.
2) Any user-defined functions passed into tf.data transformations such as
map will be wrapped in tf.py_function so that their body is executed
"eagerly" as a Python function as opposed to a traced TensorFlow graph, which
is the default behavior. Note that even when debug mode is enabled, the
user-defined function is still traced to infer the shape and type of its
outputs; as a consequence, any print statements or breakpoints will be
triggered once during the tracing before the actual execution of the input
pipeline.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.data.experimental.enable_debug_mode\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/data/ops/debug_mode.py#L24-L72) |\n\nEnables debug mode for tf.data.\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.data.experimental.enable_debug_mode`](https://www.tensorflow.org/api_docs/python/tf/data/experimental/enable_debug_mode)\n\n\u003cbr /\u003e\n\n tf.data.experimental.enable_debug_mode()\n\nExample usage with pdb module: \n\n import tensorflow as tf\n import pdb\n\n tf.data.experimental.enable_debug_mode()\n\n def func(x):\n # Python 3.7 and older requires `pdb.Pdb(nosigint=True).set_trace()`\n pdb.set_trace()\n x = x + 1\n return x\n\n dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])\n dataset = dataset.map(func)\n\n for item in dataset:\n print(item)\n\nThe effect of debug mode is two-fold:\n\n1) Any transformations that would introduce asynchrony, parallelism, or\nnon-determinism to the input pipeline execution will be forced to execute\nsynchronously, sequentially, and deterministically.\n\n2) Any user-defined functions passed into tf.data transformations such as\n`map` will be wrapped in [`tf.py_function`](../../../tf/py_function) so that their body is executed\n\"eagerly\" as a Python function as opposed to a traced TensorFlow graph, which\nis the default behavior. Note that even when debug mode is enabled, the\nuser-defined function is still traced to infer the shape and type of its\noutputs; as a consequence, any `print` statements or breakpoints will be\ntriggered once during the tracing before the actual execution of the input\npipeline.\n| **Note:** As the debug mode setting affects the construction of the tf.data input pipeline, it should be enabled before any tf.data definitions.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------|\n| `ValueError` | When invoked from graph mode. |\n\n\u003cbr /\u003e"]]