斯帕西:如何加载谷歌新闻word2vec矢量?

2024-05-13 23:10:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了几种加载google news word2vec向量(https://code.google.com/archive/p/word2vec/)的方法:

en_nlp = spacy.load('en',vector=False)
en_nlp.vocab.load_vectors_from_bin_loc('GoogleNews-vectors-negative300.bin')

以上给出:

MemoryError: Error assigning 18446744072820359357 bytes

我也尝试过使用.gz压缩向量;或者使用gensim将它们加载并保存为新格式:

from gensim.models.word2vec import Word2Vec
model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
model.save_word2vec_format('googlenews2.txt')

然后,该文件在每行包含单词及其单词向量。 我试着给他们装上:

en_nlp.vocab.load_vectors('googlenews2.txt')

但它返回“0”。

正确的方法是什么?

更新:

我可以将自己创建的文件加载到spacy中。 我使用一个test.txt文件,每行上都有“string 0.0 0.0….”。然后用.bzip2将这个txt压缩到test.txt.bz2。 然后创建一个与spacy兼容的二进制文件:

spacy.vocab.write_binary_vectors('test.txt.bz2', 'test.bin')

我可以把它装进太空:

nlp.vocab.load_vectors_from_bin_loc('test.bin')

这能行! 但是,当我对googlenews2.txt执行相同的过程时,会得到以下错误:

lib/python3.6/site-packages/spacy/cfile.pyx in spacy.cfile.CFile.read_into (spacy/cfile.cpp:1279)()

OSError: 

Tags: 文件fromtesttxtbinnlpspacyload
3条回答

我知道这个问题已经得到了回答,但我要提出一个更简单的解决办法。此解决方案将把google新闻向量加载到一个空白的spacy nlp对象中。

import gensim
import spacy

# Path to google news vectors
google_news_path = "path\to\google\news\\GoogleNews-vectors-negative300.bin.gz"

# Load google news vecs in gensim
model = gensim.models.KeyedVectors.load_word2vec_format(gn_path, binary=True)

# Init blank english spacy nlp object
nlp = spacy.blank('en')

# Loop through range of all indexes, get words associated with each index.
# The words in the keys list will correspond to the order of the google embed matrix
keys = []
for idx in range(3000000):
    keys.append(model.index2word[idx])

# Set the vectors for our nlp object to the google news vectors
nlp.vocab.vectors = spacy.vocab.Vectors(data=model.syn0, keys=keys)

>>> nlp.vocab.vectors.shape
(3000000, 300)

我用的是Spacyv2.0.10。

Create a SpaCy compatible binary file:

spacy.vocab.write_binary_vectors('googlenews.txt.bz2','googlenews.bin')

我想强调的是,接受的答案中的特定代码现在不起作用。我在运行代码时遇到了“attributerror:…”。

这在spaCy v2中已经改变。write_binary_vectors在v2中被删除。从spaCy documentations开始,当前的方法如下:

$ python -m spacy init-model en /path/to/output -v /path/to/vectors.bin.tar.gz

对于spacy 1.x,将Google news vectors加载到gensim并转换为新格式(txt中的每一行都包含一个向量:string,vec):

from gensim.models.word2vec import Word2Vec
from gensim.models import KeyedVectors
model = KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
model.wv.save_word2vec_format('googlenews.txt')

删除.txt的第一行:

tail -n +2 googlenews.txt > googlenews.new && mv -f googlenews.new googlenews.txt

将txt压缩为.bz2:

bzip2 googlenews.txt

创建与SpaCy兼容的二进制文件:

spacy.vocab.write_binary_vectors('googlenews.txt.bz2','googlenews.bin')

将googlenews.bin移到python环境的/lib/python/site packages/spacy/data/en-google-1.0.0/vocab/googlenews.bin。

然后加载字向量:

import spacy
nlp = spacy.load('en',vectors='en_google')

或稍后加载:

nlp.vocab.load_vectors_from_bin_loc('googlenews.bin')

相关问题 更多 >