|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import unittest |
| 18 | +import numpy as np |
| 19 | +import paddle |
| 20 | +import paddle.fluid as fluid |
| 21 | +import paddle.fluid.core as core |
| 22 | +from paddle.fluid import Program, program_guard |
| 23 | + |
| 24 | +np.random.seed(10) |
| 25 | + |
| 26 | + |
| 27 | +class TestNanmeanAPI(unittest.TestCase): |
| 28 | + # test paddle.tensor.math.nanmean |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + self.x_shape = [2, 3, 4, 5] |
| 32 | + self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32) |
| 33 | + self.x[0, :, :, :] = np.nan |
| 34 | + self.x_grad = np.array([[np.nan, np.nan, 3.], |
| 35 | + [0., np.nan, 2.]]).astype(np.float32) |
| 36 | + self.place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda() \ |
| 37 | + else paddle.CPUPlace() |
| 38 | + |
| 39 | + def test_api_static(self): |
| 40 | + paddle.enable_static() |
| 41 | + with paddle.static.program_guard(paddle.static.Program()): |
| 42 | + x = paddle.fluid.data('X', self.x_shape) |
| 43 | + out1 = paddle.nanmean(x) |
| 44 | + out2 = paddle.tensor.nanmean(x) |
| 45 | + out3 = paddle.tensor.math.nanmean(x) |
| 46 | + axis = np.arange(len(self.x_shape)).tolist() |
| 47 | + out4 = paddle.nanmean(x, axis) |
| 48 | + out5 = paddle.nanmean(x, tuple(axis)) |
| 49 | + exe = paddle.static.Executor(self.place) |
| 50 | + res = exe.run(feed={'X': self.x}, |
| 51 | + fetch_list=[out1, out2, out3, out4, out5]) |
| 52 | + out_ref = np.nanmean(self.x) |
| 53 | + for out in res: |
| 54 | + self.assertEqual(np.allclose(out, out_ref, rtol=1e-04), True) |
| 55 | + |
| 56 | + def test_api_dygraph(self): |
| 57 | + paddle.disable_static(self.place) |
| 58 | + |
| 59 | + def test_case(x, axis=None, keepdim=False): |
| 60 | + x_tensor = paddle.to_tensor(x) |
| 61 | + out = paddle.nanmean(x_tensor, axis, keepdim) |
| 62 | + if isinstance(axis, list): |
| 63 | + axis = tuple(axis) |
| 64 | + if len(axis) == 0: |
| 65 | + axis = None |
| 66 | + |
| 67 | + out_ref = np.nanmean(x, axis, keepdims=keepdim) |
| 68 | + if np.isnan(out_ref).sum(): |
| 69 | + nan_mask = np.isnan(out_ref) |
| 70 | + out_ref[nan_mask] = 0 |
| 71 | + out_np = out.numpy() |
| 72 | + out_np[nan_mask] = 0 |
| 73 | + self.assertEqual(np.allclose(out_np, out_ref, rtol=1e-04), True) |
| 74 | + else: |
| 75 | + self.assertEqual( |
| 76 | + np.allclose( |
| 77 | + out.numpy(), out_ref, rtol=1e-04), True) |
| 78 | + |
| 79 | + test_case(self.x) |
| 80 | + test_case(self.x, []) |
| 81 | + test_case(self.x, -1) |
| 82 | + test_case(self.x, keepdim=True) |
| 83 | + test_case(self.x, 2, keepdim=True) |
| 84 | + test_case(self.x, [0, 2]) |
| 85 | + test_case(self.x, (0, 2)) |
| 86 | + test_case(self.x, [0, 1, 2, 3]) |
| 87 | + paddle.enable_static() |
| 88 | + |
| 89 | + def test_errors(self): |
| 90 | + paddle.enable_static() |
| 91 | + with paddle.static.program_guard(paddle.static.Program()): |
| 92 | + x = paddle.fluid.data('X', [10, 12], 'int32') |
| 93 | + self.assertRaises(TypeError, paddle.nanmean, x) |
| 94 | + |
| 95 | + def test_api_dygraph_grad(self): |
| 96 | + paddle.disable_static(self.place) |
| 97 | + |
| 98 | + def test_case(x, axis=None, keepdim=False): |
| 99 | + if isinstance(axis, list): |
| 100 | + axis = list(axis) |
| 101 | + if len(axis) == 0: |
| 102 | + axis = None |
| 103 | + x_tensor = paddle.to_tensor(x, stop_gradient=False) |
| 104 | + y = paddle.nanmean(x_tensor, axis, keepdim) |
| 105 | + dx = paddle.grad(y, x_tensor)[0].numpy() |
| 106 | + sum_dx_ref = np.prod(y.shape) |
| 107 | + if np.isnan(y.numpy()).sum(): |
| 108 | + sum_dx_ref -= np.isnan(y.numpy()).sum() |
| 109 | + cnt = paddle.sum(~paddle.isnan(x_tensor), |
| 110 | + axis=axis, |
| 111 | + keepdim=keepdim) |
| 112 | + if (cnt == 0).sum(): |
| 113 | + dx[np.isnan(dx)] = 0 |
| 114 | + sum_dx = dx.sum() |
| 115 | + self.assertEqual(np.allclose(sum_dx, sum_dx_ref, rtol=1e-04), True) |
| 116 | + |
| 117 | + test_case(self.x) |
| 118 | + test_case(self.x, []) |
| 119 | + test_case(self.x, -1) |
| 120 | + test_case(self.x, keepdim=True) |
| 121 | + test_case(self.x, 2, keepdim=True) |
| 122 | + test_case(self.x, [0, 2]) |
| 123 | + test_case(self.x, (0, 2)) |
| 124 | + test_case(self.x, [0, 1, 2, 3]) |
| 125 | + |
| 126 | + test_case(self.x_grad) |
| 127 | + test_case(self.x_grad, []) |
| 128 | + test_case(self.x_grad, -1) |
| 129 | + test_case(self.x_grad, keepdim=True) |
| 130 | + test_case(self.x_grad, 0, keepdim=True) |
| 131 | + test_case(self.x_grad, 1) |
| 132 | + test_case(self.x_grad, (0, 1)) |
| 133 | + paddle.enable_static() |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + unittest.main() |
0 commit comments