test_network_with_dtype.py 2.4 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#   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.

import unittest

import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.executor import Executor

BATCH_SIZE = 20


class TestNetWithDtype(unittest.TestCase):
D
dzhwinter 已提交
27
    def setUp(self):
D
dzhwinter 已提交
28 29 30
        self.dtype = "float64"
        self.init_dtype()

D
dzhwinter 已提交
31 32 33 34 35 36 37 38
    def run_net_on_place(self, place):
        main = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(main, startup):
            x = fluid.layers.data(name='x', shape=[13], dtype=self.dtype)
            y = fluid.layers.data(name='y', shape=[1], dtype=self.dtype)
            y_predict = fluid.layers.fc(input=x, size=1, act=None)
            cost = fluid.layers.square_error_cost(input=y_predict, label=y)
D
dzhwinter 已提交
39
            avg_cost = fluid.layers.mean(cost)
D
dzhwinter 已提交
40 41
            sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
            sgd_optimizer.minimize(avg_cost)
D
dzhwinter 已提交
42

D
dzhwinter 已提交
43
        fetch_list = [avg_cost]
D
dzhwinter 已提交
44 45
        train_reader = paddle.batch(
            paddle.dataset.uci_housing.train(), batch_size=BATCH_SIZE)
D
dzhwinter 已提交
46
        feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
D
dzhwinter 已提交
47
        exe = fluid.Executor(place)
D
dzhwinter 已提交
48
        exe.run(startup)
D
dzhwinter 已提交
49
        for data in train_reader():
D
dzhwinter 已提交
50
            exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list)
D
dzhwinter 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            # the main program is runable, the datatype is fully supported
            break

    def init_dtype(self):
        pass

    def test_cpu(self):
        place = fluid.CPUPlace()
        self.run_net_on_place(place)

    def test_gpu(self):
        if not core.is_compiled_with_cuda():
            return
        place = fluid.CUDAPlace(0)
        self.run_net_on_place(place)


# TODO(dzhwinter): make sure the fp16 is runable
D
dzhwinter 已提交
69
# class TestFloat16(TestNetWithDtype):
D
dzhwinter 已提交
70 71 72 73 74
#     def init_dtype(self):
#         self.dtype = "float16"

if __name__ == '__main__':
    unittest.main()