【发布时间】:2020-10-26 12:28:46
【问题描述】:
我的目标是作为第一步从列 df["text"] 创建文档嵌入,然后作为第二步将它们与其他变量一起插入 XGBoost Regressor 模型以进行预测。这对 train_df 非常有效。
我目前正在尝试通过在看不见的 test_df 上使用 infer_vector() 推断向量来评估我训练有素的 Doc2Vec 模型,然后再次使用它进行预测。但是,结果非常糟糕。我得到了一个非常大的错误(RMSE)。
我假设,这意味着 Doc2Vec 严重过度拟合?
我实际上不确定这是否是评估我的 doc2vec 模型的正确方法(通过 infer_vector)?
如何防止doc2vec过拟合?
请在下面找到我的代码,用于从模型中推断向量:
vectors_test=[]
for i in range(0, len(test_df)):
vecs=model.infer_vector(tokenize(test_df["text"][i]))
vectors_test.append(vecs)
vectors_test= pd.DataFrame(vectors_test)
test_df = pd.concat([test_df, vectors_test], axis=1)
然后我使用我的 XGBoost 模型进行预测:
np.random.seed(0)
test_df= test_df.reindex(np.random.permutation(test_df.index))
y = test_df['target'].values
X = test_df.drop(['target'], axis=1).values
y_pred = mod.predict(X)
pred = pd.DataFrame()
pred["Prediction"] = y_pred
rmse = np.sqrt(mean_squared_error(y,y_pred))
print(rmse)
另请参阅我的 doc2vec 模型的训练:
doc_tag = train_df.apply(lambda train_df: TaggedDocument(words=tokenize(train_df["text"]), tags= [train_df.Tag]), axis = 1)
# initializing model, building a vocabulary
model = Doc2Vec(dm=0, vector_size=200, min_count=1, window=10, workers= cores)
model.build_vocab([x for x in tqdm(doc_tag.values)])
# train model for 5 epochs
for epoch in range(5):
model.train(utils.shuffle([x for x in tqdm(doc_tag.values)]), total_examples=len(doc_tag.values), epochs=1)
【问题讨论】:
标签: python testing nlp gensim doc2vec