SpaCy: how to load Google news word2vec vectors?

后端 未结 4 1655
面向向阳花
面向向阳花 2020-12-25 13:48

I\'ve tried several methods of loading the google news word2vec vectors (https://code.google.com/archive/p/word2vec/):

en_nlp = spacy.load(\'en\',vector=Fals         


        
4条回答
  •  臣服心动
    2020-12-25 14:02

    I know that this question has already been answered, but I am going to offer a simpler solution. This solution will load google news vectors into a blank spacy nlp object.

    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)
    

提交回复
热议问题