Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon No.25】为 Paddle 新增 nanquantile 数学计算API #41343

Merged
merged 20 commits into from
Apr 15, 2022
Merged
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
126720b
add nanquantile and fix quantile bug
Asthestarsfalll Apr 2, 2022
dc89e91
add unittest of nanquantile
Asthestarsfalll Apr 2, 2022
908548f
fix bug of test_quantile
Asthestarsfalll Apr 2, 2022
34f7742
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Asthestarsfalll Apr 2, 2022
b63469e
fix typo
Asthestarsfalll Apr 2, 2022
34f4df9
fig type error
Asthestarsfalll Apr 2, 2022
3306abc
update the code
Asthestarsfalll Apr 2, 2022
56e3be8
fix error
Asthestarsfalll Apr 2, 2022
1b0c13d
refactor unittest and update example code
Asthestarsfalll Apr 7, 2022
350ccad
reduce data scale
Asthestarsfalll Apr 12, 2022
09a5c25
Merge branch 'PaddlePaddle:develop' into nanquantile
Asthestarsfalll Apr 12, 2022
0e2ef42
update
Asthestarsfalll Apr 12, 2022
78d332f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Asthestarsfalll Apr 12, 2022
34ab31f
Merge branch 'nanquantile' of https://github.com/Asthestarsfalll/Padd…
Asthestarsfalll Apr 12, 2022
31e1734
add nanquantile to __all__
Asthestarsfalll Apr 13, 2022
4669518
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Asthestarsfalll Apr 13, 2022
4250918
Merge branch 'PaddlePaddle:develop' into nanquantile
Asthestarsfalll Apr 13, 2022
86d65bb
add missing comma
Asthestarsfalll Apr 14, 2022
b3bc441
Merge branch 'nanquantile' of https://github.com/Asthestarsfalll/Padd…
Asthestarsfalll Apr 14, 2022
26c993f
Merge branch 'PaddlePaddle:develop' into nanquantile
Asthestarsfalll Apr 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
@@ -323,6 +323,7 @@
from .tensor.stat import numel # noqa: F401
from .tensor.stat import median # noqa: F401
from .tensor.stat import quantile # noqa: F401
from .tensor.stat import nanquantile # noqa: F401
from .device import get_cudnn_version # noqa: F401
from .device import set_device # noqa: F401
from .device import get_device # noqa: F401
237 changes: 237 additions & 0 deletions python/paddle/fluid/tests/unittests/test_nanquantile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
# 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.
# 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.

from __future__ import print_function

import unittest
import numpy as np
import paddle


class TestNaNQuantile(unittest.TestCase):
"""
This class is used for numerical precision testing. If there is
a corresponding numpy API, the precision comparison can be performed directly.
Otherwise, it needs to be verified by numpy implementated function.
"""

def setUp(self):
np.random.seed(2022)
self.input_data = np.random.rand(6, 7, 8, 9, 10)

# Test correctness when q and axis are set.
def test_nanquantile_single_q(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.5, axis=2)
np_res = np.nanquantile(self.input_data, q=0.5, axis=2)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness for default axis.
def test_nanquantile_with_no_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.35)
np_res = np.nanquantile(self.input_data, q=0.35)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness for multiple axis.
def test_nanquantile_with_multi_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.75, axis=[0, 2, 3])
np_res = np.nanquantile(self.input_data, q=0.75, axis=[0, 2, 3])
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when keepdim is set.
def test_nanquantile_with_keepdim(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.35, axis=4, keepdim=True)
np_res = np.nanquantile(self.input_data, q=0.35, axis=4, keepdims=True)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when all parameters are set.
def test_nanquantile_with_keepdim_and_multiple_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.1, axis=[1, 4], keepdim=True)
np_res = np.nanquantile(self.input_data, q=0.1, axis=[1, 4], keepdims=True)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when q = 0.
def test_nanquantile_with_boundary_q(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0, axis=3)
np_res = np.nanquantile(self.input_data, q=0, axis=3)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when input includes NaN.
def test_nanquantile_include_NaN(self):
input_data = np.random.randn(2, 3, 4)
input_data[0, 1, 1] = np.nan
x = paddle.to_tensor(input_data)
paddle_res = paddle.nanquantile(x, q=0.35, axis=0)
np_res = np.nanquantile(x, q=0.35, axis=0)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res, equal_nan=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单测和quantile几乎一摸一样,但nanquantile更要侧重对NAN的测试:

  • 可以修改一下已有单测,每个Class里增加不同位置NAN的测试。包括一个或者多个NAN
  • 缺少全是NAN的测试

