我正在尝试理解doc2vec,并且想知道我是否可以用它来解决我的场景。我想使用TaggedSentences([words], [tags])来给句子打上一个或多个标签,但我对自己的理解不是很确定。
所以,基本上,我需要实现以下内容(或者我完全搞错了)
我创建了2个TaggedDocuments
TaggedDocument(words=["the", "bird", "flew", "over", "the", "coocoos", "nest"], labels=["animal","tree"])TaggedDocument(words=["this", "car", "is", "over", "one", "million", "dollars"], labels=["motor","money"])
我构建我的模型
model = gensim.models.Doc2Vec(documents, dm=0, alpha=0.025, size=20, min_alpha=0.025, min_count=0)
然后我训练我的模型
model.train(documents, total_examples=len(documents), epochs=1)
所以当我完成所有这些操作后,我期望当我执行
model.most_similar(positive=["bird", "flew", "over", "nest"])
时得到[animal,tree],但我得到的是
[('the', 0.4732949137687683), ('million', 0.34103643894195557),('dollars', 0.26223617792129517),('one', 0.16558100283145905),('this', 0.07230066508054733),('is', 0.012532509863376617),('cocos', -0.1093338280916214),('car', -0.13764989376068115)]
更新:当我推断时
vec_model = model.Word2Vec.load(os.path.join("save","vec.w2v"))infer = vec_model.infer_vector(["bird", "flew", "over", "nest"])print(vec_model.most_similar(positive=[infer], topn=10))
我得到
[('bird', 0.5196993350982666),('car', 0.3320297598838806), ('the', 0.1573483943939209), ('one', 0.1546170711517334), ('million', 0.05099521577358246),('over', -0.0021460093557834625), ('is', -0.02949431538581848),('dollars', -0.03168443590402603), ('flew', -0.08121247589588165),('nest', -0.30139490962028503)]
所以房间里的大象是,doc2vec是我实现上述场景所需要的吗,还是我应该回去睡觉,好好想想我在生活中试图实现什么 🙂
任何帮助将不胜感激
回答:
你的目标不太清楚。
你的代码示例有点混乱;当前显示的TaggedDocument
构造方式不会产生好的文本示例。(words
需要是一个单词列表,而不是一串用逗号分隔的标记的字符串。)
如果你向model
查询相似性,你会得到单词——如果你想要文档标签,你必须查询模型的docvecs
子属性。(也就是说,model.docvecs.most_similar()
。)
关于你的训练参数,没有好的理由将默认的min_alpha
改为与初始alpha相等。min_count=0
,保留所有单词,通常会使word2vec/doc2vec向量变差。并且该算法通常需要多次遍历数据——通常是10次或更多——而不是一次。
但同样,word2vec/doc2vec真正需要大量数据来实现其结果——玩具尺寸的测试很少能展示出在大型数据集上可能实现的相同有益特性。