How can I solve this: "RuntimeError: Attempted to use a closed Session."
23,170
Solution 1
Anything that uses sess
should be inside your with tf.Session() as sess
. You basically just have to indent everything from for step in range(max_steps):
to test_writer.close()
What happens is that you are trying to call sess.run([batch_image, batch_label])
outside of the with tf.Session() as sess
scope which automatically closes the sess object once it goes out of scope.
Solution 2
In my case:
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
I removed try:
and except:
and only using 2 last lines solve the problem .
Author by
Frank.Fan
Updated on July 22, 2022Comments
-
Frank.Fan almost 2 years
When I run the following Tensorflow code, I receive a RuntimeError that says "Attempted to use a closed Session." Can someone tell me how to get around this error? Here is the code:
# coding=utf-8 # (...imports omitted...) # (...some constant declarations and helper functions omitted: # max_steps, batch_size, log_dir, variable_with_weight_loss, variable_summaries, # layer1, full_layer1, full_layer2, full_layer3, loss # ...) def run(): image, label = read_and_decode('train.tfrecords') batch_image, batch_label = get_batch(image, label, batch_size=128, crop_size=56) test_image, test_label = read_and_decode('val.tfrecords') test_images, test_labels = get_test_batch(test_image, test_label, batch_size=128, crop_size=56) # batch 生成测试 def feed_dict(train): if train: x=image_batch y=label_batch else: x=img_batch y=lab_batch return {image_holder:x,label_holder:y} saver=tf.train.Saver() num_examples = 10000 num_iter = int(math.ceil(num_examples / batch_size)) true_count = 0 total_sample_count = num_iter * batch_size init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(log_dir + '/train', sess.graph) test_writer = tf.summary.FileWriter(log_dir + '/test') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for step in range(max_steps): start_time = time.time() image_batch, label_batch = sess.run([batch_image, batch_label]) # (...rest of function omitted...) if __name__=='__main__': run()
Here is the exception that occurs when the code is run:
File "/home/vrview/tensorflow/example/char/tfrecords/color2_board.py", line 238, in <module> run() File "/home/vrview/tensorflow/example/char/tfrecords/color2_board.py", line 207, in run image_batch, label_batch = sess.run([batch_image, batch_label]) File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run run_metadata_ptr) File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 903, in _run raise RuntimeError('Attempted to use a closed Session.') RuntimeError: Attempted to use a closed Session.
Thanks for your help!
-
Frank.Fan almost 7 yearsthank you very much.I just relized. One more question . when I run it ,I get another bug:"TypeError: Parameter to MergeFrom() must be instance of same class: expected tensorflow.Summary got list." in "test_writer.add_summary(predictions)" Do you know how to solve it ?
-
Anton Codes almost 7 yearsHi @Frank.Fan - try to put separate questions in separate question posts. Not only is it good form, it does two more things. It makes your questions more easily searchable for other people and you+responder will get more points. Cheers
-
Eddoasso almost 3 yearsIf you do that you will fit the model every time the program is run. If you do try: model.load("model.tflearn") except: model = tflearn.DNN(net) #or however your model is instanciated model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True) model.save("model.tflearn")