test_fleet_metric.py 3.7 KB
Newer Older
X
xujiaqi01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   Copyright (c) 2020 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.
"""Test fleet metric."""

from __future__ import print_function
import numpy as np
import paddle
import paddle.fluid as fluid
import os
import unittest
import paddle.fleet.metrics.metric as metric
23
from paddle.fluid.incubate.fleet.parameter_server.distribute_transpiler import fleet
X
xujiaqi01 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112


class TestFleetMetric(unittest.TestCase):
    """Test cases for fleet metric."""

    def setUp(self):
        """Set up, set envs."""

        class FakeFleet:
            """Fake fleet only for test."""

            def __init__(self):
                """Init."""
                self.gloo = fluid.core.Gloo()
                self.gloo.set_rank(0)
                self.gloo.set_size(1)
                self.gloo.set_prefix("123")
                self.gloo.set_iface("lo")
                self.gloo.set_hdfs_store("./tmp_test_metric", "", "")
                self.gloo.init()

            def _all_reduce(self, input, output, mode="sum"):
                """All reduce using gloo."""
                input_list = [i for i in input]
                ans = self.gloo.all_reduce(input_list, mode)
                for i in range(len(ans)):
                    output[i] = 1

            def _barrier_worker(self):
                """Fake barrier worker, do nothing."""
                pass

        self.fleet = FakeFleet()
        fleet._role_maker = self.fleet

    def test_metric_1(self):
        """Test cases for metrics."""
        train = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(train, startup):
            t = fluid.layers.create_global_var(
                shape=[1, 1],
                value=1,
                dtype='int64',
                persistable=True,
                force_cpu=True)
            t1 = fluid.layers.create_global_var(
                shape=[1, 1],
                value=1,
                dtype='int64',
                persistable=True,
                force_cpu=True)
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        scope = fluid.Scope()
        with fluid.scope_guard(scope):
            exe.run(startup)
            metric.sum(t, scope)
            metric.max(t, scope)
            metric.min(t, scope)
            metric.auc(t, t1, scope)
            metric.mae(t1, 3, scope)
            metric.rmse(t1, 3, scope)
            metric.mse(t1, 3, scope)
            metric.acc(t, t1, scope)
            metric.sum(str(t.name), scope)
            metric.max(str(t.name), scope)
            metric.min(str(t.name), scope)
            metric.auc(str(t1.name), str(t.name), scope)
            metric.mae(str(t1.name), 3, scope)
            metric.rmse(str(t1.name), 3, scope)
            metric.mse(str(t1.name), 3, scope)
            metric.acc(str(t.name), str(t1.name), scope)
        arr = np.array([1, 2, 3, 4])
        metric.sum(arr)
        metric.max(arr)
        metric.min(arr)
        arr1 = np.array([[1, 2, 3, 4]])
        arr2 = np.array([[1, 2, 3, 4]])
        arr3 = np.array([1, 2, 3, 4])
        metric.auc(arr1, arr2)
        metric.mae(arr, 3)
        metric.rmse(arr, 3)
        metric.mse(arr, 3)
        metric.acc(arr, arr3)


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