未验证 提交 ae88111f 编写于 作者: N Nyakku Shigure 提交者: GitHub

[xdoctest] reformat example code with google style in `paddle/tensor/math` (#55768)

* [xdoctest] reformat example code with google style in `paddle/tensor/math`

* preview, test=docs_preview

* scale, test=docs_preview

* stanh, test=docs_preview

* multiplex, test=docs_preview

* pow, test=docs_preview

* max, test=docs_preview

* others..., test=docs_preview

* update trunc example code, test=docs_preview
上级 84fe0454
...@@ -14,15 +14,12 @@ ...@@ -14,15 +14,12 @@
""" """
math functions math functions
""" """
# TODO: define math functions
import numpy as np import numpy as np
import paddle import paddle
from paddle import _C_ops, _legacy_C_ops from paddle import _C_ops, _legacy_C_ops
from paddle.common_ops_import import VarDesc, dygraph_only, dygraph_utils from paddle.common_ops_import import VarDesc, dygraph_only, dygraph_utils
# TODO: define math functions
from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only
from ..common_ops_import import Variable from ..common_ops_import import Variable
...@@ -163,12 +160,14 @@ def log(x, name=None): ...@@ -163,12 +160,14 @@ def log(x, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = [[2,3,4], [7,8,9]] >>> x = [[2, 3, 4], [7, 8, 9]]
x = paddle.to_tensor(x, dtype='float32') >>> x = paddle.to_tensor(x, dtype='float32')
res = paddle.log(x) >>> print(paddle.log(x))
# [[0.693147, 1.09861, 1.38629], [1.94591, 2.07944, 2.19722]] Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314718, 1.09861231, 1.38629436],
[1.94591010, 2.07944155, 2.19722462]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.log(x) return _C_ops.log(x)
...@@ -228,20 +227,36 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): ...@@ -228,20 +227,36 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
# scale as a float32 number >>> # scale as a float32 number
import paddle >>> import paddle
data = paddle.randn(shape=[2,3], dtype='float32') >>> data = paddle.arange(6).astype("float32").reshape([2, 3])
res = paddle.scale(data, scale=2.0, bias=1.0) >>> print(data)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 2.],
[3., 4., 5.]])
>>> res = paddle.scale(data, scale=2.0, bias=1.0)
>>> print(res)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1. , 3. , 5. ],
[7. , 9. , 11.]])
.. code-block:: python .. code-block:: python
# scale with parameter scale as a Tensor >>> # scale with parameter scale as a Tensor
import paddle >>> import paddle
data = paddle.randn(shape=[2, 3], dtype='float32') >>> data = paddle.arange(6).astype("float32").reshape([2, 3])
factor = paddle.to_tensor([2], dtype='float32') >>> print(data)
res = paddle.scale(data, scale=factor, bias=1.0) Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 2.],
[3., 4., 5.]])
>>> factor = paddle.to_tensor([2], dtype='float32')
>>> res = paddle.scale(data, scale=factor, bias=1.0)
>>> print(res)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1. , 3. , 5. ],
[7. , 9. , 11.]])
""" """
...@@ -306,10 +321,13 @@ def stanh(x, scale_a=0.67, scale_b=1.7159, name=None): ...@@ -306,10 +321,13 @@ def stanh(x, scale_a=0.67, scale_b=1.7159, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) >>> x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0])
out = paddle.stanh(x, scale_a=0.67, scale_b=1.72) # [1.00616539, 1.49927628, 1.65933108, 1.70390463] >>> out = paddle.stanh(x, scale_a=0.67, scale_b=1.72)
>>> print(out)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.00616539, 1.49927628, 1.65933096, 1.70390463])
""" """
...@@ -371,14 +389,17 @@ def multiplex(inputs, index, name=None): ...@@ -371,14 +389,17 @@ def multiplex(inputs, index, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
img1 = paddle.to_tensor([[1, 2], [3, 4]], dtype=paddle.float32) >>> img1 = paddle.to_tensor([[1, 2], [3, 4]], dtype=paddle.float32)
img2 = paddle.to_tensor([[5, 6], [7, 8]], dtype=paddle.float32) >>> img2 = paddle.to_tensor([[5, 6], [7, 8]], dtype=paddle.float32)
inputs = [img1, img2] >>> inputs = [img1, img2]
index = paddle.to_tensor([[1], [0]], dtype=paddle.int32) >>> index = paddle.to_tensor([[1], [0]], dtype=paddle.int32)
res = paddle.multiplex(inputs, index) >>> res = paddle.multiplex(inputs, index)
print(res) # Tensor([[5., 6.], [3., 4.]], dtype=float32) >>> print(res)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[5., 6.],
[3., 4.]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -444,28 +465,28 @@ def pow(x, y, name=None): ...@@ -444,28 +465,28 @@ def pow(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1, 2, 3], dtype='float32') >>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
# example 1: y is a float or int >>> # example 1: y is a float or int
res = paddle.pow(x, 2) >>> res = paddle.pow(x, 2)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1., 4., 9.]) [1., 4., 9.])
res = paddle.pow(x, 2.5) >>> res = paddle.pow(x, 2.5)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1. , 5.65685415 , 15.58845711]) [1. , 5.65685415 , 15.58845711])
# example 2: y is a Tensor >>> # example 2: y is a Tensor
y = paddle.to_tensor([2], dtype='float32') >>> y = paddle.to_tensor([2], dtype='float32')
res = paddle.pow(x, y) >>> res = paddle.pow(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1., 4., 9.]) [1., 4., 9.])
""" """
# in dynamic graph mode # in dynamic graph mode
...@@ -624,7 +645,7 @@ def add(x, y, name=None): ...@@ -624,7 +645,7 @@ def add(x, y, name=None):
For example: For example:
.. code-block:: python .. code-block:: text
shape(X) = (2, 3, 4, 5), shape(Y) = (,) shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,) shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
...@@ -643,14 +664,16 @@ def add(x, y, name=None): ...@@ -643,14 +664,16 @@ def add(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([2, 3, 4], 'float64') >>> x = paddle.to_tensor([2, 3, 4], 'float64')
y = paddle.to_tensor([1, 5, 2], 'float64') >>> y = paddle.to_tensor([1, 5, 2], 'float64')
z = paddle.add(x, y) >>> z = paddle.add(x, y)
print(z) # [3., 8., 6. ] >>> print(z)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[3., 8., 6.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -703,7 +726,7 @@ def logaddexp(x, y, name=None): ...@@ -703,7 +726,7 @@ def logaddexp(x, y, name=None):
For example: For example:
.. code-block:: python .. code-block:: text
shape(X) = (2, 3, 4, 5), shape(Y) = (,) shape(X) = (2, 3, 4, 5), shape(Y) = (,)
shape(X) = (2, 3, 4, 5), shape(Y) = (5,) shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
...@@ -722,14 +745,16 @@ def logaddexp(x, y, name=None): ...@@ -722,14 +745,16 @@ def logaddexp(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-1, -2, -3], 'float64') >>> x = paddle.to_tensor([-1, -2, -3], 'float64')
y = paddle.to_tensor([-1], 'float64') >>> y = paddle.to_tensor([-1], 'float64')
z = paddle.logaddexp(x, y) >>> z = paddle.logaddexp(x, y)
print(z) # [-0.30685282, -0.68673831, -0.87307199] >>> print(z)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[-0.30685282, -0.68673831, -0.87307199])
""" """
return paddle.log1p(paddle.exp(-paddle.abs(x - y))) + paddle.maximum(x, y) return paddle.log1p(paddle.exp(-paddle.abs(x - y))) + paddle.maximum(x, y)
...@@ -759,37 +784,37 @@ def subtract(x, y, name=None): ...@@ -759,37 +784,37 @@ def subtract(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [7, 8]]) >>> x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[5, 6], [3, 4]]) >>> y = paddle.to_tensor([[5, 6], [3, 4]])
res = paddle.subtract(x, y) >>> res = paddle.subtract(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[-4, -4], [[-4, -4],
# [ 4, 4]]) [ 4, 4]])
x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]]) >>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([1, 0, 4]) >>> y = paddle.to_tensor([1, 0, 4])
res = paddle.subtract(x, y) >>> res = paddle.subtract(x, y)
print(res) >>> print(res)
# Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[[ 0, 2, -1], [[[ 0, 2, -1],
# [ 0, 2, -1]]]) [ 0, 2, -1]]])
x = paddle.to_tensor([2, float('nan'), 5], dtype='float32') >>> x = paddle.to_tensor([2, float('nan'), 5], dtype='float32')
y = paddle.to_tensor([1, 4, float('nan')], dtype='float32') >>> y = paddle.to_tensor([1, 4, float('nan')], dtype='float32')
res = paddle.subtract(x, y) >>> res = paddle.subtract(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1. , nan, nan]) [1. , nan, nan])
x = paddle.to_tensor([5, float('inf'), -float('inf')], dtype='float64') >>> x = paddle.to_tensor([5, float('inf'), -float('inf')], dtype='float64')
y = paddle.to_tensor([1, 4, 5], dtype='float64') >>> y = paddle.to_tensor([1, 4, 5], dtype='float64')
res = paddle.subtract(x, y) >>> res = paddle.subtract(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 4. , inf., -inf.]) [ 4. , inf., -inf.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.subtract(x, y) return _C_ops.subtract(x, y)
...@@ -837,14 +862,16 @@ def divide(x, y, name=None): ...@@ -837,14 +862,16 @@ def divide(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([2, 3, 4], dtype='float64') >>> x = paddle.to_tensor([2, 3, 4], dtype='float64')
y = paddle.to_tensor([1, 5, 2], dtype='float64') >>> y = paddle.to_tensor([1, 5, 2], dtype='float64')
z = paddle.divide(x, y) >>> z = paddle.divide(x, y)
print(z) # [2., 0.6, 2.] >>> print(z)
Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
[2. , 0.60000000, 2. ])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -880,14 +907,16 @@ def floor_divide(x, y, name=None): ...@@ -880,14 +907,16 @@ def floor_divide(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([2, 3, 8, 7]) >>> x = paddle.to_tensor([2, 3, 8, 7])
y = paddle.to_tensor([1, 5, 3, 3]) >>> y = paddle.to_tensor([1, 5, 3, 3])
z = paddle.floor_divide(x, y) >>> z = paddle.floor_divide(x, y)
print(z) # [2, 0, 2, 2] >>> print(z)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[2, 0, 2, 2])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -919,14 +948,16 @@ def remainder(x, y, name=None): ...@@ -919,14 +948,16 @@ def remainder(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([2, 3, 8, 7]) >>> x = paddle.to_tensor([2, 3, 8, 7])
y = paddle.to_tensor([1, 5, 3, 3]) >>> y = paddle.to_tensor([1, 5, 3, 3])
z = paddle.remainder(x, y) >>> z = paddle.remainder(x, y)
print(z) # [0, 3, 2, 1] >>> print(z)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 3, 2, 1])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -977,19 +1008,24 @@ def multiply(x, y, name=None): ...@@ -977,19 +1008,24 @@ def multiply(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
x = paddle.to_tensor([[1, 2], [3, 4]]) >>> import paddle
y = paddle.to_tensor([[5, 6], [7, 8]])
res = paddle.multiply(x, y)
print(res) # [[5, 12], [21, 32]]
x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]]) >>> x = paddle.to_tensor([[1, 2], [3, 4]])
y = paddle.to_tensor([2]) >>> y = paddle.to_tensor([[5, 6], [7, 8]])
res = paddle.multiply(x, y) >>> res = paddle.multiply(x, y)
print(res) # [[[2, 4, 6], [2, 4, 6]]] >>> print(res)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[5 , 12],
[21, 32]])
>>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
>>> y = paddle.to_tensor([2])
>>> res = paddle.multiply(x, y)
>>> print(res)
Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[[2, 4, 6],
[2, 4, 6]]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -1114,37 +1150,37 @@ def maximum(x, y, name=None): ...@@ -1114,37 +1150,37 @@ def maximum(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [7, 8]]) >>> x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]]) >>> y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.maximum(x, y) >>> res = paddle.maximum(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 4], [[3, 4],
# [7, 8]]) [7, 8]])
x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]]) >>> x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
y = paddle.to_tensor([3, 0, 4]) >>> y = paddle.to_tensor([3, 0, 4])
res = paddle.maximum(x, y) >>> res = paddle.maximum(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 2, 4], [[3, 2, 4],
# [3, 2, 4]]) [3, 2, 4]])
x = paddle.to_tensor([2, 3, 5], dtype='float32') >>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32') >>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
res = paddle.maximum(x, y) >>> res = paddle.maximum(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2. , nan, nan]) [2. , nan, nan])
x = paddle.to_tensor([5, 3, float("inf")], dtype='float32') >>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float32')
y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32') >>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32')
res = paddle.maximum(x, y) >>> res = paddle.maximum(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [5. , 3. , inf.]) [5. , 3. , inf.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.maximum(x, y) return _C_ops.maximum(x, y)
...@@ -1176,37 +1212,37 @@ def minimum(x, y, name=None): ...@@ -1176,37 +1212,37 @@ def minimum(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [7, 8]]) >>> x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]]) >>> y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.minimum(x, y) >>> res = paddle.minimum(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 2], [[1, 2],
# [5, 6]]) [5, 6]])
x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]]) >>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([3, 0, 4]) >>> y = paddle.to_tensor([3, 0, 4])
res = paddle.minimum(x, y) >>> res = paddle.minimum(x, y)
print(res) >>> print(res)
# Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[[1, 0, 3], [[[1, 0, 3],
# [1, 0, 3]]]) [1, 0, 3]]])
x = paddle.to_tensor([2, 3, 5], dtype='float32') >>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32') >>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
res = paddle.minimum(x, y) >>> res = paddle.minimum(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1. , nan, nan]) [1. , nan, nan])
x = paddle.to_tensor([5, 3, float("inf")], dtype='float64') >>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float64')
y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64') >>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64')
res = paddle.minimum(x, y) >>> res = paddle.minimum(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 1. , -inf., 5. ]) [ 1. , -inf., 5. ])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.minimum(x, y) return _C_ops.minimum(x, y)
...@@ -1240,37 +1276,37 @@ def fmax(x, y, name=None): ...@@ -1240,37 +1276,37 @@ def fmax(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [7, 8]]) >>> x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]]) >>> y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.fmax(x, y) >>> res = paddle.fmax(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 4], [[3, 4],
# [7, 8]]) [7, 8]])
x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]]) >>> x = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
y = paddle.to_tensor([3, 0, 4]) >>> y = paddle.to_tensor([3, 0, 4])
res = paddle.fmax(x, y) >>> res = paddle.fmax(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 2, 4], [[3, 2, 4],
# [3, 2, 4]]) [3, 2, 4]])
x = paddle.to_tensor([2, 3, 5], dtype='float32') >>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32') >>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
res = paddle.fmax(x, y) >>> res = paddle.fmax(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2., 3., 5.]) [2., 3., 5.])
x = paddle.to_tensor([5, 3, float("inf")], dtype='float32') >>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float32')
y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32') >>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float32')
res = paddle.fmax(x, y) >>> res = paddle.fmax(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [5. , 3. , inf.]) [5. , 3. , inf.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.fmax(x, y) return _C_ops.fmax(x, y)
...@@ -1304,37 +1340,37 @@ def fmin(x, y, name=None): ...@@ -1304,37 +1340,37 @@ def fmin(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [7, 8]]) >>> x = paddle.to_tensor([[1, 2], [7, 8]])
y = paddle.to_tensor([[3, 4], [5, 6]]) >>> y = paddle.to_tensor([[3, 4], [5, 6]])
res = paddle.fmin(x, y) >>> res = paddle.fmin(x, y)
print(res) >>> print(res)
# Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 2], [[1, 2],
# [5, 6]]) [5, 6]])
x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]]) >>> x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y = paddle.to_tensor([3, 0, 4]) >>> y = paddle.to_tensor([3, 0, 4])
res = paddle.fmin(x, y) >>> res = paddle.fmin(x, y)
print(res) >>> print(res)
# Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[1, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[[1, 0, 3], [[[1, 0, 3],
# [1, 0, 3]]]) [1, 0, 3]]])
x = paddle.to_tensor([2, 3, 5], dtype='float32') >>> x = paddle.to_tensor([2, 3, 5], dtype='float32')
y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32') >>> y = paddle.to_tensor([1, float("nan"), float("nan")], dtype='float32')
res = paddle.fmin(x, y) >>> res = paddle.fmin(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1., 3., 5.]) [1., 3., 5.])
x = paddle.to_tensor([5, 3, float("inf")], dtype='float64') >>> x = paddle.to_tensor([5, 3, float("inf")], dtype='float64')
y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64') >>> y = paddle.to_tensor([1, -float("inf"), 5], dtype='float64')
res = paddle.fmin(x, y) >>> res = paddle.fmin(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 1. , -inf., 5. ]) [ 1. , -inf., 5. ])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.fmin(x, y) return _C_ops.fmin(x, y)
...@@ -1369,37 +1405,65 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): ...@@ -1369,37 +1405,65 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# x is a Tensor with following elements: >>> # x is a Tensor with following elements:
# [[0.2, 0.3, 0.5, 0.9] >>> # [[0.2, 0.3, 0.5, 0.9]
# [0.1, 0.2, 0.6, 0.7]] >>> # [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the corresponding output tensor. >>> # Each example is followed by the corresponding output tensor.
x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
[0.1, 0.2, 0.6, 0.7]]) ... [0.1, 0.2, 0.6, 0.7]])
out1 = paddle.sum(x) # 3.5 >>> out1 = paddle.sum(x)
out2 = paddle.sum(x, axis=0) # [0.3, 0.5, 1.1, 1.6] >>> out1
out3 = paddle.sum(x, axis=-1) # [1.9, 1.6] Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
out4 = paddle.sum(x, axis=1, keepdim=True) # [[1.9], [1.6]] 3.50000000)
>>> out2 = paddle.sum(x, axis=0)
# y is a Tensor with shape [2, 2, 2] and elements as below: >>> out2
# [[[1, 2], [3, 4]], Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[5, 6], [7, 8]]] [0.30000001, 0.50000000, 1.10000002, 1.59999990])
# Each example is followed by the corresponding output tensor. >>> out3 = paddle.sum(x, axis=-1)
y = paddle.to_tensor([[[1, 2], [3, 4]], >>> out3
[[5, 6], [7, 8]]]) Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
out5 = paddle.sum(y, axis=[1, 2]) # [10, 26] [1.89999998, 1.60000002])
out6 = paddle.sum(y, axis=[0, 1]) # [16, 20] >>> out4 = paddle.sum(x, axis=1, keepdim=True)
>>> out4
# x is a Tensor with following elements: Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[True, True, True, True] [[1.89999998],
# [False, False, False, False]] [1.60000002]])
# Each example is followed by the corresponding output tensor.
x = paddle.to_tensor([[True, True, True, True], >>> # y is a Tensor with shape [2, 2, 2] and elements as below:
[False, False, False, False]]) >>> # [[[1, 2], [3, 4]],
out7 = paddle.sum(x) # 4 >>> # [[5, 6], [7, 8]]]
out8 = paddle.sum(x, axis=0) # [1, 1, 1, 1] >>> # Each example is followed by the corresponding output tensor.
out9 = paddle.sum(x, axis=1) # [4, 0] >>> y = paddle.to_tensor([[[1, 2], [3, 4]],
... [[5, 6], [7, 8]]])
>>> out5 = paddle.sum(y, axis=[1, 2])
>>> out5
Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
[10, 26])
>>> out6 = paddle.sum(y, axis=[0, 1])
>>> out6
Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
[16, 20])
>>> # x is a Tensor with following elements:
>>> # [[True, True, True, True]
>>> # [False, False, False, False]]
>>> # Each example is followed by the corresponding output tensor.
>>> x = paddle.to_tensor([[True, True, True, True],
... [False, False, False, False]])
>>> out7 = paddle.sum(x)
>>> out7
Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
4)
>>> out8 = paddle.sum(x, axis=0)
>>> out8
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 1, 1, 1])
>>> out9 = paddle.sum(x, axis=1)
>>> out9
Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
[4, 0])
""" """
dtype_flag = False dtype_flag = False
...@@ -1469,13 +1533,37 @@ def nan_to_num(x, nan=0.0, posinf=None, neginf=None, name=None): ...@@ -1469,13 +1533,37 @@ def nan_to_num(x, nan=0.0, posinf=None, neginf=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([float('nan'), 0.3, float('+inf'), float('-inf')], dtype='float32') >>> x = paddle.to_tensor([float('nan'), 0.3, float('+inf'), float('-inf')], dtype='float32')
out1 = paddle.nan_to_num(x) # [0, 0.3, 3.4028235e+38, -3.4028235e+38] >>> out1 = paddle.nan_to_num(x)
out2 = paddle.nan_to_num(x, nan=1) # [1, 0.3, 3.4028235e+38, -3.4028235e+38] >>> out1
out3 = paddle.nan_to_num(x, posinf=5) # [0, 0.3, 5, -3.4028235e+38] Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
out4 = paddle.nan_to_num(x, nan=10, neginf=-99) # [10, 0.3, 3.4028235e+38, -99] [ 0. ,
0.30000001 ,
340282346638528859811704183484516925440.,
-340282346638528859811704183484516925440.])
>>> out2 = paddle.nan_to_num(x, nan=1)
>>> out2
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 1. ,
0.30000001 ,
340282346638528859811704183484516925440.,
-340282346638528859811704183484516925440.])
>>> out3 = paddle.nan_to_num(x, posinf=5)
>>> out3
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 0. ,
0.30000001 ,
5. ,
-340282346638528859811704183484516925440.])
>>> out4 = paddle.nan_to_num(x, nan=10, neginf=-99)
>>> out4
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 10. ,
0.30000001 ,
340282346638528859811704183484516925440.,
-99. ])
""" """
# NOTE(tiancaishaonvjituizi): it seems that paddle handles the dtype of python float number # NOTE(tiancaishaonvjituizi): it seems that paddle handles the dtype of python float number
# incorrectly, so we have to explicitly contruct tensors here # incorrectly, so we have to explicitly contruct tensors here
...@@ -1525,27 +1613,46 @@ def nansum(x, axis=None, dtype=None, keepdim=False, name=None): ...@@ -1525,27 +1613,46 @@ def nansum(x, axis=None, dtype=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# x is a Tensor with following elements: >>> # x is a Tensor with following elements:
# [[nan, 0.3, 0.5, 0.9] >>> # [[nan, 0.3, 0.5, 0.9]
# [0.1, 0.2, -nan, 0.7]] >>> # [0.1, 0.2, -nan, 0.7]]
# Each example is followed by the corresponding output tensor. >>> # Each example is followed by the corresponding output tensor.
x = paddle.to_tensor([[float('nan'), 0.3, 0.5, 0.9], >>> x = paddle.to_tensor([[float('nan'), 0.3, 0.5, 0.9],
[0.1, 0.2, float('-nan'), 0.7]],dtype="float32") ... [0.1, 0.2, float('-nan'), 0.7]],dtype="float32")
out1 = paddle.nansum(x) # 2.7 >>> out1 = paddle.nansum(x)
out2 = paddle.nansum(x, axis=0) # [0.1, 0.5, 0.5, 1.6] >>> out1
out3 = paddle.nansum(x, axis=-1) # [1.7, 1.0] Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
out4 = paddle.nansum(x, axis=1, keepdim=True) # [[1.7], [1.0]] 2.69999981)
>>> out2 = paddle.nansum(x, axis=0)
# y is a Tensor with shape [2, 2, 2] and elements as below: >>> out2
# [[[1, nan], [3, 4]], Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[5, 6], [-nan, 8]]] [0.10000000, 0.50000000, 0.50000000, 1.59999990])
# Each example is followed by the corresponding output tensor. >>> out3 = paddle.nansum(x, axis=-1)
y = paddle.to_tensor([[[1, float('nan')], [3, 4]], >>> out3
[[5, 6], [float('-nan'), 8]]]) Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
out5 = paddle.nansum(y, axis=[1, 2]) # [8, 19] [1.70000005, 1. ])
out6 = paddle.nansum(y, axis=[0, 1]) # [9, 18] >>> out4 = paddle.nansum(x, axis=1, keepdim=True)
>>> out4
Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.70000005],
[1. ]])
>>> # y is a Tensor with shape [2, 2, 2] and elements as below:
>>> # [[[1, nan], [3, 4]],
>>> # [[5, 6], [-nan, 8]]]
>>> # Each example is followed by the corresponding output tensor.
>>> y = paddle.to_tensor([[[1, float('nan')], [3, 4]],
... [[5, 6], [float('-nan'), 8]]])
>>> out5 = paddle.nansum(y, axis=[1, 2])
>>> out5
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[8. , 19.])
>>> out6 = paddle.nansum(y, axis=[0, 1])
>>> out6
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[9. , 18.])
""" """
check_variable_and_dtype( check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64', 'int32', 'int64'], 'nansum' x, 'x', ['float16', 'float32', 'float64', 'int32', 'int64'], 'nansum'
...@@ -1588,29 +1695,43 @@ def nanmean(x, axis=None, keepdim=False, name=None): ...@@ -1588,29 +1695,43 @@ def nanmean(x, axis=None, keepdim=False, name=None):
.. code-block:: python .. code-block:: python
:name: code-example1 :name: code-example1
import paddle >>> import paddle
# x is a 2-D Tensor: >>> # x is a 2-D Tensor:
x = paddle.to_tensor([[float('nan'), 0.3, 0.5, 0.9], >>> x = paddle.to_tensor([[float('nan'), 0.3, 0.5, 0.9],
[0.1, 0.2, float('-nan'), 0.7]]) ... [0.1, 0.2, float('-nan'), 0.7]])
out1 = paddle.nanmean(x) >>> out1 = paddle.nanmean(x)
# 0.44999996 >>> out1
out2 = paddle.nanmean(x, axis=0) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.1, 0.25, 0.5, 0.79999995] 0.44999996)
out3 = paddle.nanmean(x, axis=0, keepdim=True) >>> out2 = paddle.nanmean(x, axis=0)
# [[0.1, 0.25, 0.5, 0.79999995]] >>> out2
out4 = paddle.nanmean(x, axis=1) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.56666666 0.33333334] [0.10000000, 0.25000000, 0.50000000, 0.79999995])
out5 = paddle.nanmean(x, axis=1, keepdim=True) >>> out3 = paddle.nanmean(x, axis=0, keepdim=True)
# [[0.56666666] >>> out3
# [0.33333334]] Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.10000000, 0.25000000, 0.50000000, 0.79999995]])
# y is a 3-D Tensor: >>> out4 = paddle.nanmean(x, axis=1)
y = paddle.to_tensor([[[1, float('nan')], [3, 4]], >>> out4
[[5, 6], [float('-nan'), 8]]]) Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
out6 = paddle.nanmean(y, axis=[1, 2]) [0.56666666, 0.33333334])
# [2.66666675, 6.33333349] >>> out5 = paddle.nanmean(x, axis=1, keepdim=True)
out7 = paddle.nanmean(y, axis=[0, 1]) >>> out5
# [3., 6.] Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.56666666],
[0.33333334]])
>>> # y is a 3-D Tensor:
>>> y = paddle.to_tensor([[[1, float('nan')], [3, 4]],
... [[5, 6], [float('-nan'), 8]]])
>>> out6 = paddle.nanmean(y, axis=[1, 2])
>>> out6
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.66666675, 6.33333349])
>>> out7 = paddle.nanmean(y, axis=[0, 1])
>>> out7
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[3., 6.])
""" """
if isinstance(axis, int): if isinstance(axis, int):
axis = [axis] axis = [axis]
...@@ -1651,29 +1772,43 @@ def count_nonzero(x, axis=None, keepdim=False, name=None): ...@@ -1651,29 +1772,43 @@ def count_nonzero(x, axis=None, keepdim=False, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# x is a 2-D Tensor: >>> # x is a 2-D Tensor:
x = paddle.to_tensor([[0., 1.1, 1.2], [0., 0., 1.3], [0., 0., 0.]]) >>> x = paddle.to_tensor([[0., 1.1, 1.2], [0., 0., 1.3], [0., 0., 0.]])
out1 = paddle.count_nonzero(x) >>> out1 = paddle.count_nonzero(x)
# 3 >>> out1
out2 = paddle.count_nonzero(x, axis=0) Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# [0, 1, 2] 3)
out3 = paddle.count_nonzero(x, axis=0, keepdim=True) >>> out2 = paddle.count_nonzero(x, axis=0)
# [[0, 1, 2]] >>> out2
out4 = paddle.count_nonzero(x, axis=1) Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [2, 1, 0] [0, 1, 2])
out5 = paddle.count_nonzero(x, axis=1, keepdim=True) >>> out3 = paddle.count_nonzero(x, axis=0, keepdim=True)
#[[2], >>> out3
# [1], Tensor(shape=[1, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [0]] [[0, 1, 2]])
>>> out4 = paddle.count_nonzero(x, axis=1)
# y is a 3-D Tensor: >>> out4
y = paddle.to_tensor([[[0., 1.1, 1.2], [0., 0., 1.3], [0., 0., 0.]], Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0., 2.5, 2.6], [0., 0., 2.4], [2.1, 2.2, 2.3]]]) [2, 1, 0])
out6 = paddle.count_nonzero(y, axis=[1, 2]) >>> out5 = paddle.count_nonzero(x, axis=1, keepdim=True)
# [3, 6] >>> out5
out7 = paddle.count_nonzero(y, axis=[0, 1]) Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
# [1, 3, 5] [[2],
[1],
[0]])
>>> # y is a 3-D Tensor:
>>> y = paddle.to_tensor([[[0., 1.1, 1.2], [0., 0., 1.3], [0., 0., 0.]],
... [[0., 2.5, 2.6], [0., 0., 2.4], [2.1, 2.2, 2.3]]])
>>> out6 = paddle.count_nonzero(y, axis=[1, 2])
>>> out6
Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
[3, 6])
>>> out7 = paddle.count_nonzero(y, axis=[0, 1])
>>> out7
Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 3, 5])
""" """
if isinstance(axis, int): if isinstance(axis, int):
...@@ -1734,13 +1869,15 @@ def add_n(inputs, name=None): ...@@ -1734,13 +1869,15 @@ def add_n(inputs, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input0 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') >>> input0 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32')
input1 = paddle.to_tensor([[7, 8, 9], [10, 11, 12]], dtype='float32') >>> input1 = paddle.to_tensor([[7, 8, 9], [10, 11, 12]], dtype='float32')
output = paddle.add_n([input0, input1]) >>> output = paddle.add_n([input0, input1])
# [[8., 10., 12.], >>> output
# [14., 16., 18.]] Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[8. , 10., 12.],
[14., 16., 18.]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
if isinstance(inputs, Variable): if isinstance(inputs, Variable):
...@@ -1800,19 +1937,14 @@ def trunc(input, name=None): ...@@ -1800,19 +1937,14 @@ def trunc(input, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.rand([2,2],'float32')
print(input)
# Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# [[0.02331470, 0.42374918],
# [0.79647720, 0.74970269]])
output = paddle.trunc(input) >>> input = paddle.to_tensor([[0.1, 1.5], [-0.2, -2.4]], 'float32')
print(output) >>> output = paddle.trunc(input)
# Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> output
# [[0., 0.], Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0., 0.]])) [[ 0., 1.],
[-0., -2.]])
''' '''
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.trunc(input) return _C_ops.trunc(input)
...@@ -1898,14 +2030,15 @@ def mm(input, mat2, name=None): ...@@ -1898,14 +2030,15 @@ def mm(input, mat2, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.arange(1, 7).reshape((3, 2)).astype('float32') >>> input = paddle.arange(1, 7).reshape((3, 2)).astype('float32')
mat2 = paddle.arange(1, 9).reshape((2, 4)).astype('float32') >>> mat2 = paddle.arange(1, 9).reshape((2, 4)).astype('float32')
out = paddle.mm(input, mat2) >>> out = paddle.mm(input, mat2)
print(out) >>> out
# [[11., 14., 17., 20.], Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [23., 30., 37., 44.], [[11., 14., 17., 20.],
# [35., 46., 57., 68.]]) [23., 30., 37., 44.],
[35., 46., 57., 68.]])
""" """
...@@ -1987,19 +2120,20 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None): ...@@ -1987,19 +2120,20 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None):
Tensor: The output Tensor of addmm. Tensor: The output Tensor of addmm.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.ones([2,2]) >>> x = paddle.ones([2, 2])
y = paddle.ones([2,2]) >>> y = paddle.ones([2, 2])
input = paddle.ones([2,2]) >>> input = paddle.ones([2, 2])
out = paddle.addmm( input=input, x=x, y=y, beta=0.5, alpha=5.0 ) >>> out = paddle.addmm(input=input, x=x, y=y, beta=0.5, alpha=5.0)
print(out) >>> print(out)
# [[10.5 10.5] Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [10.5 10.5]] [[10.50000000, 10.50000000],
[10.50000000, 10.50000000]])
""" """
input_shape = input.shape input_shape = input.shape
x_shape = x.shape x_shape = x.shape
...@@ -2156,17 +2290,19 @@ def renorm(x, p, axis, max_norm): ...@@ -2156,17 +2290,19 @@ def renorm(x, p, axis, max_norm):
Tensor: the renorm Tensor. Tensor: the renorm Tensor.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = [[[2.0,2,-2],[3,0.3,3]],[[2,-8,2],[3.1,3.7,3]]] >>> input = [[[2.0, 2, -2], [3, 0.3, 3]],
x = paddle.to_tensor(input,dtype='float32') ... [[2, -8, 2], [3.1, 3.7, 3]]]
y = paddle.renorm(x, 1.0, 2, 2.05) >>> x = paddle.to_tensor(input,dtype='float32')
print(y) >>> y = paddle.renorm(x, 1.0, 2, 2.05)
# [[[ 0.40594056, 0.29285714, -0.41000000], >>> print(y)
# [ 0.60891086, 0.04392857, 0.61500001]], Tensor(shape=[2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[ 0.40594056, -1.17142856, 0.41000000], [[[ 0.40594056, 0.29285714, -0.41000000],
# [ 0.62920785, 0.54178572, 0.61500001]]]) [ 0.60891086, 0.04392857, 0.61500001]],
[[ 0.40594056, -1.17142856, 0.41000000],
[ 0.62920785, 0.54178572, 0.61500001]]])
""" """
input_shape = x.shape input_shape = x.shape
...@@ -2219,13 +2355,14 @@ def inner(x, y, name=None): ...@@ -2219,13 +2355,14 @@ def inner(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(1, 7).reshape((2, 3)).astype('float32') >>> x = paddle.arange(1, 7).reshape((2, 3)).astype('float32')
y = paddle.arange(1, 10).reshape((3, 3)).astype('float32') >>> y = paddle.arange(1, 10).reshape((3, 3)).astype('float32')
out = paddle.inner(x, y) >>> out = paddle.inner(x, y)
print(out) >>> print(out)
# ([[14, 32, 50], Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [32, 77, 122]]) [[14. , 32. , 50. ],
[32. , 77. , 122.]])
""" """
...@@ -2293,14 +2430,15 @@ def outer(x, y, name=None): ...@@ -2293,14 +2430,15 @@ def outer(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(1, 4).astype('float32') >>> x = paddle.arange(1, 4).astype('float32')
y = paddle.arange(1, 6).astype('float32') >>> y = paddle.arange(1, 6).astype('float32')
out = paddle.outer(x, y) >>> out = paddle.outer(x, y)
print(out) >>> print(out)
# ([[1, 2, 3, 4, 5], Tensor(shape=[3, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2, 4, 6, 8, 10], [[1. , 2. , 3. , 4. , 5. ],
# [3, 6, 9, 12, 15]]) [2. , 4. , 6. , 8. , 10.],
[3. , 6. , 9. , 12., 15.]])
""" """
...@@ -2366,11 +2504,17 @@ def logsumexp(x, axis=None, keepdim=False, name=None): ...@@ -2366,11 +2504,17 @@ def logsumexp(x, axis=None, keepdim=False, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[-1.5, 0., 2.], [3., 1.2, -2.4]]) >>> x = paddle.to_tensor([[-1.5, 0., 2.], [3., 1.2, -2.4]])
out1 = paddle.logsumexp(x) # 3.4691226 >>> out1 = paddle.logsumexp(x)
out2 = paddle.logsumexp(x, 1) # [2.15317821, 3.15684602] >>> out1
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
3.46912265)
>>> out2 = paddle.logsumexp(x, 1)
>>> out2
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.15317822, 3.15684605])
""" """
reduce_all, axis = _get_reduce_axis(axis, x) reduce_all, axis = _get_reduce_axis(axis, x)
...@@ -2411,11 +2555,14 @@ def inverse(x, name=None): ...@@ -2411,11 +2555,14 @@ def inverse(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
mat = paddle.to_tensor([[2, 0], [0, 2]], dtype='float32') >>> mat = paddle.to_tensor([[2, 0], [0, 2]], dtype='float32')
inv = paddle.inverse(mat) >>> inv = paddle.inverse(mat)
print(inv) # [[0.5, 0], [0, 0.5]] >>> print(inv)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.50000000, 0. ],
[0. , 0.50000000]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -2471,51 +2618,86 @@ def max(x, axis=None, keepdim=False, name=None): ...@@ -2471,51 +2618,86 @@ def max(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# data_x is a Tensor with shape [2, 4] >>> # data_x is a Tensor with shape [2, 4]
# the axis is a int element >>> # the axis is a int element
x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
[0.1, 0.2, 0.6, 0.7]], ... [0.1, 0.2, 0.6, 0.7]],
dtype='float64', stop_gradient=False) ... dtype='float64', stop_gradient=False)
result1 = paddle.max(x) >>> result1 = paddle.max(x)
result1.backward() >>> result1.backward()
print(result1, x.grad) >>> result1
# 0.9, [[0., 0., 0., 1.], [0., 0., 0., 0.]] Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
0.90000000)
x.clear_grad() >>> x.grad
result2 = paddle.max(x, axis=0) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result2.backward() [[0., 0., 0., 1.],
print(result2, x.grad) [0., 0., 0., 0.]])
#[0.2, 0.3, 0.6, 0.9], [[1., 1., 0., 1.], [0., 0., 1., 0.]]
>>> x.clear_grad()
x.clear_grad() >>> result2 = paddle.max(x, axis=0)
result3 = paddle.max(x, axis=-1) >>> result2.backward()
result3.backward() >>> result2
print(result3, x.grad) Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=False,
#[0.9, 0.7], [[0., 0., 0., 1.], [0., 0., 0., 1.]] [0.20000000, 0.30000000, 0.60000000, 0.90000000])
>>> x.grad
x.clear_grad() Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result4 = paddle.max(x, axis=1, keepdim=True) [[1., 1., 0., 1.],
result4.backward() [0., 0., 1., 0.]])
print(result4, x.grad)
#[[0.9], [0.7]], [[0., 0., 0., 1.], [0., 0., 0., 1.]] >>> x.clear_grad()
>>> result3 = paddle.max(x, axis=-1)
# data_y is a Tensor with shape [2, 2, 2] >>> result3.backward()
# the axis is list >>> result3
y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]], Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[5.0, 6.0], [7.0, 8.0]]], [0.90000000, 0.70000000])
dtype='float64', stop_gradient=False) >>> x.grad
result5 = paddle.max(y, axis=[1, 2]) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result5.backward() [[0., 0., 0., 1.],
print(result5, y.grad) [0., 0., 0., 1.]])
#[4., 8.], [[[0., 0.], [0., 1.]], [[0., 0.], [0., 1.]]]
>>> x.clear_grad()
y.clear_grad() >>> result4 = paddle.max(x, axis=1, keepdim=True)
result6 = paddle.max(y, axis=[0, 1]) >>> result4.backward()
result6.backward() >>> result4
print(result6, y.grad) Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=False,
#[7., 8.], [[[0., 0.], [0., 0.]], [[0., 0.], [1., 1.]]] [[0.90000000],
[0.70000000]])
>>> x.grad
Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0., 0., 0., 1.],
[0., 0., 0., 1.]])
>>> # data_y is a Tensor with shape [2, 2, 2]
>>> # the axis is list
>>> y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
... [[5.0, 6.0], [7.0, 8.0]]],
... dtype='float64', stop_gradient=False)
>>> result5 = paddle.max(y, axis=[1, 2])
>>> result5.backward()
>>> result5
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[4., 8.])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[0., 0.],
[0., 1.]],
[[0., 0.],
[0., 1.]]])
>>> y.clear_grad()
>>> result6 = paddle.max(y, axis=[0, 1])
>>> result6.backward()
>>> result6
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[7., 8.])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[0., 0.],
[0., 0.]],
[[0., 0.],
[1., 1.]]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -2572,51 +2754,86 @@ def min(x, axis=None, keepdim=False, name=None): ...@@ -2572,51 +2754,86 @@ def min(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# data_x is a Tensor with shape [2, 4] >>> # data_x is a Tensor with shape [2, 4]
# the axis is a int element >>> # the axis is a int element
x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
[0.1, 0.2, 0.6, 0.7]], ... [0.1, 0.2, 0.6, 0.7]],
dtype='float64', stop_gradient=False) ... dtype='float64', stop_gradient=False)
result1 = paddle.min(x) >>> result1 = paddle.min(x)
result1.backward() >>> result1.backward()
print(result1, x.grad) >>> result1
# 0.1, [[0., 0., 0., 0.], [1., 0., 0., 0.]] Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
0.10000000)
x.clear_grad() >>> x.grad
result2 = paddle.min(x, axis=0) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result2.backward() [[0., 0., 0., 0.],
print(result2, x.grad) [1., 0., 0., 0.]])
#[0.1, 0.2, 0.5, 0.7], [[0., 0., 1., 0.], [1., 1., 0., 1.]]
>>> x.clear_grad()
x.clear_grad() >>> result2 = paddle.min(x, axis=0)
result3 = paddle.min(x, axis=-1) >>> result2.backward()
result3.backward() >>> result2
print(result3, x.grad) Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=False,
#[0.2, 0.1], [[1., 0., 0., 0.], [1., 0., 0., 0.]] [0.10000000, 0.20000000, 0.50000000, 0.70000000])
>>> x.grad
x.clear_grad() Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result4 = paddle.min(x, axis=1, keepdim=True) [[0., 0., 1., 0.],
result4.backward() [1., 1., 0., 1.]])
print(result4, x.grad)
#[[0.2], [0.1]], [[1., 0., 0., 0.], [1., 0., 0., 0.]] >>> x.clear_grad()
>>> result3 = paddle.min(x, axis=-1)
# data_y is a Tensor with shape [2, 2, 2] >>> result3.backward()
# the axis is list >>> result3
y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]], Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[5.0, 6.0], [7.0, 8.0]]], [0.20000000, 0.10000000])
dtype='float64', stop_gradient=False) >>> x.grad
result5 = paddle.min(y, axis=[1, 2]) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result5.backward() [[1., 0., 0., 0.],
print(result5, y.grad) [1., 0., 0., 0.]])
#[1., 5.], [[[1., 0.], [0., 0.]], [[1., 0.], [0., 0.]]]
>>> x.clear_grad()
y.clear_grad() >>> result4 = paddle.min(x, axis=1, keepdim=True)
result6 = paddle.min(y, axis=[0, 1]) >>> result4.backward()
result6.backward() >>> result4
print(result6, y.grad) Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=False,
#[1., 2.], [[[1., 1.], [0., 0.]], [[0., 0.], [0., 0.]]] [[0.20000000],
[0.10000000]])
>>> x.grad
Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[1., 0., 0., 0.],
[1., 0., 0., 0.]])
>>> # data_y is a Tensor with shape [2, 2, 2]
>>> # the axis is list
>>> y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
... [[5.0, 6.0], [7.0, 8.0]]],
... dtype='float64', stop_gradient=False)
>>> result5 = paddle.min(y, axis=[1, 2])
>>> result5.backward()
>>> result5
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[1., 5.])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[1., 0.],
[0., 0.]],
[[1., 0.],
[0., 0.]]])
>>> y.clear_grad()
>>> result6 = paddle.min(y, axis=[0, 1])
>>> result6.backward()
>>> result6
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[1., 2.])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[1., 1.],
[0., 0.]],
[[0., 0.],
[0., 0.]]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -2671,64 +2888,102 @@ def amax(x, axis=None, keepdim=False, name=None): ...@@ -2671,64 +2888,102 @@ def amax(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# data_x is a Tensor with shape [2, 4] with multiple maximum elements >>> # data_x is a Tensor with shape [2, 4] with multiple maximum elements
# the axis is a int element >>> # the axis is a int element
x = paddle.to_tensor([[0.1, 0.9, 0.9, 0.9], >>> x = paddle.to_tensor([[0.1, 0.9, 0.9, 0.9],
[0.9, 0.9, 0.6, 0.7]], ... [0.9, 0.9, 0.6, 0.7]],
dtype='float64', stop_gradient=False) ... dtype='float64', stop_gradient=False)
# There are 5 maximum elements: >>> # There are 5 maximum elements:
# 1) amax evenly distributes gradient between these equal values, >>> # 1) amax evenly distributes gradient between these equal values,
# thus the corresponding gradients are 1/5=0.2; >>> # thus the corresponding gradients are 1/5=0.2;
# 2) while max propagates gradient to all of them, >>> # 2) while max propagates gradient to all of them,
# thus the corresponding gradient are 1. >>> # thus the corresponding gradient are 1.
result1 = paddle.amax(x) >>> result1 = paddle.amax(x)
result1.backward() >>> result1.backward()
print(result1, x.grad) >>> result1
# 0.9, [[0., 0.2, 0.2, 0.2], [0.2, 0.2, 0., 0.]] Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
0.90000000)
x.clear_grad() >>> x.grad
result1_max = paddle.max(x) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result1_max.backward() [[0. , 0.20000000, 0.20000000, 0.20000000],
print(result1_max, x.grad) [0.20000000, 0.20000000, 0. , 0. ]])
# 0.9, [[0., 1.0, 1.0, 1.0], [1.0, 1.0, 0., 0.]]
>>> x.clear_grad()
############################### >>> result1_max = paddle.max(x)
>>> result1_max.backward()
x.clear_grad() >>> result1_max
result2 = paddle.amax(x, axis=0) Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
result2.backward() 0.90000000)
print(result2, x.grad) >>> x.grad
#[0.9, 0.9, 0.9, 0.9], [[0., 0.5, 1., 1.], [1., 0.5, 0., 0.]] Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0., 1., 1., 1.],
x.clear_grad() [1., 1., 0., 0.]])
result3 = paddle.amax(x, axis=-1)
result3.backward() >>> x.clear_grad()
print(result3, x.grad) >>> result2 = paddle.amax(x, axis=0)
#[0.9, 0.9], [[0., 0.3333, 0.3333, 0.3333], [0.5, 0.5, 0., 0.]] >>> result2.backward()
>>> result2
x.clear_grad() Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=False,
result4 = paddle.amax(x, axis=1, keepdim=True) [0.90000000, 0.90000000, 0.90000000, 0.90000000])
result4.backward() >>> x.grad
print(result4, x.grad) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
#[[0.9], [0.9]], [[0., 0.3333, 0.3333, 0.3333.], [0.5, 0.5, 0., 0.]] [[0. , 0.50000000, 1. , 1. ],
[1. , 0.50000000, 0. , 0. ]])
# data_y is a Tensor with shape [2, 2, 2]
# the axis is list >>> x.clear_grad()
y = paddle.to_tensor([[[0.1, 0.9], [0.9, 0.9]], >>> result3 = paddle.amax(x, axis=-1)
[[0.9, 0.9], [0.6, 0.7]]], >>> result3.backward()
dtype='float64', stop_gradient=False) >>> result3
result5 = paddle.amax(y, axis=[1, 2]) Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
result5.backward() [0.90000000, 0.90000000])
print(result5, y.grad) >>> x.grad
#[0.9., 0.9], [[[0., 0.3333], [0.3333, 0.3333]], [[0.5, 0.5], [0., 1.]]] Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0. , 0.33333333, 0.33333333, 0.33333333],
y.clear_grad() [0.50000000, 0.50000000, 0. , 0. ]])
result6 = paddle.amax(y, axis=[0, 1])
result6.backward() >>> x.clear_grad()
print(result6, y.grad) >>> result4 = paddle.amax(x, axis=1, keepdim=True)
#[0.9., 0.9], [[[0., 0.3333], [0.5, 0.3333]], [[0.5, 0.3333], [1., 1.]]] >>> result4.backward()
>>> result4
Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0.90000000],
[0.90000000]])
>>> x.grad
Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0. , 0.33333333, 0.33333333, 0.33333333],
[0.50000000, 0.50000000, 0. , 0. ]])
>>> # data_y is a Tensor with shape [2, 2, 2]
>>> # the axis is list
>>> y = paddle.to_tensor([[[0.1, 0.9], [0.9, 0.9]],
... [[0.9, 0.9], [0.6, 0.7]]],
... dtype='float64', stop_gradient=False)
>>> result5 = paddle.amax(y, axis=[1, 2])
>>> result5.backward()
>>> result5
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[0.90000000, 0.90000000])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[0. , 0.33333333],
[0.33333333, 0.33333333]],
[[0.50000000, 0.50000000],
[0. , 0. ]]])
>>> y.clear_grad()
>>> result6 = paddle.amax(y, axis=[0, 1])
>>> result6.backward()
>>> result6
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[0.90000000, 0.90000000])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[0. , 0.33333333],
[0.50000000, 0.33333333]],
[[0.50000000, 0.33333333],
[0. , 0. ]]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.amax(x, axis, keepdim) return _C_ops.amax(x, axis, keepdim)
...@@ -2781,64 +3036,102 @@ def amin(x, axis=None, keepdim=False, name=None): ...@@ -2781,64 +3036,102 @@ def amin(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# data_x is a Tensor with shape [2, 4] with multiple minimum elements >>> # data_x is a Tensor with shape [2, 4] with multiple minimum elements
# the axis is a int element >>> # the axis is a int element
x = paddle.to_tensor([[0.2, 0.1, 0.1, 0.1], >>> x = paddle.to_tensor([[0.2, 0.1, 0.1, 0.1],
[0.1, 0.1, 0.6, 0.7]], ... [0.1, 0.1, 0.6, 0.7]],
dtype='float64', stop_gradient=False) ... dtype='float64', stop_gradient=False)
# There are 5 minimum elements: >>> # There are 5 minimum elements:
# 1) amin evenly distributes gradient between these equal values, >>> # 1) amin evenly distributes gradient between these equal values,
# thus the corresponding gradients are 1/5=0.2; >>> # thus the corresponding gradients are 1/5=0.2;
# 2) while min propagates gradient to all of them, >>> # 2) while min propagates gradient to all of them,
# thus the corresponding gradient are 1. >>> # thus the corresponding gradient are 1.
result1 = paddle.amin(x) >>> result1 = paddle.amin(x)
result1.backward() >>> result1.backward()
print(result1, x.grad) >>> result1
# 0.1, [[0., 0.2, 0.2, 0.2], [0.2, 0.2, 0., 0.]] Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
0.10000000)
x.clear_grad() >>> x.grad
result1_min = paddle.min(x) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
result1_min.backward() [[0. , 0.20000000, 0.20000000, 0.20000000],
print(result1_min, x.grad) [0.20000000, 0.20000000, 0. , 0. ]])
# 0.1, [[0., 1.0, 1.0, 1.0], [1.0, 1.0, 0., 0.]]
>>> x.clear_grad()
############################### >>> result1_min = paddle.min(x)
>>> result1_min.backward()
x.clear_grad() >>> result1_min
result2 = paddle.amin(x, axis=0) Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False,
result2.backward() 0.10000000)
print(result2, x.grad) >>> x.grad
#[0.1, 0.1, 0.1, 0.1], [[0., 0.5, 1., 1.], [1., 0.5, 0., 0.]] Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0., 1., 1., 1.],
x.clear_grad() [1., 1., 0., 0.]])
result3 = paddle.amin(x, axis=-1)
result3.backward() >>> x.clear_grad()
print(result3, x.grad) >>> result2 = paddle.amin(x, axis=0)
#[0.1, 0.1], [[0., 0.3333, 0.3333, 0.3333], [0.5, 0.5, 0., 0.]] >>> result2.backward()
>>> result2
x.clear_grad() Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=False,
result4 = paddle.amin(x, axis=1, keepdim=True) [0.10000000, 0.10000000, 0.10000000, 0.10000000])
result4.backward() >>> x.grad
print(result4, x.grad) Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
#[[0.1], [0.1]], [[0., 0.3333, 0.3333, 0.3333.], [0.5, 0.5, 0., 0.]] [[0. , 0.50000000, 1. , 1. ],
[1. , 0.50000000, 0. , 0. ]])
# data_y is a Tensor with shape [2, 2, 2]
# the axis is list >>> x.clear_grad()
y = paddle.to_tensor([[[0.2, 0.1], [0.1, 0.1]], >>> result3 = paddle.amin(x, axis=-1)
[[0.1, 0.1], [0.6, 0.7]]], >>> result3.backward()
dtype='float64', stop_gradient=False) >>> result3
result5 = paddle.amin(y, axis=[1, 2]) Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
result5.backward() [0.10000000, 0.10000000])
print(result5, y.grad) >>> x.grad
#[0.1., 0.1], [[[0., 0.3333], [0.3333, 0.3333]], [[0.5, 0.5], [0., 1.]]] Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0. , 0.33333333, 0.33333333, 0.33333333],
y.clear_grad() [0.50000000, 0.50000000, 0. , 0. ]])
result6 = paddle.amin(y, axis=[0, 1])
result6.backward() >>> x.clear_grad()
print(result6, y.grad) >>> result4 = paddle.amin(x, axis=1, keepdim=True)
#[0.1., 0.1], [[[0., 0.3333], [0.5, 0.3333]], [[0.5, 0.3333], [1., 1.]]] >>> result4.backward()
>>> result4
Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0.10000000],
[0.10000000]])
>>> x.grad
Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
[[0. , 0.33333333, 0.33333333, 0.33333333],
[0.50000000, 0.50000000, 0. , 0. ]])
>>> # data_y is a Tensor with shape [2, 2, 2]
>>> # the axis is list
>>> y = paddle.to_tensor([[[0.2, 0.1], [0.1, 0.1]],
... [[0.1, 0.1], [0.6, 0.7]]],
... dtype='float64', stop_gradient=False)
>>> result5 = paddle.amin(y, axis=[1, 2])
>>> result5.backward()
>>> result5
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[0.10000000, 0.10000000])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[0. , 0.33333333],
[0.33333333, 0.33333333]],
[[0.50000000, 0.50000000],
[0. , 0. ]]])
>>> y.clear_grad()
>>> result6 = paddle.amin(y, axis=[0, 1])
>>> result6.backward()
>>> result6
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=False,
[0.10000000, 0.10000000])
>>> y.grad
Tensor(shape=[2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=False,
[[[0. , 0.33333333],
[0.50000000, 0.33333333]],
[[0.50000000, 0.33333333],
[0. , 0. ]]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.amin(x, axis, keepdim) return _C_ops.amin(x, axis, keepdim)
...@@ -2877,11 +3170,14 @@ def log1p(x, name=None): ...@@ -2877,11 +3170,14 @@ def log1p(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.to_tensor([[0], [1]], dtype='float32') >>> data = paddle.to_tensor([[0], [1]], dtype='float32')
res = paddle.log1p(data) >>> res = paddle.log1p(data)
# [[0.], [0.6931472]] >>> res
Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0. ],
[0.69314718]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -2932,23 +3228,31 @@ def log2(x, name=None): ...@@ -2932,23 +3228,31 @@ def log2(x, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# example 1: x is a float >>> # example 1: x is a float
x_i = paddle.to_tensor([[1.0], [2.0]]) >>> x_i = paddle.to_tensor([[1.0], [2.0]])
res = paddle.log2(x_i) # [[0.], [1.0]] >>> res = paddle.log2(x_i)
>>> res
# example 2: x is float32 Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
x_i = paddle.full(shape=[1], fill_value=2, dtype='float32') [[0.],
paddle.to_tensor(x_i) [1.]])
res = paddle.log2(x_i)
print(res) # [1.0] >>> # example 2: x is float32
>>> x_i = paddle.full(shape=[1], fill_value=2, dtype='float32')
# example 3: x is float64 >>> paddle.to_tensor(x_i)
x_i = paddle.full(shape=[1], fill_value=2, dtype='float64') >>> res = paddle.log2(x_i)
paddle.to_tensor(x_i) >>> res
res = paddle.log2(x_i) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
print(res) # [1.0] [1.])
>>> # example 3: x is float64
>>> x_i = paddle.full(shape=[1], fill_value=2, dtype='float64')
>>> paddle.to_tensor(x_i)
>>> res = paddle.log2(x_i)
>>> res
Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True,
[1.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.log2(x) return _C_ops.log2(x)
...@@ -2998,23 +3302,31 @@ def log10(x, name=None): ...@@ -2998,23 +3302,31 @@ def log10(x, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# example 1: x is a float >>> # example 1: x is a float
x_i = paddle.to_tensor([[1.0], [10.0]]) >>> x_i = paddle.to_tensor([[1.0], [10.0]])
res = paddle.log10(x_i) # [[0.], [1.0]] >>> res = paddle.log10(x_i)
>>> res
# example 2: x is float32 Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
x_i = paddle.full(shape=[1], fill_value=10, dtype='float32') [[0.],
paddle.to_tensor(x_i) [1.]])
res = paddle.log10(x_i)
print(res) # [1.0] >>> # example 2: x is float32
>>> x_i = paddle.full(shape=[1], fill_value=10, dtype='float32')
# example 3: x is float64 >>> paddle.to_tensor(x_i)
x_i = paddle.full(shape=[1], fill_value=10, dtype='float64') >>> res = paddle.log10(x_i)
paddle.to_tensor(x_i) >>> res
res = paddle.log10(x_i) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
print(res) # [1.0] [1.])
>>> # example 3: x is float64
>>> x_i = paddle.full(shape=[1], fill_value=10, dtype='float64')
>>> paddle.to_tensor(x_i)
>>> res = paddle.log10(x_i)
>>> res
Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True,
[1.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.log10(x) return _C_ops.log10(x)
...@@ -3067,17 +3379,19 @@ def clip(x, min=None, max=None, name=None): ...@@ -3067,17 +3379,19 @@ def clip(x, min=None, max=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x1 = paddle.to_tensor([[1.2, 3.5], [4.5, 6.4]], 'float32') >>> x1 = paddle.to_tensor([[1.2, 3.5], [4.5, 6.4]], 'float32')
out1 = paddle.clip(x1, min=3.5, max=5.0) >>> out1 = paddle.clip(x1, min=3.5, max=5.0)
out2 = paddle.clip(x1, min=2.5) >>> out1
print(out1) Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[3.5, 3.5] [[3.50000000, 3.50000000],
# [4.5, 5.0]] [4.50000000, 5. ]])
print(out2) >>> out2 = paddle.clip(x1, min=2.5)
# [[2.5, 3.5] >>> out2
# [[4.5, 6.4] Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2.50000000, 3.50000000],
[4.50000000, 6.40000010]])
""" """
x_dtype = str(x.dtype) x_dtype = str(x.dtype)
...@@ -3207,14 +3521,20 @@ def trace(x, offset=0, axis1=0, axis2=1, name=None): ...@@ -3207,14 +3521,20 @@ def trace(x, offset=0, axis1=0, axis2=1, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
case1 = paddle.randn([2, 3]) >>> case1 = paddle.randn([2, 3])
case2 = paddle.randn([3, 10, 10]) >>> case2 = paddle.randn([3, 10, 10])
case3 = paddle.randn([3, 10, 5, 10]) >>> case3 = paddle.randn([3, 10, 5, 10])
data1 = paddle.trace(case1) # data1.shape = [] >>> data1 = paddle.trace(case1)
data2 = paddle.trace(case2, offset=1, axis1=1, axis2=2) # data2.shape = [3] >>> data1.shape
data3 = paddle.trace(case3, offset=-3, axis1=1, axis2=-1) # data2.shape = [3, 5] []
>>> data2 = paddle.trace(case2, offset=1, axis1=1, axis2=2)
>>> data2.shape
[3]
>>> data3 = paddle.trace(case3, offset=-3, axis1=1, axis2=-1)
>>> data3.shape
[3, 5]
""" """
def __check_input(x, offset, axis1, axis2): def __check_input(x, offset, axis1, axis2):
...@@ -3293,42 +3613,42 @@ def diagonal(x, offset=0, axis1=0, axis2=1, name=None): ...@@ -3293,42 +3613,42 @@ def diagonal(x, offset=0, axis1=0, axis2=1, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.rand([2,2,3],'float32') >>> paddle.seed(2023)
print(x) >>> x = paddle.rand([2, 2, 3],'float32')
# Tensor(shape=[2, 2, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> print(x)
# [[[0.45661032, 0.03751532, 0.90191704], Tensor(shape=[2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.43760979, 0.86177313, 0.65221709]], [[[0.86583614, 0.52014720, 0.25960937],
[0.90525323, 0.42400089, 0.40641287]],
# [[0.17020577, 0.00259554, 0.28954273], [[0.97020894, 0.74437362, 0.51785129],
# [0.51795638, 0.27325270, 0.18117726]]]) [0.73292869, 0.97786582, 0.04315904]]])
out1 = paddle.diagonal(x) >>> out1 = paddle.diagonal(x)
print(out1) >>> print(out1)
#Tensor(shape=[3, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.45661032, 0.51795638], [[0.86583614, 0.73292869],
# [0.03751532, 0.27325270], [0.52014720, 0.97786582],
# [0.90191704, 0.18117726]]) [0.25960937, 0.04315904]])
out2 = paddle.diagonal(x, offset=0, axis1=2, axis2=1) >>> out2 = paddle.diagonal(x, offset=0, axis1=2, axis2=1)
print(out2) >>> print(out2)
#Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.45661032, 0.86177313], [[0.86583614, 0.42400089],
# [0.17020577, 0.27325270]]) [0.97020894, 0.97786582]])
out3 = paddle.diagonal(x, offset=1, axis1=0, axis2=1) >>> out3 = paddle.diagonal(x, offset=1, axis1=0, axis2=1)
print(out3) >>> print(out3)
#Tensor(shape=[3, 1], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.43760979], [[0.90525323],
# [0.86177313], [0.42400089],
# [0.65221709]]) [0.40641287]])
out4 = paddle.diagonal(x, offset=0, axis1=1, axis2=2) >>> out4 = paddle.diagonal(x, offset=0, axis1=1, axis2=2)
print(out4) >>> print(out4)
#Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.45661032, 0.86177313], [[0.86583614, 0.42400089],
# [0.17020577, 0.27325270]]) [0.97020894, 0.97786582]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -3420,17 +3740,18 @@ def kron(x, y, name=None): ...@@ -3420,17 +3740,18 @@ def kron(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [3, 4]], dtype='int64') >>> x = paddle.to_tensor([[1, 2], [3, 4]], dtype='int64')
y = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='int64') >>> y = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='int64')
out = paddle.kron(x, y) >>> out = paddle.kron(x, y)
print(out) >>> out
# [[1, 2, 3, 2, 4, 6], Tensor(shape=[6, 6], dtype=int64, place=Place(cpu), stop_gradient=True,
# [ 4, 5, 6, 8, 10, 12], [[1 , 2 , 3 , 2 , 4 , 6 ],
# [ 7, 8, 9, 14, 16, 18], [4 , 5 , 6 , 8 , 10, 12],
# [ 3, 6, 9, 4, 8, 12], [7 , 8 , 9 , 14, 16, 18],
# [12, 15, 18, 16, 20, 24], [3 , 6 , 9 , 4 , 8 , 12],
# [21, 24, 27, 28, 32, 36]]) [12, 15, 18, 16, 20, 24],
[21, 24, 27, 28, 32, 36]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _legacy_C_ops.kron(x, y) return _legacy_C_ops.kron(x, y)
...@@ -3469,27 +3790,32 @@ def cumsum(x, axis=None, dtype=None, name=None): ...@@ -3469,27 +3790,32 @@ def cumsum(x, axis=None, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.arange(12) >>> data = paddle.arange(12)
data = paddle.reshape(data, (3, 4)) >>> data = paddle.reshape(data, (3, 4))
y = paddle.cumsum(data) >>> y = paddle.cumsum(data)
# [ 0 1 3 6 10 15 21 28 36 45 55 66] >>> y
Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True,
[0 , 1 , 3 , 6 , 10, 15, 21, 28, 36, 45, 55, 66])
y = paddle.cumsum(data, axis=0) >>> y = paddle.cumsum(data, axis=0)
# [[ 0 1 2 3] >>> y
# [ 4 6 8 10] Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [12 15 18 21]] [[0 , 1 , 2 , 3 ],
[4 , 6 , 8 , 10],
[12, 15, 18, 21]])
y = paddle.cumsum(data, axis=-1) >>> y = paddle.cumsum(data, axis=-1)
# [[ 0 1 3 6] >>> y
# [ 4 9 15 22] Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [ 8 17 27 38]] [[0 , 1 , 3 , 6 ],
[4 , 9 , 15, 22],
[8 , 17, 27, 38]])
y = paddle.cumsum(data, dtype='float64') >>> y = paddle.cumsum(data, dtype='float64')
print(y.dtype) >>> assert y.dtype == paddle.float64
# paddle.float64
""" """
if axis is None: if axis is None:
flatten = True flatten = True
...@@ -3540,30 +3866,41 @@ def cummax(x, axis=None, dtype='int64', name=None): ...@@ -3540,30 +3866,41 @@ def cummax(x, axis=None, dtype='int64', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.to_tensor([-1, 5, 0, -2, -3, 2]) >>> data = paddle.to_tensor([-1, 5, 0, -2, -3, 2])
data = paddle.reshape(data, (2, 3)) >>> data = paddle.reshape(data, (2, 3))
y = paddle.cummax(data) >>> value, indices = paddle.cummax(data)
# value: [-1, 5, 5, 5, 5, 5] >>> value
# indcies: [0, 1, 1, 1, 1, 1] Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
[-1, 5, 5, 5, 5, 5])
y = paddle.cummax(data, axis=0) >>> indices
# value: [[-1, 5, 0] Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
# [-1, 5, 2]] [0, 1, 1, 1, 1, 1])
# indcies: [[0, 0, 0]
# [0, 0, 1]] >>> value, indices = paddle.cummax(data, axis=0)
>>> value
y = paddle.cummax(data, axis=-1) Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# value: [[-1, 5, 5] [[-1, 5, 0],
# [-2, -2, 2]] [-1, 5, 2]])
# indcies: [[0, 1, 1] >>> indices
# [0, 0, 2]] Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0, 0],
y = paddle.cummax(data, dtype='int64') [0, 0, 1]])
print(y[1].dtype)
# indcies type: paddle.int64 >>> value, indices = paddle.cummax(data, axis=-1)
>>> value
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-1, 5, 5],
[-2, -2, 2]])
>>> indices
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 1, 1],
[0, 0, 2]])
>>> value, indices = paddle.cummax(data, dtype='int64')
>>> assert indices.dtype == paddle.int64
""" """
if axis is None: if axis is None:
axis = -1 axis = -1
...@@ -3615,29 +3952,40 @@ def cummin(x, axis=None, dtype='int64', name=None): ...@@ -3615,29 +3952,40 @@ def cummin(x, axis=None, dtype='int64', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.to_tensor([-1, 5, 0, -2, -3, 2]) >>> data = paddle.to_tensor([-1, 5, 0, -2, -3, 2])
data = paddle.reshape(data, (2, 3)) >>> data = paddle.reshape(data, (2, 3))
y = paddle.cummin(data) >>> value, indices = paddle.cummin(data)
# value: [-1, -1, -1, -2, -3, -3] >>> value
# indcies: [0, 0, 0, 3, 4, 4] Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
[-1, -1, -1, -2, -3, -3])
y = paddle.cummin(data, axis=0) >>> indices
# value: [[-1, 5, 0] Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
# [-2, -3, 0]] [0, 0, 0, 3, 4, 4])
# indcies: [[0, 0, 0]
# [1, 1, 0]] >>> value, indices = paddle.cummin(data, axis=0)
>>> value
y = paddle.cummin(data, axis=-1) Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# value: [[-1, -1, -1] [[-1, 5, 0],
# [-2, -3, -3]] [-2, -3, 0]])
# indcies: [[0, 0, 0] >>> indices
# [0, 1, 1]] Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0, 0],
y = paddle.cummin(data, dtype='int64') [1, 1, 0]])
print(y[1].dtype)
# indcies type: paddle.int64 >>> value, indices = paddle.cummin(data, axis=-1)
>>> value
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-1, -1, -1],
[-2, -3, -3]])
>>> indices
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0, 0],
[0, 1, 1]])
>>> value, indices = paddle.cummin(data, dtype='int64')
>>> assert indices.dtype == paddle.int64
""" """
if axis is None: if axis is None:
axis = -1 axis = -1
...@@ -3693,28 +4041,34 @@ def logcumsumexp(x, axis=None, dtype=None, name=None): ...@@ -3693,28 +4041,34 @@ def logcumsumexp(x, axis=None, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.arange(12, dtype='float64') >>> data = paddle.arange(12, dtype='float64')
data = paddle.reshape(data, (3, 4)) >>> data = paddle.reshape(data, (3, 4))
y = paddle.logcumsumexp(data) >>> y = paddle.logcumsumexp(data)
# [ 0. 1.3132617 2.4076061 3.4401898 4.4519143 5.4561934 >>> y
# 6.4577627 7.4583397 8.458551 9.45863 10.458658 11.458669 ] Tensor(shape=[12], dtype=float64, place=Place(cpu), stop_gradient=True,
[0. , 1.31326169 , 2.40760596 , 3.44018970 , 4.45191440 ,
5.45619332 , 6.45776285 , 7.45833963 , 8.45855173 , 9.45862974 ,
10.45865844, 11.45866900])
y = paddle.logcumsumexp(data, axis=0) >>> y = paddle.logcumsumexp(data, axis=0)
# [[ 0. 1. 2. 3. ] >>> y
# [ 4.01815 5.01815 6.01815 7.01815 ] Tensor(shape=[3, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 8.018479 9.018479 10.018479 11.018479]] [[0. , 1. , 2. , 3. ],
[4.01814993 , 5.01814993 , 6.01814993 , 7.01814993 ],
[8.01847930 , 9.01847930 , 10.01847930, 11.01847930]])
y = paddle.logcumsumexp(data, axis=-1) >>> y = paddle.logcumsumexp(data, axis=-1)
# [[ 0. 1.3132617 2.4076061 3.4401898] >>> y
# [ 4. 5.3132615 6.407606 7.44019 ] Tensor(shape=[3, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 8. 9.313262 10.407606 11.440189 ]] [[0. , 1.31326169 , 2.40760596 , 3.44018970 ],
[4. , 5.31326169 , 6.40760596 , 7.44018970 ],
[8. , 9.31326169 , 10.40760596, 11.44018970]])
y = paddle.logcumsumexp(data, dtype='float64') >>> y = paddle.logcumsumexp(data, dtype='float64')
print(y.dtype) >>> assert y.dtype == paddle.float64
# paddle.float64
""" """
if axis is None: if axis is None:
flatten = True flatten = True
...@@ -3766,31 +4120,38 @@ def cumprod(x, dim=None, dtype=None, name=None): ...@@ -3766,31 +4120,38 @@ def cumprod(x, dim=None, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.arange(12) >>> data = paddle.arange(12)
data = paddle.reshape(data, (3, 4)) >>> data = paddle.reshape(data, (3, 4))
# [[ 0 1 2 3 ] >>> data
# [ 4 5 6 7 ] Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [ 8 9 10 11]] [[0 , 1 , 2 , 3 ],
[4 , 5 , 6 , 7 ],
[8 , 9 , 10, 11]])
y = paddle.cumprod(data, dim=0) >>> y = paddle.cumprod(data, dim=0)
# [[ 0 1 2 3] >>> y
# [ 0 5 12 21] Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [ 0 45 120 231]] [[0 , 1 , 2 , 3 ],
[0 , 5 , 12 , 21 ],
[0 , 45 , 120, 231]])
y = paddle.cumprod(data, dim=-1) >>> y = paddle.cumprod(data, dim=-1)
# [[ 0 0 0 0] >>> y
# [ 4 20 120 840] Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [ 8 72 720 7920]] [[0 , 0 , 0 , 0 ],
[4 , 20 , 120 , 840 ],
[8 , 72 , 720 , 7920]])
y = paddle.cumprod(data, dim=1, dtype='float64') >>> y = paddle.cumprod(data, dim=1, dtype='float64')
# [[ 0. 0. 0. 0.] >>> y
# [ 4. 20. 120. 840.] Tensor(shape=[3, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
# [ 8. 72. 720. 7920.]] [[0. , 0. , 0. , 0. ],
[4. , 20. , 120. , 840. ],
[8. , 72. , 720. , 7920.]])
print(y.dtype) >>> assert y.dtype == paddle.float64
# paddle.float64
""" """
...@@ -3843,11 +4204,13 @@ def isfinite(x, name=None): ...@@ -3843,11 +4204,13 @@ def isfinite(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')]) >>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
out = paddle.isfinite(x) >>> out = paddle.isfinite(x)
print(out) # [False True True False True False False] >>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, True , True , False, True , False, False])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.isfinite(x) return _C_ops.isfinite(x)
...@@ -3888,11 +4251,13 @@ def isinf(x, name=None): ...@@ -3888,11 +4251,13 @@ def isinf(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')]) >>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
out = paddle.isinf(x) >>> out = paddle.isinf(x)
print(out) # [ True False False True False False False] >>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[True , False, False, True , False, False, False])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.isinf(x) return _C_ops.isinf(x)
...@@ -3931,11 +4296,13 @@ def isnan(x, name=None): ...@@ -3931,11 +4296,13 @@ def isnan(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')]) >>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')])
out = paddle.isnan(x) >>> out = paddle.isnan(x)
print(out) # [False False False False False True True] >>> out
Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, False, False, False, False, True , True ])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.isnan(x) return _C_ops.isnan(x)
...@@ -3983,34 +4350,48 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None): ...@@ -3983,34 +4350,48 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# the axis is a int element >>> # the axis is a int element
x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
[0.1, 0.2, 0.6, 0.7]]) ... [0.1, 0.2, 0.6, 0.7]])
out1 = paddle.prod(x) >>> out1 = paddle.prod(x)
# 0.0002268 >>> out1
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
out2 = paddle.prod(x, -1) 0.00022680)
# [0.027 0.0084]
>>> out2 = paddle.prod(x, -1)
out3 = paddle.prod(x, 0) >>> out2
# [0.02 0.06 0.3 0.63] Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.02700000, 0.00840000])
out4 = paddle.prod(x, 0, keepdim=True)
# [[0.02 0.06 0.3 0.63]] >>> out3 = paddle.prod(x, 0)
>>> out3
out5 = paddle.prod(x, 0, dtype='int64') Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0 0 0 0] [0.02000000, 0.06000000, 0.30000001, 0.63000000])
# the axis is list >>> out4 = paddle.prod(x, 0, keepdim=True)
y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]], >>> out4
[[5.0, 6.0], [7.0, 8.0]]]) Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
out6 = paddle.prod(y, [0, 1]) [[0.02000000, 0.06000000, 0.30000001, 0.63000000]])
# [105. 384.]
>>> out5 = paddle.prod(x, 0, dtype='int64')
out7 = paddle.prod(y, (1, 2)) >>> out5
# [ 24. 1680.] Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 0, 0, 0])
>>> # the axis is list
>>> y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
... [[5.0, 6.0], [7.0, 8.0]]])
>>> out6 = paddle.prod(y, [0, 1])
>>> out6
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[105., 384.])
>>> out7 = paddle.prod(y, (1, 2))
>>> out7
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[24. , 1680.])
""" """
if dtype is not None: if dtype is not None:
...@@ -4060,11 +4441,13 @@ def sign(x, name=None): ...@@ -4060,11 +4441,13 @@ def sign(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([3.0, 0.0, -2.0, 1.7], dtype='float32') >>> x = paddle.to_tensor([3.0, 0.0, -2.0, 1.7], dtype='float32')
out = paddle.sign(x=x) >>> out = paddle.sign(x=x)
print(out) # [1.0, 0.0, -1.0, 1.0] >>> out
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 1., 0., -1., 1.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.sign(x) return _C_ops.sign(x)
...@@ -4098,12 +4481,13 @@ def tanh(x, name=None): ...@@ -4098,12 +4481,13 @@ def tanh(x, name=None):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = paddle.tanh(x) >>> out = paddle.tanh(x)
print(out) >>> out
# [-0.37994896 -0.19737532 0.09966799 0.29131261] Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[-0.37994900, -0.19737528, 0.09966799, 0.29131261])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.tanh(x) return _C_ops.tanh(x)
...@@ -4143,11 +4527,13 @@ def increment(x, value=1.0, name=None): ...@@ -4143,11 +4527,13 @@ def increment(x, value=1.0, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.zeros(shape=[1], dtype='float32') >>> data = paddle.zeros(shape=[1], dtype='float32')
counter = paddle.increment(data) >>> counter = paddle.increment(data)
# [1.] >>> counter
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4189,30 +4575,42 @@ def all(x, axis=None, keepdim=False, name=None): ...@@ -4189,30 +4575,42 @@ def all(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# x is a bool Tensor with following elements: >>> # x is a bool Tensor with following elements:
# [[True, False] >>> # [[True, False]
# [True, True]] >>> # [True, True]]
x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32') >>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32')
print(x) >>> x
x = paddle.cast(x, 'bool') Tensor(shape=[2, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[1, 0],
# out1 should be False [1, 1]])
out1 = paddle.all(x) # False >>> x = paddle.cast(x, 'bool')
print(out1)
>>> # out1 should be False
# out2 should be [True, False] >>> out1 = paddle.all(x)
out2 = paddle.all(x, axis=0) # [True, False] >>> out1
print(out2) Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
False)
# keepdim=False, out3 should be [False, True], out.shape should be (2,)
out3 = paddle.all(x, axis=-1) # [False, True] >>> # out2 should be [True, False]
print(out3) >>> out2 = paddle.all(x, axis=0)
>>> out2
# keepdim=True, out4 should be [[False], [True]], out.shape should be (2,1) Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
out4 = paddle.all(x, axis=1, keepdim=True) # [[False], [True]] [True , False])
print(out4)
>>> # keepdim=False, out3 should be [False, True], out.shape should be (2,)
>>> out3 = paddle.all(x, axis=-1)
>>> out3
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, True ])
>>> # keepdim=True, out4 should be [[False], [True]], out.shape should be (2, 1)
>>> out4 = paddle.all(x, axis=1, keepdim=True)
>>> out4
Tensor(shape=[2, 1], dtype=bool, place=Place(cpu), stop_gradient=True,
[[False],
[True ]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4263,31 +4661,43 @@ def any(x, axis=None, keepdim=False, name=None): ...@@ -4263,31 +4661,43 @@ def any(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32') >>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32')
x = paddle.assign(x) >>> x = paddle.assign(x)
print(x) >>> x
x = paddle.cast(x, 'bool') Tensor(shape=[2, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
# x is a bool Tensor with following elements: [[1, 0],
# [[True, False] [1, 1]])
# [True, True]] >>> x = paddle.cast(x, 'bool')
>>> # x is a bool Tensor with following elements:
# out1 should be True >>> # [[True, False]
out1 = paddle.any(x) # True >>> # [True, True]]
print(out1)
>>> # out1 should be True
# out2 should be [True, True] >>> out1 = paddle.any(x)
out2 = paddle.any(x, axis=0) # [True, True] >>> out1
print(out2) Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
True)
# keepdim=False, out3 should be [True, True], out.shape should be (2,)
out3 = paddle.any(x, axis=-1) # [True, True] >>> # out2 should be [True, True]
print(out3) >>> out2 = paddle.any(x, axis=0)
>>> out2
# keepdim=True, result should be [[True], [True]], out.shape should be (2,1) Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
out4 = paddle.any(x, axis=1, keepdim=True) # [[True], [True]] [True, True])
print(out4)
>>> # keepdim=False, out3 should be [True, True], out.shape should be (2,)
>>> out3 = paddle.any(x, axis=-1)
>>> out3
Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True,
[True, True])
>>> # keepdim=True, result should be [[True], [True]], out.shape should be (2,1)
>>> out4 = paddle.any(x, axis=1, keepdim=True)
>>> out4
Tensor(shape=[2, 1], dtype=bool, place=Place(cpu), stop_gradient=True,
[[True],
[True]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4335,13 +4745,14 @@ def broadcast_shape(x_shape, y_shape): ...@@ -4335,13 +4745,14 @@ def broadcast_shape(x_shape, y_shape):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
shape = paddle.broadcast_shape([2, 1, 3], [1, 3, 1]) >>> shape = paddle.broadcast_shape([2, 1, 3], [1, 3, 1])
# [2, 3, 3] >>> shape
[2, 3, 3]
# shape = paddle.broadcast_shape([2, 1, 3], [3, 3, 1]) >>> # shape = paddle.broadcast_shape([2, 1, 3], [3, 3, 1])
# ValueError (terminated with error message). >>> # ValueError (terminated with error message).
""" """
...@@ -4363,17 +4774,19 @@ def conj(x, name=None): ...@@ -4363,17 +4774,19 @@ def conj(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data=paddle.to_tensor([[1+1j, 2+2j, 3+3j], [4+4j, 5+5j, 6+6j]]) >>> data = paddle.to_tensor([[1+1j, 2+2j, 3+3j], [4+4j, 5+5j, 6+6j]])
#Tensor(shape=[2, 3], dtype=complex64, place=CUDAPlace(0), stop_gradient=True, >>> data
# [[(1+1j), (2+2j), (3+3j)], Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [(4+4j), (5+5j), (6+6j)]]) [[(1+1j), (2+2j), (3+3j)],
[(4+4j), (5+5j), (6+6j)]])
conj_data=paddle.conj(data) >>> conj_data = paddle.conj(data)
#Tensor(shape=[2, 3], dtype=complex64, place=CUDAPlace(0), stop_gradient=True, >>> conj_data
# [[(1-1j), (2-2j), (3-3j)], Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [(4-4j), (5-5j), (6-6j)]]) [[(1-1j), (2-2j), (3-3j)],
[(4-4j), (5-5j), (6-6j)]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4420,14 +4833,14 @@ def digamma(x, name=None): ...@@ -4420,14 +4833,14 @@ def digamma(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.to_tensor([[1, 1.5], [0, -2.2]], dtype='float32') >>> data = paddle.to_tensor([[1, 1.5], [0, -2.2]], dtype='float32')
res = paddle.digamma(data) >>> res = paddle.digamma(data)
print(res) >>> res
# Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[-0.57721591, 0.03648996], [[-0.57721591, 0.03648996],
# [ nan , 5.32286835]]) [ nan , 5.32286835]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4470,12 +4883,13 @@ def lgamma(x, name=None): ...@@ -4470,12 +4883,13 @@ def lgamma(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = paddle.lgamma(x) >>> out = paddle.lgamma(x)
print(out) >>> out
# [1.31452441, 1.76149750, 2.25271273, 1.09579802] Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.31452453, 1.76149762, 2.25271273, 1.09579790])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.lgamma(x) return _C_ops.lgamma(x)
...@@ -4513,12 +4927,13 @@ def neg(x, name=None): ...@@ -4513,12 +4927,13 @@ def neg(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = paddle.neg(x) >>> out = paddle.neg(x)
print(out) >>> out
# [0.4 0.2 -0.1 -0.3] Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 0.40000001, 0.20000000, -0.10000000, -0.30000001])
""" """
return scale( return scale(
...@@ -4564,19 +4979,22 @@ def atan2(x, y, name=None): ...@@ -4564,19 +4979,22 @@ def atan2(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-1, +1, +1, -1]).astype('float32') >>> x = paddle.to_tensor([-1, +1, +1, -1]).astype('float32')
#Tensor(shape=[4], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> x
# [-1, 1, 1, -1]) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1, 1, 1, -1])
y = paddle.to_tensor([-1, -1, +1, +1]).astype('float32') >>> y = paddle.to_tensor([-1, -1, +1, +1]).astype('float32')
#Tensor(shape=[4], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> y
# [-1, -1, 1, 1]) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1, -1, 1, 1])
out = paddle.atan2(x, y) >>> out = paddle.atan2(x, y)
#Tensor(shape=[4], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> out
# [-2.35619450, 2.35619450, 0.78539819, -0.78539819]) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[-2.35619450, 2.35619450, 0.78539819, -0.78539819])
""" """
...@@ -4635,12 +5053,13 @@ def logit(x, eps=None, name=None): ...@@ -4635,12 +5053,13 @@ def logit(x, eps=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([0.2635, 0.0106, 0.2780, 0.2097, 0.8095]) >>> x = paddle.to_tensor([0.2635, 0.0106, 0.2780, 0.2097, 0.8095])
out1 = paddle.logit(x) >>> out1 = paddle.logit(x)
print(out1) >>> out1
# [-1.0277, -4.5365, -0.9544, -1.3269, 1.4468] Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[-1.02785587, -4.53624487, -0.95440406, -1.32673466, 1.44676447])
""" """
if eps is None: if eps is None:
...@@ -4695,13 +5114,15 @@ def lerp(x, y, weight, name=None): ...@@ -4695,13 +5114,15 @@ def lerp(x, y, weight, name=None):
Example: Example:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(1., 5., dtype='float32') >>> x = paddle.arange(1., 5., dtype='float32')
y = paddle.empty([4], dtype='float32') >>> y = paddle.empty([4], dtype='float32')
y.fill_(10.) >>> y.fill_(10.)
out = paddle.lerp(x, y, 0.5) >>> out = paddle.lerp(x, y, 0.5)
# out: [5.5, 6., 6.5, 7.] >>> out
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[5.50000000, 6. , 6.50000000, 7. ])
""" """
if isinstance(weight, float): if isinstance(weight, float):
...@@ -4769,11 +5190,13 @@ def erfinv(x, name=None): ...@@ -4769,11 +5190,13 @@ def erfinv(x, name=None):
Example: Example:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([0, 0.5, -1.], dtype="float32") >>> x = paddle.to_tensor([0, 0.5, -1.], dtype="float32")
out = paddle.erfinv(x) >>> out = paddle.erfinv(x)
# out: [0, 0.4769, -inf] >>> out
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 0. , 0.47693631, -inf. ])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4817,27 +5240,27 @@ def rad2deg(x, name=None): ...@@ -4817,27 +5240,27 @@ def rad2deg(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import math >>> import math
x1 = paddle.to_tensor([3.142, -3.142, 6.283, -6.283, 1.570, -1.570]) >>> x1 = paddle.to_tensor([3.142, -3.142, 6.283, -6.283, 1.570, -1.570])
result1 = paddle.rad2deg(x1) >>> result1 = paddle.rad2deg(x1)
print(result1) >>> result1
# Tensor(shape=[6], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
# [180.02334595, -180.02334595, 359.98937988, -359.98937988, [ 180.02334595, -180.02334595, 359.98937988, -359.98937988,
# 9.95437622 , -89.95437622]) 89.95437622 , -89.95437622 ])
x2 = paddle.to_tensor(math.pi/2) >>> x2 = paddle.to_tensor(math.pi/2)
result2 = paddle.rad2deg(x2) >>> result2 = paddle.rad2deg(x2)
print(result2) >>> result2
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 90.) 90.)
x3 = paddle.to_tensor(1) >>> x3 = paddle.to_tensor(1)
result3 = paddle.rad2deg(x3) >>> result3 = paddle.rad2deg(x3)
print(result3) >>> result3
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 57.29578018) 57.29578018)
""" """
rad2deg_scale = 180 / np.pi rad2deg_scale = 180 / np.pi
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4888,20 +5311,20 @@ def deg2rad(x, name=None): ...@@ -4888,20 +5311,20 @@ def deg2rad(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x1 = paddle.to_tensor([180.0, -180.0, 360.0, -360.0, 90.0, -90.0]) >>> x1 = paddle.to_tensor([180.0, -180.0, 360.0, -360.0, 90.0, -90.0])
result1 = paddle.deg2rad(x1) >>> result1 = paddle.deg2rad(x1)
print(result1) >>> result1
# Tensor(shape=[6], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
# [3.14159274, -3.14159274, 6.28318548, -6.28318548, 1.57079637, [3.14159274, -3.14159274, 6.28318548, -6.28318548, 1.57079637,
# -1.57079637]) -1.57079637])
x2 = paddle.to_tensor(180) >>> x2 = paddle.to_tensor(180)
result2 = paddle.deg2rad(x2) >>> result2 = paddle.deg2rad(x2)
print(result2) >>> result2
# Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 3.14159274) 3.14159274)
""" """
deg2rad_scale = np.pi / 180.0 deg2rad_scale = np.pi / 180.0
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -4955,32 +5378,32 @@ def gcd(x, y, name=None): ...@@ -4955,32 +5378,32 @@ def gcd(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x1 = paddle.to_tensor(12) >>> x1 = paddle.to_tensor(12)
x2 = paddle.to_tensor(20) >>> x2 = paddle.to_tensor(20)
paddle.gcd(x1, x2) >>> paddle.gcd(x1, x2)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 4) 4)
x3 = paddle.arange(6) >>> x3 = paddle.arange(6)
paddle.gcd(x3, x2) >>> paddle.gcd(x3, x2)
# Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
# [20, 1 , 2 , 1 , 4 , 5]) [20, 1 , 2 , 1 , 4 , 5])
x4 = paddle.to_tensor(0) >>> x4 = paddle.to_tensor(0)
paddle.gcd(x4, x2) >>> paddle.gcd(x4, x2)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 20) 20)
paddle.gcd(x4, x4) >>> paddle.gcd(x4, x4)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 0) 0)
x5 = paddle.to_tensor(-20) >>> x5 = paddle.to_tensor(-20)
paddle.gcd(x1, x5) >>> paddle.gcd(x1, x5)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 4) 4)
""" """
shape = paddle.broadcast_shape(x.shape, y.shape) shape = paddle.broadcast_shape(x.shape, y.shape)
x = paddle.broadcast_to(x, shape) x = paddle.broadcast_to(x, shape)
...@@ -5040,32 +5463,32 @@ def lcm(x, y, name=None): ...@@ -5040,32 +5463,32 @@ def lcm(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x1 = paddle.to_tensor(12) >>> x1 = paddle.to_tensor(12)
x2 = paddle.to_tensor(20) >>> x2 = paddle.to_tensor(20)
paddle.lcm(x1, x2) >>> paddle.lcm(x1, x2)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 60) 60)
x3 = paddle.arange(6) >>> x3 = paddle.arange(6)
paddle.lcm(x3, x2) >>> paddle.lcm(x3, x2)
# Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
# [0, 20, 20, 60, 20, 20]) [0, 20, 20, 60, 20, 20])
x4 = paddle.to_tensor(0) >>> x4 = paddle.to_tensor(0)
paddle.lcm(x4, x2) >>> paddle.lcm(x4, x2)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 0) 0)
paddle.lcm(x4, x4) >>> paddle.lcm(x4, x4)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 0) 0)
x5 = paddle.to_tensor(-20) >>> x5 = paddle.to_tensor(-20)
paddle.lcm(x1, x5) >>> paddle.lcm(x1, x5)
# Tensor(shape=[], dtype=int64, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 60) 60)
""" """
d = paddle.gcd(x, y) d = paddle.gcd(x, y)
# paddle.mod will raise an error when any element of y is 0. To avoid # paddle.mod will raise an error when any element of y is 0. To avoid
...@@ -5110,29 +5533,30 @@ def diff(x, n=1, axis=-1, prepend=None, append=None, name=None): ...@@ -5110,29 +5533,30 @@ def diff(x, n=1, axis=-1, prepend=None, append=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1, 4, 5, 2]) >>> x = paddle.to_tensor([1, 4, 5, 2])
out = paddle.diff(x) >>> out = paddle.diff(x)
print(out) >>> out
# out: Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [3, 1, -3] [ 3, 1, -3])
y = paddle.to_tensor([7, 9]) >>> y = paddle.to_tensor([7, 9])
out = paddle.diff(x, append=y) >>> out = paddle.diff(x, append=y)
print(out) >>> out
# out: Tensor(shape=[5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [3, 1, -3, 5, 2] [ 3, 1, -3, 5, 2])
z = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) >>> z = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
out = paddle.diff(z, axis=0) >>> out = paddle.diff(z, axis=0)
print(out) >>> out
# out: Tensor(shape=[1, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[3, 3, 3]] [[3, 3, 3]])
out = paddle.diff(z, axis=1) >>> out = paddle.diff(z, axis=1)
print(out) >>> out
# out: Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 1], [1, 1]] [[1, 1],
[1, 1]])
""" """
if axis < 0: if axis < 0:
...@@ -5275,25 +5699,25 @@ def angle(x, name=None): ...@@ -5275,25 +5699,25 @@ def angle(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-2, -1, 0, 1]).unsqueeze(-1).astype('float32') >>> x = paddle.to_tensor([-2, -1, 0, 1]).unsqueeze(-1).astype('float32')
y = paddle.to_tensor([-2, -1, 0, 1]).astype('float32') >>> y = paddle.to_tensor([-2, -1, 0, 1]).astype('float32')
z = x + 1j * y >>> z = x + 1j * y
print(z) >>> z
# Tensor(shape=[4, 4], dtype=complex64, place=Place(cpu), stop_gradient=True, Tensor(shape=[4, 4], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [[(-2-2j), (-2-1j), (-2+0j), (-2+1j)], [[(-2-2j), (-2-1j), (-2+0j), (-2+1j)],
# [(-1-2j), (-1-1j), (-1+0j), (-1+1j)], [(-1-2j), (-1-1j), (-1+0j), (-1+1j)],
# [-2j , -1j , 0j , 1j ], [-2j , -1j , 0j , 1j ],
# [ (1-2j), (1-1j), (1+0j), (1+1j)]]) [ (1-2j), (1-1j), (1+0j), (1+1j)]])
theta = paddle.angle(z) >>> theta = paddle.angle(z)
print(theta) >>> theta
# Tensor(shape=[4, 4], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[-2.35619450, -2.67794514, 3.14159274, 2.67794514], [[-2.35619450, -2.67794514, 3.14159274, 2.67794514],
# [-2.03444386, -2.35619450, 3.14159274, 2.35619450], [-2.03444386, -2.35619450, 3.14159274, 2.35619450],
# [-1.57079637, -1.57079637, 0. , 1.57079637], [-1.57079637, -1.57079637, 0. , 1.57079637],
# [-1.10714877, -0.78539819, 0. , 0.78539819]]) [-1.10714877, -0.78539819, 0. , 0.78539819]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -5353,16 +5777,18 @@ def heaviside(x, y, name=None): ...@@ -5353,16 +5777,18 @@ def heaviside(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([-0.5, 0, 0.5]) >>> x = paddle.to_tensor([-0.5, 0, 0.5])
y = paddle.to_tensor([0.1]) >>> y = paddle.to_tensor([0.1])
paddle.heaviside(x, y) >>> paddle.heaviside(x, y)
# [0. , 0.10000000, 1. ] Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
x = paddle.to_tensor([[-0.5, 0, 0.5], [-0.5, 0.5, 0]]) [0. , 0.10000000, 1. ])
y = paddle.to_tensor([0.1, 0.2, 0.3]) >>> x = paddle.to_tensor([[-0.5, 0, 0.5], [-0.5, 0.5, 0]])
paddle.heaviside(x, y) >>> y = paddle.to_tensor([0.1, 0.2, 0.3])
# [[0. , 0.20000000, 1. ], >>> paddle.heaviside(x, y)
# [0. , 1. , 0.30000001]] Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0. , 0.20000000, 1. ],
[0. , 1. , 0.30000001]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.heaviside(x, y) return _C_ops.heaviside(x, y)
...@@ -5385,15 +5811,15 @@ def frac(x, name=None): ...@@ -5385,15 +5811,15 @@ def frac(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.to_tensor([[12.22000003, -1.02999997], >>> input = paddle.to_tensor([[12.22000003, -1.02999997],
[-0.54999995, 0.66000003]]) ... [-0.54999995, 0.66000003]])
output = paddle.frac(input) >>> output = paddle.frac(input)
print(output) >>> output
# Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[ 0.22000003, -0.02999997], [[ 0.22000003, -0.02999997],
# [-0.54999995, 0.66000003]]) [-0.54999995, 0.66000003]])
""" """
if x.dtype not in [ if x.dtype not in [
paddle.int32, paddle.int32,
...@@ -5462,14 +5888,21 @@ def sgn(x, name=None): ...@@ -5462,14 +5888,21 @@ def sgn(x, name=None):
Tensor: A sign Tensor for real input, or normalized Tensor for complex input, shape and data type are same as input. Tensor: A sign Tensor for real input, or normalized Tensor for complex input, shape and data type are same as input.
Examples: Examples:
.. code-block:: Python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[3 + 4j, 7 - 24j, 0, 1 + 2j], [6 + 8j, 3, 0, -2]]) >>> x = paddle.to_tensor([[3 + 4j, 7 - 24j, 0, 1 + 2j], [6 + 8j, 3, 0, -2]])
print(paddle.sgn(x)) >>> paddle.sgn(x)
#[[0.6+0.8j 0.28-0.96j 0.+0.j 0.4472136+0.8944272j] Tensor(shape=[2, 4], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [0.6+0.8j 1.+0.j 0.+0.j -1.+0.j]] [[ (0.6000000238418579+0.800000011920929j),
(0.2800000011920929-0.9599999785423279j),
0j ,
(0.4472135901451111+0.8944271802902222j)],
[ (0.6000000238418579+0.800000011920929j),
(1+0j) ,
0j ,
(-1+0j) ]])
""" """
if x.dtype not in [ if x.dtype not in [
...@@ -5520,46 +5953,46 @@ def take(x, index, mode='raise', name=None): ...@@ -5520,46 +5953,46 @@ def take(x, index, mode='raise', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x_int = paddle.arange(0, 12).reshape([3, 4]) >>> x_int = paddle.arange(0, 12).reshape([3, 4])
x_float = x_int.astype(paddle.float64) >>> x_float = x_int.astype(paddle.float64)
idx_pos = paddle.arange(4, 10).reshape([2, 3]) # positive index >>> idx_pos = paddle.arange(4, 10).reshape([2, 3]) # positive index
idx_neg = paddle.arange(-2, 4).reshape([2, 3]) # negative index >>> idx_neg = paddle.arange(-2, 4).reshape([2, 3]) # negative index
idx_err = paddle.arange(-2, 13).reshape([3, 5]) # index out of range >>> idx_err = paddle.arange(-2, 13).reshape([3, 5]) # index out of range
paddle.take(x_int, idx_pos) >>> paddle.take(x_int, idx_pos)
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[4, 5, 6], [[4, 5, 6],
# [7, 8, 9]]) [7, 8, 9]])
paddle.take(x_int, idx_neg) >>> paddle.take(x_int, idx_neg)
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[10, 11, 0 ], [[10, 11, 0 ],
# [1 , 2 , 3 ]]) [1 , 2 , 3 ]])
paddle.take(x_float, idx_pos) >>> paddle.take(x_float, idx_pos)
# Tensor(shape=[2, 3], dtype=float64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[4., 5., 6.], [[4., 5., 6.],
# [7., 8., 9.]]) [7., 8., 9.]])
x_int.take(idx_pos) >>> x_int.take(idx_pos)
# Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[4, 5, 6], [[4, 5, 6],
# [7, 8, 9]]) [7, 8, 9]])
paddle.take(x_int, idx_err, mode='wrap') >>> paddle.take(x_int, idx_err, mode='wrap')
# Tensor(shape=[3, 5], dtype=int32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[10, 11, 0 , 1 , 2 ], [[10, 11, 0 , 1 , 2 ],
# [3 , 4 , 5 , 6 , 7 ], [3 , 4 , 5 , 6 , 7 ],
# [8 , 9 , 10, 11, 0 ]]) [8 , 9 , 10, 11, 0 ]])
paddle.take(x_int, idx_err, mode='clip') >>> paddle.take(x_int, idx_err, mode='clip')
# Tensor(shape=[3, 5], dtype=int32, place=Place(cpu), stop_gradient=True, Tensor(shape=[3, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0 , 0 , 0 , 1 , 2 ], [[0 , 0 , 0 , 1 , 2 ],
# [3 , 4 , 5 , 6 , 7 ], [3 , 4 , 5 , 6 , 7 ],
# [8 , 9 , 10, 11, 11]]) [8 , 9 , 10, 11, 11]])
""" """
if mode not in ['raise', 'wrap', 'clip']: if mode not in ['raise', 'wrap', 'clip']:
...@@ -5615,6 +6048,7 @@ def frexp(x, name=None): ...@@ -5615,6 +6048,7 @@ def frexp(x, name=None):
Args: Args:
x (Tensor): The input tensor, it's data type should be float32, float64. x (Tensor): The input tensor, it's data type should be float32, float64.
name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
Returns: Returns:
- mantissa (Tensor), A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are - mantissa (Tensor), A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are
...@@ -5626,12 +6060,16 @@ def frexp(x, name=None): ...@@ -5626,12 +6060,16 @@ def frexp(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2, 3, 4]], dtype="float32") >>> x = paddle.to_tensor([[1, 2, 3, 4]], dtype="float32")
print(paddle.tensor.math.frexp(x)) >>> mantissa, exponent = paddle.tensor.math.frexp(x)
# (Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[0.50000000, 0.50000000, 0.75000000, 0.50000000]]), >>> mantissa
# Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[1., 2., 2., 3.]])) Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.50000000, 0.50000000, 0.75000000, 0.50000000]])
>>> exponent
Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 2., 2., 3.]])
""" """
if x.dtype not in [paddle.float32, paddle.float64]: if x.dtype not in [paddle.float32, paddle.float64]:
raise TypeError( raise TypeError(
...@@ -5749,40 +6187,39 @@ def trapezoid(y, x=None, dx=None, axis=-1, name=None): ...@@ -5749,40 +6187,39 @@ def trapezoid(y, x=None, dx=None, axis=-1, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
y = paddle.to_tensor([4, 5, 6], dtype='float32') >>> y = paddle.to_tensor([4, 5, 6], dtype='float32')
print(paddle.trapezoid(y)) >>> paddle.trapezoid(y)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 10.) 10.)
print(paddle.trapezoid(y, dx=2.)) >>> paddle.trapezoid(y, dx=2.)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 20.) 20.)
y = paddle.to_tensor([4, 5, 6], dtype='float32') >>> y = paddle.to_tensor([4, 5, 6], dtype='float32')
x = paddle.to_tensor([1, 2, 3], dtype='float32') >>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
print(paddle.trapezoid(y, x)) >>> paddle.trapezoid(y, x)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
# 10.) 10.)
>>> y = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> x = paddle.to_tensor([8, 6, 4], dtype='float64')
y = paddle.to_tensor([1, 2, 3], dtype='float64') >>> paddle.trapezoid(y, x)
x = paddle.to_tensor([8, 6, 4], dtype='float64') Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
-8.)
>>> y = paddle.arange(6).reshape((2, 3)).astype('float32')
print(paddle.trapezoid(y, x)) >>> paddle.trapezoid(y, axis=0)
# Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# -8.) [1.50000000, 2.50000000, 3.50000000])
y = paddle.arange(6).reshape((2, 3)).astype('float32') >>> paddle.trapezoid(y, axis=1)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
print(paddle.trapezoid(y, axis=0)) [2., 8.])
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.50000000, 2.50000000, 3.50000000])
print(paddle.trapezoid(y, axis=1))
# Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2., 8.])
""" """
return _trapezoid(y, x, dx, axis, mode='sum') return _trapezoid(y, x, dx, axis, mode='sum')
...@@ -5807,41 +6244,41 @@ def cumulative_trapezoid(y, x=None, dx=None, axis=-1, name=None): ...@@ -5807,41 +6244,41 @@ def cumulative_trapezoid(y, x=None, dx=None, axis=-1, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
y = paddle.to_tensor([4, 5, 6], dtype='float32') >>> y = paddle.to_tensor([4, 5, 6], dtype='float32')
print(paddle.cumulative_trapezoid(y)) >>> paddle.cumulative_trapezoid(y)
# Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [4.50000000, 10. ]) [4.50000000, 10. ])
print(paddle.cumulative_trapezoid(y, dx=2.)) >>> paddle.cumulative_trapezoid(y, dx=2.)
# Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, >>> # Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [9. , 20.]) >>> # [9. , 20.])
y = paddle.to_tensor([4, 5, 6], dtype='float32') >>> y = paddle.to_tensor([4, 5, 6], dtype='float32')
x = paddle.to_tensor([1, 2, 3], dtype='float32') >>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
print(paddle.cumulative_trapezoid(y, x)) >>> paddle.cumulative_trapezoid(y, x)
# Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [4.50000000, 10. ]) [4.50000000, 10. ])
y = paddle.to_tensor([1, 2, 3], dtype='float64') >>> y = paddle.to_tensor([1, 2, 3], dtype='float64')
x = paddle.to_tensor([8, 6, 4], dtype='float64') >>> x = paddle.to_tensor([8, 6, 4], dtype='float64')
print(paddle.cumulative_trapezoid(y, x)) >>> paddle.cumulative_trapezoid(y, x)
# Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
# [-3., -8.]) [-3., -8.])
y = paddle.arange(6).reshape((2, 3)).astype('float32') >>> y = paddle.arange(6).reshape((2, 3)).astype('float32')
print(paddle.cumulative_trapezoid(y, axis=0)) >>> paddle.cumulative_trapezoid(y, axis=0)
# Tensor(shape=[1, 3], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[1, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[1.50000000, 2.50000000, 3.50000000]]) [[1.50000000, 2.50000000, 3.50000000]])
print(paddle.cumulative_trapezoid(y, axis=1)) >>> paddle.cumulative_trapezoid(y, axis=1)
# Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.50000000, 2. ], [[0.50000000, 2. ],
# [3.50000000, 8. ]]) [3.50000000, 8. ]])
""" """
return _trapezoid(y, x, dx, axis, mode='cumsum') return _trapezoid(y, x, dx, axis, mode='cumsum')
...@@ -5868,30 +6305,34 @@ def vander(x, n=None, increasing=False, name=None): ...@@ -5868,30 +6305,34 @@ def vander(x, n=None, increasing=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1., 2., 3.], dtype="float32") >>> x = paddle.to_tensor([1., 2., 3.], dtype="float32")
out = paddle.vander(x) >>> out = paddle.vander(x)
print(out.numpy()) >>> out
# [[1., 1., 1.], Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [4., 2., 1.], [[1., 1., 1.],
# [9., 3., 1.]] [4., 2., 1.],
out1 = paddle.vander(x,2) [9., 3., 1.]])
print(out1.numpy()) >>> out1 = paddle.vander(x,2)
# [[1., 1.], >>> out1
# [2., 1.], Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [3., 1.]] [[1., 1.],
out2 = paddle.vander(x, increasing = True) [2., 1.],
print(out2.numpy()) [3., 1.]])
# [[1., 1., 1.], >>> out2 = paddle.vander(x, increasing = True)
# [1., 2., 4.], >>> out2
# [1., 3., 9.]] Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
real = paddle.to_tensor([2., 4.]) [[1., 1., 1.],
imag = paddle.to_tensor([1., 3.]) [1., 2., 4.],
complex = paddle.complex(real, imag) [1., 3., 9.]])
out3 = paddle.vander(complex) >>> real = paddle.to_tensor([2., 4.])
print(out3.numpy()) >>> imag = paddle.to_tensor([1., 3.])
# [[2.+1.j, 1.+0.j], >>> complex = paddle.complex(real, imag)
# [4.+3.j, 1.+0.j]] >>> out3 = paddle.vander(complex)
>>> out3
Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True,
[[(2+1j), (1+0j)],
[(4+3j), (1+0j)]])
""" """
check_variable_and_dtype( check_variable_and_dtype(
x, x,
...@@ -5953,11 +6394,11 @@ def nextafter(x, y, name=None): ...@@ -5953,11 +6394,11 @@ def nextafter(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
out = paddle.nextafter(paddle.to_tensor([1.0,2.0]),paddle.to_tensor([2.0,1.0])) >>> out = paddle.nextafter(paddle.to_tensor([1.0,2.0]),paddle.to_tensor([2.0,1.0]))
print(out) >>> out
#Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.00000012, 1.99999988]) [1.00000012, 1.99999988])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.nextafter(x, y) return _C_ops.nextafter(x, y)
...@@ -5992,11 +6433,12 @@ def i0(x, name=None): ...@@ -5992,11 +6433,12 @@ def i0(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32") >>> x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32")
print(paddle.i0(x)) >>> paddle.i0(x)
# (Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True, [0.99999994 , 1.26606596 , 2.27958512 , 4.88079262 , 11.30192089]), Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.99999994 , 1.26606596 , 2.27958512 , 4.88079262 , 11.30192089])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.i0(x) return _C_ops.i0(x)
...@@ -6040,11 +6482,12 @@ def i0e(x, name=None): ...@@ -6040,11 +6482,12 @@ def i0e(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32") >>> x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32")
print(paddle.i0e(x)) >>> print(paddle.i0e(x))
# (Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True, [1., 0.46575961, 0.30850832, 0.24300035, 0.20700192]), Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.99999994, 0.46575963, 0.30850831, 0.24300036, 0.20700191])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.i0e(x) return _C_ops.i0e(x)
...@@ -6071,11 +6514,12 @@ def i1(x, name=None): ...@@ -6071,11 +6514,12 @@ def i1(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32") >>> x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32")
print(paddle.i1(x)) >>> print(paddle.i1(x))
# (Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True, [0., 0.5651591 , 1.59063685 , 3.95337022 , 9.75946515]), Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[0. , 0.56515908, 1.59063685, 3.95337057, 9.75946712])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.i1(x) return _C_ops.i1(x)
...@@ -6105,11 +6549,12 @@ def i1e(x, name=None): ...@@ -6105,11 +6549,12 @@ def i1e(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32") >>> x = paddle.to_tensor([0, 1, 2, 3, 4], dtype="float32")
print(paddle.i1e(x)) >>> print(paddle.i1e(x))
# (Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True, [0., 0.20791042, 0.21526929, 0.24300035, 0.17875084]), Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[0. , 0.20791042, 0.21526928, 0.19682673, 0.17875087])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.i1e(x) return _C_ops.i1e(x)
...@@ -6144,13 +6589,13 @@ def polygamma(x, n, name=None): ...@@ -6144,13 +6589,13 @@ def polygamma(x, n, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.to_tensor([2, 3, 25.5], dtype='float32') >>> data = paddle.to_tensor([2, 3, 25.5], dtype='float32')
res = paddle.polygamma(data, 1) >>> res = paddle.polygamma(data, 1)
print(res) >>> print(res)
# Tensor(shape=[2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [0.64493407, 0.39493407, 0.03999467]) [0.64493412, 0.39493406, 0.03999467])
""" """
if not isinstance(n, int): if not isinstance(n, int):
raise TypeError( raise TypeError(
...@@ -6220,25 +6665,25 @@ def ldexp(x, y, name=None): ...@@ -6220,25 +6665,25 @@ def ldexp(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
#example1 >>> # example1
x = paddle.to_tensor([1, 2, 3], dtype='float32') >>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
y = paddle.to_tensor([2, 3, 4], dtype='int32') >>> y = paddle.to_tensor([2, 3, 4], dtype='int32')
res = paddle.ldexp(x, y) >>> res = paddle.ldexp(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [4., 16., 48.]) [4. , 16., 48.])
#example2 >>> # example2
x = paddle.to_tensor([1, 2, 3], dtype='float32') >>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
y = paddle.to_tensor([2], dtype='int32') >>> y = paddle.to_tensor([2], dtype='int32')
res = paddle.ldexp(x, y) >>> res = paddle.ldexp(x, y)
print(res) >>> print(res)
# Tensor(shape=[3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [4., 8., 12.]) [4. , 8. , 12.])
""" """
if not isinstance(x, (paddle.Tensor, Variable)): if not isinstance(x, (paddle.Tensor, Variable)):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册