Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 我怎样才能解决这个问题呢;运行时错误:试图使用关闭的会话;_Tensorflow_Tensorboard - Fatal编程技术网

Tensorflow 我怎样才能解决这个问题呢;运行时错误:试图使用关闭的会话;

Tensorflow 我怎样才能解决这个问题呢;运行时错误:试图使用关闭的会话;,tensorflow,tensorboard,Tensorflow,Tensorboard,当我运行下面的Tensorflow代码时,我收到一个运行时错误,上面写着“试图使用一个关闭的会话”。有人能告诉我如何避免这个错误吗?代码如下: # coding=utf-8 # (...imports omitted...) # (...some constant declarations and helper functions omitted: # max_steps, batch_size, log_dir, variable_with_weight_loss, variable_su

当我运行下面的Tensorflow代码时,我收到一个运行时错误,上面写着“试图使用一个关闭的会话”。有人能告诉我如何避免这个错误吗?代码如下:

# 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()
以下是运行代码时发生的异常:

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.
文件“/home/vrview/tensorflow/example/char/tfrecords/color2_board.py”,第238行,在
运行()
文件“/home/vrview/tensorflow/example/char/tfrecords/color2_board.py”,第207行,运行中
image\u batch,label\u batch=sess.run([batch\u image,batch\u label])
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第767行,正在运行
运行_元数据_ptr)
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第903行,正在运行
raise RUNTIMERROR('试图使用关闭的会话')
RuntimeError:试图使用已关闭的会话。

谢谢你的帮助

任何使用
sess
的内容都应该在您的
中,并将tf.Session()作为sess
。基本上,您只需缩进所有内容,从
到步长范围(最大步长):
test\u writer.close()

发生的情况是,您试图调用
sess.run([batch\u image,batch\u label])
之外,将tf.Session()作为sess
作用域,一旦sess对象超出作用域就会自动关闭。

在我的例子中:

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

我删除了
尝试:
例外:
并且只使用最后两行就可以解决问题。

非常感谢。我刚刚重新体验了。还有一个问题。当我运行它时,我发现了另一个错误:“TypeError:ParametertoMergeFrom()必须是同一个类的实例:expected tensorflow.Summary get list。”在“test\u writer.add\u Summary(predictions)”中,你知道如何解决它吗?Hi@Frank.Fan-尝试在不同的问题帖子中分别提问。这不仅是一个好的形式,它还做了两件事。它使您的问题更容易为其他人搜索,您+回答者将获得更多分数。干杯