model.fit
引发错误ValueError: Error when checking input: expected embedding_1_input to have shape (32,) but got array with shape (1,)
,但是没有传递给(1,)
的形状为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)
我希望错误消息指出哪个值/参数/层/等等是不正确的大小。我不确定embedding_1_input
指的是什么。