train.py 3.5 KB
Newer Older
L
Luo Tao 已提交
1 2 3 4
import paddle.v2 as paddle


def softmax_regression(img):
5 6
    predict = paddle.layer.fc(
        input=img, size=10, act=paddle.activation.Softmax())
L
Luo Tao 已提交
7 8 9 10 11 12 13
    return predict


def multilayer_perceptron(img):
    # The first fully-connected layer
    hidden1 = paddle.layer.fc(input=img, size=128, act=paddle.activation.Relu())
    # The second fully-connected layer and the according activation function
14 15
    hidden2 = paddle.layer.fc(
        input=hidden1, size=64, act=paddle.activation.Relu())
L
Luo Tao 已提交
16 17
    # The thrid fully-connected layer, note that the hidden size should be 10,
    # which is the number of unique digits
18 19
    predict = paddle.layer.fc(
        input=hidden2, size=10, act=paddle.activation.Softmax())
L
Luo Tao 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    return predict


def convolutional_neural_network(img):
    # first conv layer
    conv_pool_1 = paddle.networks.simple_img_conv_pool(
        input=img,
        filter_size=5,
        num_filters=20,
        num_channel=1,
        pool_size=2,
        pool_stride=2,
        act=paddle.activation.Tanh())
    # second conv layer
    conv_pool_2 = paddle.networks.simple_img_conv_pool(
        input=conv_pool_1,
        filter_size=5,
        num_filters=50,
        num_channel=20,
        pool_size=2,
        pool_stride=2,
        act=paddle.activation.Tanh())
    # The first fully-connected layer
43 44
    fc1 = paddle.layer.fc(
        input=conv_pool_2, size=128, act=paddle.activation.Tanh())
L
Luo Tao 已提交
45 46
    # The softmax layer, note that the hidden size should be 10,
    # which is the number of unique digits
47 48
    predict = paddle.layer.fc(
        input=fc1, size=10, act=paddle.activation.Softmax())
L
Luo Tao 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    return predict


paddle.init(use_gpu=False, trainer_count=1)

# define network topology
images = paddle.layer.data(
    name='pixel', type=paddle.data_type.dense_vector(784))
label = paddle.layer.data(name='label', type=paddle.data_type.integer_value(10))

# Here we can build the prediction network in different ways. Please
# choose one by uncomment corresponding line.
predict = softmax_regression(images)
#predict = multilayer_perceptron(images)
#predict = convolutional_neural_network(images)

cost = paddle.layer.classification_cost(input=predict, label=label)

parameters = paddle.parameters.create(cost)

optimizer = paddle.optimizer.Momentum(
    learning_rate=0.1 / 128.0,
    momentum=0.9,
    regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))

74 75
trainer = paddle.trainer.SGD(
    cost=cost, parameters=parameters, update_equation=optimizer)
L
Luo Tao 已提交
76 77 78 79 80 81 82 83 84 85

lists = []


def event_handler(event):
    if isinstance(event, paddle.event.EndIteration):
        if event.batch_id % 100 == 0:
            print "Pass %d, Batch %d, Cost %f, %s" % (
                event.pass_id, event.batch_id, event.cost, event.metrics)
    if isinstance(event, paddle.event.EndPass):
Q
qingqing01 已提交
86
        result = trainer.test(reader=paddle.batch(
L
Luo Tao 已提交
87 88 89 90 91 92 93 94
            paddle.dataset.mnist.test(), batch_size=128))
        print "Test with Pass %d, Cost %f, %s\n" % (event.pass_id, result.cost,
                                                    result.metrics)
        lists.append((event.pass_id, result.cost,
                      result.metrics['classification_error_evaluator']))


trainer.train(
Q
qingqing01 已提交
95
    reader=paddle.batch(
96
        paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192),
L
Luo Tao 已提交
97 98 99 100 101 102 103 104
        batch_size=128),
    event_handler=event_handler,
    num_passes=100)

# find the best pass
best = sorted(lists, key=lambda list: float(list[1]))[0]
print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1])
print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100)