Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BrambleXu
Last active June 13, 2021 06:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrambleXu/634a844cdd3cd04bb2e3ba3c83aef227 to your computer and use it in GitHub Desktop.
Save BrambleXu/634a844cdd3cd04bb2e3ba3c83aef227 to your computer and use it in GitHub Desktop.
load glove and show the progress, finally save to numpy file
import numpy as np
from tqdm import tqdm
def load_glove(file):
"""Loads GloVe vectors in numpy array.
Args:
file (str): a path to a glove file.
Return:
dict: a dict of numpy arrays.
"""
embeddings_index = {}
with open(file, encoding='utf8') as f:
for i, line in tqdm(enumerate(f)):
values = line.split()
word = ''.join(values[:-300])
coefs = np.asarray(values[-300:], dtype='float32')
embeddings_index[word] = coefs
return embeddings_index
# EMBEDDING_PATH = '../embedding_weights/glove.840B.300d.txt'
EMBEDDING_PATH = 'glove.840B.300d.txt'
embeddings = load_glove(EMBEDDING_PATH)
np.save('glove_embeddings.npy', embeddings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment