Python 如何在tensorflow mnist_softmax.py中打印张量值
我刚刚尝试在TensorFlow 0.8中运行Python 如何在tensorflow mnist_softmax.py中打印张量值,python,tensorflow,Python,Tensorflow,我刚刚尝试在TensorFlow 0.8中运行mnist_softmax.py。 我想在模型测试步骤之前观察y和y的值 代码如下: print(y) # added by me print(y_) # added by me # Test trained model correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 已完成的代码可在上获得 以下是输出: Tensor("Softmax:0", shape=(?
mnist_softmax.py
。
我想在模型测试步骤之前观察y
和y
的值
代码如下:
print(y) # added by me
print(y_) # added by me
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
已完成的代码可在上获得
以下是输出:
Tensor("Softmax:0", shape=(?, 10), dtype=float32)
Tensor("Placeholder_1:0", shape=(?, 10), dtype=float32)
我也尝试过使用sess.run(y)
,和y.eval()
,但我尝试时遇到了如下错误:
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'Placeholder', defined at: ...
TL;DR:无论是
y
张量还是y
张量都依赖于运算,因此它们要求您在计算时输入一个输入值。您可以通过如下方式输入一批输入数据,在一批输入数据上打印softmax的输出:
batch_xs, _ = mnist.train.next_batch(100)
print(y.eval({x: batch_xs}))
MNIST示例包含以下行:
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
请注意,您试图打印的第一个张量,y
,是x
的函数,它被定义为tf.placeholder()
。tf.placeholder()
op是一种为计算定义符号的方法:它本身没有任何值,而是说x
必须是一个包含784
列的浮点值矩阵
运行/计算y
而不为x
提供值,就像编写以下Python函数并调用它而不使用其所有参数一样:
def y(x):
W = ...
b = ...
return softmax(matmul(x, W), b)
# This would fail with an error.
print(y())
如何为参数指定一个值?在TensorFlow中,您可以通过为占位符输入一个值来实现这一点(如和中所示):
张量
y
被定义为tf.placeholder()
。出于技术原因,即使为占位符提供了值,也不能直接计算占位符。然而,这样做并不是特别有用!相反,您可以只打印本应输入的值。TL;DR:无论是y
张量还是y
张量都依赖于运算,因此它们要求您在计算时输入一个输入值。您可以通过如下方式输入一批输入数据,在一批输入数据上打印softmax的输出:
batch_xs, _ = mnist.train.next_batch(100)
print(y.eval({x: batch_xs}))
MNIST示例包含以下行:
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
请注意,您试图打印的第一个张量,y
,是x
的函数,它被定义为tf.placeholder()
。tf.placeholder()
op是一种为计算定义符号的方法:它本身没有任何值,而是说x
必须是一个包含784
列的浮点值矩阵
运行/计算y
而不为x
提供值,就像编写以下Python函数并调用它而不使用其所有参数一样:
def y(x):
W = ...
b = ...
return softmax(matmul(x, W), b)
# This would fail with an error.
print(y())
如何为参数指定一个值?在TensorFlow中,您可以通过为占位符输入一个值来实现这一点(如和中所示):
张量
y
被定义为tf.placeholder()
。出于技术原因,即使为占位符提供了值,也不能直接计算占位符。然而,这样做并不是特别有用!相反,您可以只打印您要输入的值。签出:签出:谢谢,mrry。这个问题困扰了我一整天。你的解释比tensorflow.org清楚得多。谢谢你,mrry。这个问题困扰了我一整天。你的解释比tensorflow.org更清楚。