温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - ValueError: Error when checking input: expected dense_input to have shape (46,) but got array with s

python - ValueError:检查输入时出错:预期density_input具有形状(46,),但数组带有s

发布于 2020-03-29 21:34:41

这不是我的代码,我只是尝试更改神经网络部分,然后问题开始于此,我做错了什么?

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,)

查看更多

提问者
Arturas Druteika
被浏览
81
SoloDolo 2020-03-03 19:26

尝试使其形状=(1,46):

input_data = numpy.reshape(input_data, (1, input_data.shape[0]))