Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7bef9603
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7bef9603
编写于
10月 09, 2022
作者:
K
Kevin吴嘉文
提交者:
梦柳
11月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#46765
上级
cfee9c13
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
319 addition
and
276 deletion
+319
-276
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+84
-71
python/paddle/tensor/linalg.py
python/paddle/tensor/linalg.py
+88
-68
python/paddle/tensor/logic.py
python/paddle/tensor/logic.py
+14
-20
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+13
-17
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+82
-67
python/paddle/tensor/stat.py
python/paddle/tensor/stat.py
+38
-33
未找到文件。
python/paddle/tensor/creation.py
浏览文件 @
7bef9603
...
...
@@ -1138,33 +1138,34 @@ def triu(x, diagonal=0, name=None):
Examples:
.. code-block:: python
import numpy as np
import paddle
data = np.arange(1, 13, dtype="int64").reshape(3,-1
)
#
array([[ 1, 2, 3, 4]
,
# [
5, 6, 7, 8
],
#
[ 9, 10, 11, 12]])
x = paddle.arange(1, 13, dtype="int64").reshape([3,-1]
)
#
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True
,
# [
[1 , 2 , 3 , 4
],
#
[5 , 6 , 7 , 8 ],
# [9 , 10, 11, 12]])
# example 1, default diagonal
x = paddle.to_tensor(data)
triu1 = paddle.tensor.triu(x)
# array([[ 1, 2, 3, 4],
# [ 0, 6, 7, 8],
# [ 0, 0, 11, 12]])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1 , 2 , 3 , 4 ],
# [0 , 6 , 7 , 8 ],
# [0 , 0 , 11, 12]])
# example 2, positive diagonal value
triu2 = paddle.tensor.triu(x, diagonal=2)
# array([[0, 0, 3, 4],
# [0, 0, 0, 8],
# [0, 0, 0, 0]])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 0, 3, 4],
# [0, 0, 0, 8],
# [0, 0, 0, 0]])
# example 3, negative diagonal value
triu3 = paddle.tensor.triu(x, diagonal=-1)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 0, 10, 11, 12]])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1 , 2 , 3 , 4 ],
# [5 , 6 , 7 , 8 ],
# [0 , 10, 11, 12]])
"""
if
in_dygraph_mode
():
...
...
@@ -1275,24 +1276,27 @@ def diagflat(x, offset=0, name=None):
x = paddle.to_tensor([1, 2, 3])
y = paddle.diagflat(x)
print(y.numpy())
# [[1 0 0]
# [0 2 0]
# [0 0 3]]
print(y)
# Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 0, 0],
# [0, 2, 0],
# [0, 0, 3]])
y = paddle.diagflat(x, offset=1)
print(y.numpy())
# [[0 1 0 0]
# [0 0 2 0]
# [0 0 0 3]
# [0 0 0 0]]
print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 1, 0, 0],
# [0, 0, 2, 0],
# [0, 0, 0, 3],
# [0, 0, 0, 0]])
y = paddle.diagflat(x, offset=-1)
print(y.numpy())
# [[0 0 0 0]
# [1 0 0 0]
# [0 2 0 0]
# [0 0 3 0]]
print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 0, 0, 0],
# [1, 0, 0, 0],
# [0, 2, 0, 0],
# [0, 0, 3, 0]])
.. code-block:: python
:name: code-example-2
...
...
@@ -1301,27 +1305,30 @@ def diagflat(x, offset=0, name=None):
x = paddle.to_tensor([[1, 2], [3, 4]])
y = paddle.diagflat(x)
print(y.numpy())
# [[1 0 0 0]
# [0 2 0 0]
# [0 0 3 0]
# [0 0 0 4]]
print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 0, 0, 0],
# [0, 2, 0, 0],
# [0, 0, 3, 0],
# [0, 0, 0, 4]])
y = paddle.diagflat(x, offset=1)
print(y.numpy())
# [[0 1 0 0 0]
# [0 0 2 0 0]
# [0 0 0 3 0]
# [0 0 0 0 4]
# [0 0 0 0 0]]
print(y)
# Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 1, 0, 0, 0],
# [0, 0, 2, 0, 0],
# [0, 0, 0, 3, 0],
# [0, 0, 0, 0, 4],
# [0, 0, 0, 0, 0]])
y = paddle.diagflat(x, offset=-1)
print(y.numpy())
# [[0 0 0 0 0]
# [1 0 0 0 0]
# [0 2 0 0 0]
# [0 0 3 0 0]
# [0 0 0 4 0]]
print(y)
# Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 0, 0, 0, 0],
# [1, 0, 0, 0, 0],
# [0, 2, 0, 0, 0],
# [0, 0, 3, 0, 0],
# [0, 0, 0, 4, 0]])
"""
padding_value
=
0
if
in_dygraph_mode
():
...
...
@@ -1413,23 +1420,26 @@ def diag(x, offset=0, padding_value=0, name=None):
paddle.disable_static()
x = paddle.to_tensor([1, 2, 3])
y = paddle.diag(x)
print(y.numpy())
# [[1 0 0]
# [0 2 0]
# [0 0 3]]
print(y)
# Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 0, 0],
# [0, 2, 0],
# [0, 0, 3]])
y = paddle.diag(x, offset=1)
print(y.numpy())
# [[0 1 0 0]
# [0 0 2 0]
# [0 0 0 3]
# [0 0 0 0]]
print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 1, 0, 0],
# [0, 0, 2, 0],
# [0, 0, 0, 3],
# [0, 0, 0, 0]])
y = paddle.diag(x, padding_value=6)
print(y.numpy())
# [[1 6 6]
# [6 2 6]
# [6 6 3]]
print(y)
# Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 6, 6],
# [6, 2, 6],
# [6, 6, 3]])
.. code-block:: python
:name: code-example-2
...
...
@@ -1439,16 +1449,19 @@ def diag(x, offset=0, padding_value=0, name=None):
paddle.disable_static()
x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
y = paddle.diag(x)
print(y.numpy())
# [1 5]
print(y)
# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [1, 5])
y = paddle.diag(x, offset=1)
print(y.numpy())
# [2 6]
print(y)
# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [2, 6])
y = paddle.diag(x, offset=-1)
print(y.numpy())
# [4]
print(y)
# Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
# [4])
"""
if
in_dygraph_mode
():
return
_C_ops
.
diag
(
x
,
offset
,
padding_value
)
...
...
@@ -1911,7 +1924,7 @@ def _memcpy(input, place=None, output=None):
.. code-block:: python
import paddle
import numpy as np
data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64') # [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]]
result = paddle._memcpy(data, place=paddle.CPUPlace()) # result2 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]]
"""
...
...
@@ -1986,10 +1999,10 @@ def complex(real, imag, name=None):
x = paddle.arange(2, dtype=paddle.float32).unsqueeze(-1)
y = paddle.arange(3, dtype=paddle.float32)
z = paddle.complex(x, y)
print(z
.numpy()
)
#
[[0.+0.j 0.+1.j 0.+2.j]
#
[1.+0.j 1.+1.j 1.+2.j]]
print(z)
# Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
#
[[0j , 1j , 2j ],
#
[(1+0j), (1+1j), (1+2j)]])
"""
if
in_dygraph_mode
():
return
_C_ops
.
complex
(
real
,
imag
)
...
...
python/paddle/tensor/linalg.py
浏览文件 @
7bef9603
...
...
@@ -314,38 +314,53 @@ def norm(x, p='fro', axis=None, keepdim=False, name=None):
.. code-block:: python
import paddle
import numpy as np
shape=[2, 3, 4]
np_input = np.arange(24).astype('float32') - 12
np_input = np_input.reshape(shape)
x = paddle.to_tensor(np_input)
#[[[-12. -11. -10. -9.] [ -8. -7. -6. -5.] [ -4. -3. -2. -1.]]
# [[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.]]]
x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12
# x: Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[[-12., -11., -10., -9. ],
# [-8. , -7. , -6. , -5. ],
# [-4. , -3. , -2. , -1. ]],
# [[ 0. , 1. , 2. , 3. ],
# [ 4. , 5. , 6. , 7. ],
# [ 8. , 9. , 10., 11.]]])
# compute frobenius norm along last two dimensions.
out_fro = paddle.linalg.norm(x, p='fro', axis=[0,1])
# out_fro.numpy() [17.435596 16.911535 16.7332 16.911535]
# out_fro: Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [17.43559647, 16.91153526, 16.73320007, 16.91153526])
# compute 2-order vector norm along last dimension.
out_pnorm = paddle.linalg.norm(x, p=2, axis=-1)
#out_pnorm.numpy(): [[21.118711 13.190906 5.477226]
# [ 3.7416575 11.224972 19.131126]]
# out_pnorm: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[21.11871147, 13.19090557, 5.47722578 ],
# [3.74165750 , 11.22497177, 19.13112640]])
# compute 2-order norm along [0,1] dimension.
out_pnorm = paddle.linalg.norm(x, p=2, axis=[0,1])
#out_pnorm.numpy(): [17.435596 16.911535 16.7332 16.911535]
# out_pnorm: Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [17.43559647, 16.91153526, 16.73320007, 16.91153526])
# compute inf-order norm
out_pnorm = paddle.linalg.norm(x, p=np.inf)
#out_pnorm.numpy() = [12.]
out_pnorm = paddle.linalg.norm(x, p=np.inf, axis=0)
#out_pnorm.numpy(): [[12. 11. 10. 9.] [8. 7. 6. 7.] [8. 9. 10. 11.]]
out_pnorm = paddle.linalg.norm(x, p=float("inf"))
# out_pnorm = Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [12.])
out_pnorm = paddle.linalg.norm(x, p=float("inf"), axis=0)
# out_pnorm: Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[12., 11., 10., 9. ],
# [8. , 7. , 6. , 7. ],
# [8. , 9. , 10., 11.]])
# compute -inf-order norm
out_pnorm = paddle.linalg.norm(x, p=-np.inf)
#out_pnorm.numpy(): [0.]
out_pnorm = paddle.linalg.norm(x, p=-np.inf, axis=0)
#out_pnorm.numpy(): [[0. 1. 2. 3.] [4. 5. 6. 5.] [4. 3. 2. 1.]]
out_pnorm = paddle.linalg.norm(x, p=-float("inf"))
# out_pnorm: Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.])
out_pnorm = paddle.linalg.norm(x, p=-float("inf"), axis=0)
# out_pnorm: Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0., 1., 2., 3.],
# [4., 5., 6., 5.],
# [4., 3., 2., 1.]])
"""
def
frobenius_norm
(
input
,
dim
=
None
,
keepdim
=
False
,
name
=
None
):
...
...
@@ -699,10 +714,9 @@ def dist(x, y, p=2, name=None):
.. code-block:: python
import paddle
import numpy as np
x = paddle.to_tensor(
np.array([[3, 3],[3, 3]]),
"float32")
y = paddle.to_tensor(
np.array([[3, 3],[3, 1]]),
"float32")
x = paddle.to_tensor(
[[3, 3],[3, 3]], dtype=
"float32")
y = paddle.to_tensor(
[[3, 3],[3, 1]], dtype=
"float32")
out = paddle.dist(x, y, 0)
print(out) # out = [1.]
...
...
@@ -1160,14 +1174,18 @@ def dot(x, y, name=None):
.. code-block:: python
import paddle
import numpy as np
x_data = np.random.uniform(0.1, 1, [10]).astype(np.float32)
y_data = np.random.uniform(1, 3, [10]).astype(np.float32)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
# 1-D Tensor * 1-D Tensor
x = paddle.to_tensor([1, 2, 3])
y = paddle.to_tensor([4, 5, 6])
z = paddle.dot(x, y)
print(z)
print(z) # [32]
# 2-D Tensor * 2-D Tensor
x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]])
y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
z = paddle.dot(x, y)
print(z) # [[32], [64]]
"""
if
in_dygraph_mode
():
...
...
@@ -2583,7 +2601,6 @@ def multi_dot(x, name=None):
.. code-block:: python
import paddle
import numpy as np
# A * B
A_data = np.random.random([3, 4]).astype(np.float32)
...
...
@@ -3099,47 +3116,51 @@ def triangular_solve(
x
,
y
,
upper
=
True
,
transpose
=
False
,
unitriangular
=
False
,
name
=
None
):
r
"""
Computes the solution of a system of equations with a triangular coefficient matrix `x` and
multiple right-hand sides `y` .
Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs
is also batches.
Args:
x (Tensor): The input triangular coefficient matrix. Its shape should be `[*, M, M]`, where `*` is zero or
more batch dimensions. Its data type should be float32 or float64.
y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is
zero or more batch dimensions. Its data type should be float32 or float64.
upper (bool, optional): Whether to solve the upper-triangular system of equations (default) or the lower-triangular
system of equations. Default: True.
transpose (bool, optional): whether `x` should be transposed before calculation. Default: False.
unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed
to be 1 and not referenced from `x` . Default: False.
name(str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
Computes the solution of a system of equations with a triangular coefficient matrix `x` and
multiple right-hand sides `y` .
Returns:
Tensor: The solution of the system of equations. Its data type should be the same as that of `x`
.
Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs
is also batches
.
Examples:
.. code-block:: python
Args:
x (Tensor): The input triangular coefficient matrix. Its shape should be `[*, M, M]`, where `*` is zero or
more batch dimensions. Its data type should be float32 or float64.
y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is
zero or more batch dimensions. Its data type should be float32 or float64.
upper (bool, optional): Whether to solve the upper-triangular system of equations (default) or the lower-triangular
system of equations. Default: True.
transpose (bool, optional): whether `x` should be transposed before calculation. Default: False.
unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed
to be 1 and not referenced from `x` . Default: False.
name(str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
Returns:
Tensor: The solution of the system of equations. Its data type should be the same as that of `x`.
Examples:
.. code-block:: python
# a square system of linear equations:
# x1 + x2 + x3 = 0
# 2*x2 + x3 = -9
# -x3 = 5
# a square system of linear equations:
# x1 + x2 + x3 = 0
# 2*x2 + x3 = -9
# -x3 = 5
import paddle
import numpy as np
<<<<<<< HEAD
import paddle
import numpy as np
=======
import paddle
>>>>>>> 912be4f897 (fix numpy issue in codeblock examples for operators under python/paddle/tensor folder (#46765))
x = paddle.to_tensor([[1, 1, 1],
[0, 2, 1],
[0, 0,-1]], dtype="float64")
y = paddle.to_tensor([[0], [-9], [5]], dtype="float64")
out = paddle.linalg.triangular_solve(x, y, upper=True)
x = paddle.to_tensor([[1, 1, 1],
[0, 2, 1],
[0, 0,-1]], dtype="float64")
y = paddle.to_tensor([[0], [-9], [5]], dtype="float64")
out = paddle.linalg.triangular_solve(x, y, upper=True)
print(out)
# [7, -2, -5]
print(out)
# [7, -2, -5]
"""
if
in_dygraph_mode
():
return
_C_ops
.
triangular_solve
(
x
,
y
,
upper
,
transpose
,
unitriangular
)
...
...
@@ -3246,14 +3267,13 @@ def eigvalsh(x, UPLO='L', name=None):
Examples:
.. code-block:: python
import numpy as np
import paddle
x_data = np.array([[1, -2j], [2j, 5]])
x = paddle.to_tensor(x_data)
x = paddle.to_tensor([[1, -2j], [2j, 5]])
out_value = paddle.eigvalsh(x, UPLO='L')
print(out_value)
#[0.17157288, 5.82842712]
# Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.17157286, 5.82842731])
"""
if
in_dygraph_mode
():
values
,
_
=
_C_ops
.
eigvalsh
(
x
,
UPLO
,
x
.
stop_gradient
)
...
...
python/paddle/tensor/logic.py
浏览文件 @
7bef9603
...
...
@@ -152,14 +152,14 @@ def logical_or(x, y, out=None, name=None):
.. code-block:: python
import paddle
import numpy as np
x_data = np.array([True, False], dtype=np.bool_).reshape(2, 1)
y_data = np.array([True, False, True, False], dtype=np.bool_).reshape(2, 2)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1])
y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2])
res = paddle.logical_or(x, y)
print(res) # [[ True True] [ True False]]
print(res)
# Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
# [[True , True ],
# [True , False]])
"""
if
in_dygraph_mode
():
return
_C_ops
.
logical_or
(
x
,
y
)
...
...
@@ -194,14 +194,14 @@ def logical_xor(x, y, out=None, name=None):
.. code-block:: python
import paddle
import numpy as np
x_data = np.array([True, False], dtype=np.bool_).reshape([2, 1])
y_data = np.array([True, False, True, False], dtype=np.bool_).reshape([2, 2])
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1])
y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2])
res = paddle.logical_xor(x, y)
print(res) # [[False, True], [ True, False]]
print(res)
# Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
# [[False, True ],
# [True , False]])
"""
if
in_dygraph_mode
():
return
_C_ops
.
logical_xor
(
x
,
y
)
...
...
@@ -364,22 +364,20 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
y = paddle.to_tensor([10000.1, 1e-08])
result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [False]
result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [False]
x = paddle.to_tensor([1.0, float('nan')])
y = paddle.to_tensor([1.0, float('nan')])
result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [False]
result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [True]
"""
...
...
@@ -992,22 +990,18 @@ def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
y = paddle.to_tensor([10000.1, 1e-08])
result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [True, False]
result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [True, False]
x = paddle.to_tensor([1.0, float('nan')])
y = paddle.to_tensor([1.0, float('nan')])
result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy()
# [True, False]
result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=True, name="equal_nan")
np_result2 = result2.numpy()
# [True, True]
"""
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
7bef9603
...
...
@@ -1346,12 +1346,9 @@ def flip(x, axis, name=None):
.. code-block:: python
import paddle
import numpy as np
image_shape=(3, 2, 2)
x = np.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape)
x = x.astype('float32')
img = paddle.to_tensor(x)
img = paddle.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape)
tmp = paddle.flip(img, [0,1])
print(tmp) # [[[10,11],[8, 9]], [[6, 7],[4, 5]], [[2, 3],[0, 1]]]
...
...
@@ -3064,15 +3061,12 @@ def chunk(x, chunks, axis=0, name=None):
Returns:
list(Tensor): The list of segmented Tensors.
Example:
Example
s
:
.. code-block:: python
import numpy as np
import paddle
# x is a Tensor which shape is [3, 9, 5]
x_np = np.random.random([3, 9, 5]).astype("int32")
x = paddle.to_tensor(x_np)
x = paddle.rand([3, 9, 5])
out0, out1, out2 = paddle.chunk(x, chunks=3, axis=1)
# out0.shape [3, 3, 5]
...
...
@@ -4752,10 +4746,11 @@ def index_add(x, index, axis, value, name=None):
index = paddle.to_tensor([0, 2], dtype="int32")
value = paddle.to_tensor([[1, 1, 1], [1, 1, 1]], dtype="float32")
outplace_res = paddle.index_add(input_tensor, index, 0, value)
print(outplace_res.numpy())
# [[2 2 2]
# [1 1 1]
# [2 2 2]]
print(outplace_res)
# Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [[2., 2., 2.],
# [1., 1., 1.],
# [2., 2., 2.]])
"""
_index_add_params_check
(
x
,
index
,
axis
,
value
)
...
...
@@ -4813,10 +4808,11 @@ def index_add_(x, index, axis, value, name=None):
index = paddle.to_tensor([0, 2], dtype="int32")
value = paddle.to_tensor([[1, 1], [1, 1], [1, 1]], dtype="float32")
inplace_res = paddle.index_add_(input_tensor, index, 1, value)
print(inplace_res.numpy())
# [[2, 1, 2]
# [2, 1, 2]
# [2, 1, 2]]
print(inplace_res)
# Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [[2., 1., 2.],
# [2., 1., 2.],
# [2., 1., 2.]])
"""
_index_add_params_check
(
x
,
index
,
axis
,
value
)
...
...
python/paddle/tensor/math.py
浏览文件 @
7bef9603
...
...
@@ -905,34 +905,37 @@ def maximum(x, y, name=None):
.. code-block:: python
import numpy as np
import paddle
x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.maximum(x, y)
print(res)
# [[3, 4],
# [7, 8]]
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 4],
# [7, 8]])
x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
y = paddle.to_tensor([3, 0, 4])
res = paddle.maximum(x, y)
print(res)
# [[3, 2, 4],
# [3, 2, 4]]
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 2, 4],
# [3, 2, 4]])
x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1,
np.nan, np.nan
], dtype='float32')
y = paddle.to_tensor([1,
float("nan"), float("nan")
], dtype='float32')
res = paddle.maximum(x, y)
print(res)
# [ 2., nan, nan]
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2. , nan, nan])
x = paddle.to_tensor([5, 3,
np.inf
], dtype='float32')
y = paddle.to_tensor([1, -
np.inf
, 5], dtype='float32')
x = paddle.to_tensor([5, 3,
float("inf")
], dtype='float32')
y = paddle.to_tensor([1, -
float("inf")
, 5], dtype='float32')
res = paddle.maximum(x, y)
print(res)
# [ 5., 3., inf.]
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [5. , 3. , inf.])
"""
op_type
=
'elementwise_max'
axis
=
-
1
...
...
@@ -966,34 +969,37 @@ def minimum(x, y, name=None):
.. code-block:: python
import numpy as np
import paddle
x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.minimum(x, y)
print(res)
# [[1, 2],
# [5, 6]]
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 2],
# [5, 6]])
x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([3, 0, 4])
res = paddle.minimum(x, y)
print(res)
# [[[1, 0, 3],
# [1, 0, 3]]]
# Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[[1, 0, 3],
# [1, 0, 3]]])
x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1,
np.nan, np.nan
], dtype='float32')
y = paddle.to_tensor([1,
float("nan"), float("nan")
], dtype='float32')
res = paddle.minimum(x, y)
print(res)
# [ 1., nan, nan]
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1. , nan, nan])
x = paddle.to_tensor([5, 3,
np.inf
], dtype='float64')
y = paddle.to_tensor([1, -
np.inf
, 5], dtype='float64')
x = paddle.to_tensor([5, 3,
float("inf")
], dtype='float64')
y = paddle.to_tensor([1, -
float("inf")
, 5], dtype='float64')
res = paddle.minimum(x, y)
print(res)
# [ 1., -inf., 5.]
# Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 1. , -inf., 5. ])
"""
op_type
=
'elementwise_min'
axis
=
-
1
...
...
@@ -1029,34 +1035,37 @@ def fmax(x, y, name=None):
.. code-block:: python
import numpy as np
import paddle
x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.fmax(x, y)
print(res)
# [[3, 4],
# [7, 8]]
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 4],
# [7, 8]])
x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
y = paddle.to_tensor([3, 0, 4])
res = paddle.fmax(x, y)
print(res)
# [[3, 2, 4],
# [3, 2, 4]]
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 2, 4],
# [3, 2, 4]])
x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1,
np.nan, np.nan
], dtype='float32')
y = paddle.to_tensor([1,
float("nan"), float("nan")
], dtype='float32')
res = paddle.fmax(x, y)
print(res)
# [ 2., 3., 5.]
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2., 3., 5.])
x = paddle.to_tensor([5, 3,
np.inf
], dtype='float32')
y = paddle.to_tensor([1, -
np.inf
, 5], dtype='float32')
x = paddle.to_tensor([5, 3,
float("inf")
], dtype='float32')
y = paddle.to_tensor([1, -
float("inf")
, 5], dtype='float32')
res = paddle.fmax(x, y)
print(res)
# [ 5., 3., inf.]
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [5. , 3. , inf.])
"""
op_type
=
'elementwise_fmax'
axis
=
-
1
...
...
@@ -1092,34 +1101,37 @@ def fmin(x, y, name=None):
.. code-block:: python
import numpy as np
import paddle
x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.fmin(x, y)
print(res)
# [[1, 2],
# [5, 6]]
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 2],
# [5, 6]])
x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([3, 0, 4])
res = paddle.fmin(x, y)
print(res)
# [[[1, 0, 3],
# [1, 0, 3]]]
# Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[[1, 0, 3],
# [1, 0, 3]]])
x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1,
np.nan, np.nan
], dtype='float32')
y = paddle.to_tensor([1,
float("nan"), float("nan")
], dtype='float32')
res = paddle.fmin(x, y)
print(res)
# [ 1., 3., 5.]
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1., 3., 5.])
x = paddle.to_tensor([5, 3,
np.inf
], dtype='float64')
y = paddle.to_tensor([1, -
np.inf
, 5], dtype='float64')
x = paddle.to_tensor([5, 3,
float("inf")
], dtype='float64')
y = paddle.to_tensor([1, -
float("inf")
, 5], dtype='float64')
res = paddle.fmin(x, y)
print(res)
# [ 1., -inf., 5.]
# Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 1. , -inf., 5. ])
"""
op_type
=
'elementwise_fmin'
axis
=
-
1
...
...
@@ -1290,15 +1302,13 @@ def nansum(x, axis=None, dtype=None, keepdim=False, name=None):
.. code-block:: python
import paddle
import numpy as np
# x is a Tensor with following elements:
# [[nan, 0.3, 0.5, 0.9]
# [0.1, 0.2, -nan, 0.7]]
# Each example is followed by the corresponding output tensor.
x = np.array([[float('nan'), 0.3, 0.5, 0.9],
[0.1, 0.2, float('-nan'), 0.7]]).astype(np.float32)
x = paddle.to_tensor(x)
x = paddle.to_tensor([[float('nan'), 0.3, 0.5, 0.9],
[0.1, 0.2, float('-nan'), 0.7]],dtype="float32")
out1 = paddle.nansum(x) # [2.7]
out2 = paddle.nansum(x, axis=0) # [0.1, 0.5, 0.5, 1.6]
out3 = paddle.nansum(x, axis=-1) # [1.7, 1.0]
...
...
@@ -1308,9 +1318,12 @@ def nansum(x, axis=None, dtype=None, keepdim=False, name=None):
# [[[1, nan], [3, 4]],
# [[5, 6], [-nan, 8]]]
# Each example is followed by the corresponding output tensor.
<<<<<<< HEAD
y = np.array([[[1, float('nan')], [3, 4]],
=======
y = paddle.to_tensor([[[1, float('nan')], [3, 4]],
>>>>>>> 912be4f897 (fix numpy issue in codeblock examples for operators under python/paddle/tensor folder (#46765))
[[5, 6], [float('-nan'), 8]]])
y = paddle.to_tensor(y)
out5 = paddle.nansum(y, axis=[1, 2]) # [8, 19]
out6 = paddle.nansum(y, axis=[0, 1]) # [9, 18]
"""
...
...
@@ -4205,8 +4218,13 @@ def rad2deg(x, name=None):
.. code-block:: python
import paddle
<<<<<<< HEAD
import numpy as np
=======
import math
>>>>>>> 912be4f897 (fix numpy issue in codeblock examples for operators under python/paddle/tensor folder (#46765))
x1 = paddle.to_tensor([3.142, -3.142, 6.283, -6.283, 1.570, -1.570])
result1 = paddle.rad2deg(x1)
print(result1)
...
...
@@ -4214,7 +4232,7 @@ def rad2deg(x, name=None):
# [180.02334595, -180.02334595, 359.98937988, -359.98937988,
# 9.95437622 , -89.95437622])
x2 = paddle.to_tensor(
np
.pi/2)
x2 = paddle.to_tensor(
math
.pi/2)
result2 = paddle.rad2deg(x2)
print(result2)
# Tensor(shape=[1], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
...
...
@@ -4663,18 +4681,20 @@ def angle(x, name=None):
x = paddle.to_tensor([-2, -1, 0, 1]).unsqueeze(-1).astype('float32')
y = paddle.to_tensor([-2, -1, 0, 1]).astype('float32')
z = x + 1j * y
print(z.numpy())
# [[-2.-2.j -2.-1.j -2.+0.j -2.+1.j]
# [-1.-2.j -1.-1.j -1.+0.j -1.+1.j]
# [ 0.-2.j 0.-1.j 0.+0.j 0.+1.j]
# [ 1.-2.j 1.-1.j 1.+0.j 1.+1.j]]
print(z)
# Tensor(shape=[4, 4], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [[(-2-2j), (-2-1j), (-2+0j), (-2+1j)],
# [(-1-2j), (-1-1j), (-1+0j), (-1+1j)],
# [-2j , -1j , 0j , 1j ],
# [ (1-2j), (1-1j), (1+0j), (1+1j)]])
theta = paddle.angle(z)
print(theta.numpy())
# [[-2.3561945 -2.6779451 3.1415927 2.6779451]
# [-2.0344439 -2.3561945 3.1415927 2.3561945]
# [-1.5707964 -1.5707964 0. 1.5707964]
# [-1.1071488 -0.7853982 0. 0.7853982]]
print(theta)
# Tensor(shape=[4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[-2.35619450, -2.67794514, 3.14159274, 2.67794514],
# [-2.03444386, -2.35619450, 3.14159274, 2.35619450],
# [-1.57079637, -1.57079637, 0. , 1.57079637],
# [-1.10714877, -0.78539819, 0. , 0.78539819]])
"""
if
in_dygraph_mode
():
...
...
@@ -4755,19 +4775,14 @@ def frac(x, name=None):
.. code-block:: python
import paddle
import numpy as np
input = paddle.rand([3, 3], 'float32')
print(input.numpy())
# [[ 1.2203873 -1.0035421 -0.35193074]
# [-0.00928353 0.58917075 -0.8407828 ]
# [-1.5131804 0.5850153 -0.17597814]]
input = paddle.to_tensor([[12.22000003, -1.02999997],
[-0.54999995, 0.66000003]])
output = paddle.frac(input)
print(output
.numpy()
)
#
[[ 0.22038734 -0.00354207 -0.35193074]
#
[-0.00928353 0.58917075 -0.8407828 ]
#
[-0.5131804 0.5850153 -0.17597814]]
print(output)
#
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
#
[[ 0.22000003, -0.02999997],
#
[-0.54999995, 0.66000003]])
"""
op_type
=
'elementwise_sub'
axis
=
-
1
...
...
python/paddle/tensor/stat.py
浏览文件 @
7bef9603
...
...
@@ -628,32 +628,35 @@ def quantile(x, q, axis=None, keepdim=False):
Examples:
.. code-block:: python
import numpy as np
import paddle
x = np.arange(0, 8, dtype=np.float32).reshape(4, 2)
# [[0 1]
# [2 3]
# [4 5]
# [6 7]]
y = paddle.to_tensor(x)
y = paddle.arange(0, 8 ,dtype="float32").reshape([4, 2])
# Tensor(shape=[4, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0., 1.],
# [2., 3.],
# [4., 5.],
# [6., 7.]])
y1 = paddle.quantile(y, q=0.5, axis=[0, 1])
# 3.5
# Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
# 3.50000000)
y2 = paddle.quantile(y, q=0.5, axis=1)
# [0.5 2.5 4.5 6.5]
# Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=True,
# [0.50000000, 2.50000000, 4.50000000, 6.50000000])
y3 = paddle.quantile(y, q=[0.3, 0.5], axis=0)
# [[1.8 2.8]
# [3. 4. ]]
# Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[1.80000000, 2.80000000],
# [3. , 4. ]])
x[0][0] = np.nan
y = paddle.to_tensor(x)
y[0,0] = float("nan")
y4 = paddle.quantile(y, q=0.8, axis=1, keepdim=True)
# [[nan]
# [2.8]
# [4.8]
# [6.8]]
# Tensor(shape=[4, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[nan ],
# [2.80000000],
# [4.80000000],
# [6.80000000]])
"""
return
_compute_quantile
(
x
,
q
,
axis
=
axis
,
keepdim
=
keepdim
,
ignore_nan
=
False
)
...
...
@@ -688,35 +691,37 @@ def nanquantile(x, q, axis=None, keepdim=False):
Examples:
.. code-block:: python
import numpy as np
import paddle
x =
np.array
(
x =
paddle.to_tensor
(
[[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]],
dtype=np.float32
)
x[0][0] = np.nan
[5, 6, 7, 8, 9]],
dtype="float32")
x[0,0] = float("nan")
x = paddle.to_tensor(x)
y1 = paddle.nanquantile(x, q=0.5, axis=[0, 1])
# 5.0
# Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
# 5.)
y2 = paddle.nanquantile(x, q=0.5, axis=1)
# [2.5 7. ]
# Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
# [2.50000000, 7. ])
y3 = paddle.nanquantile(x, q=[0.3, 0.5], axis=0)
# [[5. 2.5 3.5 4.5 5.5]
# [5. 3.5 4.5 5.5 6.5]
# Tensor(shape=[2, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[5. , 2.50000000, 3.50000000, 4.50000000, 5.50000000],
# [5. , 3.50000000, 4.50000000, 5.50000000, 6.50000000]])
y4 = paddle.nanquantile(x, q=0.8, axis=1, keepdim=True)
# [[3.4]
# [8.2]]
# Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[3.40000000],
# [8.20000000]])
nan = paddle.full(shape=[2, 3], fill_value=
np.nan
)
nan = paddle.full(shape=[2, 3], fill_value=
float("nan")
)
y5 = paddle.nanquantile(nan, q=0.8, axis=1, keepdim=True)
# [[nan]
# [nan]]
# Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[nan],
# [nan]])
"""
return
_compute_quantile
(
x
,
q
,
axis
=
axis
,
keepdim
=
keepdim
,
ignore_nan
=
True
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录