因为两份单测非常类似,如果可以的话,看如何更好地进行复用(非强制要求),如

test_case(self.x)
test_case(self.x, [])
test_case(self.x, -1)
test_case(self.x, keepdim=True)
test_case(self.x, 2, keepdim=True)
test_case(self.x, [0, 2])
test_case(self.x, (0, 2))
test_case(self.x, [0, 1, 2, 3])
paddle.enable_static()

_test_static_graph('amax')
_test_static_graph('amin')
_test_static_graph('max')
_test_static_graph('min')



class TestNaNQuantileMuitlpleQ(unittest.TestCase):
"""
This class is used to test multiple input of q.
"""

def setUp(self):
np.random.seed(2022)
self.input_data = np.random.rand(10, 3, 4, 5, 4)

def test_nanquantile(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=[0.3, 0.44], axis=-2)
np_res = np.nanquantile(self.input_data, q=[0.3, 0.44], axis=-2)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

def test_nanquantile_multiple_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=[0.2, 0.67], axis=[1, -1])
np_res = np.nanquantile(self.input_data, q=[0.2, 0.67], axis=[1, -1])
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

def test_nanquantile_multiple_axis_keepdim(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(
x, q=[0.1, 0.2, 0.3], axis=[1, 2], keepdim=True)
np_res = np.nanquantile(
self.input_data, q=[0.1, 0.2, 0.3], axis=[1, 2], keepdims=True)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))


class TestNaNQuantileError(unittest.TestCase):
"""
This class is used to test that exceptions are thrown correctly.
Validity of all parameter values and types should be considered.
"""

def setUp(self):
self.x = paddle.randn((2, 3, 4))

def test_errors(self):
# Test error when q > 1
def test_q_range_error_1():
paddle_res = paddle.nanquantile(self.x, q=1.5)

self.assertRaises(ValueError, test_q_range_error_1)

# Test error when q < 0
def test_q_range_error_2():
paddle_res = paddle.nanquantile(self.x, q=[0.2, -0.3])

self.assertRaises(ValueError, test_q_range_error_2)

# Test error with no valid q
def test_q_range_error_3():
paddle_res = paddle.nanquantile(self.x, q=[])

self.assertRaises(ValueError, test_q_range_error_3)

# Test error when x is not Tensor
def test_x_type_error():
x = [1, 3, 4]
paddle_res = paddle.nanquantile(x, q=0.9)

self.assertRaises(TypeError, test_x_type_error)

# Test error when scalar axis is not int
def test_axis_type_error_1():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=0.4)

self.assertRaises(ValueError, test_axis_type_error_1)

# Test error when axis in List is not int
def test_axis_type_error_2():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[1, 0.4])

self.assertRaises(ValueError, test_axis_type_error_2)

# Test error when axis not in [-D, D)
def test_axis_value_error_1():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=10)

self.assertRaises(ValueError, test_axis_value_error_1)

# Test error when axis not in [-D, D)
def test_axis_value_error_2():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[1, -10])

self.assertRaises(ValueError, test_axis_value_error_2)

# Test error with no valid axis
def test_axis_value_error_3():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[])

self.assertRaises(ValueError, test_axis_value_error_3)


class TestNaNQuantileRuntime(unittest.TestCase):
"""
This class is used to test the API could run correctly with
different devices, different data types, and dygraph/static mode.
"""

def setUp(self):
np.random.seed(2022)
self.input_data = np.random.rand(6, 7, 8, 9, 10)
self.dtypes = ['float32', 'float64']
self.devices = ['cpu']
if paddle.device.is_compiled_with_cuda():
self.devices.append('gpu')

def test_dygraph(self):
paddle.disable_static()
for device in self.devices:
# Check different devices
paddle.set_device(device)
for dtype in self.dtypes:
# Check different dtypes
np_input_data = self.input_data.astype(dtype)
x = paddle.to_tensor(np_input_data, dtype=dtype)
paddle_res = paddle.nanquantile(x, q=0.5, axis=2)
np_res = np.nanquantile(np_input_data, q=0.5, axis=2)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

def test_static(self):
paddle.enable_static()
for device in self.devices:
x = paddle.static.data(
name="x", shape=self.input_data.shape, dtype=paddle.float32)
x_fp64 = paddle.static.data(
name="x_fp64",
shape=self.input_data.shape,
dtype=paddle.float64)

results = paddle.nanquantile(x, q=0.5, axis=2)
np_input_data = self.input_data.astype('float32')
results_fp64 = paddle.nanquantile(x_fp64, q=0.5, axis=2)
np_input_data_fp64 = self.input_data.astype('float64')

exe = paddle.static.Executor(device)
paddle_res, paddle_res_fp64 = exe.run(
paddle.static.default_main_program(),
feed={"x": np_input_data,
"x_fp64": np_input_data_fp64},
fetch_list=[results, results_fp64])
np_res = np.nanquantile(np_input_data, q=0.5, axis=2)
np_res_fp64 = np.nanquantile(np_input_data_fp64, q=0.5, axis=2)
self.assertTrue(
np.allclose(paddle_res, np_res) and np.allclose(paddle_res_fp64,
np_res_fp64))


if __name__ == '__main__':
unittest.main()
3 changes: 2 additions & 1 deletion python/paddle/fluid/tests/unittests/test_quantile.py
Original file line number Diff line number Diff line change
@@ -78,7 +78,8 @@ def test_quantile_include_NaN(self):
input_data[0, 1, 1] = np.nan
x = paddle.to_tensor(input_data)
paddle_res = paddle.quantile(x, q=0.35, axis=0)
self.assertTrue(paddle.isnan(paddle_res[1, 1]))
np_res = np.quantile(x, q=0.35, axis=0)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res, equal_nan=True))


class TestQuantileMuitlpleQ(unittest.TestCase):
2 changes: 2 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
@@ -260,6 +260,7 @@
from .stat import numel # noqa: F401
from .stat import median # noqa: F401
from .stat import quantile # noqa: F401
from .stat import nanquantile # noqa: F401

from .to_string import set_printoptions # noqa: F401

@@ -442,6 +443,7 @@
'numel',
'median',
'quantile',
'nanquantile'
Copy link
Contributor

@luotao1 luotao1 Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

又少了一个逗号了。。
可以通过看日志发现:https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/5392118/job/14041205


