我已经成功创建了Keras顺序模型并对其进行了一段时间的训练。现在,我正在尝试做出一些预测,但是即使使用与培训阶段所使用的数据相同的方法,也无法成功。
我收到此错误:{ValueError}检查输入时出错:预期embedding_1_input具有形状(2139,)但形状为(1,)的数组
但是,在检查我要使用的输入时,它显示为(2139,)。我想知道是否有人知道这可能是什么
df = pd.read_csv('../../data/parsed-data/data.csv')
df = ModelUtil().remove_entries_based_on_threshold(df, 'Author', 2)
#show_column_distribution(df, 'Author')
y = df.pop('Author')
le = LabelEncoder()
le.fit(y)
encoded_Y = le.transform(y)
tokenizer, padded_sentences, max_sentence_len \
= PortugueseTextualProcessing().convert_corpus_to_number(df)
ModelUtil().save_tokenizer(tokenizer)
vocab_len = len(tokenizer.word_index) + 1
glove_embedding = PortugueseTextualProcessing().load_vector(tokenizer)
embedded_matrix = PortugueseTextualProcessing().build_embedding_matrix(glove_embedding, vocab_len, tokenizer)
cv_scores = []
kfold = StratifiedKFold(n_splits=4, shuffle=True, random_state=7)
models = []
nn = NeuralNetwork()
nn.build_baseline_model(embedded_matrix, max_sentence_len, vocab_len, len(np_utils.to_categorical(encoded_Y)[0]))
# Separate some validation samples
val_data, X, Y = ModelUtil().extract_validation_data(padded_sentences, encoded_Y)
for train_index, test_index in kfold.split(X, Y):
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(Y)
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = dummy_y[train_index], dummy_y[test_index]
nn.train(X_train, y_train, 100)
scores = nn.evaluate_model(X_test, y_test)
cv_scores.append(scores[1] * 100)
models.append(nn)
print("%.2f%% (+/- %.2f%%)" % (np.mean(cv_scores), np.std(cv_scores)))
best_model = models[cv_scores.index(max(cv_scores))]
best_model.save_model()
best_model.predict_entries(X[0])
执行预测和模型创建的方法
def build_baseline_model(self, emd_matrix, long_sent_size, vocab_len, number_of_classes):
self.model = Sequential()
embedding_layer = Embedding(vocab_len, 100, weights=[emd_matrix], input_length=long_sent_size,
trainable=False)
self.model.add(embedding_layer)
self.model.add(Dropout(0.2))
self.model.add(Flatten())
# softmax performing better than relu
self.model.add(Dense(number_of_classes, activation='softmax'))
self.model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
return self.model
def predict_entries(self, entry):
predictions = self.model.predict_classes(entry)
# show the inputs and predicted outputs
print("X=%s, Predicted=%s" % (entry, predictions[0]))
return predictions
X [0] .shape计算为 :(2139,)
答案 0 :(得分:1)
在这种情况下,您应该应用整形,以便获得具有包含句子的唯一元素的数组。
X_reshape = X [0] .reshape(1,2139)
best_model.predict_entries(X_reshape)