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

Function predict

chatbotv5/demo.py:199–237  ·  view source on GitHub ↗

预测过程

()

Source from the content-addressed store, hash-verified

197
198
199def predict():
200 """
201 预测过程
202 """
203 with tf.Session() as sess:
204 encoder_inputs, decoder_inputs, target_weights, outputs, loss, update, saver, learning_rate_decay_op, learning_rate = get_model(feed_previous=True)
205 saver.restore(sess, './model/demo')
206 sys.stdout.write("> ")
207 sys.stdout.flush()
208 input_seq = sys.stdin.readline()
209 while input_seq:
210 input_seq = input_seq.strip()
211 input_id_list = get_id_list_from(input_seq)
212 if (len(input_id_list)):
213 sample_encoder_inputs, sample_decoder_inputs, sample_target_weights = seq_to_encoder(' '.join([str(v) for v in input_id_list]))
214
215 input_feed = {}
216 for l in xrange(input_seq_len):
217 input_feed[encoder_inputs[l].name] = sample_encoder_inputs[l]
218 for l in xrange(output_seq_len):
219 input_feed[decoder_inputs[l].name] = sample_decoder_inputs[l]
220 input_feed[target_weights[l].name] = sample_target_weights[l]
221 input_feed[decoder_inputs[output_seq_len].name] = np.zeros([2], dtype=np.int32)
222
223 # 预测输出
224 outputs_seq = sess.run(outputs, input_feed)
225 # 因为输出数据每一个是num_decoder_symbols维的,因此找到数值最大的那个就是预测的id,就是这里的argmax函数的功能
226 outputs_seq = [int(np.argmax(logit[0], axis=0)) for logit in outputs_seq]
227 # 如果是结尾符,那么后面的语句就不输出了
228 if EOS_ID in outputs_seq:
229 outputs_seq = outputs_seq[:outputs_seq.index(EOS_ID)]
230 outputs_seq = [wordToken.id2word(v) for v in outputs_seq]
231 print " ".join(outputs_seq)
232 else:
233 print "WARN:词汇不在服务区"
234
235 sys.stdout.write("> ")
236 sys.stdout.flush()
237 input_seq = sys.stdin.readline()
238
239
240if __name__ == "__main__":

Callers 1

demo.pyFile · 0.70

Calls 4

get_id_list_fromFunction · 0.85
seq_to_encoderFunction · 0.85
id2wordMethod · 0.80
get_modelFunction · 0.70

Tested by

no test coverage detected