test_recognize_digits_mlp.py 2.7 KB
Newer Older
1
from __future__ import print_function
Q
Qiao Longfei 已提交
2
import numpy as np
Q
qijun 已提交
3
import paddle.v2 as paddle
4
import paddle.v2.fluid as fluid
Q
qijun 已提交
5

6
BATCH_SIZE = 128
7
image = fluid.layers.data(name='x', shape=[784], dtype='float32')
Q
qijun 已提交
8

Y
Yu Yang 已提交
9
regularizer = fluid.regularizer.L2Decay(0.0005 * BATCH_SIZE)
10

11 12 13
hidden1 = fluid.layers.fc(input=image,
                          size=128,
                          act='relu',
Y
Yu Yang 已提交
14 15 16
                          param_attr=fluid.ParamAttr(
                              regularizer=regularizer,
                              clip=fluid.clip.ClipByValue(10)))
17 18 19
hidden2 = fluid.layers.fc(input=hidden1,
                          size=64,
                          act='relu',
Y
Yu Yang 已提交
20
                          param_attr=regularizer)
Q
qijun 已提交
21

22 23 24
predict = fluid.layers.fc(input=hidden2,
                          size=10,
                          act='softmax',
Y
Yu Yang 已提交
25
                          param_attr=regularizer)
Q
qijun 已提交
26

27
label = fluid.layers.data(name='y', shape=[1], dtype='int64')
Q
qijun 已提交
28

29 30
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(x=cost)
Q
qijun 已提交
31

32
optimizer = fluid.optimizer.Momentum(learning_rate=0.001, momentum=0.9)
33
opts = optimizer.minimize(avg_cost)
Q
qijun 已提交
34

35
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
F
fengjiayi 已提交
36

37
inference_program = fluid.default_main_program().clone()
38 39 40 41
with fluid.program_guard(inference_program):
    test_accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
    test_target = [avg_cost] + test_accuracy.metrics + test_accuracy.states
    inference_program = fluid.io.get_inference_program(test_target)
42

Q
qijun 已提交
43 44 45 46 47
train_reader = paddle.batch(
    paddle.reader.shuffle(
        paddle.dataset.mnist.train(), buf_size=8192),
    batch_size=BATCH_SIZE)

48 49
test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)

50 51
place = fluid.CPUPlace()
exe = fluid.Executor(place)
Y
Yu Yang 已提交
52
feeder = fluid.DataFeeder(feed_list=[image, label], place=place)
53
exe.run(fluid.default_startup_program())
Q
qijun 已提交
54 55 56

PASS_NUM = 100
for pass_id in range(PASS_NUM):
F
fengjiayi 已提交
57
    accuracy.reset(exe)
Q
qijun 已提交
58
    for data in train_reader():
Y
Yu Yang 已提交
59 60 61
        out, acc = exe.run(fluid.default_main_program(),
                           feed=feeder.feed(data),
                           fetch_list=[avg_cost] + accuracy.metrics)
F
fengjiayi 已提交
62 63
        pass_acc = accuracy.eval(exe)

64 65
        test_accuracy.reset(exe)
        for data in test_reader():
66
            out, acc = exe.run(inference_program,
Y
Yu Yang 已提交
67
                               feed=feeder.feed(data),
68
                               fetch_list=[avg_cost] + test_accuracy.metrics)
69 70 71 72 73 74 75

        test_pass_acc = test_accuracy.eval(exe)
        print("pass_id=" + str(pass_id) + " train_cost=" + str(
            out) + " train_acc=" + str(acc) + " train_pass_acc=" + str(pass_acc)
              + " test_acc=" + str(test_pass_acc))

        if test_pass_acc > 0.7:
F
fengjiayi 已提交
76
            exit(0)
Q
qijun 已提交
77
exit(1)