Warm tip: This article is reproduced from stackoverflow.com, please click
python tensorflow tf.keras

ValueError: Error when checking input: expected dense_input to have shape (46,) but got array with s

发布于 2020-03-29 20:59:32

This is not my code I just tried changing the neural networks part and the problems started there, What am I doing wrong?

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: Error when checking input: expected dense_input to have shape (46,) but got array with shape (1,)

Questioner
Arturas Druteika
Viewed
215
SoloDolo 2020-03-03 19:26

Try making it shape = (1, 46):

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