Skip to content

Instantly share code, notes, and snippets.

@WillKoehrsen
Created November 5, 2018 00:43
Show Gist options
  • Save WillKoehrsen/93f2643f9596916b72888796cf2710e9 to your computer and use it in GitHub Desktop.
Save WillKoehrsen/93f2643f9596916b72888796cf2710e9 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