2022-04-14 01:51:31 There are 3 approved errors.
2022-04-14 01:51:31 ****************
2022-04-14 01:51:32 API Difference is: 
2022-04-14 01:51:32 - paddle.Tensor.is_complex (ArgSpec(args=['x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={}), ('document', '9d4dc47b098ce34e65cc23e14ad02281'))
2022-04-14 01:51:32 - paddle.Tensor.quantile (ArgSpec(args=['x', 'q', 'axis', 'keepdim'], varargs=None, varkw=None, defaults=(None, False), kwonlyargs=[], kwonlydefaults=None, annotations={}), ('document', 'd6e25fbeb7751f8e57ad215209f36e00'))
2022-04-14 01:51:32 ?                                                                                                                                                                                          ^ ^^^^^^^   ^^^^^ ^^^^^^^ ^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,已修改。当时看的时候还以为是__all__里的。。

'is_complex',
'is_integer',
'rank',
238 changes: 170 additions & 68 deletions python/paddle/tensor/stat.py
Original file line number Diff line number Diff line change
@@ -342,13 +342,14 @@ def median(x, axis=None, keepdim=False, name=None):
return out_tensor


def quantile(x, q, axis=None, keepdim=False):
def _compute_quantile(x, q, axis=None, keepdim=False, ignore_nan=False):
"""
Compute the quantile of the input along the specified axis.
Args:
Args:
x (Tensor): The input Tensor, it's data type can be float32, float64.
q (int|float|list): The q for calculate quantile, which should be in range [0, 1]. If q is a list,
q (int|float|list): The q for calculate quantile, which should be in range [0, 1]. If q is a list,
each q will be calculated and the first dimension of output is same to the number of ``q`` .
axis (int|list, optional): The axis along which to calculate quantile. ``axis`` should be int or list of int.
``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
@@ -360,37 +361,28 @@ def quantile(x, q, axis=None, keepdim=False):
the output Tensor is the same as ``x`` except in the reduced
dimensions(it is of size 1 in this case). Otherwise, the shape of
the output Tensor is squeezed in ``axis`` . Default is False.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
ignore_nan: (bool, optional): Whether to ignore NaN of input Tensor.
If ``ignore_nan`` is True, it will calculate nanquantile.
Otherwise it will calculate quantile. Default is False.
Returns:
Tensor, results of quantile along ``axis`` of ``x``. If data type of ``x`` is float64, data type of results will be float64, otherwise data type will be float32.
Examples:
.. code-block:: python
import paddle
x = paddle.randn((2,3))
#[[-1.28740597, 0.49533170, -1.00698614],
# [-1.11656201, -1.01010525, -2.23457789]])
y1 = paddle.quantile(x, q=0.5, axis=[0, 1])
# y1 = -1.06333363
y2 = paddle.quantile(x, q=0.5, axis=1)
# y2 = [-1.00698614, -1.11656201]
y3 = paddle.quantile(x, q=[0.3, 0.5], axis=1)
# y3 =[[-1.11915410, -1.56376839],
# [-1.00698614, -1.11656201]]
y4 = paddle.quantile(x, q=0.8, axis=1, keepdim=True)
# y4 = [[-0.10559537],
# [-1.05268800]])
Tensor, results of quantile along ``axis`` of ``x``.
In order to obtain higher precision, data type of results will be float64.
"""
# Validate x
if not isinstance(x, Variable):
raise TypeError("input x should be a Tensor.")

# Validate q
if isinstance(q, (int, float)):
q = [q]
elif isinstance(q, (list, tuple)):
if len(q) <= 0:
raise ValueError("q should not be empty")
else:
raise TypeError("Type of q should be int, float, list or tuple.")

# Validate axis
dims = len(x.shape)
out_shape = list(x.shape)
if axis is None:
@@ -399,7 +391,7 @@ def quantile(x, q, axis=None, keepdim=False):
out_shape = [1] * dims
else:
if isinstance(axis, list):
if (len(axis) <= 0):
if len(axis) <= 0:
raise ValueError("axis should not be empty")
axis_src, axis_dst = [], []
for axis_single in axis:
@@ -424,54 +416,164 @@ def quantile(x, q, axis=None, keepdim=False):
if axis < 0:
axis += dims
out_shape[axis] = 1

mask = x.isnan()
valid_counts = mask.logical_not().sum(
axis=axis, keepdim=True, dtype='float64')

indices = []
if isinstance(q, (int, float)):
if q < 0 or q > 1:

for q_num in q:
if q_num < 0 or q_num > 1:
raise ValueError("q should be in range [0, 1]")
indices.append(q * (x.shape[axis] - 1))
elif isinstance(q, (list, tuple)):
if len(q) <= 0:
raise ValueError("q should not be empty")
for q_num in q:
if q_num < 0 or q_num > 1:
raise ValueError("q should be in range [0, 1]")
indices.append(q_num * (x.shape[axis] - 1))
else:
raise TypeError("Type of q should be int, float, list or tuple.")
if paddle.in_dynamic_mode():
q_num = paddle.to_tensor(q_num, dtype='float64')
if ignore_nan:
indices.append(q_num * (valid_counts - 1))
else:
# TODO(Asthestarsfalll): Use paddle.index_fill instead of where
index = q_num * (valid_counts - 1)
last_index = x.shape[axis] - 1
nums = paddle.full_like(index, fill_value=last_index)
index = paddle.where(mask.any(axis=axis, keepdim=True), nums, index)
indices.append(index)

sorted_tensor = paddle.sort(x, axis)
indices_tensor = paddle.assign(indices).astype(paddle.float32)
indices_below = paddle.floor(indices_tensor).astype(paddle.int32)
indices_upper = paddle.ceil(indices_tensor).astype(paddle.int32)
outputs = []

def expand_dim(indices, sorted_tensor_shape, axis):
assert axis < len(list(sorted_tensor_shape))
expanded_shape = [1] * len(list(sorted_tensor_shape))
expanded_shape = tuple(expanded_shape)
indices = indices.reshape(expanded_shape)
return indices
outputs = []

# TODO(chenjianye): replace the for-loop to directly take elements.
for i in range(len(indices)):
if (indices_upper[i] != indices_below[i]):
tensor_below = paddle.take_along_axis(
sorted_tensor,
expand_dim(indices_below[i], sorted_tensor.shape, axis), axis)
tensor_upper = paddle.take_along_axis(
sorted_tensor,
expand_dim(indices_upper[i], sorted_tensor.shape, axis), axis)
weights = (indices[i] - indices_below[i]).astype(x.dtype)
out = paddle.lerp(tensor_below, tensor_upper, weights)
else:
out = paddle.take_along_axis(
sorted_tensor,
expand_dim(indices_below[i], sorted_tensor.shape, axis), axis)
for index in indices:
indices_below = paddle.floor(index).astype(paddle.int32)
indices_upper = paddle.ceil(index).astype(paddle.int32)
tensor_upper = paddle.take_along_axis(
sorted_tensor, indices_upper, axis=axis)
tensor_below = paddle.take_along_axis(
sorted_tensor, indices_below, axis=axis)
weights = (index - indices_below.astype('float64'))
out = paddle.lerp(
tensor_below.astype('float64'),
tensor_upper.astype('float64'), weights)
if not keepdim:
out = paddle.squeeze(out, axis=axis)
else:
out = out.reshape(out_shape)
outputs.append(out)
if isinstance(q, (list, tuple)):
return paddle.stack(outputs, 0)

if len(q) > 1:
outputs = paddle.stack(outputs, 0)
else:
return outputs[0]
outputs = outputs[0]

return outputs


def quantile(x, q, axis=None, keepdim=False):
"""
Compute the quantile of the input along the specified axis.
If any values in a reduced row are NaN then the quantiles for that reduction will be NaN.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If any values in a reduced row are NaN
后面加逗号

Args:
x (Tensor): The input Tensor, it's data type can be float32, float64.
q (int|float|list): The q for calculate quantile, which should be in range [0, 1]. If q is a list,
each q will be calculated and the first dimension of output is same to the number of ``q`` .
axis (int|list, optional): The axis along which to calculate quantile. ``axis`` should be int or list of int.
``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
If ``axis`` is a list, quantile is calculated over all elements of given axises.
If ``axis`` is None, quantile is calculated over all elements of ``x``. Default is None.
keepdim (bool, optional): Whether to reserve the reduced dimension(s)
in the output Tensor. If ``keepdim`` is True, the dimensions of
the output Tensor is the same as ``x`` except in the reduced
dimensions(it is of size 1 in this case). Otherwise, the shape of
the output Tensor is squeezed in ``axis`` . Default is False.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
Returns:
Tensor, results of quantile along ``axis`` of ``x``.
In order to obtain higher precision, data type of results will be float64.
Examples:
.. code-block:: python
import numpy as np
import paddle
x = np.random.randn(2, 3)
Copy link
Contributor

@luotao1 luotao1 Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议示例不要用随机数,用一个固定构造的数组。2*3矩阵里的元素可以简单便于手动计算:

  • quantile和nanquantile更容易让读者发现两者的区别
  • 对于插值计算也容易理解

x[0][0] = float('nan')
# [[ nan 0.41205737 0.36599339]
# [ 0.84388305 -1.21257817 0.25973139]]
x = paddle.to_tensor(x)
y1 = paddle.quantile(x, q=0.5, axis=[0, 1])
# nan
y2 = paddle.quantile(x, q=0.5, axis=1)
# [ nan 0.25973139]
y3 = paddle.quantile(x, q=[0.3, 0.5], axis=1)
# [[ nan -0.32919244]
# [ nan 0.25973139]]
y4 = paddle.quantile(x, q=0.8, axis=1, keepdim=True)
# [[ nan]
# [0.61022238]]
"""
return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=False)


def nanquantile(x, q, axis=None, keepdim=False):
"""
Compute the quantile of the input as if NaN values in input did not exist.
If all values in a reduced row are NaN then the quantiles for that reduction will be NaN.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all values in a reduced row are NaN
后面加逗号

需要补充全NAN的示例

Args:
x (Tensor): The input Tensor, it's data type can be float32, float64.
q (int|float|list): The q for calculate quantile, which should be in range [0, 1]. If q is a list,
each q will be calculated and the first dimension of output is same to the number of ``q`` .
axis (int|list, optional): The axis along which to calculate quantile. ``axis`` should be int or list of int.
``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
If ``axis`` is a list, quantile is calculated over all elements of given axises.
If ``axis`` is None, quantile is calculated over all elements of ``x``. Default is None.
keepdim (bool, optional): Whether to reserve the reduced dimension(s)
in the output Tensor. If ``keepdim`` is True, the dimensions of
the output Tensor is the same as ``x`` except in the reduced
dimensions(it is of size 1 in this case). Otherwise, the shape of
the output Tensor is squeezed in ``axis`` . Default is False.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
Returns:
Tensor, results of quantile along ``axis`` of ``x``.
In order to obtain higher precision, data type of results will be float64.
Examples:
.. code-block:: python
import numpy as np
import paddle
x = np.random.randn(2, 3)
x[0][0] = float('nan')
# [[ nan 1.26085129 -0.35944291]
# [-0.62427785 1.73718584 1.06024497]]
x = paddle.to_tensor(x)
y1 = paddle.nanquantile(x, q=0.5, axis=[0, 1])
# 1.0602449747672356
y2 = paddle.nanquantile(x, q=0.5, axis=1)
# [0.45070419 1.06024497]
y3 = paddle.nanquantile(x, q=[0.3, 0.5], axis=1)
# [[0.12664535 0.38643585]
# [0.45070419 1.06024497]]
y4 = paddle.nanquantile(x, q=0.8, axis=1, keepdim=True)
# [[0.93679245]
# [1.4664095 ]]
"""
return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=True)