From 001a5f1e9518454991fe2c0cd71c14a27b9bdaa4 Mon Sep 17 00:00:00 2001 From: liqiotng Date: Thu, 17 Mar 2022 23:57:50 +0800 Subject: [PATCH 01/30] corrcoef commit --- .../paddle/fluid/tests/unittests/test_corr.py | 134 ++++++++++++++++++ python/paddle/linalg.py | 2 + python/paddle/tensor/__init__.py | 2 + python/paddle/tensor/linalg.py | 68 +++++++++ 4 files changed, 206 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_corr.py diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py new file mode 100644 index 0000000000000..1d35f7e1b5d9c --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -0,0 +1,134 @@ +# Copyright (c) 2019 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 paddle.fluid as fluid +import unittest +import numpy as np +import six +import paddle +import warnings + + +def numpy_corr(np_arr, rowvar=True, ddof=0): + return np.corrcoef(np_arr, rowvar=rowvar, ddof=int(ddof)) + + +class Corr_Test(unittest.TestCase): + def setUp(self): + self.shape = [20, 10] + + def test_tensor_corr_default(self): + typelist = ['float64'] + places = [fluid.CPUPlace()] + if fluid.core.is_compiled_with_cuda(): + places.append(fluid.CUDAPlace(0)) + for idx, p in enumerate(places): + if idx == 0: + paddle.set_device('cpu') + else: + paddle.set_device('gpu') + + for dtype in typelist: + np_arr = np.random.rand(*self.shape).astype(dtype) + tensor = paddle.to_tensor(np_arr, place=p) + corr = paddle.linalg.corrcoef(tensor, ddof=False) + np_corr = numpy_corr(np_arr, rowvar=True, ddof=0) + self.assertTrue(np.allclose(np_corr, corr.numpy())) + + def test_tensor_corr_rowvar(self): + typelist = ['float64'] + places = [fluid.CPUPlace()] + if fluid.core.is_compiled_with_cuda(): + places.append(fluid.CUDAPlace(0)) + + for idx, p in enumerate(places): + if idx == 0: + paddle.set_device('cpu') + else: + paddle.set_device('gpu') + + for dtype in typelist: + np_arr = np.random.rand(*self.shape).astype(dtype) + tensor = paddle.to_tensor(np_arr, place=p) + corr = paddle.linalg.corrcoef(tensor, rowvar=False, ddof=False) + np_corr = numpy_corr(np_arr, rowvar=False, ddof=0) + self.assertTrue(np.allclose(np_corr, corr.numpy())) + + def test_tensor_corr_ddof(self): + typelist = ['float64'] + places = [fluid.CPUPlace()] + if fluid.core.is_compiled_with_cuda(): + places.append(fluid.CUDAPlace(0)) + + for idx, p in enumerate(places): + if idx == 0: + paddle.set_device('cpu') + else: + paddle.set_device('gpu') + + for dtype in typelist: + np_arr = np.random.rand(*self.shape).astype(dtype) + tensor = paddle.to_tensor(np_arr, place=p) + corr = paddle.linalg.corrcoef(tensor, ddof=True) + np_corr = numpy_corr(np_arr, rowvar=True, ddof=1) + self.assertTrue(np.allclose(np_corr, corr.numpy())) + + +class Corr_Test2(Corr_Test): + def setUp(self): + self.shape = [10] + + +# Input(x) only support N-D (1<=N<=2) tensor +class Corr_Test3(unittest.TestCase): + def setUp(self): + self.shape = [2, 5, 10] + + def test_errors(self): + def test_err(): + np_arr = np.random.rand(*self.shape).astype('float64') + tensor = paddle.to_tensor(np_arr) + covrr = paddle.linalg.corrcoef(tensor, ddof=False) + + self.assertRaises(ValueError, test_err) + + +class Corr_Test4(unittest.TestCase): + def setUp(self): + self.shape = [2, 2, 5, 10] + + def test_errors(self): + def test_err(): + np_arr = np.random.rand(*self.shape).astype('float64') + tensor = paddle.to_tensor(np_arr) + corr = paddle.linalg.corrcoef(tensor, ddof=False) + + self.assertRaises(ValueError, test_err) + + +class Corr_Test5(unittest.TestCase): + def setUp(self): + self.shape = [2, 5, 10, 6, 7] + + def test_errors(self): + def test_err(): + np_arr = np.random.rand(*self.shape).astype('float64') + tensor = paddle.to_tensor(np_arr) + corr = paddle.linalg.corrcoef(tensor, ddof=False) + + self.assertRaises(ValueError, test_err) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/linalg.py b/python/paddle/linalg.py index d6b8d6363690a..834b631e5c519 100644 --- a/python/paddle/linalg.py +++ b/python/paddle/linalg.py @@ -16,6 +16,7 @@ from .tensor.linalg import norm # noqa: F401 from .tensor.linalg import eig # noqa: F401 from .tensor.linalg import cov # noqa: F401 +from .tensor.linalg import corrcoef # noqa: F401 from .tensor.linalg import cond # noqa: F401 from .tensor.linalg import matrix_power # noqa: F401 from .tensor.linalg import solve # noqa: F401 @@ -41,6 +42,7 @@ 'norm', 'cond', 'cov', + 'corrcoef', 'inv', 'eig', 'eigvals', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 32902029b8a47..5b6ef0211885e 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -40,6 +40,7 @@ from .linalg import matmul # noqa: F401 from .linalg import dot # noqa: F401 from .linalg import cov # noqa: F401 +from .linalg import corrcoef # noqa: F401 from .linalg import norm # noqa: F401 from .linalg import cond # noqa: F401 from .linalg import transpose # noqa: F401 @@ -275,6 +276,7 @@ 'matmul', 'dot', 'cov', + 'corrcoef', 'norm', 'cond', 'transpose', diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index fef1652040835..4ee266eb6f73c 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -2990,3 +2990,71 @@ def lstsq(x, y, rcond=None, driver=None, name=None): singular_values = paddle.static.data(name='singular_values', shape=[0]) return solution, residuals, rank, singular_values + + +def corrcoef(x, rowvar=True, ddof=False, name=None): + """ + Return Pearson product-moment correlation coefficients. + + Please refer to the documentation for `cov` for more detail. The + relationship between the correlation coefficient matrix, `R`, and the + covariance matrix, `C`, is + + .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } + + The values of `R` are between -1 and 1, inclusive. + + Parameters: + + x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. + rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True + ddof(Bool, optional): Has no effect, do not use. + name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` + + Returns: + + Tensor: The correlation coefficient matrix of the variables. + + Examples: + + .. code-block:: python + + import paddle + + xt = paddle.rand((3,4)) + paddle.linalg.corrcoef(xt) + + ''' + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 1. , -0.73702252, 0.66228950], + [-0.73702258, 1. , -0.77104872], + [ 0.66228974, -0.77104825, 1. ]]) + ''' + + """ + + if ddof is not False: + warnings.warn('ddof have no effect and are deprecated', + DeprecationWarning) + c = cov(x, rowvar) + try: + d = paddle.diag(c) + except ValueError: + # scalar covariance + # nan if incorrect value (nan, inf, 0), 1 otherwise + return c / c + + if paddle.is_complex(d): + d = d.real() + stddev = paddle.sqrt(d) + c /= stddev[:, None] + c /= stddev[None, :] + + # Clip to [-1, 1]. This does not guarantee + if paddle.is_complex(c): + return paddle.complex( + paddle.clip(c.real(), -1, 1), paddle.clip(c.imag(), -1, 1)) + else: + c = paddle.clip(c, -1, 1) + + return c \ No newline at end of file From 45a53eb03701d6511dd5d67657421cd17fe2ed70 Mon Sep 17 00:00:00 2001 From: liqiotng Date: Fri, 18 Mar 2022 00:30:01 +0800 Subject: [PATCH 02/30] corrcoef commit --- python/paddle/tensor/linalg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 4ee266eb6f73c..01610f15c1aa9 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -21,6 +21,7 @@ from ..fluid.layers import transpose, cast # noqa: F401 from ..fluid import layers import paddle +import warnings from paddle.common_ops_import import core from paddle.common_ops_import import VarDesc from paddle import _C_ops @@ -3057,4 +3058,4 @@ def corrcoef(x, rowvar=True, ddof=False, name=None): else: c = paddle.clip(c, -1, 1) - return c \ No newline at end of file + return c From bb5c04d050f9e4cf32a6848ff2f4d9b42a7a01d7 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Tue, 22 Mar 2022 15:53:43 +0800 Subject: [PATCH 03/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 36 +++++-------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 1d35f7e1b5d9c..7b26e65a8c206 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -20,8 +20,8 @@ import warnings -def numpy_corr(np_arr, rowvar=True, ddof=0): - return np.corrcoef(np_arr, rowvar=rowvar, ddof=int(ddof)) +def numpy_corr(np_arr, rowvar=True): + return np.corrcoef(np_arr, rowvar=rowvar) class Corr_Test(unittest.TestCase): @@ -42,8 +42,8 @@ def test_tensor_corr_default(self): for dtype in typelist: np_arr = np.random.rand(*self.shape).astype(dtype) tensor = paddle.to_tensor(np_arr, place=p) - corr = paddle.linalg.corrcoef(tensor, ddof=False) - np_corr = numpy_corr(np_arr, rowvar=True, ddof=0) + corr = paddle.linalg.corrcoef(tensor) + np_corr = numpy_corr(np_arr, rowvar=True) self.assertTrue(np.allclose(np_corr, corr.numpy())) def test_tensor_corr_rowvar(self): @@ -61,28 +61,10 @@ def test_tensor_corr_rowvar(self): for dtype in typelist: np_arr = np.random.rand(*self.shape).astype(dtype) tensor = paddle.to_tensor(np_arr, place=p) - corr = paddle.linalg.corrcoef(tensor, rowvar=False, ddof=False) - np_corr = numpy_corr(np_arr, rowvar=False, ddof=0) + corr = paddle.linalg.corrcoef(tensor, rowvar=False) + np_corr = numpy_corr(np_arr, rowvar=False) self.assertTrue(np.allclose(np_corr, corr.numpy())) - def test_tensor_corr_ddof(self): - typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) - - for idx, p in enumerate(places): - if idx == 0: - paddle.set_device('cpu') - else: - paddle.set_device('gpu') - - for dtype in typelist: - np_arr = np.random.rand(*self.shape).astype(dtype) - tensor = paddle.to_tensor(np_arr, place=p) - corr = paddle.linalg.corrcoef(tensor, ddof=True) - np_corr = numpy_corr(np_arr, rowvar=True, ddof=1) - self.assertTrue(np.allclose(np_corr, corr.numpy())) class Corr_Test2(Corr_Test): @@ -99,7 +81,7 @@ def test_errors(self): def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) - covrr = paddle.linalg.corrcoef(tensor, ddof=False) + covrr = paddle.linalg.corrcoef(tensor) self.assertRaises(ValueError, test_err) @@ -112,7 +94,7 @@ def test_errors(self): def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) - corr = paddle.linalg.corrcoef(tensor, ddof=False) + corr = paddle.linalg.corrcoef(tensor) self.assertRaises(ValueError, test_err) @@ -125,7 +107,7 @@ def test_errors(self): def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) - corr = paddle.linalg.corrcoef(tensor, ddof=False) + corr = paddle.linalg.corrcoef(tensor) self.assertRaises(ValueError, test_err) From 6d88d9ab2f53f9244d7964b3049abe24b55d4af7 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Tue, 22 Mar 2022 15:55:44 +0800 Subject: [PATCH 04/30] Update linalg.py --- python/paddle/tensor/linalg.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 340b3c7527bcb..2134a9d59efe6 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -2997,23 +2997,25 @@ def lstsq(x, y, rcond=None, driver=None, name=None): return solution, residuals, rank, singular_values -def corrcoef(x, rowvar=True, ddof=False, name=None): +def corrcoef(x, rowvar=True, name=None): """ Return Pearson product-moment correlation coefficients. - Please refer to the documentation for `cov` for more detail. The - relationship between the correlation coefficient matrix, `R`, and the - covariance matrix, `C`, is + A correlation coefficient matrix indicate the ccorrelation of each pair variables in the input matrix. + For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the correlation coefficient matrix + element Rij is the correlation of xi and xj. The element Rii is the covariance of xi itself. + + The relationship between the correlation coefficient matrix `R` and the + covariance matrix `C`, is .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } - The values of `R` are between -1 and 1, inclusive. + The values of `R` are between -1 and 1. Parameters: x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True - ddof(Bool, optional): Has no effect, do not use. name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` Returns: @@ -3037,18 +3039,20 @@ def corrcoef(x, rowvar=True, ddof=False, name=None): ''' """ + if len(x.shape) > 2 or len(x.shape) < 1: + raise ValueError( + "Input(x) only support N-D (1<=N<=2) tensor in corrcoef, but received " + "length of Input(input) is %s." % len(x.shape)) + check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'corrcoef') - if ddof is not False: - warnings.warn('ddof have no effect and are deprecated', - DeprecationWarning) c = cov(x, rowvar) - try: - d = paddle.diag(c) - except ValueError: + if (c.ndim == 0): # scalar covariance # nan if incorrect value (nan, inf, 0), 1 otherwise return c / c + d = paddle.diag(c) + if paddle.is_complex(d): d = d.real() stddev = paddle.sqrt(d) From 2d654fd036b8fa8cca828abcd670049cd51c14c5 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Fri, 25 Mar 2022 20:04:04 +0800 Subject: [PATCH 05/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 7b26e65a8c206..b85a41d71959a 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -66,14 +66,21 @@ def test_tensor_corr_rowvar(self): self.assertTrue(np.allclose(np_corr, corr.numpy())) - +# Input(x) only support N-D (1<=N<=2) tensor +# test normal input class Corr_Test2(Corr_Test): def setUp(self): self.shape = [10] +class Corr_Test3(Corr_Test): + def setUp(self): + self.shape = [20, 10] + + # Input(x) only support N-D (1<=N<=2) tensor -class Corr_Test3(unittest.TestCase): +# test error input +class Corr_Test4(unittest.TestCase): def setUp(self): self.shape = [2, 5, 10] @@ -86,7 +93,8 @@ def test_err(): self.assertRaises(ValueError, test_err) -class Corr_Test4(unittest.TestCase): +# Input(x) only support N-D (1<=N<=2) tensor +class Corr_Test5(unittest.TestCase): def setUp(self): self.shape = [2, 2, 5, 10] @@ -99,18 +107,21 @@ def test_err(): self.assertRaises(ValueError, test_err) -class Corr_Test5(unittest.TestCase): +# test unsupported complex input +class Corr_Test6(unittest.TestCase): def setUp(self): - self.shape = [2, 5, 10, 6, 7] + self.shape = [10] def test_errors(self): - def test_err(): - np_arr = np.random.rand(*self.shape).astype('float64') - tensor = paddle.to_tensor(np_arr) - corr = paddle.linalg.corrcoef(tensor) + with program_guard(Program(), Program()): + def test_err(): + np_arr = np.random.rand(*self.shape).astype('complex64') + tensor = paddle.to_tensor(np_arr) + covrr = paddle.linalg.corrcoef(tensor) - self.assertRaises(ValueError, test_err) + self.assertRaises(TypeError, test_err) if __name__ == '__main__': unittest.main() + From 6efaca65980fe65144c513bde469b86c2dc09365 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Fri, 25 Mar 2022 23:52:50 +0800 Subject: [PATCH 06/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index b85a41d71959a..542fd0eb813ce 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -18,7 +18,7 @@ import six import paddle import warnings - +from paddle.fluid import Program, program_guard def numpy_corr(np_arr, rowvar=True): return np.corrcoef(np_arr, rowvar=rowvar) From fe98fcda85543b4c83b768fff330958da79fa754 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Sat, 26 Mar 2022 12:58:47 +0800 Subject: [PATCH 07/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 542fd0eb813ce..8b5de0c763f71 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -18,7 +18,6 @@ import six import paddle import warnings -from paddle.fluid import Program, program_guard def numpy_corr(np_arr, rowvar=True): return np.corrcoef(np_arr, rowvar=rowvar) @@ -108,18 +107,27 @@ def test_err(): # test unsupported complex input -class Corr_Test6(unittest.TestCase): - def setUp(self): - self.shape = [10] +class Cov_Test7(unittest.TestCase): def test_errors(self): - with program_guard(Program(), Program()): - def test_err(): - np_arr = np.random.rand(*self.shape).astype('complex64') - tensor = paddle.to_tensor(np_arr) - covrr = paddle.linalg.corrcoef(tensor) + paddle.enable_static() + + x1 = fluid.data(name='x1', shape=[2], dtype='complex128') + self.assertRaises(TypeError, paddle.linalg.corrcoef,x=x1) + + paddle.disable_static() + - self.assertRaises(TypeError, test_err) +# test unsupported complex input +class Cov_Test6(unittest.TestCase): + + def test_errors(self): + paddle.enable_static() + + x1 = fluid.data(name='x1', shape=[2,2], dtype='complex64') + self.assertRaises(TypeError, paddle.linalg.corrcoef,x=x1) + + paddle.disable_static() if __name__ == '__main__': From f39ad0788440c5c995a35ecdb342fa5a068fe066 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:46:20 +0800 Subject: [PATCH 08/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 8b5de0c763f71..bb4c67c74ffb5 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -107,26 +107,26 @@ def test_err(): # test unsupported complex input -class Cov_Test7(unittest.TestCase): +class Cov_Test6(unittest.TestCase): def test_errors(self): paddle.enable_static() - + x1 = fluid.data(name='x1', shape=[2], dtype='complex128') - self.assertRaises(TypeError, paddle.linalg.corrcoef,x=x1) - + self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x1) + paddle.disable_static() - + # test unsupported complex input -class Cov_Test6(unittest.TestCase): +class Cov_Test7(unittest.TestCase): def test_errors(self): paddle.enable_static() - - x1 = fluid.data(name='x1', shape=[2,2], dtype='complex64') - self.assertRaises(TypeError, paddle.linalg.corrcoef,x=x1) - + + x2 = fluid.data(name='x2', shape=[2, 2], dtype='complex64') + self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x2) + paddle.disable_static() From 794e36843b79c1272c2f972843327bb6ba649f65 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Sat, 26 Mar 2022 23:05:06 +0800 Subject: [PATCH 09/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index bb4c67c74ffb5..370fae6664a68 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -19,6 +19,7 @@ import paddle import warnings + def numpy_corr(np_arr, rowvar=True): return np.corrcoef(np_arr, rowvar=rowvar) From af6b514825c5f1acc989214a0ab2e61c2d4f0847 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Sun, 27 Mar 2022 14:30:32 +0800 Subject: [PATCH 10/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 370fae6664a68..a657ce4c222a4 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -67,7 +67,6 @@ def test_tensor_corr_rowvar(self): # Input(x) only support N-D (1<=N<=2) tensor -# test normal input class Corr_Test2(Corr_Test): def setUp(self): self.shape = [10] @@ -79,7 +78,6 @@ def setUp(self): # Input(x) only support N-D (1<=N<=2) tensor -# test error input class Corr_Test4(unittest.TestCase): def setUp(self): self.shape = [2, 5, 10] @@ -89,7 +87,6 @@ def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) covrr = paddle.linalg.corrcoef(tensor) - self.assertRaises(ValueError, test_err) @@ -103,31 +100,24 @@ def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) corr = paddle.linalg.corrcoef(tensor) - self.assertRaises(ValueError, test_err) # test unsupported complex input class Cov_Test6(unittest.TestCase): - def test_errors(self): paddle.enable_static() - x1 = fluid.data(name='x1', shape=[2], dtype='complex128') self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x1) - paddle.disable_static() # test unsupported complex input class Cov_Test7(unittest.TestCase): - def test_errors(self): paddle.enable_static() - x2 = fluid.data(name='x2', shape=[2, 2], dtype='complex64') self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x2) - paddle.disable_static() From 3d8e0b02a31cef453ba19640768d5d4152794809 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Sun, 27 Mar 2022 22:04:50 +0800 Subject: [PATCH 11/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index a657ce4c222a4..ae8586de52634 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -87,6 +87,7 @@ def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) covrr = paddle.linalg.corrcoef(tensor) + self.assertRaises(ValueError, test_err) @@ -100,6 +101,7 @@ def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) corr = paddle.linalg.corrcoef(tensor) + self.assertRaises(ValueError, test_err) From 760b858f4fd853989c48bc79510894ca234771a7 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Mon, 28 Mar 2022 09:45:36 +0800 Subject: [PATCH 12/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index ae8586de52634..82e95f8fa3c9a 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2022 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. @@ -91,22 +91,8 @@ def test_err(): self.assertRaises(ValueError, test_err) -# Input(x) only support N-D (1<=N<=2) tensor -class Corr_Test5(unittest.TestCase): - def setUp(self): - self.shape = [2, 2, 5, 10] - - def test_errors(self): - def test_err(): - np_arr = np.random.rand(*self.shape).astype('float64') - tensor = paddle.to_tensor(np_arr) - corr = paddle.linalg.corrcoef(tensor) - - self.assertRaises(ValueError, test_err) - - # test unsupported complex input -class Cov_Test6(unittest.TestCase): +class Corr_Test5(unittest.TestCase): def test_errors(self): paddle.enable_static() x1 = fluid.data(name='x1', shape=[2], dtype='complex128') @@ -115,7 +101,7 @@ def test_errors(self): # test unsupported complex input -class Cov_Test7(unittest.TestCase): +class Corr_Test6(unittest.TestCase): def test_errors(self): paddle.enable_static() x2 = fluid.data(name='x2', shape=[2, 2], dtype='complex64') From 510c6e72bd1e9cf70652427bbb1443374bbaa034 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Mon, 28 Mar 2022 15:17:58 +0800 Subject: [PATCH 13/30] Update test_corr.py From 677ba6f1a08e1ac0510cc15a4ceb2a7fd2cec449 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Mon, 28 Mar 2022 18:47:22 +0800 Subject: [PATCH 14/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 82e95f8fa3c9a..2270f05ad06d6 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -87,7 +87,7 @@ def test_err(): np_arr = np.random.rand(*self.shape).astype('float64') tensor = paddle.to_tensor(np_arr) covrr = paddle.linalg.corrcoef(tensor) - + self.assertRaises(ValueError, test_err) @@ -111,4 +111,3 @@ def test_errors(self): if __name__ == '__main__': unittest.main() - From 6b8e3d36b7250a5a26caed7be3ac4ae006eb323f Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:14:31 +0800 Subject: [PATCH 15/30] Update linalg.py --- python/paddle/tensor/linalg.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index b27a947e0250b..58d84060df8e5 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -3005,8 +3005,7 @@ def lstsq(x, y, rcond=None, driver=None, name=None): def corrcoef(x, rowvar=True, name=None): """ - Return Pearson product-moment correlation coefficients. - + A correlation coefficient matrix indicate the ccorrelation of each pair variables in the input matrix. For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the correlation coefficient matrix element Rij is the correlation of xi and xj. The element Rii is the covariance of xi itself. @@ -3014,7 +3013,7 @@ def corrcoef(x, rowvar=True, name=None): The relationship between the correlation coefficient matrix `R` and the covariance matrix `C`, is - .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } + .. math:: R_{ij} = C_{ij} / sqrt{ C_{ii} * C_{jj} } The values of `R` are between -1 and 1. @@ -3026,23 +3025,22 @@ def corrcoef(x, rowvar=True, name=None): Returns: - Tensor: The correlation coefficient matrix of the variables. + The correlation coefficient matrix of the variables. Examples: .. code-block:: python - + :name: code-example1 + import paddle xt = paddle.rand((3,4)) - paddle.linalg.corrcoef(xt) + print(paddle.linalg.corrcoef(xt)) - ''' - Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - [[ 1. , -0.73702252, 0.66228950], - [-0.73702258, 1. , -0.77104872], - [ 0.66228974, -0.77104825, 1. ]]) - ''' + # Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + # [[ 1. , -0.73702252, 0.66228950], + # [-0.73702258, 1. , -0.77104872], + # [ 0.66228974, -0.77104825, 1. ]]) """ if len(x.shape) > 2 or len(x.shape) < 1: From 49227fbca08ce7b93ea8d3f315bcb46e9689f461 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Thu, 7 Apr 2022 19:02:32 +0800 Subject: [PATCH 16/30] Update linalg.py --- python/paddle/tensor/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 16129fddfc38d..7661a6c8098f7 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -3053,7 +3053,7 @@ def corrcoef(x, rowvar=True, name=None): The relationship between the correlation coefficient matrix `R` and the covariance matrix `C`, is - .. math:: R_{ij} = C_{ij} / sqrt{ C_{ii} * C_{jj} } + .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } The values of `R` are between -1 and 1. From a80e7a52b1985f2c7fd106a6588b5e782e484a31 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Fri, 8 Apr 2022 21:31:43 +0800 Subject: [PATCH 17/30] Update linalg.py --- python/paddle/tensor/linalg.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index ddd516f96e0c1..a7b57fd3e28e7 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -3064,7 +3064,7 @@ def lstsq(x, y, rcond=None, driver=None, name=None): def corrcoef(x, rowvar=True, name=None): """ - A correlation coefficient matrix indicate the ccorrelation of each pair variables in the input matrix. + A correlation coefficient matrix indicate the correlation of each pair variables in the input matrix. For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the correlation coefficient matrix element Rij is the correlation of xi and xj. The element Rii is the covariance of xi itself. @@ -3078,27 +3078,26 @@ def corrcoef(x, rowvar=True, name=None): Parameters: x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. - rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True - name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` + rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True. + name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name`. Returns: The correlation coefficient matrix of the variables. Examples: - - .. code-block:: python - :name: code-example1 + .. code-block:: python + :name: code-example1 - import paddle + import paddle - xt = paddle.rand((3,4)) - print(paddle.linalg.corrcoef(xt)) + xt = paddle.rand((3,4)) + print(paddle.linalg.corrcoef(xt)) - # Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[ 1. , -0.73702252, 0.66228950], - # [-0.73702258, 1. , -0.77104872], - # [ 0.66228974, -0.77104825, 1. ]]) + # Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + # [[ 1. , -0.73702252, 0.66228950], + # [-0.73702258, 1. , -0.77104872], + # [ 0.66228974, -0.77104825, 1. ]]) """ if len(x.shape) > 2 or len(x.shape) < 1: From d63e2dc4feb215f783d4904a8b37a3ad0652352b Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Tue, 12 Apr 2022 16:18:57 +0800 Subject: [PATCH 18/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 2270f05ad06d6..7571bc172f244 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -92,21 +92,20 @@ def test_err(): # test unsupported complex input -class Corr_Test5(unittest.TestCase): +class Corr_Comeplex_Test(unittest.TestCase): + def setUp(self): + self.dtype = 'complex128' + def test_errors(self): paddle.enable_static() - x1 = fluid.data(name='x1', shape=[2], dtype='complex128') + x1 = fluid.data(name=self.dtype, shape=[2], dtype=self.dtype) self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x1) paddle.disable_static() +class Corr_Test5(Corr_Comeplex_Test): -# test unsupported complex input -class Corr_Test6(unittest.TestCase): - def test_errors(self): - paddle.enable_static() - x2 = fluid.data(name='x2', shape=[2, 2], dtype='complex64') - self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x2) - paddle.disable_static() + def setUp(self): + self.dtype = 'complex64' if __name__ == '__main__': From 9b66604e517d3b0e64768baf30e37a832c331d47 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Tue, 12 Apr 2022 19:47:47 +0800 Subject: [PATCH 19/30] Update test_corr.py --- .../paddle/fluid/tests/unittests/test_corr.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 7571bc172f244..2aa97703eb300 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -20,8 +20,8 @@ import warnings -def numpy_corr(np_arr, rowvar=True): - return np.corrcoef(np_arr, rowvar=rowvar) +def numpy_corr(np_arr, rowvar=True,dtype='float64'): + return np.corrcoef(np_arr, rowvar=rowvar,dtype=dtype) class Corr_Test(unittest.TestCase): @@ -29,7 +29,7 @@ def setUp(self): self.shape = [20, 10] def test_tensor_corr_default(self): - typelist = ['float64'] + typelist = ['float64','float32'] places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) @@ -43,11 +43,11 @@ def test_tensor_corr_default(self): np_arr = np.random.rand(*self.shape).astype(dtype) tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) - np_corr = numpy_corr(np_arr, rowvar=True) - self.assertTrue(np.allclose(np_corr, corr.numpy())) + np_corr = numpy_corr(np_arr, rowvar=True,dtype=dtype) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-6)) def test_tensor_corr_rowvar(self): - typelist = ['float64'] + typelist = ['float64','float32'] places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) @@ -62,8 +62,8 @@ def test_tensor_corr_rowvar(self): np_arr = np.random.rand(*self.shape).astype(dtype) tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) - np_corr = numpy_corr(np_arr, rowvar=False) - self.assertTrue(np.allclose(np_corr, corr.numpy())) + np_corr = numpy_corr(np_arr, rowvar=False,dtype=dtype) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-6)) # Input(x) only support N-D (1<=N<=2) tensor @@ -102,6 +102,7 @@ def test_errors(self): self.assertRaises(TypeError, paddle.linalg.corrcoef, x=x1) paddle.disable_static() + class Corr_Test5(Corr_Comeplex_Test): def setUp(self): From 46b90216ff27aa74b545df550587091ea28e4dd9 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Wed, 13 Apr 2022 14:08:51 +0800 Subject: [PATCH 20/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 2aa97703eb300..bc57eb1a6d908 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -44,7 +44,7 @@ def test_tensor_corr_default(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-6)) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-5)) def test_tensor_corr_rowvar(self): typelist = ['float64','float32'] @@ -63,7 +63,7 @@ def test_tensor_corr_rowvar(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-6)) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-5)) # Input(x) only support N-D (1<=N<=2) tensor From f631db9d256654180026bc2f00a2751e15f34ce0 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Thu, 14 Apr 2022 09:17:31 +0800 Subject: [PATCH 21/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index bc57eb1a6d908..a525407af86b9 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -44,7 +44,7 @@ def test_tensor_corr_default(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-5)) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-4,rtol=1.e-4)) def test_tensor_corr_rowvar(self): typelist = ['float64','float32'] @@ -63,7 +63,7 @@ def test_tensor_corr_rowvar(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-5)) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-4,rtol=1.e-4)) # Input(x) only support N-D (1<=N<=2) tensor From dde2566a699f42c0172429134d6dcb9a580d6b1e Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Thu, 14 Apr 2022 20:13:58 +0800 Subject: [PATCH 22/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index a525407af86b9..d70d57791f4d6 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -44,7 +44,7 @@ def test_tensor_corr_default(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-4,rtol=1.e-4)) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-2)) def test_tensor_corr_rowvar(self): typelist = ['float64','float32'] @@ -63,7 +63,7 @@ def test_tensor_corr_rowvar(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-4,rtol=1.e-4)) + self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-2)) # Input(x) only support N-D (1<=N<=2) tensor From 67be88e70f3fc0e1b7e9dbb3a191c1cf07296f29 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Mon, 18 Apr 2022 08:41:53 +0800 Subject: [PATCH 23/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index d70d57791f4d6..6f7fc64bb172b 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -104,7 +104,6 @@ def test_errors(self): class Corr_Test5(Corr_Comeplex_Test): - def setUp(self): self.dtype = 'complex64' From 552c07ba5fb7b9b5cc4e4ad85514f08e3d40573f Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Mon, 18 Apr 2022 20:13:13 +0800 Subject: [PATCH 24/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 6f7fc64bb172b..0805c63fbf79a 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -20,7 +20,7 @@ import warnings -def numpy_corr(np_arr, rowvar=True,dtype='float64'): +def numpy_corr(np_arr, rowvar=True, dtype='float64'): return np.corrcoef(np_arr, rowvar=rowvar,dtype=dtype) @@ -29,7 +29,7 @@ def setUp(self): self.shape = [20, 10] def test_tensor_corr_default(self): - typelist = ['float64','float32'] + typelist = ['float64', 'float32'] places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) @@ -43,11 +43,11 @@ def test_tensor_corr_default(self): np_arr = np.random.rand(*self.shape).astype(dtype) tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) - np_corr = numpy_corr(np_arr, rowvar=True,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-2)) + np_corr = numpy_corr(np_arr, rowvar=True, dtype=dtype) + self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-2)) def test_tensor_corr_rowvar(self): - typelist = ['float64','float32'] + typelist = ['float64', 'float32'] places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) @@ -62,8 +62,8 @@ def test_tensor_corr_rowvar(self): np_arr = np.random.rand(*self.shape).astype(dtype) tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) - np_corr = numpy_corr(np_arr, rowvar=False,dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(),atol=1.e-2)) + np_corr = numpy_corr(np_arr, rowvar=False, dtype=dtype) + self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-2)) # Input(x) only support N-D (1<=N<=2) tensor From 3652bbd9b2dc4700cb41a0de6e966031d1879421 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Tue, 19 Apr 2022 07:34:51 +0800 Subject: [PATCH 25/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 0805c63fbf79a..85c2801b97bab 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -21,7 +21,7 @@ def numpy_corr(np_arr, rowvar=True, dtype='float64'): - return np.corrcoef(np_arr, rowvar=rowvar,dtype=dtype) + return np.corrcoef(np_arr, rowvar=rowvar, dtype=dtype) class Corr_Test(unittest.TestCase): From decb986d5308b73c5b680abf33f29ea396b14a0a Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Sun, 24 Apr 2022 16:09:21 +0800 Subject: [PATCH 26/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 85c2801b97bab..25f17fc75d538 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -44,7 +44,7 @@ def test_tensor_corr_default(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True, dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-2)) + self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-4)) def test_tensor_corr_rowvar(self): typelist = ['float64', 'float32'] @@ -63,7 +63,7 @@ def test_tensor_corr_rowvar(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False, dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-2)) + self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-4)) # Input(x) only support N-D (1<=N<=2) tensor From 69064d259ebec784350b9ce25de82d60251d71eb Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Wed, 27 Apr 2022 13:21:27 +0800 Subject: [PATCH 27/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 25f17fc75d538..fbf31f6351f68 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -44,7 +44,10 @@ def test_tensor_corr_default(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True, dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-4)) + if dtype=='float32': + self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-5)) + else: + self.assertTrue(np.allclose(np_corr, corr.numpy())) def test_tensor_corr_rowvar(self): typelist = ['float64', 'float32'] @@ -63,7 +66,10 @@ def test_tensor_corr_rowvar(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False, dtype=dtype) - self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-4)) + if dtype=='float32': + self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-5)) + else: + self.assertTrue(np.allclose(np_corr, corr.numpy())) # Input(x) only support N-D (1<=N<=2) tensor From 7c5efcf0d566a71ef18cd476b24831c50603c7b0 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Wed, 27 Apr 2022 19:42:35 +0800 Subject: [PATCH 28/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index fbf31f6351f68..2c350b512a64a 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -45,7 +45,9 @@ def test_tensor_corr_default(self): corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True, dtype=dtype) if dtype=='float32': - self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-5)) + self.assertTrue( + np.allclose( + np_corr, corr.numpy(), atol=1.e-5)) else: self.assertTrue(np.allclose(np_corr, corr.numpy())) @@ -67,7 +69,9 @@ def test_tensor_corr_rowvar(self): corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False, dtype=dtype) if dtype=='float32': - self.assertTrue(np.allclose(np_corr, corr.numpy(), atol=1.e-5)) + self.assertTrue( + np.allclose( + np_corr, corr.numpy(), atol=1.e-5)) else: self.assertTrue(np.allclose(np_corr, corr.numpy())) From 4fca073db1b01322fa8a7d3e99a399530bf9f08f Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Fri, 29 Apr 2022 14:03:23 +0800 Subject: [PATCH 29/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index 2c350b512a64a..d0cb8507ffa77 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -44,7 +44,7 @@ def test_tensor_corr_default(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor) np_corr = numpy_corr(np_arr, rowvar=True, dtype=dtype) - if dtype=='float32': + if dtype == 'float32': self.assertTrue( np.allclose( np_corr, corr.numpy(), atol=1.e-5)) @@ -68,7 +68,7 @@ def test_tensor_corr_rowvar(self): tensor = paddle.to_tensor(np_arr, place=p) corr = paddle.linalg.corrcoef(tensor, rowvar=False) np_corr = numpy_corr(np_arr, rowvar=False, dtype=dtype) - if dtype=='float32': + if dtype == 'float32': self.assertTrue( np.allclose( np_corr, corr.numpy(), atol=1.e-5)) From 2255acf8e40685ccc82eb5094f9989d6e9f67195 Mon Sep 17 00:00:00 2001 From: liqitong-a <71805392+liqitong-a@users.noreply.github.com> Date: Thu, 5 May 2022 15:49:01 +0800 Subject: [PATCH 30/30] Update test_corr.py --- python/paddle/fluid/tests/unittests/test_corr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_corr.py b/python/paddle/fluid/tests/unittests/test_corr.py index d0cb8507ffa77..99fd21c047b07 100644 --- a/python/paddle/fluid/tests/unittests/test_corr.py +++ b/python/paddle/fluid/tests/unittests/test_corr.py @@ -26,7 +26,7 @@ def numpy_corr(np_arr, rowvar=True, dtype='float64'): class Corr_Test(unittest.TestCase): def setUp(self): - self.shape = [20, 10] + self.shape = [4, 5] def test_tensor_corr_default(self): typelist = ['float64', 'float32'] @@ -84,13 +84,13 @@ def setUp(self): class Corr_Test3(Corr_Test): def setUp(self): - self.shape = [20, 10] + self.shape = [4, 5] # Input(x) only support N-D (1<=N<=2) tensor class Corr_Test4(unittest.TestCase): def setUp(self): - self.shape = [2, 5, 10] + self.shape = [2, 5, 2] def test_errors(self): def test_err():