Last active
June 13, 2021 06:53
-
-
Save BrambleXu/634a844cdd3cd04bb2e3ba3c83aef227 to your computer and use it in GitHub Desktop.
load glove and show the progress, finally save to numpy file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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