首页 文章

SpaCy:如何加载Google新闻word2vec向量?

提问于
浏览
13

我已经尝试了几种加载谷歌新闻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” .

这样做的正确方法是什么?

Update:

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

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

我可以加载到spacy:

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:

4 回答

  • 0

    使用gensim api更容易通过谷歌下载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
    
  • 2

    我正在使用spaCy v2.0.10 .

    创建SpaCy兼容的二进制文件:spacy.vocab.write_binary_vectors('googlenews.txt.bz2','googlenews.bin')

    我想强调一下,接受的答案中的具体代码现在不起作用 . 我运行代码时遇到“AttributeError:...” .

    这在spaCy v2中发生了变化 . 在v2中删除了 write_binary_vectors . 从spaCy documentations开始,目前的方法如下:

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

    对于spacy 1.x,将Google新闻向量加载到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 .

    然后加载wordvectors:

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

    或者在以后加载它们:

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

    我知道这个问题已经得到解答,但我将提供一个更简单的解决方案 . 此解决方案将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)
    

相关问题