我一直在使用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]))

其他回答

你应该认为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]))

启用热切执行,这是在1.10版之后tensorflow中引入的。 它很容易使用。

# Initialize session
import tensorflow as tf
tf.enable_eager_execution()


# Some tensor we want to print the value of
a = tf.constant([1.0, 3.0])

print(a)

在Tensorflow V2中,使用:tf.keras.backend打印张量的值。print_tensor (x,消息= ")

特遣部队。Print现在已弃用,下面是如何使用tf。而是打印(小写p)。

虽然运行会话是一个很好的选择,但它并不总是正确的方法。例如,你可能想在一个特定的会话中打印一些张量。

新的print方法返回一个没有输出张量的打印操作:

print_op = tf.print(tensor_to_print)

由于它没有输出,所以不能像使用tf.Print那样将它插入图中。相反,您可以将它添加到会话中的控制依赖项中,以便打印它。

sess = tf.compat.v1.Session()
with sess.as_default():
  tensor_to_print = tf.range(10)
  print_op = tf.print(tensor_to_print)
with tf.control_dependencies([print_op]):
  tripled_tensor = tensor_to_print * 3
sess.run(tripled_tensor)

有时,在较大的图(可能部分是在子函数中创建的)中,将print_op传播到会话调用是很麻烦的。然后,特遣部队。Tuple可用于将打印操作与另一个操作耦合,然后无论哪个会话执行该代码,该操作都将与该操作一起运行。以下是如何做到的:

print_op = tf.print(tensor_to_print)
some_tensor_list = tf.tuple([some_tensor], control_inputs=[print_op])
# Use some_tensor_list[0] instead of any_tensor below.

请注意,tf.Print()将改变张量名称。 如果你想要打印的张量是一个占位符,那么向它输入数据将会失败,因为在输入过程中找不到原始的名称。 例如:

import tensorflow as tf
tens = tf.placeholder(tf.float32,[None,2],name="placeholder")
print(eval("tens"))
tens = tf.Print(tens,[tens, tf.shape(tens)],summarize=10,message="tens:")
print(eval("tens"))
res = tens + tens
sess = tf.Session()
sess.run(tf.global_variables_initializer())

print(sess.run(res))

输出是:

python test.py
Tensor("placeholder:0", shape=(?, 2), dtype=float32)
Tensor("Print:0", shape=(?, 2), dtype=float32)
Traceback (most recent call last):
[...]
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'placeholder' with dtype float