pyreader.py 3.2 KB
Newer Older
Y
yuyang18 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

Y
yuyang18 已提交
17
import numpy
M
minqiyang 已提交
18
import six
Y
yuyang18 已提交
19

Y
yuyang18 已提交
20
import paddle
Y
yuyang18 已提交
21 22
import paddle.dataset.mnist as mnist
import paddle.fluid as fluid
Y
yuyang18 已提交
23
import paddle.v2
Y
yuyang18 已提交
24 25 26


def network(is_train):
Y
yuyang18 已提交
27
    reader = fluid.layers.py_reader(
Y
yuyang18 已提交
28 29 30
        capacity=10,
        shapes=((-1, 784), (-1, 1)),
        dtypes=('float32', 'int64'),
F
fengjiayi 已提交
31 32
        name="train_reader" if is_train else "test_reader",
        use_double_buffer=True)
Y
yuyang18 已提交
33
    img, label = fluid.layers.read_file(reader)
Y
yuyang18 已提交
34 35 36

    hidden = img

M
minqiyang 已提交
37
    for i in six.moves.xrange(2):
Y
yuyang18 已提交
38 39 40 41 42 43
        hidden = fluid.layers.fc(input=hidden, size=100, act='tanh')
        hidden = fluid.layers.dropout(
            hidden, dropout_prob=0.5, is_test=not is_train)

    prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
    loss = fluid.layers.cross_entropy(input=prediction, label=label)
Y
yuyang18 已提交
44
    return fluid.layers.mean(loss), reader
Y
yuyang18 已提交
45 46 47 48 49 50 51 52


def main():
    train_prog = fluid.Program()
    startup_prog = fluid.Program()

    with fluid.program_guard(train_prog, startup_prog):
        with fluid.unique_name.guard():
Y
yuyang18 已提交
53
            loss, train_reader = network(True)
Y
yuyang18 已提交
54 55 56 57
            adam = fluid.optimizer.Adam(learning_rate=0.01)
            adam.minimize(loss)

    test_prog = fluid.Program()
Y
yuyang18 已提交
58 59
    test_startup = fluid.Program()
    with fluid.program_guard(test_prog, test_startup):
Y
yuyang18 已提交
60
        with fluid.unique_name.guard():
Y
yuyang18 已提交
61
            test_loss, test_reader = network(False)
Y
yuyang18 已提交
62

F
fengjiayi 已提交
63 64 65 66
    use_cuda = fluid.core.is_compiled_with_cuda()
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    fluid.Executor(place).run(startup_prog)
    fluid.Executor(place).run(test_startup)
Y
yuyang18 已提交
67

Y
yuyang18 已提交
68
    trainer = fluid.ParallelExecutor(
F
fengjiayi 已提交
69
        use_cuda=use_cuda, loss_name=loss.name, main_program=train_prog)
Y
yuyang18 已提交
70 71

    tester = fluid.ParallelExecutor(
F
fengjiayi 已提交
72
        use_cuda=use_cuda, share_vars_from=trainer, main_program=test_prog)
Y
yuyang18 已提交
73

Y
yuyang18 已提交
74 75
    train_reader.decorate_paddle_reader(
        paddle.v2.reader.shuffle(
Y
yuyang18 已提交
76
            paddle.batch(mnist.train(), 512), buf_size=8192))
Y
yuyang18 已提交
77

Y
yuyang18 已提交
78
    test_reader.decorate_paddle_reader(paddle.batch(mnist.test(), 512))
Y
yuyang18 已提交
79

M
minqiyang 已提交
80
    for epoch_id in six.moves.xrange(10):
Y
yuyang18 已提交
81
        train_reader.start()
Y
yuyang18 已提交
82
        try:
Y
yuyang18 已提交
83 84 85
            while True:
                print 'train_loss', numpy.array(
                    trainer.run(fetch_list=[loss.name]))
Y
yuyang18 已提交
86 87
        except fluid.core.EOFException:
            print 'End of epoch', epoch_id
Y
yuyang18 已提交
88
            train_reader.reset()
Y
yuyang18 已提交
89

Y
yuyang18 已提交
90
        test_reader.start()
Y
yuyang18 已提交
91 92
        try:
            while True:
Y
yuyang18 已提交
93 94
                print 'test loss', numpy.array(
                    tester.run(fetch_list=[test_loss.name]))
Y
yuyang18 已提交
95 96
        except fluid.core.EOFException:
            print 'End of testing'
Y
yuyang18 已提交
97
            test_reader.reset()
Y
yuyang18 已提交
98

Y
yuyang18 已提交
99 100 101

if __name__ == '__main__':
    main()