Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eval() and run() in tensorflow

I'm referring to Deep MNIST for Experts tutorial given by the tensorflow. I have a problem in Train and Evaluate part of that tutorial. There they have given a sample code as follows.

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    print("step %d, training accuracy %g"%(i, train_accuracy))
  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images,
                       y_: mnist.test.labels, keep_prob: 1.0}))

So in these code segment they have used accuracy.eval() at one time. And other time train_step.run(). As I know of both of them are tensor variables.

And in some cases, I have seen like

sess.run(variable, feed_dict)

So my question is what are the differences between these 3 implementations. And how can I know what to use when..?

Thank You!!

like image 830
Ramesh-X Avatar asked Aug 17 '16 03:08

Ramesh-X


People also ask

What is session run in TensorFlow?

Session in TensorFlow. It's simple: A graph defines the computation. It doesn't compute anything, it doesn't hold any values, it just defines the operations that you specified in your code. A session allows to execute graphs or part of graphs.

What is placeholder in TensorFlow?

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders.

How do I use TF session?

It is important to release these resources when they are no longer required. To do this, either invoke the tf. Session. close method on the session, or use the session as a context manager.


1 Answers

If you have only one default session, they are basically the same.

From https://github.com/tensorflow/tensorflow/blob/v1.12.0/tensorflow/python/framework/ops.py#L2351:

op.run() is a shortcut for calling tf.get_default_session().run(op)

From https://github.com/tensorflow/tensorflow/blob/v1.12.0/tensorflow/python/framework/ops.py#L691:

t.eval() is a shortcut for calling tf.get_default_session().run(t)

Difference between Tensor and Operation:

Tensor: https://www.tensorflow.org/api_docs/python/tf/Tensor

Operation: https://www.tensorflow.org/api_docs/python/tf/Operation

Note: the Tensor class will be replaced by Output in the future. Currently these two are aliases for each other.

like image 171
fwalch Avatar answered Oct 11 '22 00:10

fwalch