我写了一个张量板代码,当我跑步时,它说"试图使用一个封闭的会话。"有人告诉我如何解决这个问题吗?这是代码:
# coding=utf-8
from color_1 import read_and_decode, get_batch, get_test_batch
import cv2
import os
import time
import math
import numpy as np
import tensorflow as tf
max_steps=3000
batch_size=128
log_dir='/home/vrview/tensorflow/example/char/tfrecords'
def variable_with_weight_loss(shape,stddev,w1):
var=tf.Variable(tf.truncated_normal(shape,stddev=stddev))
if w1 is not None:
weight_loss=tf.multiply(tf.nn.l2_loss(var),w1,name='weight_loss')
tf.add_to_collection('losses',weight_loss)
return var
with tf.name_scope('input'):
image_holder=tf.placeholder(tf.float32,[batch_size,56,56,3],name='x-input')
label_holder=tf.placeholder(tf.int32,[batch_size],name='y-input')
with tf.name_scope('input_reshape'):
image_input=tf.reshape(image_holder,[-1,56,56,3])
tf.summary.image('input',image_input,10)
def variable_summaries(var):
with tf.name_scope('summaries'):
mean=tf.reduce_mean(var)
tf.summary.scalar('mean',mean)
with tf.name_scope('stddev'):
stddev=tf.sqrt(tf.reduce_mean(tf.square(var-mean)))
tf.summary.scalar('stddev',stddev)
tf.summary.scalar('max',tf.reduce_max(var))
tf.summary.scalar('min',tf.reduce_min(var))
tf.summary.histogram('histogram',var)
def layer1(layer_name):
with tf.name_scope(layer_name):
with tf.name_scope('weight1'):
weight1=variable_with_weight_loss(shape=[5,5,3,64],stddev=5e-2,w1=0.0)
variable_summaries(weight1)
kernel1=tf.nn.conv2d(image_holder,weight1,[1,1,1,1],padding='SAME')
with tf.name_scope('biases'):
bias1=tf.Variable(tf.constant(0.0,shape=[64]))
variable_summaries(bias1)
conv1=tf.nn.relu(tf.nn.bias_add(kernel1,bias1))
pool1=tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
norm1=tf.nn.lrn(pool1,4,bias=1.0,alpha=0.001/9.0,beta=0.75)
return norm1
def full_layer1(pool,name):
with tf.name_scope(name):
reshape=tf.reshape(pool,[batch_size,-1])
dim=reshape.get_shape()[1].value
with tf.name_scope('weight3'):
weight3=variable_with_weight_loss(shape=[dim,384],stddev=0.04,w1=0.004)
variable_summaries(weight3)
with tf.name_scope('bias3'):
bias3=tf.Variable(tf.constant(0.1,shape=[384]))
variable_summaries(bias3)
with tf.name_scope('Wx_plus'):
preactivate1=tf.matmul(reshape,weight3)+bias3
tf.summary.histogram('pre_activations1',preactivate1)
activations=tf.nn.relu(preactivate1,name='activation')
tf.summary.histogram('activations',activations)
return activations
def full_layer2(local,name):
with tf.name_scope('weight4'):
weight4=variable_with_weight_loss(shape=[384,192],stddev=0.04,w1=0.004)
variable_summaries(weight4)
with tf.name_scope('bias4'):
bias4=tf.Variable(tf.constant(0.1,shape=[192]))
variable_summaries(bias4)
with tf.name_scope('Wx_plus2'):
preactivate2 =tf.matmul(local,weight4)+bias4
tf.summary.histogram('pre_activations2', preactivate2)
activations = tf.nn.relu(preactivate2, name='activation')
tf.summary.histogram('activations', activations)
return activations
def full_layer3(local,name):
with tf.name_scope('weight5'):
weight5=variable_with_weight_loss(shape=[192,10],stddev=1/192,w1=0.0)
variable_summaries(weight5)
with tf.name_scope('bias5'):
bias5=tf.Variable(tf.constant(0.0,shape=[10]))
variable_summaries(bias5)
logits=tf.add(tf.matmul(local,weight5),bias5)
return logits
def loss(logits,labels):
labels=tf.cast(labels,tf.int64)
with tf.name_scope('cross_entropy'):
cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits,labels=labels,name ='cross_entropy_per_example')
cross_entropy_mean=tf.reduce_mean(cross_entropy,name='cross_entropy')
tf.add_to_collection('losses', cross_entropy_mean)
tf.summary.scalar('cross_entropy',cross_entropy_mean)
return tf.add_n(tf.get_collection('losses'),name='total_loss')
norm1=layer1('layer1')
pool2=layer2(norm1,'layer2')
act1=full_layer1(pool2,'full_layer1')
act2=full_layer2(act1,'full_layer2')
act3=full_layer3(act2,'full_layer3')
loss=loss(act3,label_holder)
with tf.name_scope('train'):
train_op=tf.train.AdamOptimizer(1e-3).minimize(loss)
top_k_op=tf.nn.in_top_k(act3,label_holder,1)
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])
if step % 10 == 0:
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
img_batch, lab_batch = sess.run([test_images, test_labels])
summary,loss_value=sess.run([merged,loss],feed_dict=feed_dict(True))
train_writer.add_run_metadata(run_metadata,'step%03d' % step)
train_writer.add_summary(summary, step)
saver.save(sess,log_dir+"/model.ckpt",step)
step = 0
while step < num_iter:
img_batch, lab_batch = sess.run([test_images, test_labels])
predictions = sess.run([top_k_op], feed_dict=feed_dict(False))
true_count += np.sum(predictions)
step += 1
test_writer.add_summary(predictions)
print('Predict:', predictions)
precision = true_count * 1.0 / total_sample_count
print 'true_count:', true_count, 'total_sample_count:', total_sample_count
print 'precision:', precision
train_writer.close()
test_writer.close()
if __name__=='__main__':
run()
当我运行它时,它说&#34;尝试使用已关闭的会话。&#34;就像这样:
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.
剂量任何人都知道发生了什么?非常感谢你的帮助!
答案 0 :(得分:2)
使用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对象。
答案 1 :(得分:0)
就我而言:
GetOrAddAsync
我删除了try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
和try:
,仅使用最后两行即可解决问题。