Gensim的FastText KeyedVector out of vocab

Gensim's FastText KeyedVector out of vocab

与完整模型相比,我想使用 Gensim 的 FastText 嵌入的只读版本来节省一些 RAM。

加载 KeyVectors 版本后,获取向量时出现以下错误:

IndexError: index 878080 is out of bounds for axis 0 with size 761210

错误发生在使用不属于词汇的单词时,例如"lawyerxy" 而不是 "lawyer"。完整模型 returns 两者的向量。

from gensim.models import KeyedVectors
model = KeyedVectors.load("model.kv")
model .wv.__getitem__("lawyerxy")

因此,我的假设是 KeyedVectors 不提供 FastText 的空缺功能——我的用例的一个关键特性。文档中没有给出此限制: https://radimrehurek.com/gensim/models/word2vec.html

任何人都可以证明假设 and/or 命名一个修复程序以允许 "lawyerxy" 等的向量吗?

KeyedVectors 名称(从 gensim-3.8.0 开始)只是 class Word2VecKeyedVectorsalias,它只维护一个简单的词(作为键) 到矢量(作为值)映射。

您不应该期望 FastText 的高级能力能够为词汇外的单词合成向量,出现在任何没有明确声称提供该能力的 model/representation 中。

(我希望查找一个词汇外的单词可以提供更清晰的 KeyError 而不是您报告的 IndexError。但是,您需要准确显示是什么代码创建了您正在加载的文件,并触发了错误和完整的错误堆栈,以进一步猜测您的情况出了什么问题。)

根据您的 model.kv 文件的保存方式,您可以通过使用 class FastTextKeyedVectors 而不是普通 KeyedVectors