【发布时间】:2015-12-21 01:49:01
【问题描述】:
我正在学习 Doc2Vec 和 gensim 库。我已经能够通过创建一个文档语料库来训练我的模型,例如
LabeledSentence(['what', 'happens', 'when', 'an', 'army', 'of', 'wetbacks', 'towelheads', 'and', 'godless', 'eastern', 'european', 'commies', 'gather', 'their', 'forces', 'south', 'of', 'the', 'border', 'gary', 'busey', 'kicks', 'their', 'butts', 'of', 'course', 'another', 'laughable', 'example', 'of', 'reagan-era', 'cultural', 'fallout', 'bulletproof', 'wastes', 'a', 'decent', 'supporting', 'cast', 'headed', 'by', 'l', 'q', 'jones', 'and', 'thalmus', 'rasulala'], ['LABELED_10', '0'])`
请注意,此特定文档有两个标签,即“LABELED_10”和“0”。
现在在我加载我的模型并执行之后
print(model.docvecs.most_similar("LABELED_10"))
我明白了
[('LABELED_107', 0.48432376980781555), ('LABELED_110', 0.4827481508255005), ('LABELED_214', 0.48039984703063965), ('LABELED_207', 0.479473352432251), ('LABELED_315', 0.47931796312332153), ('LABELED_307', 0.47898322343826294), ('LABELED_124', 0.4776897132396698), ('LABELED_222', 0.4768940210342407), ('LABELED_413', 0.47479286789894104), ('LABELED_735', 0.47462597489356995)]
这是完美的!因为我得到了与 LABELED_10 最相似的所有标签。
现在我想在训练我的模型时有一个反馈循环。因此,如果我给我的模型一个新文档,我想知道模型的分类在标记该文档并将其添加到我的语料库之前有多好或坏。我将如何使用 Doc2Vec 来做到这一点?那么我怎么知道 LABELED_107 和 LABELED_10 的文档实际上是否相似。这是我想到的一种方法。这是我的随机森林分类器的代码
result = cfun.rfClassifer(n_estimators, trainingDataFV, train["sentiment"],testDataFV)
这里是函数
def rfClassifer(n_estimators, trainingSet, label, testSet):
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
forest = RandomForestClassifier(n_estimators)
forest = forest.fit(trainingSet, label)
result = forest.predict(testSet)
return result
我终于可以做到了
output = pd.DataFrame(data={"id": test["id"], "sentiment": result})
output.to_csv("../../submits/Doc2Vec_AvgVecPredict.csv", index=False, quoting=3)
反馈过程
保留正确标记的验证集。
移除标签后将标签验证集馈送到分类器,并将结果保存在 csv 中。
将结果与另一个具有正确标签的 csv 进行比较。
对于每个不匹配,将这些文档添加到标记的训练集中并再次训练模型。
重复更多验证集。
这种方法正确吗?另外,我可以增量训练 doc2vec 模型吗?可以说,最初我用 100k 标记文档训练了我的 doc2vec 模型。现在在验证步骤之后,我需要在另外 10k 个文档上训练我的模型。我必须从一开始就训练我的模型吗?这意味着我需要再次在最初的 100k 标记文档上训练我的模型吗?
非常感谢您的见解。
谢谢
【问题讨论】:
-
这些标签有什么意义还是只是句子索引?你也有每个句子独立的句子情感,是吗?
-
每个句子都有自己的情感。标签可以是句子索引,如本例所示,但我也尝试过使用多个标签。表示索引标签和情绪标签
标签: nlp python-3.4 gensim word2vec