A TensorFlow operator that prints the specified inputs to a desired
output stream or logging level. The inputs may be dense or sparse Tensors,
primitive python objects, data structures that contain tensors, and printable
Python objects. Printed tensors will recursively show the first and last
elements of each dimension to summarize.
In graphs manually created outside of tf.function, this method returns
the created TF operator that prints the data. To make sure the
operator runs, users need to pass the produced op to
tf.compat.v1.Session's run method, or to use the op as a control
dependency for executed ops by specifying
with tf.compat.v1.control_dependencies([print_op]).
tf.compat.v1.disable_v2_behavior()# for TF1 compatibility onlysess=tf.compat.v1.Session()withsess.as_default():tensor=tf.range(10)print_op=tf.print("tensors:",tensor,{2:tensor*2},output_stream=sys.stdout)withtf.control_dependencies([print_op]):tripled_tensor=tensor*3sess.run(tripled_tensor)
(This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
sys.stdout)
Args
*inputs
Positional arguments that are the inputs to print. Inputs in the
printed output will be separated by spaces. Inputs may be python
primitives, tensors, data structures such as dicts and lists that may
contain tensors (with the data structures possibly nested in arbitrary
ways), and printable python objects.
output_stream
The output stream, logging level, or file to print to.
Defaults to sys.stderr, but sys.stdout, tf.compat.v1.logging.info,
tf.compat.v1.logging.warning, tf.compat.v1.logging.error,
absl.logging.info, absl.logging.warning and absl.logging.error are also
supported. To print to a file, pass a string started with "file://"
followed by the file path, e.g., "file:///tmp/foo.out".
summarize
The first and last summarize elements within each dimension are
recursively printed per Tensor. If None, then the first 3 and last 3
elements of each dimension are printed for each tensor. If set to -1, it
will print all elements of every tensor.
sep
The string to use to separate the inputs. Defaults to " ".
end
End character that is appended at the end the printed string. Defaults
to the newline character.
name
A name for the operation (optional).
Returns
None when executing eagerly. During graph tracing this returns
a TF operator that prints the specified inputs in the specified output
stream or logging level. This operator will be automatically executed
except inside of tf.compat.v1 graphs and sessions.
[[["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.print\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/logging_ops.py#L147-L395) |\n\nPrint the specified inputs.\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.print`](https://www.tensorflow.org/api_docs/python/tf/print)\n\n\u003cbr /\u003e\n\n tf.print(\n *inputs, **kwargs\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|\n| - [Better performance with tf.function](https://www.tensorflow.org/guide/function) - [Better performance with the tf.data API](https://www.tensorflow.org/guide/data_performance) - [Debug a TensorFlow 2 migrated training pipeline](https://www.tensorflow.org/guide/migrate/migration_debugging) | - [Distributed Input](https://www.tensorflow.org/tutorials/distribute/input) |\n\nA TensorFlow operator that prints the specified inputs to a desired\noutput stream or logging level. The inputs may be dense or sparse Tensors,\nprimitive python objects, data structures that contain tensors, and printable\nPython objects. Printed tensors will recursively show the first and last\nelements of each dimension to summarize.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Example ------- ||\n|---|---|\n| Single-input usage: \u003cbr /\u003e tensor = tf.range(10) tf.print(tensor, output_stream=sys.stderr) (This prints \"\\[0 1 2 ... 7 8 9\\]\" to sys.stderr) Multi-input usage: tensor = tf.range(10) tf.print(\"tensors:\", tensor, {2: tensor * 2}, output_stream=sys.stdout) (This prints \"tensors: \\[0 1 2 ... 7 8 9\\] {2: \\[0 2 4 ... 14 16 18\\]}\" to sys.stdout) Changing the input separator: tensor_a = tf.range(2) tensor_b = tensor_a * 2 tf.print(tensor_a, tensor_b, output_stream=sys.stderr, sep=',') (This prints \"\\[0 1\\],\\[0 2\\]\" to sys.stderr) Usage in a [`tf.function`](../tf/function): @tf.function def f(): tensor = tf.range(10) tf.print(tensor, output_stream=sys.stderr) return tensor range_tensor = f() (This prints \"\\[0 1 2 ... 7 8 9\\]\" to sys.stderr) ||\n\n\u003cbr /\u003e\n\n*Compatibility usage in TF 1.x graphs*:\n\nIn graphs manually created outside of [`tf.function`](../tf/function), this method returns\nthe created TF operator that prints the data. To make sure the\noperator runs, users need to pass the produced op to\n[`tf.compat.v1.Session`](../tf/compat/v1/Session)'s run method, or to use the op as a control\ndependency for executed ops by specifying\n`with tf.compat.v1.control_dependencies([print_op])`. \n\n tf.compat.v1.disable_v2_behavior() # for TF1 compatibility only\n\n sess = tf.compat.v1.Session()\n with sess.as_default():\n tensor = tf.range(10)\n print_op = tf.print(\"tensors:\", tensor, {2: tensor * 2},\n output_stream=sys.stdout)\n with tf.control_dependencies([print_op]):\n tripled_tensor = tensor * 3\n\n sess.run(tripled_tensor)\n\n(This prints \"tensors: \\[0 1 2 ... 7 8 9\\] {2: \\[0 2 4 ... 14 16 18\\]}\" to\nsys.stdout)\n| **Note:** In Jupyter notebooks and colabs, [`tf.print`](../tf/print) prints to the notebook cell outputs. It will not write to the notebook kernel's console logs.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `*inputs` | Positional arguments that are the inputs to print. Inputs in the printed output will be separated by spaces. Inputs may be python primitives, tensors, data structures such as dicts and lists that may contain tensors (with the data structures possibly nested in arbitrary ways), and printable python objects. |\n| `output_stream` | The output stream, logging level, or file to print to. Defaults to sys.stderr, but sys.stdout, tf.compat.v1.logging.info, tf.compat.v1.logging.warning, tf.compat.v1.logging.error, absl.logging.info, absl.logging.warning and absl.logging.error are also supported. To print to a file, pass a string started with \"file://\" followed by the file path, e.g., \"file:///tmp/foo.out\". |\n| `summarize` | The first and last `summarize` elements within each dimension are recursively printed per Tensor. If None, then the first 3 and last 3 elements of each dimension are printed for each tensor. If set to -1, it will print all elements of every tensor. |\n| `sep` | The string to use to separate the inputs. Defaults to \" \". |\n| `end` | End character that is appended at the end the printed string. Defaults to the newline character. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| None when executing eagerly. During graph tracing this returns a TF operator that prints the specified inputs in the specified output stream or logging level. This operator will be automatically executed except inside of [`tf.compat.v1`](../tf/compat/v1) graphs and sessions. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-----------------------------------------------|\n| `ValueError` | If an unsupported output stream is specified. |\n\n\u003cbr /\u003e"]]