原文地址:
https://blog.csdn.net/weixin_36670529/article/details/100191674
----------------------------------------------------------------------------------------------
调试程序的时候,经常会需要检查中间的参数,这些参数一般是定义在model或是别的函数中的局部参数,由于tensorflow要求先构建计算图再运算的机制,也不能定义后直接print出来。tensorflow有一个函数tf.Print()。
tf.Print(input, data, message=None, first_n=None, summarize=None, name=None)
最低要求两个输入,input和data,input是需要打印的变量的名字,data要求是一个list,里面包含要打印的内容。
参数:
- message是需要输出的错误信息
- first_n指只记录(打log日志)前n次
- summarize是对每个tensor只打印的条目数量,如果是None,对于每个输入tensor只打印3个元素
- name是op的名字
需要注意的是tf.Print()只是构建一个op,需要run之后才会打印。
例子:
x=tf.constant([2,3,4,5]) y=tf.Print(x,[x,x.shape,'test', x],message='Debug message:',summarize=100) with tf.Session() as sess: sess.run(y) #Debug message:[2 3 4 5][4][test][2 3 4 5] z=tf.Print(x,[x,x.shape,'test', x],message='Debug message:',summarize=2) with tf.Session() as sess: sess.run(z) #Debug message:[2 3...][4][test][2 3...]
输出是在命令窗口中,和print有区别
x=tf.constant([2,3,4,5]) with tf.Session() as sess: print(sess.run(x)) #[2,3,4,5]
————————————————
版权声明:本文为CSDN博主「于小勇」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_36670529/article/details/100191674
附:
Print(input_, data, message=None, first_n=None, summarize=None, name=None)
Prints a list of tensors.
This is an identity op with the side effect of printing `data` when
evaluating.
Args:
input_: A tensor passed through this op.
data: A list of tensors to print out when op is evaluated.
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.
summarize: Only print this many entries of each tensor. If None, then a
maximum of 3 elements are printed per input tensor.
name: A name for the operation (optional).
Returns:
Same tensor as `input_`.