From d5be1d4dd0cdcc7eb37f9f6a98cf1290bf4bd786 Mon Sep 17 00:00:00 2001 From: fengjiayi Date: Mon, 20 Nov 2017 20:25:16 +0800 Subject: [PATCH] use Evaluator in book tests (#5778) --- .../book/test_image_classification_train.py | 11 ++++++++--- .../tests/book/test_recognize_digits_conv.py | 8 ++++---- .../tests/book/test_recognize_digits_mlp.py | 15 +++++++++++---- .../book/test_understand_sentiment_conv.py | 18 +++++++++++------- .../test_understand_sentiment_dynamic_lstm.py | 18 +++++++++++------- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/python/paddle/v2/fluid/tests/book/test_image_classification_train.py b/python/paddle/v2/fluid/tests/book/test_image_classification_train.py index b8506125501..efe63a68f07 100644 --- a/python/paddle/v2/fluid/tests/book/test_image_classification_train.py +++ b/python/paddle/v2/fluid/tests/book/test_image_classification_train.py @@ -4,6 +4,7 @@ import paddle.v2.fluid.core as core import paddle.v2.fluid.framework as framework import paddle.v2.fluid.layers as layers import paddle.v2.fluid.nets as nets +import paddle.v2.fluid.evaluator as evaluator from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.initializer import XavierInitializer from paddle.v2.fluid.optimizer import AdamOptimizer @@ -103,12 +104,13 @@ net = vgg16_bn_drop(images) predict = layers.fc(input=net, size=classdim, act='softmax') cost = layers.cross_entropy(input=predict, label=label) avg_cost = layers.mean(x=cost) -accuracy = layers.accuracy(input=predict, label=label) # optimizer = SGDOptimizer(learning_rate=0.001) optimizer = AdamOptimizer(learning_rate=0.001) opts = optimizer.minimize(avg_cost) +accuracy, acc_out = evaluator.accuracy(input=predict, label=label) + BATCH_SIZE = 128 PASS_NUM = 1 @@ -124,6 +126,7 @@ exe.run(framework.default_startup_program()) for pass_id in range(PASS_NUM): batch_id = 0 + accuracy.reset(exe) for data in train_reader(): img_data = np.array(map(lambda x: x[0].reshape(data_shape), data)).astype("float32") @@ -141,12 +144,14 @@ for pass_id in range(PASS_NUM): outs = exe.run(framework.default_main_program(), feed={"pixel": tensor_img, "label": tensor_y}, - fetch_list=[avg_cost, accuracy]) + fetch_list=[avg_cost, acc_out]) loss = np.array(outs[0]) acc = np.array(outs[1]) + pass_acc = accuracy.eval(exe) print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) + - " loss:" + str(loss) + " acc:" + str(acc)) + " loss:" + str(loss) + " acc:" + str(acc) + " pass_acc:" + str( + pass_acc)) batch_id = batch_id + 1 if batch_id > 1: diff --git a/python/paddle/v2/fluid/tests/book/test_recognize_digits_conv.py b/python/paddle/v2/fluid/tests/book/test_recognize_digits_conv.py index 75fbaf83e8f..8f737689609 100644 --- a/python/paddle/v2/fluid/tests/book/test_recognize_digits_conv.py +++ b/python/paddle/v2/fluid/tests/book/test_recognize_digits_conv.py @@ -46,7 +46,6 @@ exe = Executor(place) exe.run(framework.default_startup_program()) for pass_id in range(PASS_NUM): - count = 0 accuracy.reset(exe) for data in train_reader(): img_data = np.array(map(lambda x: x[0].reshape([1, 28, 28]), @@ -66,13 +65,14 @@ for pass_id in range(PASS_NUM): loss = np.array(outs[0]) acc = np.array(outs[1]) pass_acc = accuracy.eval(exe) - print "pass id : ", pass_id, pass_acc + print("pass_id=" + str(pass_id) + " acc=" + str(acc) + " pass_acc=" + + str(pass_acc)) # print loss, acc - if loss < 10.0 and acc > 0.9: + if loss < 10.0 and pass_acc > 0.9: # if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good. exit(0) pass_acc = accuracy.eval(exe) - print "pass id : ", pass_id, pass_acc + print("pass_id=" + str(pass_id) + " pass_acc=" + str(pass_acc)) exit(1) diff --git a/python/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py b/python/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py index cf10b1942e6..e42e4c9cc00 100644 --- a/python/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py +++ b/python/paddle/v2/fluid/tests/book/test_recognize_digits_mlp.py @@ -3,6 +3,7 @@ import paddle.v2 as paddle import paddle.v2.fluid.core as core import paddle.v2.fluid.framework as framework import paddle.v2.fluid.layers as layers +import paddle.v2.fluid.evaluator as evaluator from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.initializer import UniformInitializer from paddle.v2.fluid.optimizer import MomentumOptimizer @@ -30,11 +31,12 @@ label = layers.data(name='y', shape=[1], data_type='int64') cost = layers.cross_entropy(input=predict, label=label) avg_cost = layers.mean(x=cost) -accuracy = layers.accuracy(input=predict, label=label) optimizer = MomentumOptimizer(learning_rate=0.001, momentum=0.9) opts = optimizer.minimize(avg_cost) +accuracy, acc_out = evaluator.accuracy(input=predict, label=label) + train_reader = paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=8192), @@ -47,6 +49,7 @@ exe.run(framework.default_startup_program()) PASS_NUM = 100 for pass_id in range(PASS_NUM): + accuracy.reset(exe) for data in train_reader(): x_data = np.array(map(lambda x: x[0], data)).astype("float32") y_data = np.array(map(lambda x: x[1], data)).astype("int64") @@ -61,9 +64,13 @@ for pass_id in range(PASS_NUM): outs = exe.run(framework.default_main_program(), feed={'x': tensor_x, 'y': tensor_y}, - fetch_list=[avg_cost, accuracy]) + fetch_list=[avg_cost, acc_out]) out = np.array(outs[0]) acc = np.array(outs[1]) - if out[0] < 5.0: - exit(0) # if avg cost less than 5.0, we think our code is good. + pass_acc = accuracy.eval(exe) + + if pass_acc > 0.7: + exit(0) + # print("pass_id=" + str(pass_id) + " auc=" + + # str(acc) + " pass_acc=" + str(pass_acc)) exit(1) diff --git a/python/paddle/v2/fluid/tests/book/test_understand_sentiment_conv.py b/python/paddle/v2/fluid/tests/book/test_understand_sentiment_conv.py index e69b915a9cf..4929f7cf615 100644 --- a/python/paddle/v2/fluid/tests/book/test_understand_sentiment_conv.py +++ b/python/paddle/v2/fluid/tests/book/test_understand_sentiment_conv.py @@ -1,6 +1,7 @@ import numpy as np import paddle.v2 as paddle import paddle.v2.fluid.core as core +import paddle.v2.fluid.evaluator as evaluator import paddle.v2.fluid.framework as framework import paddle.v2.fluid.layers as layers import paddle.v2.fluid.nets as nets @@ -32,8 +33,8 @@ def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32): avg_cost = layers.mean(x=cost) adam_optimizer = AdamOptimizer(learning_rate=0.002) opts = adam_optimizer.minimize(avg_cost) - acc = layers.accuracy(input=prediction, label=label) - return avg_cost, acc + accuracy, acc_out = evaluator.accuracy(input=prediction, label=label) + return avg_cost, accuracy, acc_out def to_lodtensor(data, place): @@ -59,7 +60,8 @@ def main(): dict_dim = len(word_dict) class_dim = 2 - cost, acc = convolution_net(input_dim=dict_dim, class_dim=class_dim) + cost, accuracy, acc_out = convolution_net( + input_dim=dict_dim, class_dim=class_dim) train_data = paddle.batch( paddle.reader.shuffle( @@ -71,6 +73,7 @@ def main(): exe.run(framework.default_startup_program()) for pass_id in xrange(PASS_NUM): + accuracy.reset(exe) for data in train_data(): tensor_words = to_lodtensor(map(lambda x: x[0], data), place) @@ -83,12 +86,13 @@ def main(): outs = exe.run(framework.default_main_program(), feed={"words": tensor_words, "label": tensor_label}, - fetch_list=[cost, acc]) + fetch_list=[cost, acc_out]) cost_val = np.array(outs[0]) acc_val = np.array(outs[1]) - - print("cost=" + str(cost_val) + " acc=" + str(acc_val)) - if cost_val < 1.0 and acc_val > 0.7: + pass_acc = accuracy.eval(exe) + print("cost=" + str(cost_val) + " acc=" + str(acc_val) + + " pass_acc=" + str(pass_acc)) + if cost_val < 1.0 and pass_acc > 0.8: exit(0) exit(1) diff --git a/python/paddle/v2/fluid/tests/book/test_understand_sentiment_dynamic_lstm.py b/python/paddle/v2/fluid/tests/book/test_understand_sentiment_dynamic_lstm.py index 65d44542501..b3ee9193886 100644 --- a/python/paddle/v2/fluid/tests/book/test_understand_sentiment_dynamic_lstm.py +++ b/python/paddle/v2/fluid/tests/book/test_understand_sentiment_dynamic_lstm.py @@ -1,6 +1,7 @@ import numpy as np import paddle.v2 as paddle import paddle.v2.fluid.core as core +import paddle.v2.fluid.evaluator as evaluator import paddle.v2.fluid.framework as framework import paddle.v2.fluid.layers as layers from paddle.v2.fluid.executor import Executor @@ -41,8 +42,8 @@ def stacked_lstm_net(input_dim, avg_cost = layers.mean(x=cost) adam_optimizer = AdamOptimizer(learning_rate=0.002) opts = adam_optimizer.minimize(avg_cost) - acc = layers.accuracy(input=prediction, label=label) - return avg_cost, acc + accuracy, acc_out = evaluator.accuracy(input=prediction, label=label) + return avg_cost, accuracy, acc_out def to_lodtensor(data, place): @@ -69,7 +70,8 @@ def main(): dict_dim = len(word_dict) class_dim = 2 - cost, acc = stacked_lstm_net(input_dim=dict_dim, class_dim=class_dim) + cost, accuracy, acc_out = stacked_lstm_net( + input_dim=dict_dim, class_dim=class_dim) train_data = paddle.batch( paddle.reader.shuffle( @@ -81,6 +83,7 @@ def main(): exe.run(framework.default_startup_program()) for pass_id in xrange(PASS_NUM): + accuracy.reset(exe) for data in train_data(): tensor_words = to_lodtensor(map(lambda x: x[0], data), place) @@ -93,12 +96,13 @@ def main(): outs = exe.run(framework.default_main_program(), feed={"words": tensor_words, "label": tensor_label}, - fetch_list=[cost, acc]) + fetch_list=[cost, acc_out]) cost_val = np.array(outs[0]) acc_val = np.array(outs[1]) - - print("cost=" + str(cost_val) + " acc=" + str(acc_val)) - if cost_val < 1.0 and acc_val > 0.7: + pass_acc = accuracy.eval(exe) + print("cost=" + str(cost_val) + " acc=" + str(acc_val) + + " pass_acc=" + str(pass_acc)) + if cost_val < 1.0 and acc_val > 0.8: exit(0) exit(1) -- GitLab