Load Pretrained glove vectors in python

前端 未结 10 565
眼角桃花
眼角桃花 2021-01-29 22:12

I have downloaded pretrained glove vector file from the internet. It is a .txt file. I am unable to load and access it. It is easy to load and access a word vector binary file u

10条回答
  •  礼貌的吻别
    2021-01-29 22:44

    EMBEDDING_LIFE = 'path/to/your/glove.txt'
    
    def get_coefs(word,*arr): 
          return word, np.asarray(arr, dtype='float32')
    
    embeddings_index = dict(get_coefs(*o.strip().split()) for o in open(EMBEDDING_FILE))
    
    all_embs = np.stack(embeddings_index.values())
    emb_mean,emb_std = all_embs.mean(), all_embs.std()
    word_index = tokenizer.word_index
    nb_words = min(max_features, len(word_index))
    
    embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))
    
    for word, i in word_index.items():
    if i >= max_features: continue
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None: embedding_matrix[i] = embedding_vector
    

提交回复
热议问题