【发布时间】:2020-12-29 18:49:12
【问题描述】:
我想要实现的目标是找到一个好的 word_and_phrase 嵌入模型,它可以做到: (1) 对于我感兴趣的单词和短语,它们有嵌入。 (2) 我可以使用嵌入来比较两个事物之间的相似性(可以是单词或短语)
到目前为止,我已经尝试了两条路径:
1:一些 Gensim 加载的预训练模型,例如:
from gensim.models.word2vec import Word2Vec
import gensim.downloader as api
# download the model and return as object ready for use
model_glove_twitter = api.load("fasttext-wiki-news-subwords-300")
model_glove_twitter.similarity('computer-science', 'machine-learning')
这条路径的问题是我不知道一个短语是否有嵌入。对于这个例子,我得到了这个错误:
KeyError: "word 'computer-science' not in vocabulary"
我将不得不尝试不同的预训练模型,例如 word2vec-google-news-300、glove-wiki-gigaword-300、glove-twitter-200 等。结果相似,总有感兴趣的短语没有嵌入。
- 然后我尝试使用一些基于BERT的句子嵌入方法:https://github.com/UKPLab/sentence-transformers。
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('distilbert-base-nli-mean-tokens')
from scipy.spatial.distance import cosine
def cosine_similarity(embedding_1, embedding_2):
# Calculate the cosine similarity of the two embeddings.
sim = 1 - cosine(embedding_1, embedding_2)
print('Cosine similarity: {:.2}'.format(sim))
phrase_1 = 'baby girl'
phrase_2 = 'annual report'
embedding_1 = model.encode(phrase_1)
embedding_2 = model.encode(phrase_2)
cosine_similarity(embedding_1[0], embedding_2[0])
使用这种方法,我能够获得词组的嵌入,但相似度得分为 0.93,这似乎不合理。
那么我还能尝试什么来实现上述两个目标?
【问题讨论】:
-
为什么这个分数不合理?通过标记化,单词将被拆分为子单词,这些子单词被引用到嵌入中,因此它将有效地解决您的一个问题。要嵌入整个句子,您可以计算每个词嵌入的平均值?可以看这篇文章engineering.talkdesk.com/…
-
因为 0.93 应该是非常相似的短语。我没有看到“女婴”和“年报”相似
-
您如何评价 0.93 不合理?我认为您应该评估某个最终任务的嵌入,而不是查看原始相似性值,无论是您想到的任务,还是仅使用嵌入器提供的嵌入来使用一些分类/匹配数据集
-
相似度比较几乎是我的最终任务。也许我不应该认为路径 2 是一个好的选择?
-
那么我建议从互联网上删除许多应该相似或不同的对(句子或单词),然后检查相似输出的分布。也许匹配对是 0.99 相似,而 0.93 实际上是有区别的
标签: nlp gensim word2vec fasttext