api_train_v2.py 1.6 KB
Newer Older
Y
Yu Yang 已提交
1
import numpy
Q
qiaolongfei 已提交
2 3
import paddle.v2 as paddle

Y
Yu Yang 已提交
4 5

def main():
Y
Yu Yang 已提交
6
    paddle.init(use_gpu=False, trainer_count=1)
Q
qiaolongfei 已提交
7 8

    # define network topology
9 10 11 12
    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))
Q
qiaolongfei 已提交
13 14 15 16 17 18 19
    hidden1 = paddle.layer.fc(input=images, size=200)
    hidden2 = paddle.layer.fc(input=hidden1, size=200)
    inference = paddle.layer.fc(input=hidden2,
                                size=10,
                                act=paddle.activation.Softmax())
    cost = paddle.layer.classification_cost(input=inference, label=label)

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

Q
qiaolongfei 已提交
22
    adam_optimizer = paddle.optimizer.Adam(learning_rate=0.01)
Y
Yu Yang 已提交
23 24

    def event_handler(event):
Y
Yu Yang 已提交
25
        if isinstance(event, paddle.event.EndIteration):
Y
Yu Yang 已提交
26 27 28
            if event.batch_id % 100 == 0:
                print "Pass %d, Batch %d, Cost %f, %s" % (
                    event.pass_id, event.batch_id, event.cost, event.metrics)
Y
Yu Yang 已提交
29 30
        else:
            pass
31

Y
Yu Yang 已提交
32
    trainer = paddle.trainer.SGD(update_equation=adam_optimizer)
Y
Yu Yang 已提交
33
    trainer.train(
Y
Yu Yang 已提交
34
        reader=paddle.reader.batched(
Y
Yu Yang 已提交
35 36 37 38 39 40 41 42 43 44 45
            paddle.reader.shuffle(paddle.dataset.mnist.train_creator(),
                                  buf_size=8192), batch_size=32),
        topology=cost,
        parameters=parameters,
        event_handler=event_handler,
        data_types=[  # data_types will be removed, It should be in
            # network topology
            ('pixel', images.type),
            ('label', label.type)],
        reader_dict={'pixel': 0, 'label': 1}
    )
Y
Yu Yang 已提交
46 47 48 49


if __name__ == '__main__':
    main()