我是编码的新手,并且在Tim的chatbot教程中关注Tech。我相信所有内容都完全按照他的写法来复制,但始终会引发该错误。我正在使用tflearn和张量流1.15 它一直给我警告不要使用,所以我不得不添加忽略警告部分 进口警告 warnings.filterwarnings(“ ignore”)
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tflearn
import tensorflow
import random
import json
import pickle
with open("/Users/Jerrod/Documents/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 not in "?"]
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 words if w != "?"]
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()
WARNING:tensorflow:From /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/collections.py:13: The name tf.GraphKeys is deprecated. Please use tf.compat.v1.GraphKeys instead.
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/main.py:71:名称tf.reset_default_graph已弃用。请改用tf.compat.v1.reset_default_graph。
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/layers/core.py:66:名称tf.placeholder已弃用。请改用tf.compat.v1.placeholder。
警告:tensorflow:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/layers/core.py:69:不建议使用名称tf.add_to_collection。请改用tf.compat.v1.add_to_collection。
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/initializations.py:173:调用TruncatedNormal。 init (from dtype的tensorflow.python.ops.init_ops)已过时,并将在以后的版本中删除。 更新说明: 使用dtype参数调用初始化程序实例,而不是将其传递给构造函数 警告:tensorflow:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/variables.py:44:名称tf.get_variable已弃用。请改用tf.compat.v1.get_variable。
警告:tensorflow:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/optimizers.py:238:不建议使用名称tf.train.AdamOptimizer。请改用tf.compat.v1.train.AdamOptimizer。
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/layers/estimator.py:96:名称tf.get_collection已弃用。请改用tf.compat.v1.get_collection。
警告:tensorflow:从/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/objectives.py:114:使用带有*的reduce_sum_v1(来自tensorflow.python.ops.math_ops)调用keep_dims已过时,将在以后的版本中删除。 更新说明: 不建议使用keep_dims,请改用keepdims 警告:tensorflow:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/objectives.py:118:名称tf.log已弃用。请改用tf.math.log。
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/layers/estimator.py:141:名称tf.trainable_variables已弃用。请改用tf.compat.v1.trainable_variables。
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/helpers/trainer.py:457:不建议使用名称tf.get_default_graph。请改用tf.compat.v1.get_default_graph。
警告:tensorflow:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/config.py:130:名称tf.assign已弃用。请改用tf.compat.v1.assign。
警告:张量流:来自/Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/helpers/trainer.py:95:不建议使用名称tf.Session。请改用tf.compat.v1.Session。
2020-01-11 13:22:30.927045:I tensorflow / core / platform / cpu_feature_guard.cc:142]您的CPU支持该TensorFlow二进制文件未编译为使用的指令:AVX2 FMA 2020-01-11 13:22:30.940158:我tensorflow / compiler / xla / service / service.cc:168] XLA服务0x7f9fba93cac0已针对平台Host初始化(这不保证将使用XLA)。设备: 2020-01-11 13:22:30.940171:I tensorflow / compiler / xla / service / service.cc:176] StreamExecutor设备(0):主机,默认版本 追溯(最近一次通话): 在第79行中输入文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/main.py” 型号= tflearn.DNN(net) init 中的第57行中的文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/models/dnn.py” 会话=会话) init 中的文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/helpers/trainer.py”,第111行 clip_gradients) 文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/helpers/trainer.py”,第561行,在initialize_training_ops中 ema_num_updates = self.training_steps) 文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/summaries.py”,第243行,位于add_loss_summaries中 summaries_collection_key) 文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tflearn/summaries.py”,第46行,位于get_summary summ = tf.scalar_summary(标签,值) getattr 中的第193行中的文件“ /Users/Jerrodthompson/PycharmProjects/justanotherchatbotattempt/venv/lib/python3.6/site-packages/tensorflow_core/python/util/module_wrapper.py” attr = getattr(self._tfmw_wrapped_module,名称) AttributeError:模块“ tensorflow”没有属性“ scalar_summary”
答案 0 :(得分:0)
在0.12版本发布后,tf.scalar_summary()函数已在master分支中移动。现在您可以找到它作为tf.summary.scalar()。 祝好运!
答案 1 :(得分:0)
您的tflearn版本有点旧。所以,您要做的就是:
pip install tflearn --upgrade
我遇到了与您相同的问题,并使用此方法进行了修复。