api_train_v2.py 3.9 KB
Newer Older
Q
qiaolongfei 已提交
1 2
import paddle.v2 as paddle

Y
Yu Yang 已提交
3

L
Luo Tao 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
def softmax_regression(img):
    predict = paddle.layer.fc(input=img,
                              size=10,
                              act=paddle.activation.Softmax())
    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
    hidden2 = paddle.layer.fc(input=hidden1,
                              size=64,
                              act=paddle.activation.Relu())
    # The thrid fully-connected layer, note that the hidden size should be 10,
    # which is the number of unique digits
    predict = paddle.layer.fc(input=hidden2,
                              size=10,
                              act=paddle.activation.Softmax())
    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
    fc1 = paddle.layer.fc(input=conv_pool_2,
                          size=128,
                          act=paddle.activation.Tanh())
    # The softmax layer, note that the hidden size should be 10,
    # which is the number of unique digits
    predict = paddle.layer.fc(input=fc1,
                              size=10,
                              act=paddle.activation.Softmax())
    return predict


Y
Yu Yang 已提交
57
def main():
L
Luo Tao 已提交
58
    paddle.init(use_gpu=True, trainer_count=1)
Q
qiaolongfei 已提交
59 60

    # define network topology
61 62 63 64
    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))
L
Luo Tao 已提交
65 66 67 68 69 70

    predict = softmax_regression(images)
    #predict = multilayer_perceptron(images)
    #predict = convolutional_neural_network(images)

    cost = paddle.layer.classification_cost(input=predict, label=label)
Q
qiaolongfei 已提交
71

Q
qiaolongfei 已提交
72
    parameters = paddle.parameters.create(cost)
Y
Yu Yang 已提交
73

L
Luo Tao 已提交
74 75 76 77
    optimizer = paddle.optimizer.Momentum(
        learning_rate=0.1 / 128.0,
        momentum=0.9,
        regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))
Y
Yu Yang 已提交
78

Y
Yu Yang 已提交
79
    trainer = paddle.trainer.SGD(cost=cost,
Y
Yu Yang 已提交
80
                                 parameters=parameters,
L
Luo Tao 已提交
81
                                 update_equation=optimizer)
Y
Yu Yang 已提交
82

Y
Yu Yang 已提交
83
    def event_handler(event):
Y
Yu Yang 已提交
84
        if isinstance(event, paddle.event.EndIteration):
L
Luo Tao 已提交
85
            if event.batch_id % 100 == 0:
Y
Yu Yang 已提交
86
                result = trainer.test(reader=paddle.reader.batched(
L
Luo Tao 已提交
87
                    paddle.dataset.mnist.test(), batch_size=128))
Y
Yu Yang 已提交
88 89 90 91
                print "Pass %d, Batch %d, Cost %f, %s, Testing metrics %s" % (
                    event.pass_id, event.batch_id, event.cost, event.metrics,
                    result.metrics)

Y
Yu Yang 已提交
92
    trainer.train(
Y
Yu Yang 已提交
93
        reader=paddle.reader.batched(
Y
Yu Yang 已提交
94
            paddle.reader.shuffle(
Y
Yu Yang 已提交
95
                paddle.dataset.mnist.train(), buf_size=8192),
L
Luo Tao 已提交
96 97 98
            batch_size=128),
        event_handler=event_handler,
        num_passes=100)
Y
Yu Yang 已提交
99

Y
Yu Yang 已提交
100 101 102 103 104 105
    # output is a softmax layer. It returns probabilities.
    # Shape should be (100, 10)
    probs = paddle.infer(
        output=inference,
        parameters=parameters,
        reader=paddle.reader.batched(
Y
Yu Yang 已提交
106
            paddle.reader.firstn(
Y
Yu Yang 已提交
107 108
                paddle.reader.map_readers(lambda item: (item[0], ),
                                          paddle.dataset.mnist.test()),
Y
Yu Yang 已提交
109
                n=100),
Y
Yu Yang 已提交
110 111 112
            batch_size=32))
    print probs.shape

Y
Yu Yang 已提交
113 114 115

if __name__ == '__main__':
    main()