-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtrain_anti.py
156 lines (129 loc) · 4.44 KB
/
train_anti.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
import random
import pickle
import numpy as np
import tensorflow as tf
from tqdm import tqdm
from sequence_to_sequence import SequenceToSequence
from data_utils import batch_flow_bucket as batch_flow
from word_sequence import WordSequence
from threadedgenerator import ThreadedGenerator
import os
import json
def test(params):
x_data, y_data = pickle.load(open('./data/chatbot.pkl', 'rb'))
ws = pickle.load(open('./data/ws.pkl', 'rb'))
n_epoch = 2
batch_size=128
steps = int(len(x_data) / batch_size) +1
config = tf.ConfigProto(
allow_soft_placement=True,
log_device_placement=False
)
save_path = 'model/s2s_chatbot_anti.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)
flow = ThreadedGenerator(
batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]),
queue_maxsize=30
)
dummy_encoder_inputs = np.array([
np.array([WordSequence.PAD]) for _ in range(batch_size)
])
dummy_encoder_inputs_length = np.array([1] * batch_size)
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)
add_loss = model.train(
sess,
dummy_encoder_inputs,
dummy_encoder_inputs_length,
y, yl, loss_only=True
)
add_loss *= -0.5
cost, lr = model.train(sess, x, xl, y, yl,
return_lr=True,
add_loss=add_loss
)
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,
**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,
**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
def main():
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
test(json.load(open('params.json')))
if __name__ == '__main__':
main()