From 2c3799d1079eaf9b67bf5128b5f2ee4a6c283296 Mon Sep 17 00:00:00 2001 From: 123malin Date: Mon, 18 Jan 2021 11:12:56 +0800 Subject: [PATCH] test=develop, fix fleet.metric (#30438) (#30473) * test=develop, fix fleet.metrics(mse, rmse, mae) --- .../distributed/fleet/metrics/metric.py | 31 +++++++++++++++---- .../tests/unittests/test_fleet_metric.py | 18 +++++------ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/python/paddle/distributed/fleet/metrics/metric.py b/python/paddle/distributed/fleet/metrics/metric.py index d057f20731..9ed0a0df4b 100644 --- a/python/paddle/distributed/fleet/metrics/metric.py +++ b/python/paddle/distributed/fleet/metrics/metric.py @@ -228,7 +228,7 @@ def mae(abserr, total_ins_num, scope=None, util=None): Args: abserr(numpy.array|Variable|string): abserr in output of fluid.contrib.layers.ctr_metric_bundle - total_ins_num(int|float): total train/infer instance count + total_ins_num(numpy.array|Variable|string): total variable scope(Scope): specific scope Returns: @@ -253,6 +253,11 @@ def mae(abserr, total_ins_num, scope=None, util=None): abserr = np.array(scope.find_var(abserr.name).get_tensor()) elif isinstance(abserr, str): abserr = np.array(scope.find_var(abserr).get_tensor()) + if isinstance(total_ins_num, Variable): + total_ins_num = np.array( + scope.find_var(total_ins_num.name).get_tensor()) + elif isinstance(total_ins_num, str): + total_ins_num = np.array(scope.find_var(total_ins_num).get_tensor()) old_metric_shape = np.array(abserr.shape) abserr = abserr.reshape(-1) @@ -260,8 +265,9 @@ def mae(abserr, total_ins_num, scope=None, util=None): global_metric = util.all_reduce(abserr, "sum") global_metric = global_metric.reshape(old_metric_shape) + global_total_num = util.all_reduce(total_ins_num, "sum") - mae_value = global_metric[0] / total_ins_num + mae_value = float(global_metric[0]) / float(global_total_num[0]) return mae_value @@ -271,7 +277,7 @@ def rmse(sqrerr, total_ins_num, scope=None, util=None): Args: sqrerr(numpy.array|Variable|string): sqrerr in output of fluid.contrib.layers.ctr_metric_bundle - total_ins_num(int|float): total train/infer instance count + total_ins_num(numpy.array|Variable|string): total variable scope(Scope): specific scope Returns: @@ -296,14 +302,21 @@ def rmse(sqrerr, total_ins_num, scope=None, util=None): sqrerr = np.array(scope.find_var(sqrerr.name).get_tensor()) elif isinstance(sqrerr, str): sqrerr = np.array(scope.find_var(sqrerr).get_tensor()) + if isinstance(total_ins_num, Variable): + total_ins_num = np.array( + scope.find_var(total_ins_num.name).get_tensor()) + elif isinstance(total_ins_num, str): + total_ins_num = np.array(scope.find_var(total_ins_num).get_tensor()) old_metric_shape = np.array(sqrerr.shape) sqrerr = sqrerr.reshape(-1) global_metric = np.copy(sqrerr) * 0 global_metric = util.all_reduce(sqrerr, "sum") global_metric = global_metric.reshape(old_metric_shape) + global_total_num = util.all_reduce(total_ins_num, "sum") + + rmse_value = math.sqrt(float(global_metric[0]) / float(global_total_num[0])) - rmse_value = math.sqrt(global_metric[0] / total_ins_num) return rmse_value @@ -313,7 +326,7 @@ def mse(sqrerr, total_ins_num, scope=None, util=None): Args: sqrerr(numpy.array|Variable|string): sqrerr in output of fluid.contrib.layers.ctr_metric_bundle - total_ins_num(int|float): total train/infer instance count + total_ins_num(numpy.array|Variable|string): total variable scope(Scope): specific scope Returns: @@ -338,14 +351,20 @@ def mse(sqrerr, total_ins_num, scope=None, util=None): sqrerr = np.array(scope.find_var(sqrerr.name).get_tensor()) elif isinstance(sqrerr, str): sqrerr = np.array(scope.find_var(sqrerr).get_tensor()) + if isinstance(total_ins_num, Variable): + total_ins_num = np.array( + scope.find_var(total_ins_num.name).get_tensor()) + elif isinstance(total_ins_num, str): + total_ins_num = np.array(scope.find_var(total_ins_num).get_tensor()) old_metric_shape = np.array(sqrerr.shape) sqrerr = sqrerr.reshape(-1) global_metric = np.copy(sqrerr) * 0 global_metric = util.all_reduce(sqrerr, "sum") global_metric = global_metric.reshape(old_metric_shape) + global_total_num = util.all_reduce(total_ins_num, "sum") - mse_value = global_metric[0] / total_ins_num + mse_value = float(global_metric[0]) / float(global_total_num[0]) return mse_value diff --git a/python/paddle/fluid/tests/unittests/test_fleet_metric.py b/python/paddle/fluid/tests/unittests/test_fleet_metric.py index aae2d7f3aa..724a0dfe01 100644 --- a/python/paddle/fluid/tests/unittests/test_fleet_metric.py +++ b/python/paddle/fluid/tests/unittests/test_fleet_metric.py @@ -101,17 +101,17 @@ class TestFleetMetric(unittest.TestCase): metric.max(t, scope, self.util) metric.min(t, scope, self.util) metric.auc(t, t1, scope, self.util) - metric.mae(t1, 3, scope, self.util) - metric.rmse(t1, 3, scope, self.util) - metric.mse(t1, 3, scope, self.util) + metric.mae(t, t1, scope, self.util) + metric.rmse(t, t1, scope, self.util) + metric.mse(t, t1, scope, self.util) metric.acc(t, t1, scope, self.util) metric.sum(str(t.name)) metric.max(str(t.name)) metric.min(str(t.name)) metric.auc(str(t1.name), str(t.name)) - metric.mae(str(t1.name), 3) - metric.rmse(str(t1.name), 3) - metric.mse(str(t1.name), 3) + metric.mae(str(t1.name), str(t.name)) + metric.rmse(str(t1.name), str(t.name)) + metric.mse(str(t1.name), str(t.name)) metric.acc(str(t.name), str(t1.name)) arr = np.array([1, 2, 3, 4]) metric.sum(arr, util=self.util) @@ -121,9 +121,9 @@ class TestFleetMetric(unittest.TestCase): arr2 = np.array([[1, 2, 3, 4]]) arr3 = np.array([1, 2, 3, 4]) metric.auc(arr1, arr2, util=self.util) - metric.mae(arr, 3, util=self.util) - metric.rmse(arr, 3, util=self.util) - metric.mse(arr, 3, util=self.util) + metric.mae(arr, arr3, util=self.util) + metric.rmse(arr, arr3, util=self.util) + metric.mse(arr, arr3, util=self.util) metric.acc(arr, arr3, util=self.util) -- GitLab