forked from DongjunLee/conversation-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.py
40 lines (29 loc) · 981 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
37
38
39
40
from hbconfig import Config
import numpy as np
import tensorflow as tf
def print_variables(variables, rev_vocab=None, every_n_iter=100):
return tf.train.LoggingTensorHook(
variables,
every_n_iter=every_n_iter,
formatter=format_variable(variables, rev_vocab=rev_vocab))
def format_variable(keys, rev_vocab=None):
def to_str(sequence):
if type(sequence) == np.ndarray:
tokens = [
rev_vocab.get(x, '') for x in sequence if x != Config.data.PAD_ID]
return ' '.join(tokens)
else:
x = int(sequence)
return rev_vocab[x]
def format(values):
result = []
for key in keys:
if rev_vocab is None:
result.append(f"{key} = {values[key]}")
else:
result.append(f"{key} = {to_str(values[key])}")
try:
return '\n - '.join(result)
except:
pass
return format