forked from DongjunLee/char-rnn-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.py
36 lines (25 loc) · 864 Bytes
/
hook.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
import tensorflow as tf
def print_variables(variables, vocab=None, every_n_iter=100):
return tf.train.LoggingTensorHook(
variables,
every_n_iter=every_n_iter,
formatter=format_variable(variables, vocab=vocab))
def format_variable(keys, vocab=None):
rev_vocab = get_rev_vocab(vocab)
def to_str(sequence):
tokens = [
rev_vocab.get(x, '') for x in sequence]
return ''.join(tokens)
def format(values):
result = []
for key in keys:
if vocab is None:
result.append(f"{key} = {values[key]}")
else:
result.append(f"{key} = {to_str(values[key])}")
print('\n - '.join(result))
return format
def get_rev_vocab(vocab):
if vocab is None:
return None
return {idx: key for key, idx in vocab.items()}