python打印tensor_学习TensorFlow,打印输出tensor的值

http://blog.csdn.net/helei001/article/details/51750910

在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候。也许你会说,这个很容易啊,直接print就可以了。其实不然,print只能打印输出shape的信息,而要打印输出tensor的值,需要借助class tf.Session, class tf.InteractiveSession。因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。

一 class tf.Session

运行tensorflow操作的类,其对象封装了执行操作对象和评估tensor数值的环境。这个我们之前介绍过,在定义好所有的数据结构和操作后,其最后运行。

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))

二 class tf.InteractiveSession

顾名思义,用于交互上下文的session,便于输出tensor的数值。与上一个Session相比,其有默认的session执行相关操作,比如:Tensor.eval(), Operation.run()。Tensor.eval()是执行这个tensor之前的所有操作,Operation.run()也同理。

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())

Reference:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值