def test_case(sess, model, data, onset='VALIDATION'): """pred must be list""" def pad_list(lst, pad=-1): inner_max_len = max(map(len, lst)) map(lambda x: x.extend([pad] * (inner_max_len - len(x))), lst) return np.array(lst) print '#' * 20, 'ON ' + onset + ' SET START ', '#' * 20 loss = model.fit(sess, data) pred, true_label, lengths = model.predict(sess, data) true_label = pad_list(true_label) true_label = np.array(true_label) pred = pad_list(pred, pad=0) pred = np.array(pred) true_label = true_label.tolist() pred = pred.tolist() accuracy = helper.calculate_accuracy_seq(pred, true_label, lengths) helper.print_pred_seq(pred[:10], true_label[:10]) print 'Overall ' + onset + ' loss is: {}'.format(loss) print 'Overall ' + onset + ' accuracy is: {}'.format(accuracy) logging.info('Overall ' + onset + ' loss is: {}'.format(loss)) logging.info('Overall ' + onset + ' accuracy is: {}'.format(accuracy)) print '#' * 20, 'ON ' + onset + ' SET END ', '#' * 20 # return loss, pred, true_label, accuracy return loss, pred, true_label, lengths
def test_run(): with tf.Graph().as_default(): with tf.device("/gpu:" + str(args.gpu_num)): model = rnn_autoEncoder(test='test') saver = tf.train.Saver() config=tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True with tf.Session(config=config) as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, model.weight_Path+'/parameter.weight') _, pred = test_case(sess, model, model.test_data, onset='TEST') decode_max_len = np.max([len(i) for i in model.test_data[2]]) _, decode_label_batch = helper.encodeNpad(model.test_data[2], model.vocab, trunLen=decode_max_len) helper.print_pred_seq(pred, decode_label_batch, vocab=model.vocab)