train.py 5.1 KB
Newer Older
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
from __future__ import print_function
16

17 18 19
import paddle
import paddle.fluid as fluid
import numpy
20 21
import math
import sys
22 23


24
def main():
25

26 27 28 29 30 31 32
    batch_size = 20
    train_reader = paddle.batch(
        paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500),
        batch_size=batch_size)
    test_reader = paddle.batch(
        paddle.reader.shuffle(paddle.dataset.uci_housing.test(), buf_size=500),
        batch_size=batch_size)
33 34 35

    # feature vector of length 13
    x = fluid.layers.data(name='x', shape=[13], dtype='float32')
36
    y = fluid.layers.data(name='y', shape=[1], dtype='float32')
37 38
    y_predict = fluid.layers.fc(input=x, size=1, act=None)

39 40 41 42 43 44 45 46 47 48 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    main_program = fluid.default_main_program()
    star_program = fluid.default_startup_program()

    cost = fluid.layers.square_error_cost(input=y_predict, label=y)
    avg_loss = fluid.layers.mean(cost)

    sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
    sgd_optimizer.minimize(avg_loss)

    test_program = main_program.clone(for_test=True)

    # can use CPU or GPU
    use_cuda = False
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    exe = fluid.Executor(place)

    # Specify the directory to save the parameters
    params_dirname = "fit_a_line.inference.model"
    num_epochs = 100

    # For training test cost
    def train_test(program, feeder):
        exe_test = fluid.Executor(place)
        accumulated = 1 * [0]
        count = 0
        for data_test in test_reader():
            outs = exe_test.run(
                program=program,
                feed=feeder.feed(data_test),
                fetch_list=[avg_loss])
            accumulated = [x_c[0] + x_c[1][0] for x_c in zip(accumulated, outs)]
            count += 1
        return [x_d / count for x_d in accumulated]

    # main train loop.
    def train_loop():

        feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
        feeder_test = fluid.DataFeeder(place=place, feed_list=[x, y])
        exe.run(star_program)

        train_title = "Train cost"
        test_title = "Test cost"
        step = 0

        for pass_id in range(num_epochs):
            for data_train in train_reader():
                avg_loss_value, = exe.run(
                    main_program,
                    feed=feeder.feed(data_train),
                    fetch_list=[avg_loss])
                if step % 10 == 0:  # record a train cost every 10 batches
L
lujun 已提交
91 92 93
                    print("%s, Step %d, Cost %f" %
                          (train_title, step, avg_loss_value[0]))
                if step % 100 == 0:  # record a test cost every 100 batches
94 95
                    test_metics = train_test(
                        program=test_program, feeder=feeder_test)
L
lujun 已提交
96 97
                    print("%s, Step %d, Cost %f" %
                          (test_title, step, test_metics[0]))
98 99 100 101 102 103 104 105
                    # If the accuracy is good enough, we can stop the training.
                    if test_metics[0] < 10.0:
                        return

                step += 1

                if math.isnan(float(avg_loss_value)):
                    sys.exit("got NaN loss, training failed.")
L
lujun 已提交
106 107 108 109
            if params_dirname is not None:
                # We can save the trained parameters for the inferences later
                fluid.io.save_inference_model(params_dirname, ['x'],
                                              [y_predict], exe)
Y
Yan Xu 已提交
110

111
    train_loop()
112

113 114
    infer_exe = fluid.Executor(place)
    inference_scope = fluid.core.Scope()
115

116 117 118 119 120
    # infer
    with fluid.scope_guard(inference_scope):
        [inference_program, feed_target_names,
         fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
        batch_size = 10
121

122 123
        infer_reader = paddle.batch(
            paddle.dataset.uci_housing.test(), batch_size=batch_size)
124

125 126 127 128 129
        infer_data = next(infer_reader())
        infer_feat = numpy.array(
            [data[0] for data in infer_data]).astype("float32")
        infer_label = numpy.array(
            [data[1] for data in infer_data]).astype("float32")
130

131 132 133 134 135
        assert feed_target_names[0] == 'x'
        results = infer_exe.run(
            inference_program,
            feed={feed_target_names[0]: numpy.array(infer_feat)},
            fetch_list=fetch_targets)
136

137 138 139
        print("infer results: (House Price)")
        for idx, val in enumerate(results[0]):
            print("%d: %.2f" % (idx, val))
140

141 142 143
        print("\nground truth:")
        for idx, val in enumerate(infer_label):
            print("%d: %.2f" % (idx, val))
144

145

146 147
if __name__ == '__main__':
    main()