0

I am making a AI chat bot program with tflearn but every time I run it, it gives me a Traceback error on tflearn.DNN(). Here is the error:

Traceback (most recent call last):
  File "c:/Users/sande/Desktop/Vihaan/ThirdPartySoftware/Python/ChatBot/main.py", line 85, in <module>
    model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tflearn\models\dnn.py", line 196, in fit
    self.trainer.fit(feed_dicts, val_feed_dicts=val_feed_dicts,
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tflearn\helpers\trainer.py", line 341, in fit
    snapshot = train_op._train(self.training_state.step,
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tflearn\helpers\trainer.py", line 826, in _train
    tflearn.is_training(True, session=self.session)
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tflearn\config.py", line 95, in is_training
    tf.get_collection('is_training_ops')[0].eval(session=session)
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 913, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 5512, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\client\session.py", line 957, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "C:\Users\sande\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\client\session.py", line 1104, in _run
    raise RuntimeError('Attempted to use a closed Session.')
RuntimeError: Attempted to use a closed Session.

Here is the code:

import numpy
import tflearn
import tensorflow
import random
import json
import pickle
import nltk
nltk.download("punkt")
from nltk.stem.lancaster import LancasterStemmer

stemmer = LancasterStemmer()

with open("intents.json") as file:
    data = json.load(file)

try:
    with open("data.pickle", "rb")as f:
        words, label, training, output = pickle.load(f)

except:
    words = []
    labels = []
    docs_x = []
    docs_y = []

    for intent in data["intents"]:
        for pattern in intent["patterns"]:
            wrds = nltk.word_tokenize(pattern)
            words.extend(wrds)
            docs_x.append(wrds)
            docs_y.append(intent["tag"])

        if intent["tag"] not in labels:
            labels.append(intent["tag"])

    words = [stemmer.stem(w.lower()) for w in words if w != "?"]
    words = sorted(list(set(words)))

    labels = sorted(labels)

    training = []
    output = []

    out_empty = [0 for _ in range(len(labels))]

    for x, doc in enumerate(docs_x):
        bag = []

        wrds = [stemmer.stem(w) for w in doc]

        for w in words:
            if w in wrds:
                bag.append(1)

            else:
                bag.append(0)

        output_row = out_empty[:]
        output_row[labels.index(docs_y[x])] = 1

        training.append(bag)
        output.append(output_row)


    training = numpy.array(training)
    output = numpy.array(output)

    with open("data.pickle", "wb")as f:
            pickle.dump((words, labels, training, output), f)

tensorflow.compat.v1.reset_default_graph()

net = tflearn.input_data(shape=[None, len(training[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
net = tflearn.regression(net)

model = tflearn.DNN(net)

try:
    model.load("model.tflearn")

except:
    model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
    model.save("model.tflearn")

I tried multiple fixes, but none seemed to work. I even tried re - installing Tensorflow and Tflearn as tensorflow was also giving me an error.

How can I solve this?

BTW, Here is the file.intents:

{"intents": [
        {"tag": "greeting",
         "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
         "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
         "context_set": ""
        },
        {"tag": "goodbye",
         "patterns": ["See you later", "Goodbye", "I am Leaving", "Have a Good day"],
         "responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
         "context_set": ""
        },
        {"tag": "age",
         "patterns": ["how old", "how old is vihaan", "what is your age", "how old are you", "age?"],
         "responses": ["I am 12 years old!", "12 years young!"],
         "context_set": ""
        },
        {"tag": "name",
         "patterns": ["what is your name", "what should I call you", "whats your name?"],
         "responses": ["You can call me Vihaan.", "I'm Vihaan!", "I'm Vihaan aka PyGamerViharo."],
         "context_set": ""
        },
        {"tag": "code",
         "patterns": ["Do you like python?", "like python?", "what language", "what language do you recommend?"],
         "responses": ["Yes! We have made multiple projects. Check them out at www.github.com/PyGamerViharo", "I always recommend Python, even for developers!"],
         "context_set": ""
        }
   ]
}
Viharo
  • 40
  • 7
  • It seems you are using a Tensorflow 1 Version. To run a training in tensorflow 1 it requieres a session environment. This website describes how session is working https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session?hl=de – MaKaNu Nov 26 '20 at 13:02
  • I tried this and it fixed one error, but another popped up after that. A tensorflow has no attribute reset_default_graph – Viharo Nov 26 '20 at 15:30
  • Watch this Question: https://stackoverflow.com/questions/40782271/attributeerror-module-tensorflow-has-no-attribute-reset-default-graph I just googled your Error. Maybe this problem occurs because you put your complete code into the session? – MaKaNu Nov 26 '20 at 16:42
  • I have done that, but it didn't work – Viharo Nov 27 '20 at 07:53
  • Could you proved the file ```intents.json```? – MaKaNu Nov 29 '20 at 13:16

1 Answers1

1

I run your code and change the code like this, that 's working for me. I guess for some reason, the object model was disposed after broken at ' model.load("model.tflearn")'

model = tflearn.DNN(net)
try:
    model.load("model.tflearn")

except:
    model = tflearn.DNN(net)
    model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
    model.save("model.tflearn")`
SnailBai
  • 25
  • 6