MCPcopy Create free account
hub / github.com/chinawithfrank/ChatBotCourse / train

Function train

chatbotv5/demo.py:162–196  ·  view source on GitHub ↗

训练过程

()

Source from the content-addressed store, hash-verified

160
161
162def train():
163 """
164 训练过程
165 """
166 # train_set = [[[5, 7, 9], [11, 13, 15, EOS_ID]], [[7, 9, 11], [13, 15, 17, EOS_ID]],
167 # [[15, 17, 19], [21, 23, 25, EOS_ID]]]
168 train_set = get_train_set()
169 with tf.Session() as sess:
170
171 encoder_inputs, decoder_inputs, target_weights, outputs, loss, update, saver, learning_rate_decay_op, learning_rate = get_model()
172
173 # 全部变量初始化
174 sess.run(tf.global_variables_initializer())
175
176 # 训练很多次迭代,每隔10次打印一次loss,可以看情况直接ctrl+c停止
177 previous_losses = []
178 for step in xrange(20000):
179 sample_encoder_inputs, sample_decoder_inputs, sample_target_weights = get_samples(train_set, 1000)
180 input_feed = {}
181 for l in xrange(input_seq_len):
182 input_feed[encoder_inputs[l].name] = sample_encoder_inputs[l]
183 for l in xrange(output_seq_len):
184 input_feed[decoder_inputs[l].name] = sample_decoder_inputs[l]
185 input_feed[target_weights[l].name] = sample_target_weights[l]
186 input_feed[decoder_inputs[output_seq_len].name] = np.zeros([len(sample_decoder_inputs[0])], dtype=np.int32)
187 [loss_ret, _] = sess.run([loss, update], input_feed)
188 if step % 10 == 0:
189 print 'step=', step, 'loss=', loss_ret, 'learning_rate=', learning_rate.eval()
190
191 if len(previous_losses) > 5 and loss_ret > max(previous_losses[-5:]):
192 sess.run(learning_rate_decay_op)
193 previous_losses.append(loss_ret)
194
195 # 模型持久化
196 saver.save(sess, './model/demo')
197
198
199def predict():

Callers 1

demo.pyFile · 0.70

Calls 4

get_train_setFunction · 0.85
get_modelFunction · 0.70
get_samplesFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected