-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathhelper.py
executable file
·146 lines (115 loc) · 3.77 KB
/
helper.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os, sys, pdb, numpy as np, random, argparse, codecs, pickle, time, json, queue, re
import gzip, queue, threading, scipy.sparse as sp
import logging, logging.config, itertools, pathlib
from pprint import pprint
from threading import Thread
from collections import defaultdict as ddict
np.set_printoptions(precision=4)
def set_gpu(gpus):
"""
Sets the GPU to be used for the run
Parameters
----------
gpus: List of GPUs to be used for the run
Returns
-------
"""
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = gpus
def debug_nn(res_list, feed_dict):
"""
Function for debugging Tensorflow model
Parameters
----------
res_list: List of tensors/variables to view
feed_dict: Feed dict required for getting values
Returns
-------
Returns the list of values of given tensors/variables after execution
"""
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
summ_writer = tf.summary.FileWriter("tf_board/debug_nn", sess.graph)
res = sess.run(res_list, feed_dict = feed_dict)
return res
def get_logger(name, log_dir, config_dir):
"""
Creates a logger object
Parameters
----------
name: Name of the logger file
log_dir: Directory where logger file needs to be stored
config_dir: Directory from where log_config.json needs to be read
Returns
-------
A logger object which writes to both file and stdout
"""
config_dict = json.load(open( config_dir + 'log_config.json'))
config_dict['handlers']['file_handler']['filename'] = log_dir + name.replace('/', '-')
logging.config.dictConfig(config_dict)
logger = logging.getLogger(name)
std_out_format = '%(asctime)s - [%(levelname)s] - %(message)s'
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(logging.Formatter(std_out_format))
logger.addHandler(consoleHandler)
return logger
def getChunks(inp_list, chunk_size):
"""
Splits inp_list into lists of size chunk_size
Parameters
----------
inp_list: List to be splittted
chunk_size: Size of each chunk required
Returns
-------
chunks of the inp_list each of size chunk_size, last one can be smaller (leftout data)
"""
return [inp_list[x:x+chunk_size] for x in range(0, len(inp_list), chunk_size)]
def read_mappings(fname):
"""
A helper function for reading an object to identifier mapping
Parameters
----------
fname: Name of the file containing mapping
Returns
-------
mapping: Dictionary object containing mapping information
"""
mapping = {}
for line in open(fname):
vals = line.strip().split('\t')
if len(vals) < 2: continue
mapping[vals[0]] = vals[1]
return mapping
def getEmbeddings(embed_loc, wrd_list, embed_dims):
"""
Gives embedding for each word in wrd_list
Parameters
----------
model: Word2vec model
wrd_list: List of words for which embedding is required
embed_dims: Dimension of the embedding
Returns
-------
embed_matrix: (len(wrd_list) x embed_dims) matrix containing embedding for each word in wrd_list in the same order
"""
embed_list = []
wrd2embed = {}
for line in open(embed_loc, encoding='utf-8', errors='ignore'):
data = line.strip().split(' ')
# wrd, embed = data[0], data[1:]
# Some words may be separated by space (telephone numbers, for example).
# It's more robust to load data as follows.
embed = data[-1*embed_dims: ]
wrd = ' '.join(data[: -1*embed_dims])
embed = list(map(float, embed))
wrd2embed[wrd] = embed
for wrd in wrd_list:
if wrd in wrd2embed: embed_list.append(wrd2embed[wrd])
else:
print('Word not in embeddings dump {}'.format(wrd))
embed_list.append(np.random.randn(embed_dims))
return np.array(embed_list, dtype=np.float32)