Web Development
ImportError when trying to use tflearn – Python – SitePoint Forums
I am learning to code AI Chatbots in Python, and I tried to run this code from here:
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tflearn
import tensorflow
import random
import json
import pickle
with open("intents.json") as file:
data = json.load(file)
try:
with open("data.pickle", "rb") as f:
words, labels, 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.lower()) 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.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")
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i, w in enumerate(words):
if w == se:
bag[i] = 1
return numpy.array(bag)
def chat():
print("Start talking with the bot (type quit to stop)!")
while True:
inp = input("You: ")
if inp.lower() == "quit":
break
results = model.predict([bag_of_words(inp, words)])
results_index = numpy.argmax(results)
tag = labels[results_index]
for tg in data["intents"]:
if tg['tag'] == tag:
responses = tg['responses']
print(random.choice(responses))
chat()
when I get this error:
WARNING:tensorflow:From C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tflearn\__init__.py:5: The name tf.disable_v2_behavior is deprecated. Please use tf.compat.v1.disable_v2_behavior instead.
WARNING:tensorflow:From C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\compat\v2_compat.py:108: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
curses is not supported on this machine (please install/reinstall curses for an optimal experience)
WARNING:tensorflow:From C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tflearn\helpers\summarizer.py:9: The name tf.summary.merge is deprecated. Please use tf.compat.v1.summary.merge instead.
Traceback (most recent call last):
File "C:\Users\user\Documents\ChatBot.py", line 6, in <module>
import tflearn
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tflearn\__init__.py", line 25, in <module>
from .layers import normalization
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tflearn\layers\__init__.py", line 11, in <module>
from .recurrent import lstm, gru, simple_rnn, bidirectional_rnn, \
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tflearn\layers\recurrent.py", line 17, in <module>
from tensorflow.python.util.nest import is_sequence
ImportError: cannot import name 'is_sequence' from 'tensorflow.python.util.nest' (C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\util\nest.py)
And it’s coming from this line:
import tflearn
Any fixes? I use Python 3.10.6.
Far as I can see from a little basic googling, tflearn
was abandoned 4 years ago. You can try to install the very latest version by doing
pip uninstall tflearn
pip install git+https://github.com/tflearn/tflearn.git
But I cant guarantee that’ll fix it, or if it does, for how long. It looks like you’ve got other installation issues there too.
Agh, I should have thought about that, is there any up to date libraries apart from this github package? Now that you mentioned it, many of the tutorials that I Googled uses abandoned libraries…
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.