我该如何解决这个问题:“RuntimeError: Attempted to use a closed Session.”

Fra*_*Fan 4 tensorflow tensorboard

当我运行以下 Tensorflow 代码时,我收到一个 RuntimeError,显示“尝试使用关闭的会话”。有人能告诉我如何解决这个错误吗?这是代码:

# 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()
Run Code Online (Sandbox Code Playgroud)

下面是代码运行时发生的异常:

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.
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

Moh*_*Hrt 7

就我而言:

try:
    model.load("model.tflearn")
except:
    model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
    model.save("model.tflearn")
Run Code Online (Sandbox Code Playgroud)

我删除了try:并且except:仅使用最后两行解决了问题。

  • 如果这样做,您将在每次运行程序时拟合模型。如果您尝试: model.load("model.tflearn") except: model = tflearn.DNN(net) #或者您的模型已实例化 model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=真) model.save("model.tflearn") (2认同)

gde*_*lab 6

任何使用的东西都sess应该在你的with tf.Session() as sess. 基本上,你只需要缩进一切从for step in range(max_steps):test_writer.close()

发生的情况是您试图sess.run([batch_image, batch_label])with tf.Session() as sess范围之外调用,一旦超出范围,它会自动关闭 sess 对象。