我一直在使用TensorFlow中矩阵乘法的介绍性示例。

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

当我打印乘积时,它显示为一个张量对象:

<tensorflow.python.framework.ops.Tensor object at 0x10470fcd0>

但是我怎么知道产品的价值呢?

下面的方法不起作用:

print product
Tensor("MatMul:0", shape=TensorShape([Dimension(1), Dimension(1)]), dtype=float32)

我知道图在会话上运行,但是没有任何方法可以检查张量对象的输出而不在会话中运行图吗?


当前回答

你应该认为TensorFlow核心程序由两个独立的部分组成:

构建计算图。 运行计算图。

因此,对于下面的代码,您只需构建计算图。

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

为了初始化TensorFlow程序中的所有变量,你必须显式调用一个特殊操作,如下所示:

init = tf.global_variables_initializer()

现在您构建了图并初始化了所有变量,下一步是计算节点,您必须在会话中运行计算图。会话封装了TensorFlow运行时的控件和状态。

下面的代码创建一个Session对象,然后调用它的run方法来运行足够的计算图来计算product:

sess = tf.Session()
// run variables initializer
sess.run(init)

print(sess.run([product]))

其他回答

不,你不能在不运行图(执行session.run())的情况下看到张量的内容。你能看到的只有:

张量的维数(但我假设对TF的操作列表计算它并不难) 用于生成张量(transpose_1:0, random_uniform:0)的操作类型。 张量中元素的类型(float32)

我没有在文档中找到这一点,但我相信变量的值(和一些常数在赋值时没有计算)。


看看这个例子:

import tensorflow as tf
from datetime import datetime
dim = 7000

第一个例子中,我刚刚启动了一个常数的随机数张量运行的时间几乎是相同的不管dim (0:00:00.003261)

startTime = datetime.now()
m1 = tf.truncated_normal([dim, dim], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
print datetime.now() - startTime

在第二种情况中,实际计算了常数并分配了值,时间显然取决于dim (0:00:01.244642)

startTime = datetime.now()
m1 = tf.truncated_normal([dim, dim], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
sess = tf.Session()
sess.run(m1)
print datetime.now() - startTime

你可以通过计算一些东西来让它更清楚(d = tf. matrix_行列式(m1),记住时间将以O(dim^2.8)为单位运行)

附注:我在文档中找到了解释:

张量对象是运算结果的符号句柄, 但是实际上并不保存操作输出的值。

我认为你需要掌握一些基本知识。通过上面的例子,你已经创建了张量(多维数组)。但是要让张量流真正工作,你必须启动一个“会话”,并在会话中运行你的“操作”。注意单词“session”和“operation”。 要使用张量流,你需要知道4件事:

张量 操作 会话 图

现在,从你写出来的东西中,你已经给出了张量和操作,但你没有运行会话,也没有图。张量(图的边)在图中流动,并由操作(图的节点)操作。有默认的图形,但你可以在会话中初始化你的。

当你说打印时,你只访问你定义的变量或常数的形状。

所以你可以看到你错过了什么:

 with tf.Session() as sess:     
           print(sess.run(product))
           print (product.eval())

希望能有所帮助!

你应该认为TensorFlow核心程序由两个独立的部分组成:

构建计算图。 运行计算图。

因此,对于下面的代码,您只需构建计算图。

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

为了初始化TensorFlow程序中的所有变量,你必须显式调用一个特殊操作,如下所示:

init = tf.global_variables_initializer()

现在您构建了图并初始化了所有变量,下一步是计算节点,您必须在会话中运行计算图。会话封装了TensorFlow运行时的控件和状态。

下面的代码创建一个Session对象,然后调用它的run方法来运行足够的计算图来计算product:

sess = tf.Session()
// run variables initializer
sess.run(init)

print(sess.run([product]))

基于上面的答案,使用特定的代码片段,您可以像这样打印产品:

import tensorflow as tf
#Initialize the session
sess = tf.InteractiveSession()

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

#print the product
print(product.eval())

#close the session to release resources
sess.close()

在Tensorflow 1.x中

import tensorflow as tf
tf.enable_eager_execution()
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

#print the product
print(product)         # tf.Tensor([[12.]], shape=(1, 1), dtype=float32)
print(product.numpy()) # [[12.]]

用Tensorflow 2。X,默认开启急切模式。因此下面的代码与TF2.0一起工作。

import tensorflow as tf
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

#print the product
print(product)         # tf.Tensor([[12.]], shape=(1, 1), dtype=float32)
print(product.numpy()) # [[12.]]