forked from qhduan/just_another_seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_compare.py
140 lines (122 loc) · 3.82 KB
/
test_compare.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
"""
对SequenceToSequence模型进行基本的参数组合测试
"""
import sys
import random
import pickle
import numpy as np
import tensorflow as tf
import jieba
sys.path.append('..')
def test(bidirectional, cell_type, depth,
attention_type, use_residual, use_dropout, time_major, hidden_units):
"""测试不同参数在生成的假数据上的运行结果"""
from sequence_to_sequence import SequenceToSequence
from data_utils import batch_flow
from word_sequence import WordSequence # pylint: disable=unused-variable
_, _, ws = pickle.load(open('chatbot.pkl', 'rb'))
# for x in x_data[:5]:
# print(' '.join(x))
config = tf.ConfigProto(
device_count={'CPU': 1, 'GPU': 0},
allow_soft_placement=True,
log_device_placement=False
)
# save_path = '/tmp/s2ss_chatbot.ckpt'
save_path = './s2ss_chatbot.ckpt'
save_path_rl = './s2ss_chatbot_anti.ckpt'
graph = tf.Graph()
graph_rl = tf.Graph()
with graph_rl.as_default():
model_rl = SequenceToSequence(
input_vocab_size=len(ws),
target_vocab_size=len(ws),
batch_size=1,
mode='decode',
beam_width=12,
bidirectional=bidirectional,
cell_type=cell_type,
depth=depth,
attention_type=attention_type,
use_residual=use_residual,
use_dropout=use_dropout,
parallel_iterations=1,
time_major=time_major,
hidden_units=hidden_units,
share_embedding=True,
pretrained_embedding=True
)
init = tf.global_variables_initializer()
sess_rl = tf.Session(config=config)
sess_rl.run(init)
model_rl.load(sess_rl, save_path_rl)
# 测试部分
with graph.as_default():
model_pred = SequenceToSequence(
input_vocab_size=len(ws),
target_vocab_size=len(ws),
batch_size=1,
mode='decode',
beam_width=12,
bidirectional=bidirectional,
cell_type=cell_type,
depth=depth,
attention_type=attention_type,
use_residual=use_residual,
use_dropout=use_dropout,
parallel_iterations=1,
time_major=time_major,
hidden_units=hidden_units,
share_embedding=True,
pretrained_embedding=True
)
init = tf.global_variables_initializer()
sess = tf.Session(config=config)
sess.run(init)
model_pred.load(sess, save_path)
while True:
user_text = input('Input Chat Sentence:')
if user_text in ('exit', 'quit'):
exit(0)
x_test = [jieba.lcut(user_text.lower())]
bar = batch_flow([x_test], [ws], 1)
x, xl = next(bar)
x = np.flip(x, axis=1)
print(x, xl)
pred = model_pred.predict(
sess,
np.array(x),
np.array(xl)
)
pred_rl = model_rl.predict(
sess_rl,
np.array(x),
np.array(xl)
)
print(ws.inverse_transform(x[0]))
print('no:', ws.inverse_transform(pred[0]))
print('rl:', ws.inverse_transform(pred_rl[0]))
p = []
for pp in ws.inverse_transform(pred_rl[0]):
if pp == WordSequence.END_TAG:
break
if pp == WordSequence.PAD_TAG:
break
p.append(pp)
def main():
"""入口程序,开始测试不同参数组合"""
random.seed(0)
np.random.seed(0)
tf.set_random_seed(0)
test(
bidirectional=True,
cell_type='lstm',
depth=2,
attention_type='Bahdanau',
use_residual=False,
use_dropout=False,
time_major=False,
hidden_units=512
)
if __name__ == '__main__':
main()