我想创建一个文本文件,基本上是一个字典,每个单词都与其通过word2vec训练得到的向量表示相对应。我假设这个过程是先训练word2vec,然后从我的列表中查找每个词并找到它的表示(然后保存在新的文本文件中)?
我对word2vec还很陌生,不知道该如何实现。我已经阅读了一些主要网站和Stack上的几个问题,但还没有找到好的教程。
我想创建一个文本文件,基本上是一个字典,每个单词都与其通过word2vec训练得到的向量表示相对应。我假设这个过程是先训练word2vec,然后从我的列表中查找每个词并找到它的表示(然后保存在新的文本文件中)?
我对word2vec还很陌生,不知道该如何实现。我已经阅读了一些主要网站和Stack上的几个问题,但还没有找到好的教程。
直接访问的model[word]
模型已经被弃用,并将在Gensim 4.0.0中移除,以便分离训练和嵌入。该命令应替换为简单的model.wv[word]
。
在Python中使用Gensim,在构建词汇表并完成模型训练后,您可以在model.wv.vocab
中找到单词计数和采样信息,其中model
是您的Word2Vec
对象的变量名。
因此,要创建字典对象,您可以执行以下操作:
my_dict = dict({})
for idx, key in enumerate(model.wv.vocab):
my_dict[key] = model.wv[key]
# Or my_dict[key] = model.wv.get_vector(key)
# Or my_dict[key] = model.wv.word_vec(key, use_norm=False)
现在你已经有了自己的字典,可以使用自己喜欢的任何方式将其写入文件。例如,您可以使用pickle库。或者,如果您正在使用Jupyter Notebook,它们有一个便捷的“魔法命令”%store my_dict > filename.txt
。您的filename.txt将如下所示:
{'one': array([-0.06590105, 0.01573388, 0.00682817, 0.53970253, -0.20303348,
-0.24792041, 0.08682659, -0.45504045, 0.89248925, 0.0655603 ,
......
-0.8175681 , 0.27659689, 0.22305458, 0.39095637, 0.43375066,
0.36215973, 0.4040089 , -0.72396156, 0.3385369 , -0.600869 ],
dtype=float32),
'two': array([ 0.04694849, 0.13303463, -0.12208422, 0.02010536, 0.05969441,
-0.04734801, -0.08465996, 0.10344813, 0.03990637, 0.07126121,
......
0.31673026, 0.22282903, -0.18084198, -0.07555179, 0.22873943,
-0.72985399, -0.05103955, -0.10911274, -0.27275378, 0.01439812],
dtype=float32),
'three': array([-0.21048863, 0.4945509 , -0.15050395, -0.29089224, -0.29454648,
0.3420335 , -0.3419629 , 0.87303966, 0.21656844, -0.07530259,
......
-0.80034876, 0.02006451, 0.5299498 , -0.6286509 , -0.6182588 ,
-1.0569025 , 0.4557548 , 0.4697938 , 0.8928275 , -0.7877308 ],
dtype=float32),
'four': ......
}
model.wv.get_vector()
和model.wv.word_vec()
有什么区别? - E.K.words
到底是指什么。它是指 model.wv.vocab
的词汇表还是来自你的语料库中的单词? - Mitali Cyruslist(model.wv.vocab.keys())
。 - TrickOrTreatmodel = Word2Vec(sentences, size=100, window=5, min_count=5, workers=4)
model.wv.vectors
并通过单词
model.wv.vocab.keys()
vectors
不对应于通过取 keys
获得的单词。也就是说,顺序不同,即使键已排序。 - Mitali Cyrusmodel.wv[model.wv.vocab.keys()]
可以按键的顺序给出向量。 - Mitali Cyrus如果您想使用带有gensim包的Python,那么可以基于这个答案和Gensim Word2Vec文档来进行操作,如下所示:
from gensim.models import Word2Vec
# Take some sample sentences
tokenized_sentences = [["here","is","one"],["and","here","is","another"]]
# Initialise model, for more information, please check the Gensim Word2vec documentation
model = Word2Vec(tokenized_sentences, size=100, window=2, min_count=0)
# Get the ordered list of words in the vocabulary
words = model.wv.vocab.keys()
# Make a dictionary
we_dict = {word:model.wv[word] for word in words}
and another here is one
。有没有一种方法可以保留句子的顺序? - spectreimport collections
并定义we_dict = collections.OrderedDict()
。只需记住使用循环而不是字典推导来保存结果即可。希望这有所帮助。 - Mitali Cyruswords = list(w for w in model.wv.index_to_key)
print(model.wv['also'])
words = ["apple", "machine", "learning]
word_vectors = model.wv.vectors_for_all(words)
对于 gensim 4.0:
my_dict = dict({})
for word in word_list:
my_dict[word] = model.wv.get_vector('0', norm = True)
使用基本的Python:
all_vectors = []
for index, vector in enumerate(model.wv.vectors):
vector_object = {}
vector_object[list(model.wv.vocab.keys())[index]] = vector
all_vectors.append(vector_object)