这不是我的代码,我只是尝试更改神经网络部分,然后问题开始了, 我在做什么错了?
training = np.array(training) # Shape = (46, 26)
output = np.array(output) # Shape = (26, 6)
model = Sequential()
model.add(Dense(8, input_shape=(46,)))
model.add(Dense(8))
model.add(Dense(units=len(output[0]), activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(training, output, batch_size=8, epochs=100)
def get_bag_of_words(sentence, words_list):
bag_of_words = [0 for _ in range(len(words_list))]
sentence_words = word_tokenize(sentence)
sentence_words = [stemmer.stem(word.lower()) for word in sentence_words if word.isalpha()]
for word_in_sentence in sentence_words:
for i, word in enumerate(words_list):
if word == word_in_sentence:
bag_of_words[i] = 1
bag_of_words = np.array(bag_of_words)
return bag_of_words
def chat():
print("Start talking with the bot (type quit to stop)!")
while True:
inp = input("You: ")
if inp.lower() == 'quit':
break
input_data = get_bag_of_words(inp, words_list) # Shape = (46,)
results = model.predict(input_data) ##### Error happens here
results_index = np.argmax(results)
tag = labels[results_index]
for tg in data['intents']:
if tg['tag'] == tag:
responses = tg['responses']
print(random.choice(responses))
ValueError:检查输入时出错:预期density_input具有形状(46,)但具有形状(1,)的数组
答案 0 :(得分:0)
尝试使其形状=(1,46):
input_data = numpy.reshape(input_data, (1, input_data.shape[0]))