RuntimeError: Attempted to use a closed Session in tflearn

烂漫一生 提交于 2019-12-23 19:55:25

问题


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


回答1:


I had same problem.To resolve it,you should delete except: and try: ,such this:

    print("no model")

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

then it will works correctly.




回答2:


I replaced try/except with 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)

Created as answer to question: Creating an ai chatbot, but getting a traceback error



来源:https://stackoverflow.com/questions/45312698/runtimeerror-attempted-to-use-a-closed-session-in-tflearn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!