ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.22k stars 12.91k forks source link

chapter13Cannot use the default session to execute operation #496

Open Taylor-Tian opened 5 years ago

Taylor-Tian commented 5 years ago
n_epochs = 10
batch_size = 40
n_iterations_per_epoch = len(flower_paths_and_classes_train) // batch_size

with tf.Session() as sess:
    init.run()
    inception_saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)

    for epoch in range(n_epochs):
        print("Epoch", epoch, end="")
        for iteration in range(n_iterations_per_epoch):
            print(".", end="")
            X_batch, y_batch = prepare_batch(flower_paths_and_classes_train, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch, training: True})

        acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
        print("  Last batch accuracy:", acc_batch)

        save_path = saver.save(sess, "./my_flowers_model")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-85-214dfc6100e5> in <module>
      4 
      5 with tf.Session() as sess:
----> 6     init.run()
      7     inception_saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)
      8 

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in run(self, feed_dict, session)
   2448         none, the default session will be used.
   2449     """
-> 2450     _run_using_default_session(self, feed_dict, self.graph, session)
   2451 
   2452 _gradient_registry = registry.Registry("gradient")

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _run_using_default_session(operation, feed_dict, graph, session)
   5205                        "`run(session=sess)`")
   5206     if session.graph is not graph:
-> 5207       raise ValueError("Cannot use the default session to execute operation: "
   5208                        "the operation's graph is different from the "
   5209                        "session's graph. Pass an explicit session to "

ValueError: Cannot use the default session to execute operation: the operation's graph is different from the session's graph. Pass an explicit session to run(session=sess).

Can anyone talk to me how to solve this problem?

ageron commented 5 years ago

Hi @Taylor-Tian , Thanks for your question. Sorry for the delay, I just got back from vacation. In TF 1.x a session can only execute one graph. All the operations you ask it to run must come from that one graph. If you ask it to execute an operation that belongs to any other graph, it raises the exception you got. Since the stacktrace shows that the error took place on the line init.run(), we can deduce that the init operation does not belong to the current session's graph.

The init operation is created in cell [62], and it belongs to the default graph (since there is no 'with graph.as_default()block). Moreover, theSessionis created without anygraphas argument, meaning it uses the default graph. So the operation's graph _should_ be the same as the session's graph, i.e., the default graph. So the only explanation I can see is that you reset the graph after creating theinitoperation. In that case, theinit` operation would belong to the old default graph, while the session would use the new default graph.

So please ensure that:

  1. the notebook is exactly like the one in the project, no modified code.
  2. you run all the cells sequentially, especially between cell [54] (when the graph is reset) to cell [78] (when the session runs the graph).

I hope this helps, Aurélien