gpt4 book ai didi

python - 检查输入时出错 : expected embedding_1 input to have shape but got shape

转载 作者:行者123 更新时间:2023-11-30 09:39:23 26 4
gpt4 key购买 nike

我已经成功创建了 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,)

最佳答案

在这种情况下,您应该应用 reshape ,以便获得一个具有包含句子的唯一元素的数组。

X_reshape = X[0].reshape(1, 2139)

best_model.predict_entries(X_reshape)

关于python - 检查输入时出错 : expected embedding_1 input to have shape but got shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com