Skip to content

Instantly share code, notes, and snippets.

@stjordanis
Forked from WillKoehrsen/load_word_embeddings.py
Created November 18, 2018 04:18
Show Gist options
  • Save stjordanis/82fb4da4d2a3db3ff7577a8a726a442e to your computer and use it in GitHub Desktop.
Save stjordanis/82fb4da4d2a3db3ff7577a8a726a442e to your computer and use it in GitHub Desktop.
# Load in embeddings
glove_vectors = '/home/ubuntu/.keras/datasets/glove.6B.100d.txt'
glove = np.loadtxt(glove_vectors, dtype='str', comments=None)
# Extract the vectors and words
vectors = glove[:, 1:].astype('float')
words = glove[:, 0]
# Create lookup of words to vectors
word_lookup = {word: vector for word, vector in zip(words, vectors)}
# New matrix to hold word embeddings
embedding_matrix = np.zeros((num_words, vectors.shape[1]))
for i, word in enumerate(word_idx.keys()):
# Look up the word embedding
vector = word_lookup.get(word, None)
# Record in matrix
if vector is not None:
embedding_matrix[i + 1, :] = vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment