RuntimeError:尝试在tflearn中使用已关闭的Session

时间:2017-07-25 20:15:06

标签: python numpy tflearn

我想用tflearn训练我的模型,但我得到上面显示的错误。 这是我的训练循环: 顺便说一下,我将训练输入分成了单独的numpy文件

for i in range(EPOCHS):
    for file in filess:

        file = np.load(file)
        x = []
        y = []
        for a, b in file:
            x.append(a)
            y.append(b[0])
        x = np.array(x).reshape(-1,WIDTH,HEIGHT,1)
        for sd in range(len(y)):
            idx = genres.index(y[sd])
            y[sd] = idx
        print(y)
        y = np.array(y)
        try:
            model.load(MODEL_NAME)
        except:
            print("no model")

        model.fit({'input': x}, {'targets': y}, n_epoch=1, 
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

        model.save(MODEL_NAME)`

以下是完整的错误消息:

`Traceback (most recent call last):
File "main.py", line 39, in <module>
model.fit({'input': x}, {'targets': y}, n_epoch=1, snapshot_step=500, 
show_metric=True, run_id=MODEL_NAME)
File "D:\Anaconda3\envs\python35\lib\site-packages\tflearn\models\dnn.py", 
line 215, in fit
callbacks=callbacks)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tflearn\helpers\trainer.py", line 356, in fit
self.train_ops = original_train_ops
File "D:\Anaconda3\envs\python35\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tensorflow\python\framework\ops.py", line 3625, in get_controller
yield default
File "D:\Anaconda3\envs\python35\lib\site-
packages\tflearn\helpers\trainer.py", line 336, in fit
show_metric)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tflearn\helpers\trainer.py", line 775, in _train
tflearn.is_training(True, session=self.session)
File "D:\Anaconda3\envs\python35\lib\site-packages\tflearn\config.py", line 
95, in is_training
tf.get_collection('is_training_ops')[0].eval(session=session)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tensorflow\python\framework\ops.py", line 569, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tensorflow\python\framework\ops.py", line 3741, in 
_eval_using_default_session
return session.run(tensors, feed_dict)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tensorflow\python\client\session.py", line 778, in run
run_metadata_ptr)
File "D:\Anaconda3\envs\python35\lib\site-
packages\tensorflow\python\client\session.py", line 914, in _run
raise RuntimeError('Attempted to use a closed Session.')
RuntimeError: Attempted to use a closed Session.`

我真的希望你可以帮助我,因为我现在试了一段时间,但我没有找到任何解决方案

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。要解决此问题,应删除except:try:,例如:

    print("no model")

    model.fit({'input': x}, {'targets': y}, n_epoch=1, 

然后它将正常工作。

答案 1 :(得分:0)

我将try/except替换为if os.path.exists(...)

但是save(MODEL_NAME)不会创建一个名称为MODEL_NAME的文件,而是创建了几个名称为"MODEL_NAME.meta""MODEL_NAME.index""MODEL_NAME.data-00000-of-00001"的文件,所以if os.path.exists(...)必须检查这些文件之一。

import os

if os.path.exists(MODEL_NAME + ".meta"):
    model.load(MODEL_NAME)
else:
    model.fit(...)
    model.save(MODEL_NAME)

创建为问题{@ {3}}的答案