How can I solve this: "RuntimeError: Attempted to use a closed Session."
How can I solve this: "RuntimeError: Attempted to use a closed Session."
当我 运行 以下 Tensorflow 代码时,我收到一个 RuntimeError,上面写着 "Attempted to use a closed Session." 谁能告诉我如何解决这个错误?这是代码:
# 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.
感谢您的帮助!
任何使用 sess
的东西都应该在你的 with tf.Session() as sess
中。您基本上只需要缩进从 for step in range(max_steps):
到 test_writer.close()
的所有内容
您尝试在 with tf.Session() as sess
范围之外调用 sess.run([batch_image, batch_label])
会在 sess 对象超出范围后自动关闭它。
就我而言:
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
我删除了 try:
和 except:
并且仅使用最后两行解决了问题。
当我 运行 以下 Tensorflow 代码时,我收到一个 RuntimeError,上面写着 "Attempted to use a closed Session." 谁能告诉我如何解决这个错误?这是代码:
# 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.
感谢您的帮助!
任何使用 sess
的东西都应该在你的 with tf.Session() as sess
中。您基本上只需要缩进从 for step in range(max_steps):
到 test_writer.close()
您尝试在 with tf.Session() as sess
范围之外调用 sess.run([batch_image, batch_label])
会在 sess 对象超出范围后自动关闭它。
就我而言:
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
我删除了 try:
和 except:
并且仅使用最后两行解决了问题。