How to print the value of a tensor in tensorflow mnist_softmax.py
我只是试图在TensorFlow 0.8中运行
我想在模型测试步骤之前观察
下面是代码:
1 2 3 4 5 | 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上找到。
以下是输出:
1 2 | Tensor("Softmax:0", shape=(?, 10), dtype=float32) Tensor("Placeholder_1:0", shape=(?, 10), dtype=float32) |
我也尝试过使用
1 2 3 | 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:
1 2 | batch_xs, _ = mnist.train.next_batch(100) print(y.eval({x: batch_xs})) |
MNIST示例包含以下几行:
1 2 3 4 5 | # 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) |
请注意,您尝试打印的第一个张量
在不提供
1 2 3 4 5 6 7 | def y(x): W = ... b = ... return softmax(matmul(x, W), b) # This would fail with an error. print(y()) |
如何为参数指定值?在TensorFlow中,您可以通过输入占位符的值来实现此目的(就像在训练循环和准确性计算中一样):
1 2 3 4 5 6 | # 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})) |
张量