ValueError: Error when checking input: expected embedding_1_input to have shape (32,) but got array with shape (1,)

China☆狼群 提交于 2021-02-19 08:32:31

问题


model.fit throws an error ValueError: Error when checking input: expected embedding_1_input to have shape (32,) but got array with shape (1,), but there are no arrays of shape (1,) passed to model.fit.

def create_model(vocabulary_size, input_word_count, embedding_dims=50):
    model = Sequential()
    model.add(Embedding(vocabulary_size, embedding_dims, input_length=input_word_count))
    model.add(GlobalAveragePooling1D())
    model.add(Dense(1, activation="sigmoid"))
    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
    return model


def main(epochs, batch_size):
    # Parse input data as a numpy array
    positive_words = ...
    negative_words = ...
    words = np.concatenate((positive_words, negative_words), axis=None)

    # Create labels
    labels = np.empty(words.size)
    for i in range(words.size):
        labels[i] = 1 if i < positive_words.size else 2

    # Split into train & test
    split_at = math.floor(words.size * 0.75)
    [words_train, words_test] = [words[split_at:], words[:split_at]]
    [labels_train, labels_test] = [labels[split_at:], labels[:split_at]]

    # Create model
    model = create_model(len(word_dict), batch_size)

    # Train model on first batch
    print(words_train.shape, labels_train.shape) # => (51565,) (51565,)
    model.fit(words_train[0:batch_size], labels_train[0:batch_size],
        batch_size=batch_size, epochs=epochs, verbose=2, #validation_data=(words_test, labels_test)
    )


main(200, batch_size=32)

I would expect the error message to indicate which value / parameter / layer / etc was the incorrect size. I am unsure what embedding_1_input refers to.

来源:https://stackoverflow.com/questions/56960432/valueerror-error-when-checking-input-expected-embedding-1-input-to-have-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!