查看tensor的值

2019-04-30  本文已影响0人  D_Major

参考 https://www.cnblogs.com/huangshiyu13/p/6721805.html

1. tf.Print()

参数:
input_: 通过此op的一个tensor.
data: 当此op被计算之后打印输出的tensor list。
message: 错误消息的前缀,是一个string。
first_n: 只记录first_n次. 总是记录负数;这是个缺省.
summarize: 对每个tensor只打印的条目数量。如果是None,对于每个输入tensor只打印3个元素。
name: op的名字.

返回值:
和input_相同的tensor.
使用方法: sess.run(tf.Print(train_logits,[train_logits]))

2. 使用Session

import tensorflow as tf
# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor `c`.
print(sess.run(c))

该方法效果一般

import tensorflow as tf
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.Session():
  # We can also use 'c.eval()' here.
  print(c.eval())

优点:简单易行
缺点:必须要在图中保持需要观察的变量,必须要进行一次完整的运行

比如我们要获取各个层的输出,会变得很繁琐, 所以使用end_points[]字典保存各层输出: 或是将模型新建成一个类:

3. TensorBoard查看变量

tf.summary.histogram('gxs', gxs) 添加直方图
image = tf.identity(image, 'input_image') 在统计图查看

4. tf.py_func()


Tout要指定返回类型, 即[tf.bool], 输入inp要使用列表输入


将函数转换为op后可以添加进统计图中

with tf.control_dependencies(debug_print_op):
  out = tf.identity(out, name='out')
return out
上一篇 下一篇

猜你喜欢

热点阅读