-
Notifications
You must be signed in to change notification settings - Fork 6
/
init_glove.py
76 lines (62 loc) · 2.36 KB
/
init_glove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Taken from rva repo
import argparse
from tqdm import tqdm
import yaml
import numpy as np
from visdialch.data.dataset import VisDialDataset
from visdialch.data.vocabulary import Vocabulary
parser = argparse.ArgumentParser()
parser.add_argument(
"--config-yml", default="configs/rva.yml",
help="Path to a config file listing reader, model and solver parameters."
)
parser.add_argument(
"--pretrained-txt", default="data/glove.6B.300d.txt",
help="Path to GloVe pretrained word vectors."
)
parser.add_argument(
"--save-npy", default="data/glove.npy",
help="Path to save word embeddings."
)
parser.add_argument(
"--data_dir", default="data/",
help="Path to save data."
)
# ================================================================================================
# INPUT ARGUMENTS AND CONFIG
# ================================================================================================
args = parser.parse_args()
print(args.data_dir)
# keys: {"dataset", "model", "solver"}
config = yaml.load(open(args.config_yml))
# ================================================================================================
# SETUP DATASET
# ================================================================================================
config["dataset"]["glove_npy"] = "{}/{}".format(args.data_dir, config["dataset"]["glove_npy"])
config["dataset"]["word_counts_json"] = "{}/{}".format(args.data_dir, config["dataset"]["word_counts_json"])
vocabulary = Vocabulary(
config["dataset"]["word_counts_json"], min_count=config["dataset"]["vocab_min_count"]
)
def loadGloveModel(gloveFile):
print("Loading pretrained word vectors...")
with open(gloveFile,'r') as f:
model = {}
for line in f:
splitLine = line.split()
word = splitLine[0]
embedding = np.array([float(val) for val in splitLine[1:]])
model[word] = embedding
print("Done.",len(model)," words loaded!")
return model
glove = loadGloveModel(args.pretrained_txt)
vocab_size = len(vocabulary.index2word)
glove_data = np.zeros(shape=[vocab_size, 300], dtype=np.float32)
for i in range(0, vocab_size):
word = vocabulary.index2word[i]
if word in ['<PAD>', '<S>', '</S>']:
continue
if word in glove:
glove_data[i] = glove[word]
else:
glove_data[i] = glove['unk']
np.save(args.save_npy, glove_data)