Python 训练过的word2vec模型词汇表中缺少单词

Python 训练过的word2vec模型词汇表中缺少单词,python,tensorflow,nltk,gensim,word2vec,Python,Tensorflow,Nltk,Gensim,Word2vec,我目前正在使用python,使用我提供的句子训练Word2Vec模型。然后,我保存并加载模型,以获得用于训练模型的句子中每个单词的单词嵌入。但是,我得到了以下错误 KeyError:“词汇表中没有“n1985芝加哥熊”一词” 鉴于,培训期间提供的其中一句话如下 sportsteam n1985_chicago_bears teamplaysincity city chicago 因此,我想知道为什么有些单词会从词汇表中消失,尽管这些单词是从句子语料库中训练出来的 在自己的语料库上训练word2

我目前正在使用python,使用我提供的句子训练Word2Vec模型。然后,我保存并加载模型,以获得用于训练模型的句子中每个单词的单词嵌入。但是,我得到了以下错误

KeyError:“词汇表中没有“n1985芝加哥熊”一词”

鉴于,培训期间提供的其中一句话如下

sportsteam n1985_chicago_bears teamplaysincity city chicago
因此,我想知道为什么有些单词会从词汇表中消失,尽管这些单词是从句子语料库中训练出来的

在自己的语料库上训练word2vec模型

import nltk
import numpy as np
from termcolor import colored
from gensim.models import Word2Vec
from gensim.models import KeyedVectors
from sklearn.decomposition import PCA


#PREPARING DATA

fname = '../data/sentences.txt'

with open(fname) as f:
    content = f.readlines()

# remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]


#TOKENIZING SENTENCES

sentences = []

for x in content:
    nltk_tokens = nltk.word_tokenize(x)
    sentences.append(nltk_tokens)

#TRAINING THE WORD2VEC MODEL

model = Word2Vec(sentences)

words = list(model.wv.vocab)
model.wv.save_word2vec_format('model.bin')
句子.txt中的示例句子

sportsteam hawks teamplaysincity city atlanta
stadiumoreventvenue honda_center stadiumlocatedincity city anaheim
sportsteam ducks teamplaysincity city anaheim
sportsteam n1985_chicago_bears teamplaysincity city chicago
stadiumoreventvenue philips_arena stadiumlocatedincity city atlanta
stadiumoreventvenue united_center stadiumlocatedincity city chicago
...
句子.txt
文件中有1860行这样的行,每行正好包含5个单词,没有停止词

保存模型后,我尝试从与保存的
model.bin
相同目录中的不同python文件加载它,如下所示

加载保存的模型.bin

import nltk
import numpy as np
from gensim import models

w = models.KeyedVectors.load_word2vec_format('model.bin', binary=True)
print(w['n1985_chicago_bears'])
但是,我最终出现了以下错误

KeyError: "word 'n1985_chicago_bears' not in vocabulary"
是否有一种方法可以使用相同的方法为训练过的句子语料库中的每个单词嵌入单词


如果您在这方面有任何建议,我们将不胜感激。

gensim的Word2Vec实现的默认
min\u count=5
,看起来您正在寻找的标记
n1985\u chicago\u bears
在您的语料库中出现的次数少于5次。适当更改您的最小计数

类gensim.models.word2vec.word2vec(句子=无, 语料库文件=None,大小=100,alpha=0.025,窗口=5,最小计数=5, 最大尺寸=无,样本=0.001,种子=1,工人=3, 最小α=0.0001,sg=0,hs=0,负=5,ns指数=0.75, cbow_mean=1,hashfxn=,iter=5,null_word=0, trim\u rule=None,sorted\u vocab=1,batch\u words=10000,compute\u loss=False, 回调=(),最大值(最终值=无)


但是,请注意,
min_count=5
的存在是有充分理由的:很少出现的单词往往无法从其少量的训练示例中获得非常好的单词向量,并且进一步给定典型的单词分布(其中有许多这样的“长尾”很少出现的单词),可能会有很多这样的单词,如果保留,则会使其他单词的单词向量更糟糕。由于示例太少且种类不够多样,它们本质上是“噪音”。因此,至少在通常的自然语言语料库中,丢弃稀有词可以改善word2vec对更重要词的派生结果。谢谢。这就像魔术一样,因为我目前正试图让一个基本的系统工作,所以我得到了所有术语的嵌入(即使是那些至少出现一次的术语)。但我将对此进行调整,使这些术语的嵌入向量更有意义。再次感谢。正如@gojomo所提到的,不建议将
min\u count=1
。解决这个问题的一种方法是用“UNK”替换所有非常低频的单词,并使用UNK的向量表示法来表示缺失的单词。
content = [
    "sportsteam hawks teamplaysincity city atlanta",
    "stadiumoreventvenue honda_center stadiumlocatedincity city anaheim",
    "sportsteam ducks teamplaysincity city anaheim",
    "sportsteam n1985_chicago_bears teamplaysincity city chicago",
    "stadiumoreventvenue philips_arena stadiumlocatedincity city atlanta",
    "stadiumoreventvenue united_center stadiumlocatedincity city chicago"
]

sentences = []

for x in content:
    nltk_tokens = nltk.word_tokenize(x)
    sentences.append(nltk_tokens)

model = Word2Vec(sentences, min_count=1)
print (model['n1985_chicago_bears'])