Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I solve this: "RuntimeError: Attempted to use a closed Session."

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!

like image 380
Frank.Fan Avatar asked Dec 24 '22 17:12

Frank.Fan


1 Answers

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.

like image 104
gdelab Avatar answered Dec 30 '22 20:12

gdelab