-1

I am trying to learn deep learning with tensorflow, so excuse my stupid questions. I have been reading different tutorials as 'https://github.com/Hvass-Labs/TensorFlow-Tutorials' and 'https://github.com/u04617/deeplearning/blob/master/mnist_experiments.ipynb' unfortunately I have been confused by some difference in their writing. More specifically I have a question regarding opening a session:

1) what is the difference between

session = tf.Session()
session.run(tf.global_variables_initializer())
session.run(optimizer, feed_dict=feed_dict_train)

and

sess = tf.InteractiveSession()
tf.initialize_all_variables().run()
optimizer.run({x: batch_xs, y_: batch_ys, keep_prob: 0.5})

Indeed, I understand the basic idea behind each of these lines (open a session to execute the graph, initialize the variable for the graph and finally execute the graph given in a dictionary the needed input), but I don't understand the difference from the two above code, especially the last line.

miki
  • 639
  • 2
  • 6
  • 16

1 Answers1

1

Both are working.

  • In Jupyter Notebook files: use InteractiveSession

Read here to understand the difference between an InactiveSession and a Session. But do not even try eval(). Please do yourself a favor and use the only correct and clean way:

init_op = tf.global_variables_initializer()

tf.get_default_graph().finalize()
with tf.Session() as session:
  session.run(init_op)
  session.run(optimizer, feed_dict=feed_dict_train)

The way of operation.run(), tensor.eval() is spread somewhere in the internet. But there are tons of caveats:

Here are two of them:

Tensorflow subtract strange result

Official ZeroOut gradient example error: AttributeError: 'list' object has no attribute 'eval'

I hope at some point they will deprecate `eval() at some point.

Patwie
  • 4,360
  • 1
  • 21
  • 41
  • Thank you very much Patwie for your helpful answer! DO you have mroe details why I should use InteractiveSession()? Also, the correct way you suggest doe snot have this InteractiveSession(), so it does not meant to be the most correct way in jupyternotebook? – miki Aug 14 '18 at 14:33