-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
43 lines (33 loc) · 965 Bytes
/
utils.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
import html
import tensorflow as tf
def find_trainable_variables(key):
return tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, ".*{}.*".format(key))
def preprocess(text, front_pad='\n ', end_pad=' '):
text = html.unescape(text)
text = text.replace('\n', ' ').strip()
text = front_pad+text+end_pad
text = text.encode()
return text
def iter_data(*data, **kwargs):
size = kwargs.get('size', 128)
try:
n = len(data[0])
except:
n = data[0].shape[0]
batches = n // size
if n % size != 0:
batches += 1
for b in range(batches):
start = b * size
end = (b + 1) * size
if end > n:
end = n
if len(data) == 1:
yield data[0][start:end]
else:
yield tuple([d[start:end] for d in data])
class HParams(object):
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)