RuntimeError: Attempt to use closed session in tflearn

I want to train my model using tflearn, but I am getting the error above. Here is my training loop: BTW I split my training inputs into separate 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 the complete 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 that you can help me because I have tried for a while but I have not found any solutions.

+3


source to share


2 answers


I had the same problem. To solve it, you have to remove, except:

and try:

this:

    print("no model")

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

      



then it will work correctly.

0


source


I replaced try/except

withif os.path.exists(...)

But save(MODEL_NAME)

does not create a file with the name MODEL_NAME

and number of the files with the names "MODEL_NAME.meta"

, "MODEL_NAME.index"

, "MODEL_NAME.data-00000-of-00001"

, why if os.path.exists(...)

should check out 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 an answer to the question: Building ai chatbot but getting a trace error

0


source







All Articles