-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathtrain.py
163 lines (135 loc) · 4.45 KB
/
train.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
对SequenceToSequence模型进行基本的参数组合测试
"""
import sys
import random
import pickle
import numpy as np
import tensorflow as tf
from tqdm import tqdm
# from sklearn.utils import shuffle
sys.path.append('..')
def test(params):
"""测试不同参数在生成的假数据上的运行结果"""
from sequence_to_sequence import SequenceToSequence
from data_utils import batch_flow_bucket as batch_flow
from word_sequence import WordSequence # pylint: disable=unused-variable
from threadedgenerator import ThreadedGenerator
x_data, y_data = pickle.load(open('chatbot.pkl', 'rb'))
ws = pickle.load(open('ws.pkl', 'rb'))
# 训练部分
n_epoch = 2
batch_size = 128
# x_data, y_data = shuffle(x_data, y_data, random_state=0)
# x_data = x_data[:10000]
# y_data = y_data[:10000]
steps = int(len(x_data) / batch_size) + 1
config = tf.ConfigProto(
# device_count={'CPU': 1, 'GPU': 0},
allow_soft_placement=True,
log_device_placement=False
)
save_path = './s2ss_chatbot.ckpt'
tf.reset_default_graph()
with tf.Graph().as_default():
random.seed(0)
np.random.seed(0)
tf.set_random_seed(0)
with tf.Session(config=config) as sess:
model = SequenceToSequence(
input_vocab_size=len(ws),
target_vocab_size=len(ws),
batch_size=batch_size,
**params
)
init = tf.global_variables_initializer()
sess.run(init)
# print(sess.run(model.input_layer.kernel))
# exit(1)
flow = ThreadedGenerator(
batch_flow([x_data, y_data], ws, batch_size,
add_end=[False, True]),
queue_maxsize=30)
for epoch in range(1, n_epoch + 1):
costs = []
bar = tqdm(range(steps), total=steps,
desc='epoch {}, loss=0.000000'.format(epoch))
for _ in bar:
x, xl, y, yl = next(flow)
x = np.flip(x, axis=1)
# print(x, y)
# print(xl, yl)
# exit(1)
cost, lr = model.train(sess, x, xl, y, yl, return_lr=True)
costs.append(cost)
bar.set_description('epoch {} loss={:.6f} lr={:.6f}'.format(
epoch,
np.mean(costs),
lr
))
model.save(sess, save_path)
flow.close()
# 测试部分
tf.reset_default_graph()
model_pred = SequenceToSequence(
input_vocab_size=len(ws),
target_vocab_size=len(ws),
batch_size=1,
mode='decode',
beam_width=12,
parallel_iterations=1,
**params
)
init = tf.global_variables_initializer()
with tf.Session(config=config) as sess:
sess.run(init)
model_pred.load(sess, save_path)
bar = batch_flow([x_data, y_data], ws, 1, add_end=False)
t = 0
for x, xl, y, yl in bar:
x = np.flip(x, axis=1)
pred = model_pred.predict(
sess,
np.array(x),
np.array(xl)
)
print(ws.inverse_transform(x[0]))
print(ws.inverse_transform(y[0]))
print(ws.inverse_transform(pred[0]))
t += 1
if t >= 3:
break
tf.reset_default_graph()
model_pred = SequenceToSequence(
input_vocab_size=len(ws),
target_vocab_size=len(ws),
batch_size=1,
mode='decode',
beam_width=1,
parallel_iterations=1,
**params
)
init = tf.global_variables_initializer()
with tf.Session(config=config) as sess:
sess.run(init)
model_pred.load(sess, save_path)
bar = batch_flow([x_data, y_data], ws, 1, add_end=False)
t = 0
for x, xl, y, yl in bar:
pred = model_pred.predict(
sess,
np.array(x),
np.array(xl)
)
print(ws.inverse_transform(x[0]))
print(ws.inverse_transform(y[0]))
print(ws.inverse_transform(pred[0]))
t += 1
if t >= 3:
break
def main():
"""入口程序"""
import json
test(json.load(open('params.json')))
if __name__ == '__main__':
main()