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
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'])
You can do it much faster with pandas:
import pandas as pd
import csv
words = pd.read_table(glove_data_file, sep=" ", index_col=0, header=None, quoting=csv.QUOTE_NONE)
Then to get the vector for a word:
def vec(w):
return words.loc[w].as_matrix()
And to find the closest word to a vector:
words_matrix = words.as_matrix()
def find_closest_word(v):
diff = words_matrix - v
delta = np.sum(diff * diff, axis=1)
i = np.argmin(delta)
return words.iloc[i].name
I suggest using gensim to do everything. You can read the file, and also benefit from having a lot of methods already implemented on this great package.
Suppose you generated GloVe vectors using the C++ program and that your "-save-file" parameter is "vectors". Glove executable will generate you two files, "vectors.bin" and "vectors.txt".
Use glove2word2vec to convert GloVe vectors in text format into the word2vec text format:
from gensim.scripts.glove2word2vec import glove2word2vec
glove2word2vec(glove_input_file="vectors.txt", word2vec_output_file="gensim_glove_vectors.txt")
Finally, read the word2vec txt to a gensim model using KeyedVectors:
from gensim.models.keyedvectors import KeyedVectors
glove_model = KeyedVectors.load_word2vec_format("gensim_glove_vectors.txt", binary=False)
Now you can use gensim word2vec methods (for example, similarity) as you'd like.
I found this approach faster.
import pandas as pd
df = pd.read_csv('glove.840B.300d.txt', sep=" ", quoting=3, header=None, index_col=0)
glove = {key: val.values for key, val in df.T.items()}
Save the dictionary:
import pickle
with open('glove.840B.300d.pkl', 'wb') as fp:
pickle.dump(glove, fp)
Here's a one liner if all you want is the embedding matrix
np.loadtxt(path, usecols=range(1, dim+1), comments=None)
where path
is path to your downloaded GloVe file and dim
is the dimension of the word embedding.
If you want both the words and corresponding vectors you can do
glove = np.loadtxt(path, dtype='str', comments=None)
and seperate the words and vectors as follows
words = glove[:, 0]
vectors = glove[:, 1:].astype('float')