SpaCy:如何加载谷歌新闻word2vec向量



我尝试了几种加载谷歌新闻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"。

正确的方法是什么?

更新:

我可以将自己创建的文件加载到空间中。我使用带有"字符串 0.0 0.0 ...." 的测试.txt文件。在每一行上。然后用 .bzip2 压缩这个 txt 来测试.txt.bz2。然后我创建一个空间兼容的二进制文件:

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: 

对于 spacy 1.x,将 Google 新闻向量加载到 gensim 中并转换为新格式(.txt中的每一行都包含一个向量:字符串、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')

我知道这个问题已经得到了回答,但我将提供一个更简单的解决方案。此解决方案会将谷歌新闻向量加载到空白的空间 nlp 对象中。

import gensim
import spacy
# Path to google news vectors
google_news_path = "pathtogooglenews\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)

我正在使用spaCy v2.0.10。

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

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

我想强调的是,接受答案中的特定代码现在不起作用。我遇到了"属性错误:..."当我运行代码时。

这在 spaCy v2 中发生了变化。 write_binary_vectors已在 v2 中删除。从 spaCy 文档中,当前执行此操作的方法如下:

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

Gensim API 来下载 Google 的 word2vec 压缩模型要容易得多,它将存储在 /home/"your_username"/gensim-data/word2vec-google-news-300/ 中。加载矢量并打球。我有 16GB 的 RAM,足以处理模型

import gensim.downloader as api
model = api.load("word2vec-google-news-300")  # download the model and return as object ready for use
word_vectors = model.wv #load the vectors from the model

最新更新