-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
79 lines (60 loc) · 1.75 KB
/
Copy pathutils.py
File metadata and controls
79 lines (60 loc) · 1.75 KB
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
import tensorflow as tf
import numpy as np
import string
import os
META = ['<PAD>', '<START>', '<END>']
UNK = ['<UNK>']
def mkdir(path):
try:
os.mkdir(path)
except FileExistsError:
...
def make_vocab_passwords(num_oov_buckets=1, is_username=False):
if is_username:
C = list(string.printable[:36])
else:
C = list(string.printable[:-5])
D = META + C
vocab_size = len(D) + num_oov_buckets
init = tf.lookup.KeyValueTensorInitializer(
keys=tf.constant(D),
values=tf.constant(np.arange(len(D)), dtype=tf.int64)
)
char_id_table = tf.lookup.StaticVocabularyTable(
init,
num_oov_buckets=num_oov_buckets
)
D += [UNK]
return char_id_table, D
def make_vocab(path, num_oov_buckets=1):
def read_vocab(path):
with open(path, 'r') as f:
vocab = [l[:-1] for l in f]
return vocab
D = read_vocab(path)
vocab_size = len(D) + num_oov_buckets
init = tf.lookup.KeyValueTensorInitializer(
keys=tf.constant(D),
values=tf.constant(np.arange(len(D)), dtype=tf.int64)
)
id_table = tf.lookup.StaticVocabularyTable(
init,
num_oov_buckets=num_oov_buckets
)
D += [UNK]
return id_table, D
def print_model_size(model):
model_mem_footprint = (model.count_params() * 4) // (10 ** 6)
print("MODEL_MEM: %dMB" % model_mem_footprint)
def makeNameTest(name, score):
if name:
return '%s_%s' % (score, name)
else:
return score
def flush_metric(iteration, metric, tfb_log=False):
value = metric.result()
if tfb_log:
name = metric.name
tf.summary.scalar(name, value, step=iteration)
metric.reset_states()
return value.numpy()