tf.Print

https://github.com/tensorflow/docs/tree/r1.4/site/en/api_docs/api_docs/python/tf
site/en/api_docs/api_docs/python/tf/Print.md

Print(
    input_,
    data,
    message=None,
    first_n=None,
    summarize=None,
    name=None
)

Defined in tensorflow/python/ops/logging_ops.py.
See the guide: Control Flow > Debugging Operations

最少需要两个输入,input_ 和 data。input 是需要打印的变量的名字,data 要求是一个 list,里面包含要打印的内容。

Prints a list of tensors.
输出 tensor 列表。

This is an identity op with the side effect of printing data when evaluating.

This op prints to the standard error. It is not currently compatible with jupyter notebook (printing to the notebook server’s output, not into the notebook).
此操作打印到 the standard error。它目前不兼容 jupyter notebook (printing to the notebook server’s output, not into the notebook)。

1. Args - 参数

  • input_: A tensor passed through this op. (需要通过 op 计算的张量。)
  • data: A list of tensors to print out when op is evaluated. (当 op 被计算时打印出来的张量的列表。)
  • message: A string, prefix of the error message. (一个字符串,错误消息的前缀。)
  • first_n: Only log first_n number of times. Negative numbers log always; this is the default. (只记录 first_n 次数,负数总是记录,这是默认值。)
  • summarize: Only print this many entries of each tensor. If None, then a maximum of 3 elements are printed per input tensor. (只打印每个张量的这么多条目。如果为 None,则每个输入张量最多打印 3 个元素。)
  • name: A name for the operation (optional). (操作的名称 (可选))

2. Returns - 返回

Same tensor as input_.
该操作将返回与 input_ 相同的张量。

tf.Print() 只是构建一个 op,需要 run 之后才会打印。

3. example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

x = tf.constant([1, 9, 8, 8])

with tf.Session() as sess:
    print(sess.run(x))
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
2019-07-25 21:36:27.363011: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-07-25 21:36:27.443865: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-07-25 21:36:27.444102: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.44GiB
2019-07-25 21:36:27.444112: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
[1 9 8 8]

Process finished with exit code 0

4. example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

x = tf.constant([1, 9, 8, 8])

x = tf.Print(x, [x, "cheng", x.shape, "yong", x, "qiang"], message='debug message:', summarize=100)

with tf.Session() as sess:
    sess.run(x)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
2019-07-25 21:40:25.502494: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-07-25 21:40:25.588103: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-07-25 21:40:25.588345: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.44GiB
2019-07-25 21:40:25.588355: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
2019-07-25 21:40:25.683213: I tensorflow/core/kernels/logging_ops.cc:79] debug message:[1 9 8 8][cheng][4][yong][1 9 8 8][qiang]

Process finished with exit code 0

5. example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

x = tf.constant([1, 9, 8, 8])

x = tf.Print(x, [x, "cheng", x.shape, "yong", x, "qiang"], message='debug message:')

with tf.Session() as sess:
    sess.run(x)
......
2019-07-25 21:43:42.134132: I tensorflow/core/kernels/logging_ops.cc:79] debug message:[1 9 8...][cheng][4][yong][1 9 8...][qiang]
......

6. example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

x = tf.constant([1, 9, 8, 8])

x = tf.Print(x, [x, "cheng", x.shape, "yong", x, "qiang"], message='debug message:', summarize=2)

with tf.Session() as sess:
    sess.run(x)
......
2019-07-25 21:45:33.959670: I tensorflow/core/kernels/logging_ops.cc:79] debug message:[1 9...][cheng][4][yong][1 9...][qiang]
......

7. example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

x = tf.constant([1, 9, 8, 8])

x = tf.Print(x, [x, "cheng", x.shape, "yong", x, "qiang"], message='debug message:', summarize=1)

with tf.Session() as sess:
    sess.run(x)
......
2019-07-25 21:46:26.221198: I tensorflow/core/kernels/logging_ops.cc:79] debug message:[1...][cheng][4][yong][1...][qiang]
......
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