nguyenhoai890
nguyenhoai890

Reputation: 1219

Tensorflow: Print value when Session running

First of all, I'm very new in Python and Tensorflow either. I'm trying on demo of link: https://www.tensorflow.org/get_started/mnist/beginners and it runs well. However, I would like to debug (or log) the value of some placeholders, variables which are changed when I run Session.run(). I

Could you please show me the way to "debug" or log them when Session running in the loops? Here is my code

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("mnist/", one_hot=True)

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)

y1 = tf.add(tf.matmul(x,W),b)
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

cross_entropy1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y1, y_))

train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy1)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

sess.run(tf.argmax(y,1), feed_dict={x: mnist.test.images, y_: mnist.test.labels})

In this script, I would like to log the value of y and tf.argmax(y, 1) for each test image processed.

Upvotes: 0

Views: 7664

Answers (2)

saeta
saeta

Reputation: 639

While @rmeerten's answer is correct, you can consider also using TensorBoard which can be a useful tool for debugging your models and seeing what's happening. For background, you can also check out the TensorBoard session from the TensorFlow Dev Summit.

Upvotes: 2

rmeertens
rmeertens

Reputation: 4451

Mrry answered it best in this stackoverflow answer: https://stackoverflow.com/a/33633839/6487788

Exactly what you are asking (printing during sess.run) would be this part of his answer:

  • To print the value of a tensor without returning it to your Python program, you can use the tf.Print() op, as And suggests in another answer. Note that you still need to run part of the graph to see the output of this op, which is printed to standard output. If you're running distributed TensorFlow, the tf.Print() op will print its output to the standard output of the task where that op runs.

This would be this code for argmax:

argmaxy = tf.Print(tf.argmax(y,1))
correct_prediction = tf.equal(argmaxy, tf.argmax(y_,1))

Good luck!

Upvotes: 3

Related Questions