Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Pretrained glove vectors in python

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 using gensim but I don't know how to do it when it is a text file format.

Thanks in advance

like image 870
Same Avatar asked Jun 13 '16 15:06

Same


People also ask

How do you use Pretrained GloVe vectors?

To load the pre-trained vectors, we must first create a dictionary that will hold the mappings between words, and the embedding vectors of those words. Assuming that your Python file is in the same directory as the GloVe vectors, we can now open the text file containing the embeddings with: with open("glove.


1 Answers

glove model files are in a word - vector format. You can open the textfile to verify this. Here is a small snippet of code you can use to load a pretrained glove file:

import numpy as np  def load_glove_model(File):     print("Loading Glove Model")     glove_model = {}     with open(File,'r') as f:         for line in f:             split_line = line.split()             word = split_line[0]             embedding = np.array(split_line[1:], dtype=np.float64)             glove_model[word] = embedding     print(f"{len(glove_model)} words loaded!")     return glove_model 

You can then access the word vectors by simply using the gloveModel variable.

print(gloveModel['hello'])

like image 148
Karishma Malkan Avatar answered Oct 06 '22 11:10

Karishma Malkan