本文介绍了RuntimeError:尝试在tflearn中使用封闭的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用tflearn训练我的模型,但是出现上面显示的错误.这是我的训练循环:顺便说一句,我将训练输入拆分为单独的numpy文件

I want to train my model with tflearn, but i get the error showed above.Here is my training loop:BTW I splitted my training inputs in seperate numpy files

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)`

这里是完整的错误消息:

Here is full error message:

`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.`

我真的希望您能帮助我,因为我已经尝试了一段时间,但是我没有找到任何解决方法

I really hope you can help me cause I tried for some time now, but I didnt found any solutions

推荐答案

我将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(...)必须检查其中一个文件.

But save(MODEL_NAME) doesn't create one file with name MODEL_NAME but few files with names "MODEL_NAME.meta", "MODEL_NAME.index", "MODEL_NAME.data-00000-of-00001" so if os.path.exists(...) has to check one of these files.

import os

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


创建为问题的答案:创建一个AI聊天机器人,但出现回溯错误

这篇关于RuntimeError:尝试在tflearn中使用封闭的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 11:13