预测过程
()
| 124 | |
| 125 | |
| 126 | def predict(): |
| 127 | """ |
| 128 | 预测过程 |
| 129 | """ |
| 130 | with tf.Session() as sess: |
| 131 | sample_encoder_inputs, sample_decoder_inputs, sample_target_weights = get_samples() |
| 132 | encoder_inputs, decoder_inputs, target_weights, outputs, loss, update, saver = get_model(feed_previous=True) |
| 133 | # 从文件恢复模型 |
| 134 | saver.restore(sess, './model/demo') |
| 135 | |
| 136 | input_feed = {} |
| 137 | for l in xrange(input_seq_len): |
| 138 | input_feed[encoder_inputs[l].name] = sample_encoder_inputs[l] |
| 139 | for l in xrange(output_seq_len): |
| 140 | input_feed[decoder_inputs[l].name] = sample_decoder_inputs[l] |
| 141 | input_feed[target_weights[l].name] = sample_target_weights[l] |
| 142 | input_feed[decoder_inputs[output_seq_len].name] = np.zeros([2], dtype=np.int32) |
| 143 | |
| 144 | # 预测输出 |
| 145 | outputs = sess.run(outputs, input_feed) |
| 146 | # 一共试验样本有2个,所以分别遍历 |
| 147 | for sample_index in xrange(2): |
| 148 | # 因为输出数据每一个是num_decoder_symbols维的,因此找到数值最大的那个就是预测的id,就是这里的argmax函数的功能 |
| 149 | outputs_seq = [int(np.argmax(logit[sample_index], axis=0)) for logit in outputs] |
| 150 | # 如果是结尾符,那么后面的语句就不输出了 |
| 151 | if EOS_ID in outputs_seq: |
| 152 | outputs_seq = outputs_seq[:outputs_seq.index(EOS_ID)] |
| 153 | outputs_seq = [str(v) for v in outputs_seq] |
| 154 | print " ".join(outputs_seq) |
| 155 | |
| 156 | |
| 157 | if __name__ == "__main__": |
no test coverage detected