在Tensorflow 2.0+中(或在Eager模式环境中),您可以调用.numpy()
方法:
import tensorflow as tf
matrix1 = tf.constant([[3., 3.0]])
matrix2 = tf.constant([[2.0],[2.0]])
product = tf.matmul(matrix1, matrix2)
print(product.numpy())
1
在Tensorflow 2.0+中(或在Eager模式环境中),您可以调用.numpy()
方法:
import tensorflow as tf
matrix1 = tf.constant([[3., 3.0]])
matrix2 = tf.constant([[2.0],[2.0]])
product = tf.matmul(matrix1, matrix2)
print(product.numpy())
0
在最近的Tensorflow 1.13.1中
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.0中,默认情况下启用了eager模式。因此以下代码适用于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.]]
0
0
不,不运行图形就无法看到张量的内容(执行session.run()
)。您只能看到的是:
transpose_1:0
, random_uniform:0
) float32
) 我没有在文档中找到此信息,但我相信变量的值(以及某些常量在赋值时并未计算)。
看一下这个例子:
import tensorflow as tf
from datetime import datetime
dim = 7000
我刚刚启动随机数的恒定张量的第一个示例几乎在相同的时间运行,而不0:00:00.003261
变暗( 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
在第二种情况下,实际上是对常数求值并分配了值,时间显然取决于暗淡( 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_determinant(m1)
,请记住时间将以O(dim^2.8)
)
我发现的PS是在文档中解释的:
Tensor对象是操作结果的符号句柄,但实际上并不保存操作输出的值。
0
根据以上答案,您可以使用特定的代码段来打印产品,如下所示:
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()
0
重申其他人说的话,如果不运行图形就无法检查值。
想要寻找简单示例来打印值的任何人的简单代码段如下所示。该代码无需修改即可在ipython Notebook中执行
import tensorflow as tf
#define a variable to hold normal random values
normal_rv = tf.Variable( tf.truncated_normal([2,3],stddev = 0.1))
#initialize the variable
init_op = tf.initialize_all_variables()
#run the graph
with tf.Session() as sess:
sess.run(init_op) #execute init_op
#print the random values that we sample
print (sess.run(normal_rv))
输出:
[[-0.16702934 0.07173464 -0.04512421]
[-0.02265321 0.06509651 -0.01419079]]
0
tf.keras.backend.eval
对于评估小表达式很有用。
tf.keras.backend.eval(op)
TF 1.x和TF 2.0兼容。
最小的可验证示例
from tensorflow.keras.backend import eval
m1 = tf.constant([[3., 3.]])
m2 = tf.constant([[2.],[2.]])
eval(tf.matmul(m1, m2))
# array([[12.]], dtype=float32)
这很有用,因为您不必显式创建Session
或InteractiveSession
。
0
尽管其他答案是正确的,即您不能在评估图形之前就无法打印该值,但它们并没有讨论在评估图形后在图形内部实际打印值的一种简单方法。
每当评估图形(使用run
或eval
)时,查看张量值的最简单方法是使用Print
操作,如以下示例所示:
# Initialize session
import tensorflow as tf
sess = tf.InteractiveSession()
# Some tensor we want to print the value of
a = tf.constant([1.0, 3.0])
# Add print operation
a = tf.Print(a, [a], message="This is a: ")
# Add more elements of the graph using a
b = tf.add(a, a)
现在,无论何时我们评估整个图,例如使用b.eval()
,我们都会得到:
I tensorflow/core/kernels/logging_ops.cc:79] This is a: [1 3]
0
我认为您需要弄清一些基本知识。在上面的示例中,您已经创建了张量(多维数组)。但是要使张量流真正起作用,您必须启动一个“ 会话 ”并在该会话中运行“ 操作 ”。注意单词“会话”和“操作”。您需要了解4件事才能使用tensorflow:
现在,从您写的内容中,您已经给出了张量和操作,但是您没有会话在运行,也没有图。张量(图的边缘)流经图,并由操作(图的节点)操纵。有默认图形,但您可以在会话中启动图形。
说出print时,只能访问定义的变量或常量的形状。
这样您就可以看到丢失的内容:
with tf.Session() as sess:
print(sess.run(product))
print (product.eval())
希望能帮助到你!
0
最简单的[A]的方法来评估一个的实际值Tensor
目标是把它传递给Session.run()
方法,或致电Tensor.eval()
当你有一个默认的会话(即在with tf.Session():
屏蔽,或参见下文。通常[B] ,如果不在会话中运行某些代码,就无法打印张量的值。
如果您正在试验编程模型,并且想要一种简单的方法来评估张量,则tf.InteractiveSession
允许您在程序开始时打开一个会话,然后将该会话用于所有Tensor.eval()
(和Operation.run()
)调用。当在一个无处不在的环境中传递Session
对象时,在诸如外壳程序或IPython笔记本之类的交互式设置中,这可能会更容易。例如,以下内容在Jupyter笔记本中起作用:
with tf.Session() as sess: print(product.eval())
对于这么小的表达式来说,这似乎很愚蠢,但是Tensorflow中的关键思想之一是延迟执行 :构建一个大型而复杂的表达式非常便宜,并且当您要对其进行评估时,您可以将其后端(连接到该表达式)使用Session
)能够更有效地安排其执行(例如,并行执行独立的部分并使用GPU)。
[A]:要打印张量的值而不将其返回到Python程序,可以使用tf.Print()
运算符,如Andrzej在另一个答案中建议的那样 。请注意,您仍然需要运行部分图形以查看此op的输出,该输出已打印到标准输出中。如果您正在运行分布式TensorFlow,则tf.Print()
会将其输出打印到运行该op的任务的标准输出中。这意味着,例如,如果您使用https://colab.research.google.com或任何其他Jupyter Notebook,则您在笔记本中将看不到 tf.Print()
的输出;在这种情况下,请参考此答案以获取如何使其仍然打印。
[B]:您也许可以使用实验性的tf.contrib.util.constant_value()
函数来获取恒定张量的值,但它并非供一般使用,并且未为许多运算符定义。
0
我一直在使用TensorFlow中矩阵乘法的入门示例。
当我打印产品时,它显示为
Tensor
对象:但是我怎么知道
product
的价值呢?以下内容无济于事:
我知道图形可以在
Sessions
运行,但是没有任何方法可以在不运行session
情况下检查Tensor
对象的输出吗?