api_train_v2.py 2.1 KB
Newer Older
Y
Yu Yang 已提交
1 2
from paddle.trainer_config_helpers import *
from paddle.trainer.PyDataProvider2 import dense_vector, integer_value
Y
Yu Yang 已提交
3
import paddle.v2 as paddle
Y
Yu Yang 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import numpy
import mnist_util


def train_reader():
    train_file = './data/raw_data/train'
    generator = mnist_util.read_from_mnist(train_file)
    for item in generator:
        yield item


def network_config():
    imgs = data_layer(name='pixel', size=784)
    hidden1 = fc_layer(input=imgs, size=200)
    hidden2 = fc_layer(input=hidden1, size=200)
    inference = fc_layer(input=hidden2, size=10, act=SoftmaxActivation())
    cost = classification_cost(
        input=inference, label=data_layer(
            name='label', size=10))
    outputs(cost)


def main():
Y
Yu Yang 已提交
27
    paddle.init(use_gpu=False, trainer_count=1)
Y
Yu Yang 已提交
28
    model_config = parse_network_config(network_config)
Y
Yu Yang 已提交
29 30 31
    parameters = paddle.parameters.create(model_config)
    for param_name in parameters.keys():
        array = parameters[param_name]
Y
Yu Yang 已提交
32
        array[:] = numpy.random.uniform(low=-1.0, high=1.0, size=array.shape)
Y
Yu Yang 已提交
33 34 35 36 37 38
        parameters[param_name] = array

    adam_optimizer = paddle.optimizer.Optimizer(
        learning_rate=0.01, learning_method=AdamOptimizer())

    def event_handler(event):
Y
Yu Yang 已提交
39
        if isinstance(event, paddle.trainer.EndIteration):
Y
Yu Yang 已提交
40 41 42
            para = parameters['___fc_layer_2__.w0']
            print "Pass %d, Batch %d, Cost %f, Weight Mean Of Fc 2 is %f" % (
                event.pass_id, event.batch_id, event.cost, para.mean())
Y
Yu Yang 已提交
43

Y
Yu Yang 已提交
44 45
        else:
            pass
46

Y
Yu Yang 已提交
47
    trainer = paddle.trainer.SGDTrainer(update_equation=adam_optimizer)
Y
Yu Yang 已提交
48 49 50

    trainer.train(train_data_reader=train_reader,
                  topology=model_config,
Y
Yu Yang 已提交
51
                  parameters=parameters,
Y
Yu Yang 已提交
52 53 54 55 56 57 58 59 60 61 62
                  event_handler=event_handler,
                  batch_size=32,  # batch size should be refactor in Data reader
                  data_types={  # data_types will be removed, It should be in
                      # network topology
                      'pixel': dense_vector(784),
                      'label': integer_value(10)
                  })


if __name__ == '__main__':
    main()