如何在 tensorflow 中打印张量的值 mnist_softmax.py
How to print the value of a tensor in tensorflow mnist_softmax.py
我刚刚尝试在 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))
完整代码可在 GitHub 上获得。
下面是输出:
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_
张量都依赖于 tf.placeholder()
操作,因此它们需要您 feed 当你评估它们时输入一个值。您可以像这样输入一批输入,在一批输入数据上打印 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 是一种为计算定义符号 argument 的方法:它本身没有任何值,而是表示 x
必须是浮点矩阵784
列的值。
Running/evaluating y
没有为 x
提供值就像编写以下 Python 函数并在没有所有参数的情况下调用它:
def y(x):
W = ...
b = ...
return softmax(matmul(x, W), b)
# This would fail with an error.
print(y())
如何指定参数的值?在 TensorFlow 中,您可以通过 为占位符输入 一个值(就像在 training loop and accuracy calculation 中):
# Get a batch of input data to feed to the computation.
batch_xs, _ = mnist.train.next_batch(100)
print(sess.run(y, feed_dict={x: batch_xs}))
# or
print(y.eval({x: batch_xs}))
张量y_
被定义为一个tf.placeholder()
。由于技术原因,您无法直接评估占位符,即使您为它提供了一个值。但是,这样做并不是特别有用!相反,您可以只打印您将输入的值。
我刚刚尝试在 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))
完整代码可在 GitHub 上获得。
下面是输出:
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_
张量都依赖于 tf.placeholder()
操作,因此它们需要您 feed 当你评估它们时输入一个值。您可以像这样输入一批输入,在一批输入数据上打印 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 是一种为计算定义符号 argument 的方法:它本身没有任何值,而是表示 x
必须是浮点矩阵784
列的值。
Running/evaluating y
没有为 x
提供值就像编写以下 Python 函数并在没有所有参数的情况下调用它:
def y(x):
W = ...
b = ...
return softmax(matmul(x, W), b)
# This would fail with an error.
print(y())
如何指定参数的值?在 TensorFlow 中,您可以通过 为占位符输入 一个值(就像在 training loop and accuracy calculation 中):
# Get a batch of input data to feed to the computation.
batch_xs, _ = mnist.train.next_batch(100)
print(sess.run(y, feed_dict={x: batch_xs}))
# or
print(y.eval({x: batch_xs}))
张量y_
被定义为一个tf.placeholder()
。由于技术原因,您无法直接评估占位符,即使您为它提供了一个值。但是,这样做并不是特别有用!相反,您可以只打印您将输入的值。