未验证 提交 352ac149 编写于 作者: Z Zhou Wei 提交者: GitHub

update doc of paddle.to_tensor (#26820)

update doc of paddle.to_tensor
上级 72f6e566
...@@ -12175,13 +12175,10 @@ def logical_and(x, y, out=None, name=None): ...@@ -12175,13 +12175,10 @@ def logical_and(x, y, out=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_data = np.array([True], dtype=np.bool) x = paddle.to_tensor([True])
y_data = np.array([True, False, True, False], dtype=np.bool) y = paddle.to_tensor([True, False, True, False])
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
res = paddle.logical_and(x, y) res = paddle.logical_and(x, y)
print(res.numpy()) # [True False True False] print(res.numpy()) # [True False True False]
""" """
...@@ -12294,11 +12291,9 @@ def logical_not(x, out=None, name=None): ...@@ -12294,11 +12291,9 @@ def logical_not(x, out=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_data = np.array([True, False, True, False], dtype=np.bool) x = paddle.to_tensor([True, False, True, False])
x = paddle.to_variable(x_data)
res = paddle.logical_not(x) res = paddle.logical_not(x)
print(res.numpy()) # [False True False True] print(res.numpy()) # [False True False True]
""" """
......
...@@ -86,13 +86,11 @@ add_sample_code(globals()["sigmoid"], r""" ...@@ -86,13 +86,11 @@ add_sample_code(globals()["sigmoid"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = F.sigmoid(x) out = F.sigmoid(x)
print(out.numpy()) print(out.numpy())
# [0.40131234 0.450166 0.52497919 0.57444252] # [0.40131234 0.450166 0.52497919 0.57444252]
...@@ -103,13 +101,11 @@ add_sample_code(globals()["logsigmoid"], r""" ...@@ -103,13 +101,11 @@ add_sample_code(globals()["logsigmoid"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = F.logsigmoid(x) out = F.logsigmoid(x)
print(out.numpy()) print(out.numpy())
# [-0.91301525 -0.79813887 -0.64439666 -0.55435524] # [-0.91301525 -0.79813887 -0.64439666 -0.55435524]
...@@ -120,12 +116,10 @@ add_sample_code(globals()["exp"], r""" ...@@ -120,12 +116,10 @@ add_sample_code(globals()["exp"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.exp(x) out = paddle.exp(x)
print(out.numpy()) print(out.numpy())
# [0.67032005 0.81873075 1.10517092 1.34985881] # [0.67032005 0.81873075 1.10517092 1.34985881]
...@@ -136,12 +130,10 @@ add_sample_code(globals()["tanh"], r""" ...@@ -136,12 +130,10 @@ add_sample_code(globals()["tanh"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.tanh(x) out = paddle.tanh(x)
print(out.numpy()) print(out.numpy())
# [-0.37994896 -0.19737532 0.09966799 0.29131261] # [-0.37994896 -0.19737532 0.09966799 0.29131261]
...@@ -152,12 +144,10 @@ add_sample_code(globals()["atan"], r""" ...@@ -152,12 +144,10 @@ add_sample_code(globals()["atan"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.atan(x) out = paddle.atan(x)
print(out.numpy()) print(out.numpy())
# [-0.38050638 -0.19739556 0.09966865 0.29145679] # [-0.38050638 -0.19739556 0.09966865 0.29145679]
...@@ -170,11 +160,10 @@ Examples: ...@@ -170,11 +160,10 @@ Examples:
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
import numpy as np
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3])) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = F.tanhshrink(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739] out = F.tanhshrink(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739]
""") """)
...@@ -183,12 +172,10 @@ add_sample_code(globals()["sqrt"], r""" ...@@ -183,12 +172,10 @@ add_sample_code(globals()["sqrt"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([0.1, 0.2, 0.3, 0.4]) x = paddle.to_tensor([0.1, 0.2, 0.3, 0.4])
x = paddle.to_variable(x_data)
out = paddle.sqrt(x) out = paddle.sqrt(x)
print(out.numpy()) print(out.numpy())
# [0.31622777 0.4472136 0.54772256 0.63245553] # [0.31622777 0.4472136 0.54772256 0.63245553]
...@@ -199,12 +186,10 @@ add_sample_code(globals()["rsqrt"], r""" ...@@ -199,12 +186,10 @@ add_sample_code(globals()["rsqrt"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([0.1, 0.2, 0.3, 0.4]) x = paddle.to_tensor([0.1, 0.2, 0.3, 0.4])
x = paddle.to_variable(x_data)
out = paddle.rsqrt(x) out = paddle.rsqrt(x)
print(out.numpy()) print(out.numpy())
# [3.16227766 2.23606798 1.82574186 1.58113883] # [3.16227766 2.23606798 1.82574186 1.58113883]
...@@ -215,12 +200,10 @@ add_sample_code(globals()["abs"], r""" ...@@ -215,12 +200,10 @@ add_sample_code(globals()["abs"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.abs(x) out = paddle.abs(x)
print(out.numpy()) print(out.numpy())
# [0.4 0.2 0.1 0.3] # [0.4 0.2 0.1 0.3]
...@@ -231,12 +214,10 @@ add_sample_code(globals()["ceil"], r""" ...@@ -231,12 +214,10 @@ add_sample_code(globals()["ceil"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.ceil(x) out = paddle.ceil(x)
print(out.numpy()) print(out.numpy())
# [-0. -0. 1. 1.] # [-0. -0. 1. 1.]
...@@ -247,12 +228,10 @@ add_sample_code(globals()["floor"], r""" ...@@ -247,12 +228,10 @@ add_sample_code(globals()["floor"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.floor(x) out = paddle.floor(x)
print(out.numpy()) print(out.numpy())
# [-1. -1. 0. 0.] # [-1. -1. 0. 0.]
...@@ -263,12 +242,10 @@ add_sample_code(globals()["cos"], r""" ...@@ -263,12 +242,10 @@ add_sample_code(globals()["cos"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.cos(x) out = paddle.cos(x)
print(out.numpy()) print(out.numpy())
# [0.92106099 0.98006658 0.99500417 0.95533649] # [0.92106099 0.98006658 0.99500417 0.95533649]
...@@ -279,12 +256,10 @@ add_sample_code(globals()["acos"], r""" ...@@ -279,12 +256,10 @@ add_sample_code(globals()["acos"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.acos(x) out = paddle.acos(x)
print(out.numpy()) print(out.numpy())
# [1.98231317 1.77215425 1.47062891 1.26610367] # [1.98231317 1.77215425 1.47062891 1.26610367]
...@@ -295,12 +270,10 @@ add_sample_code(globals()["sin"], r""" ...@@ -295,12 +270,10 @@ add_sample_code(globals()["sin"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.sin(x) out = paddle.sin(x)
print(out.numpy()) print(out.numpy())
# [-0.38941834 -0.19866933 0.09983342 0.29552021] # [-0.38941834 -0.19866933 0.09983342 0.29552021]
...@@ -311,12 +284,10 @@ add_sample_code(globals()["asin"], r""" ...@@ -311,12 +284,10 @@ add_sample_code(globals()["asin"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.asin(x) out = paddle.asin(x)
print(out.numpy()) print(out.numpy())
# [-0.41151685 -0.20135792 0.10016742 0.30469265] # [-0.41151685 -0.20135792 0.10016742 0.30469265]
...@@ -327,12 +298,10 @@ add_sample_code(globals()["cosh"], r""" ...@@ -327,12 +298,10 @@ add_sample_code(globals()["cosh"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.cosh(x) out = paddle.cosh(x)
print(out.numpy()) print(out.numpy())
# [1.08107237 1.02006676 1.00500417 1.04533851] # [1.08107237 1.02006676 1.00500417 1.04533851]
...@@ -343,12 +312,10 @@ add_sample_code(globals()["sinh"], r""" ...@@ -343,12 +312,10 @@ add_sample_code(globals()["sinh"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.sinh(x) out = paddle.sinh(x)
print(out.numpy()) print(out.numpy())
# [-0.41075233 -0.201336 0.10016675 0.30452029] # [-0.41075233 -0.201336 0.10016675 0.30452029]
...@@ -359,12 +326,10 @@ add_sample_code(globals()["round"], r""" ...@@ -359,12 +326,10 @@ add_sample_code(globals()["round"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.5, -0.2, 0.6, 1.5]) x = paddle.to_tensor([-0.5, -0.2, 0.6, 1.5])
x = paddle.to_variable(x_data)
out = paddle.round(x) out = paddle.round(x)
print(out.numpy()) print(out.numpy())
# [-1. -0. 1. 2.] # [-1. -0. 1. 2.]
...@@ -375,12 +340,10 @@ add_sample_code(globals()["reciprocal"], r""" ...@@ -375,12 +340,10 @@ add_sample_code(globals()["reciprocal"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.reciprocal(x) out = paddle.reciprocal(x)
print(out.numpy()) print(out.numpy())
# [-2.5 -5. 10. 3.33333333] # [-2.5 -5. 10. 3.33333333]
...@@ -391,12 +354,10 @@ add_sample_code(globals()["square"], r""" ...@@ -391,12 +354,10 @@ add_sample_code(globals()["square"], r"""
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_variable(x_data)
out = paddle.square(x) out = paddle.square(x)
print(out.numpy()) print(out.numpy())
# [0.16 0.04 0.01 0.09] # [0.16 0.04 0.01 0.09]
...@@ -409,11 +370,10 @@ Examples: ...@@ -409,11 +370,10 @@ Examples:
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
import numpy as np
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3])) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = F.softplus(x) # [0.513015, 0.598139, 0.744397, 0.854355] out = F.softplus(x) # [0.513015, 0.598139, 0.744397, 0.854355]
""") """)
...@@ -424,11 +384,10 @@ Examples: ...@@ -424,11 +384,10 @@ Examples:
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
import numpy as np
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3])) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = F.softsign(x) # [-0.285714, -0.166667, 0.0909091, 0.230769] out = F.softsign(x) # [-0.285714, -0.166667, 0.0909091, 0.230769]
""") """)
...@@ -761,11 +720,9 @@ Examples: ...@@ -761,11 +720,9 @@ Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([-0.4, -0.2, 0.1, 0.3]) x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_tensor(x_data)
out = paddle.erf(x) out = paddle.erf(x)
print(out.numpy()) print(out.numpy())
# [-0.42839236 -0.22270259 0.11246292 0.32862676] # [-0.42839236 -0.22270259 0.11246292 0.32862676]
......
...@@ -138,13 +138,10 @@ def binary_cross_entropy(input, label, weight=None, reduction='mean', ...@@ -138,13 +138,10 @@ def binary_cross_entropy(input, label, weight=None, reduction='mean',
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
input_data = np.array([0.5, 0.6, 0.7]).astype("float32")
label_data = np.array([1.0, 0.0, 1.0]).astype("float32")
paddle.disable_static() paddle.disable_static()
input = paddle.to_tensor(input_data) input = paddle.to_tensor([0.5, 0.6, 0.7], 'float32')
label = paddle.to_tensor(label_data) label = paddle.to_tensor([1.0, 0.0, 1.0], 'float32')
output = paddle.nn.functional.binary_cross_entropy(input, label) output = paddle.nn.functional.binary_cross_entropy(input, label)
print(output.numpy()) # [0.65537095] print(output.numpy()) # [0.65537095]
...@@ -277,8 +274,8 @@ def binary_cross_entropy_with_logits(logit, ...@@ -277,8 +274,8 @@ def binary_cross_entropy_with_logits(logit,
import paddle import paddle
paddle.disable_static() paddle.disable_static()
logit = paddle.to_tensor([5.0, 1.0, 3.0], dtype="float32") logit = paddle.to_tensor([5.0, 1.0, 3.0])
label = paddle.to_tensor([1.0, 0.0, 1.0], dtype="float32") label = paddle.to_tensor([1.0, 0.0, 1.0])
output = paddle.nn.functional.binary_cross_entropy_with_logits(logit, label) output = paddle.nn.functional.binary_cross_entropy_with_logits(logit, label)
print(output.numpy()) # [0.45618808] print(output.numpy()) # [0.45618808]
...@@ -569,13 +566,10 @@ def l1_loss(input, label, reduction='mean', name=None): ...@@ -569,13 +566,10 @@ def l1_loss(input, label, reduction='mean', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
input_data = np.array([[1.5, 0.8], [0.2, 1.3]]).astype("float32") input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]])
label_data = np.array([[1.7, 1], [0.4, 0.5]]).astype("float32") label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]])
input = paddle.to_tensor(input_data)
label = paddle.to_tensor(label_data)
l1_loss = paddle.nn.functional.l1_loss(input, label) l1_loss = paddle.nn.functional.l1_loss(input, label)
print(l1_loss.numpy()) print(l1_loss.numpy())
...@@ -868,7 +862,7 @@ def mse_loss(input, label, reduction='mean', name=None): ...@@ -868,7 +862,7 @@ def mse_loss(input, label, reduction='mean', name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
...@@ -878,8 +872,6 @@ def mse_loss(input, label, reduction='mean', name=None): ...@@ -878,8 +872,6 @@ def mse_loss(input, label, reduction='mean', name=None):
input = paddle.data(name="input", shape=[1]) input = paddle.data(name="input", shape=[1])
label = paddle.data(name="label", shape=[1]) label = paddle.data(name="label", shape=[1])
place = paddle.CPUPlace() place = paddle.CPUPlace()
input_data = np.array([1.5]).astype("float32")
label_data = np.array([1.7]).astype("float32")
output = mse_loss(input,label) output = mse_loss(input,label)
exe = paddle.static.Executor(place) exe = paddle.static.Executor(place)
...@@ -894,8 +886,8 @@ def mse_loss(input, label, reduction='mean', name=None): ...@@ -894,8 +886,8 @@ def mse_loss(input, label, reduction='mean', name=None):
# dynamic graph mode # dynamic graph mode
paddle.disable_static() paddle.disable_static()
input = paddle.to_variable(input_data) input = paddle.to_tensor(1.5)
label = paddle.to_variable(label_data) label = paddle.to_tensor(1.7)
output = mse_loss(input, label) output = mse_loss(input, label)
print(output.numpy()) print(output.numpy())
# [0.04000002] # [0.04000002]
......
...@@ -366,11 +366,10 @@ def ones_like(x, dtype=None, name=None): ...@@ -366,11 +366,10 @@ def ones_like(x, dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(np.array([1,2,3], dtype='float32')) x = paddle.to_tensor([1,2,3])
out1 = paddle.zeros_like(x) # [1., 1., 1.] out1 = paddle.zeros_like(x) # [1., 1., 1.]
out2 = paddle.zeros_like(x, dtype='int32') # [1, 1, 1] out2 = paddle.zeros_like(x, dtype='int32') # [1, 1, 1]
...@@ -453,11 +452,10 @@ def zeros_like(x, dtype=None, name=None): ...@@ -453,11 +452,10 @@ def zeros_like(x, dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(np.array([1,2,3], dtype='float32')) x = paddle.to_tensor([1,2,3])
out1 = paddle.zeros_like(x) # [0., 0., 0.] out1 = paddle.zeros_like(x) # [0., 0., 0.]
out2 = paddle.zeros_like(x, dtype='int32') # [0, 0, 0] out2 = paddle.zeros_like(x, dtype='int32') # [0, 0, 0]
...@@ -619,7 +617,6 @@ def arange(start=0, end=None, step=1, dtype=None, name=None): ...@@ -619,7 +617,6 @@ def arange(start=0, end=None, step=1, dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
...@@ -633,7 +630,7 @@ def arange(start=0, end=None, step=1, dtype=None, name=None): ...@@ -633,7 +630,7 @@ def arange(start=0, end=None, step=1, dtype=None, name=None):
out3 = paddle.arange(4.999, dtype='float32') out3 = paddle.arange(4.999, dtype='float32')
# [0., 1., 2., 3., 4.] # [0., 1., 2., 3., 4.]
start_var = paddle.to_tensor(np.array([3])) start_var = paddle.to_tensor([3])
out4 = paddle.arange(start_var, 7) out4 = paddle.arange(start_var, 7)
# [3, 4, 5, 6] # [3, 4, 5, 6]
...@@ -725,7 +722,7 @@ def tril(x, diagonal=0, name=None): ...@@ -725,7 +722,7 @@ def tril(x, diagonal=0, name=None):
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(data) x = paddle.to_tensor(data)
tril1 = paddle.tensor.tril(x) tril1 = paddle.tensor.tril(x)
# array([[ 1, 0, 0, 0], # array([[ 1, 0, 0, 0],
...@@ -797,7 +794,7 @@ def triu(x, diagonal=0, name=None): ...@@ -797,7 +794,7 @@ def triu(x, diagonal=0, name=None):
paddle.disable_static() paddle.disable_static()
# example 1, default diagonal # example 1, default diagonal
x = paddle.to_variable(data) x = paddle.to_tensor(data)
triu1 = paddle.tensor.triu(x) triu1 = paddle.tensor.triu(x)
# array([[ 1, 2, 3, 4], # array([[ 1, 2, 3, 4],
# [ 0, 6, 7, 8], # [ 0, 6, 7, 8],
......
...@@ -810,7 +810,7 @@ def cholesky(x, upper=False, name=None): ...@@ -810,7 +810,7 @@ def cholesky(x, upper=False, name=None):
a = np.random.rand(3, 3) a = np.random.rand(3, 3)
a_t = np.transpose(a, [1, 0]) a_t = np.transpose(a, [1, 0])
x_data = np.matmul(a, a_t) + 1e-03 x_data = np.matmul(a, a_t) + 1e-03
x = paddle.to_variable(x_data) x = paddle.to_tensor(x_data)
out = paddle.cholesky(x, upper=False) out = paddle.cholesky(x, upper=False)
print(out.numpy()) print(out.numpy())
# [[1.190523 0. 0. ] # [[1.190523 0. 0. ]
...@@ -855,15 +855,16 @@ def bmm(x, y, name=None): ...@@ -855,15 +855,16 @@ def bmm(x, y, name=None):
Examples: Examples:
import paddle import paddle
# In imperative mode:
# size input1: (2, 2, 3) and input2: (2, 3, 2)
input1 = np.array([[[1.0, 1.0, 1.0],[2.0, 2.0, 2.0]],[[3.0, 3.0, 3.0],[4.0, 4.0, 4.0]]])
input2 = np.array([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],[[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]])
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(input1) # In imperative mode:
y = paddle.to_variable(input2) # size x: (2, 2, 3) and y: (2, 3, 2)
x = paddle.to_tensor([[[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0]],
[[3.0, 3.0, 3.0],
[4.0, 4.0, 4.0]]])
y = paddle.to_tensor([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],
[[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]])
out = paddle.bmm(x, y) out = paddle.bmm(x, y)
#output size: (2, 2, 2) #output size: (2, 2, 2)
#output value: #output value:
...@@ -924,10 +925,8 @@ def histogram(input, bins=100, min=0, max=0): ...@@ -924,10 +925,8 @@ def histogram(input, bins=100, min=0, max=0):
Code Example 2: Code Example 2:
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
inputs_np = np.array([1, 2, 1]).astype(np.float) inputs = paddle.to_tensor([1, 2, 1])
inputs = paddle.to_variable(inputs_np)
result = paddle.histogram(inputs, bins=4, min=0, max=3) result = paddle.histogram(inputs, bins=4, min=0, max=3)
print(result) # [0, 2, 1, 0] print(result) # [0, 2, 1, 0]
paddle.enable_static() paddle.enable_static()
......
...@@ -71,13 +71,12 @@ def equal_all(x, y, name=None): ...@@ -71,13 +71,12 @@ def equal_all(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 2, 3])) y = paddle.to_tensor([1, 2, 3])
z = paddle.to_variable(np.array([1, 4, 3])) z = paddle.to_tensor([1, 4, 3])
result1 = paddle.equal_all(x, y) result1 = paddle.equal_all(x, y)
print(result1.numpy()) # result1 = [True ] print(result1.numpy()) # result1 = [True ]
result2 = paddle.equal_all(x, z) result2 = paddle.equal_all(x, z)
...@@ -120,14 +119,11 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): ...@@ -120,14 +119,11 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_x = np.array([10000., 1e-07]).astype("float32") x = paddle.to_tensor([10000., 1e-07])
np_y = np.array([10000.1, 1e-08]).astype("float32") y = paddle.to_tensor([10000.1, 1e-08])
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan") equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy() np_result1 = result1.numpy()
...@@ -137,10 +133,8 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): ...@@ -137,10 +133,8 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
np_result2 = result2.numpy() np_result2 = result2.numpy()
# [False] # [False]
np_x = np.array([1.0, float('nan')]).astype("float32") x = paddle.to_tensor([1.0, float('nan')])
np_y = np.array([1.0, float('nan')]).astype("float32") y = paddle.to_tensor([1.0, float('nan')])
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
equal_nan=False, name="ignore_nan") equal_nan=False, name="ignore_nan")
np_result1 = result1.numpy() np_result1 = result1.numpy()
...@@ -195,12 +189,11 @@ def equal(x, y, name=None): ...@@ -195,12 +189,11 @@ def equal(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 3, 2])) y = paddle.to_tensor([1, 3, 2])
result1 = paddle.equal(x, y) result1 = paddle.equal(x, y)
print(result1.numpy()) # result1 = [True False False] print(result1.numpy()) # result1 = [True False False]
""" """
...@@ -227,12 +220,11 @@ def greater_equal(x, y, name=None): ...@@ -227,12 +220,11 @@ def greater_equal(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 3, 2])) y = paddle.to_tensor([1, 3, 2])
result1 = paddle.greater_equal(x, y) result1 = paddle.greater_equal(x, y)
print(result1.numpy()) # result1 = [True False True] print(result1.numpy()) # result1 = [True False True]
""" """
...@@ -259,12 +251,11 @@ def greater_than(x, y, name=None): ...@@ -259,12 +251,11 @@ def greater_than(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 3, 2])) y = paddle.to_tensor([1, 3, 2])
result1 = paddle.greater_than(x, y) result1 = paddle.greater_than(x, y)
print(result1.numpy()) # result1 = [False False True] print(result1.numpy()) # result1 = [False False True]
""" """
...@@ -292,12 +283,11 @@ def less_equal(x, y, name=None): ...@@ -292,12 +283,11 @@ def less_equal(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 3, 2])) y = paddle.to_tensor([1, 3, 2])
result1 = paddle.less_equal(x, y) result1 = paddle.less_equal(x, y)
print(result1.numpy()) # result1 = [True True False] print(result1.numpy()) # result1 = [True True False]
""" """
...@@ -325,12 +315,11 @@ def less_than(x, y, name=None): ...@@ -325,12 +315,11 @@ def less_than(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 3, 2])) y = paddle.to_tensor([1, 3, 2])
result1 = paddle.less_than(x, y) result1 = paddle.less_than(x, y)
print(result1.numpy()) # result1 = [False True False] print(result1.numpy()) # result1 = [False True False]
""" """
...@@ -358,12 +347,12 @@ def not_equal(x, y, name=None): ...@@ -358,12 +347,12 @@ def not_equal(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(np.array([1, 2, 3])) x = paddle.to_tensor([1, 2, 3])
y = paddle.to_variable(np.array([1, 3, 2])) y = paddle.to_tensor([1, 3, 2])
result1 = paddle.not_equal(x, y) result1 = paddle.not_equal(x, y)
print(result1.numpy()) # result1 = [False True True] print(result1.numpy()) # result1 = [False True True]
""" """
......
...@@ -98,18 +98,14 @@ def concat(x, axis=0, name=None): ...@@ -98,18 +98,14 @@ def concat(x, axis=0, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() # Now we are in imperative mode paddle.disable_static() # Now we are in imperative mode
in1 = np.array([[1, 2, 3], x1 = paddle.to_tensor([[1, 2, 3],
[4, 5, 6]]) [4, 5, 6]])
in2 = np.array([[11, 12, 13], x2 = paddle.to_tensor([[11, 12, 13],
[14, 15, 16]]) [14, 15, 16]])
in3 = np.array([[21, 22], x3 = paddle.to_tensor([[21, 22],
[23, 24]]) [23, 24]])
x1 = paddle.to_tensor(in1)
x2 = paddle.to_tensor(in2)
x3 = paddle.to_tensor(in3)
zero = paddle.full(shape=[1], dtype='int32', fill_value=0) zero = paddle.full(shape=[1], dtype='int32', fill_value=0)
# When the axis is negative, the real axis is (axis + Rank(x)) # When the axis is negative, the real axis is (axis + Rank(x))
# As follow, axis is -1, Rank(x) is 2, the real axis is 1 # As follow, axis is -1, Rank(x) is 2, the real axis is 1
...@@ -158,7 +154,7 @@ def flip(x, axis, name=None): ...@@ -158,7 +154,7 @@ def flip(x, axis, name=None):
image_shape=(3, 2, 2) image_shape=(3, 2, 2)
x = np.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape) x = np.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape)
x = x.astype('float32') x = x.astype('float32')
img = paddle.to_variable(x) img = paddle.to_tensor(x)
out = paddle.flip(img, [0,1]) out = paddle.flip(img, [0,1])
print(out) # [[[10,11][8, 9]],[[6, 7],[4, 5]] [[2, 3],[0, 1]]] print(out) # [[[10,11][8, 9]],[[6, 7],[4, 5]] [[2, 3],[0, 1]]]
...@@ -250,7 +246,7 @@ def flatten(x, start_axis=0, stop_axis=-1, name=None): ...@@ -250,7 +246,7 @@ def flatten(x, start_axis=0, stop_axis=-1, name=None):
x = np.arange(image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3]).reshape(image_shape) / 100. x = np.arange(image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3]).reshape(image_shape) / 100.
x = x.astype('float32') x = x.astype('float32')
img = paddle.to_variable(x) img = paddle.to_tensor(x)
out = paddle.flatten(img, start_axis=1, stop_axis=2) out = paddle.flatten(img, start_axis=1, stop_axis=2)
# out shape is [2, 12, 4] # out shape is [2, 12, 4]
""" """
...@@ -315,15 +311,13 @@ def roll(x, shifts, axis=None, name=None): ...@@ -315,15 +311,13 @@ def roll(x, shifts, axis=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
data = np.array([[1.0, 2.0, 3.0], paddle.disable_static()
x = paddle.to_tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0], [4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]]) [7.0, 8.0, 9.0]])
paddle.disable_static()
x = paddle.to_variable(data)
out_z1 = paddle.roll(x, shifts=1) out_z1 = paddle.roll(x, shifts=1)
print(out_z1.numpy()) print(out_z1.numpy())
#[[9. 1. 2.] #[[9. 1. 2.]
...@@ -447,7 +441,6 @@ def stack(x, axis=0, name=None): ...@@ -447,7 +441,6 @@ def stack(x, axis=0, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x1 = paddle.to_tensor([[1.0, 2.0]]) x1 = paddle.to_tensor([[1.0, 2.0]])
...@@ -632,12 +625,10 @@ def unique(x, ...@@ -632,12 +625,10 @@ def unique(x,
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
x_data = np.array([2, 3, 3, 1, 5, 3]) x = paddle.to_tensor([2, 3, 3, 1, 5, 3])
x = paddle.to_tensor(x_data)
unique = paddle.unique(x) unique = paddle.unique(x)
np_unique = unique.numpy() # [1 2 3 5] np_unique = unique.numpy() # [1 2 3 5]
_, indices, inverse, counts = paddle.unique(x, return_index=True, return_inverse=True, return_counts=True) _, indices, inverse, counts = paddle.unique(x, return_index=True, return_inverse=True, return_counts=True)
...@@ -645,8 +636,7 @@ def unique(x, ...@@ -645,8 +636,7 @@ def unique(x,
np_inverse = inverse.numpy() # [1 2 2 0 3 2] np_inverse = inverse.numpy() # [1 2 2 0 3 2]
np_counts = counts.numpy() # [1 1 3 1] np_counts = counts.numpy() # [1 1 3 1]
x_data = np.array([[2, 1, 3], [3, 0, 1], [2, 1, 3]]) x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3]])
x = paddle.to_tensor(x_data)
unique = paddle.unique(x) unique = paddle.unique(x)
np_unique = unique.numpy() # [0 1 2 3] np_unique = unique.numpy() # [0 1 2 3]
...@@ -815,14 +805,11 @@ def gather(x, index, axis=None, name=None): ...@@ -815,14 +805,11 @@ def gather(x, index, axis=None, name=None):
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
input_1 = np.array([[1,2],[3,4],[5,6]]) input = paddle.to_tensor([[1,2],[3,4],[5,6]])
index_1 = np.array([0,1]) index = paddle.to_tensor([0,1])
input = paddle.to_tensor(input_1)
index = paddle.to_tensor(index_1)
output = paddle.gather(input, index, axis=0) output = paddle.gather(input, index, axis=0)
# expected output: [[1,2],[3,4]] # expected output: [[1,2],[3,4]]
""" """
...@@ -958,16 +945,11 @@ def scatter(x, index, updates, overwrite=True, name=None): ...@@ -958,16 +945,11 @@ def scatter(x, index, updates, overwrite=True, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_data = np.array([[1, 1], [2, 2], [3, 3]]).astype(np.float32) x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
index_data = np.array([2, 1, 0, 1]).astype(np.int64) index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
updates_data = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]).astype(np.float32) updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')
x = paddle.to_tensor(x_data)
index = paddle.to_tensor(index_data)
updates = paddle.to_tensor(updates_data)
output1 = paddle.scatter(x, index, updates, overwrite=False) output1 = paddle.scatter(x, index, updates, overwrite=False)
# [[3., 3.], # [[3., 3.],
...@@ -1074,11 +1056,9 @@ def tile(x, repeat_times, name=None): ...@@ -1074,11 +1056,9 @@ def tile(x, repeat_times, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_data = np.array([1, 2, 3]).astype('int32') data = paddle.to_tensor([1, 2, 3], dtype='int32')
data = paddle.to_tensor(np_data)
out = paddle.tile(data, repeat_times=[2, 1]) out = paddle.tile(data, repeat_times=[2, 1])
np_out = out.numpy() np_out = out.numpy()
# [[1, 2, 3], [1, 2, 3]] # [[1, 2, 3], [1, 2, 3]]
...@@ -1087,8 +1067,7 @@ def tile(x, repeat_times, name=None): ...@@ -1087,8 +1067,7 @@ def tile(x, repeat_times, name=None):
np_out = out.numpy() np_out = out.numpy()
# [[1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3]] # [[1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3]]
np_repeat_times = np.array([2, 1]).astype("int32") repeat_times = paddle.to_tensor([2, 1], dtype='int32')
repeat_times = paddle.to_tensor(np_repeat_times)
out = paddle.tile(data, repeat_times=repeat_times) out = paddle.tile(data, repeat_times=repeat_times)
np_out = out.numpy() np_out = out.numpy()
# [[1, 2, 3], [1, 2, 3]] # [[1, 2, 3], [1, 2, 3]]
...@@ -1156,15 +1135,12 @@ def expand_as(x, y, name=None): ...@@ -1156,15 +1135,12 @@ def expand_as(x, y, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
np_data_x = np.array([1, 2, 3]).astype('int32') data_x = paddle.to_tensor([1, 2, 3], 'int32')
np_data_y = np.array([[1, 2, 3], [4, 5, 6]]).astype('int32') data_y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], 'int32')
data_x = paddle.to_tensor(np_data_x)
data_y = paddle.to_tensor(np_data_y)
out = paddle.expand_as(data_x, data_y) out = paddle.expand_as(data_x, data_y)
np_out = out.numpy() np_out = out.numpy()
# [[1, 2, 3], [1, 2, 3]] # [[1, 2, 3], [1, 2, 3]]
...@@ -1212,12 +1188,10 @@ def expand(x, shape, name=None): ...@@ -1212,12 +1188,10 @@ def expand(x, shape, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
np_data = np.array([1, 2, 3]).astype('int32') data = paddle.to_tensor([1, 2, 3], dtype='int32')
data = paddle.to_tensor(np_data)
out = paddle.expand(data, shape=[2, 3]) out = paddle.expand(data, shape=[2, 3])
out = out.numpy() out = out.numpy()
# [[1, 2, 3], [1, 2, 3]] # [[1, 2, 3], [1, 2, 3]]
...@@ -1416,14 +1390,11 @@ def gather_nd(x, index, name=None): ...@@ -1416,14 +1390,11 @@ def gather_nd(x, index, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_x = np.array([[[1, 2], [3, 4], [5, 6]], x = paddle.to_tensor([[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]]]) [[7, 8], [9, 10], [11, 12]]])
np_index = [[0, 1]] index = paddle.to_tensor([[0, 1]])
x = paddle.to_tensor(np_x)
index = paddle.to_tensor(np_index)
output = paddle.gather_nd(x, index) #[[3, 4]] output = paddle.gather_nd(x, index) #[[3, 4]]
......
...@@ -174,14 +174,12 @@ def pow(x, y, name=None): ...@@ -174,14 +174,12 @@ def pow(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
# example 1: y is a float # example 1: y is a float
x_data = np.array([1, 2, 3]) x = paddle.to_tensor([1, 2, 3])
y = 2 y = 2
x = paddle.to_tensor(x_data)
res = paddle.pow(x, y) res = paddle.pow(x, y)
print(res.numpy()) # [1 4 9] print(res.numpy()) # [1 4 9]
...@@ -291,13 +289,10 @@ Examples: ...@@ -291,13 +289,10 @@ Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_x = np.array([2, 3, 4]).astype('float64') x = paddle.to_tensor([2, 3, 4], 'float64')
np_y = np.array([1, 5, 2]).astype('float64') y = paddle.to_tensor([1, 5, 2], 'float64')
x = paddle.to_variable(np_x)
y = paddle.to_variable(np_y)
z = paddle.add(x, y) z = paddle.add(x, y)
np_z = z.numpy() np_z = z.numpy()
print(np_z) # [3., 8., 6. ] print(np_z) # [3., 8., 6. ]
...@@ -335,14 +330,11 @@ def divide(x, y, name=None): ...@@ -335,14 +330,11 @@ def divide(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_x = np.array([2, 3, 4]).astype('float64') x = paddle.to_tensor([2, 3, 4], dtype='float64')
np_y = np.array([1, 5, 2]).astype('float64') y = paddle.to_tensor([1, 5, 2], dtype='float64')
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
z = paddle.divide(x, y) z = paddle.divide(x, y)
print(z.numpy()) # [2., 0.6, 2.] print(z.numpy()) # [2., 0.6, 2.]
...@@ -440,14 +432,11 @@ def floor_divide(x, y, name=None): ...@@ -440,14 +432,11 @@ def floor_divide(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_x = np.array([2, 3, 8, 7]) x = paddle.to_tensor([2, 3, 8, 7])
np_y = np.array([1, 5, 3, 3]) y = paddle.to_tensor([1, 5, 3, 3])
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
z = paddle.floor_divide(x, y) z = paddle.floor_divide(x, y)
print(z.numpy()) # [2, 0, 2, 2] print(z.numpy()) # [2, 0, 2, 2]
...@@ -530,14 +519,11 @@ def remainder(x, y, name=None): ...@@ -530,14 +519,11 @@ def remainder(x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
np_x = np.array([2, 3, 8, 7]) x = paddle.to_tensor([2, 3, 8, 7])
np_y = np.array([1, 5, 3, 3]) y = paddle.to_tensor([1, 5, 3, 3])
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
z = paddle.remainder(x, y) z = paddle.remainder(x, y)
print(z.numpy()) # [0, 3, 2, 1] print(z.numpy()) # [0, 3, 2, 1]
...@@ -612,20 +598,15 @@ def multiply(x, y, axis=-1, name=None): ...@@ -612,20 +598,15 @@ def multiply(x, y, axis=-1, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_data = np.array([[1, 2], [3, 4]], dtype=np.float32) x = paddle.to_tensor([[1, 2], [3, 4]])
y_data = np.array([[5, 6], [7, 8]], dtype=np.float32) y = paddle.to_tensor([[5, 6], [7, 8]])
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
res = paddle.multiply(x, y) res = paddle.multiply(x, y)
print(res.numpy()) # [[5, 12], [21, 32]] print(res.numpy()) # [[5, 12], [21, 32]]
x_data = np.array([[[1, 2, 3], [1, 2, 3]]], dtype=np.float32) x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y_data = np.array([1, 2], dtype=np.float32) y = paddle.to_tensor([1, 2])
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
res = paddle.multiply(x, y, axis=1) res = paddle.multiply(x, y, axis=1)
print(res.numpy()) # [[[1, 2, 3], [2, 4, 6]]] print(res.numpy()) # [[[1, 2, 3], [2, 4, 6]]]
...@@ -654,36 +635,28 @@ Examples: ...@@ -654,36 +635,28 @@ Examples:
paddle.disable_static() paddle.disable_static()
x_data = np.array([[1, 2], [3, 4]], dtype=np.float32) x = paddle.to_tensor([[1, 2], [3, 4]])
y_data = np.array([[5, 6], [7, 8]], dtype=np.float32) y = paddle.to_tensor([[5, 6], [7, 8]])
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.maximum(x, y) res = paddle.maximum(x, y)
print(res.numpy()) print(res.numpy())
#[[5. 6.] #[[5. 6.]
# [7. 8.]] # [7. 8.]]
x_data = np.array([[[1, 2, 3], [1, 2, 3]]], dtype=np.float32) x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]])
y_data = np.array([1, 2], dtype=np.float32) y = paddle.to_tensor([1, 2])
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.maximum(x, y, axis=1) res = paddle.maximum(x, y, axis=1)
print(res.numpy()) print(res.numpy())
#[[[1. 2. 3.] #[[[1. 2. 3.]
# [2. 2. 3.]]] # [2. 2. 3.]]]
x_data = np.array([2, 3, 5], dtype=np.float32) x = paddle.to_tensor([2, 3, 5], dtype='float32')
y_data = np.array([1, 4, np.nan], dtype=np.float32) y = paddle.to_tensor([1, 4, np.nan], dtype='float32')
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.maximum(x, y) res = paddle.maximum(x, y)
print(res.numpy()) print(res.numpy())
#[ 2. 4. nan] #[ 2. 4. nan]
x_data = np.array([5, 3, np.inf], dtype=np.float32) x = paddle.to_tensor([5, 3, np.inf], dtype='float32')
y_data = np.array([1, 4, 5], dtype=np.float32) y = paddle.to_tensor([1, 4, 5], dtype='float32')
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.maximum(x, y) res = paddle.maximum(x, y)
print(res.numpy()) print(res.numpy())
#[ 5. 4. inf] #[ 5. 4. inf]
...@@ -703,38 +676,31 @@ Examples: ...@@ -703,38 +676,31 @@ Examples:
import paddle import paddle
import numpy as np import numpy as np
paddle.disable_static() paddle.disable_static()
x_data = np.array([[1, 2], [3, 4]], dtype=np.float32) x = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32')
y_data = np.array([[5, 6], [7, 8]], dtype=np.float32) y = paddle.to_tensor([[5, 6], [7, 8]], dtype='float32')
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.minimum(x, y) res = paddle.minimum(x, y)
print(res.numpy()) print(res.numpy())
#[[1. 2.] #[[1. 2.]
# [3. 4.]] # [3. 4.]]
x_data = np.array([[[1, 2, 3], [1, 2, 3]]], dtype=np.float32) x = paddle.to_tensor([[[1, 2, 3], [1, 2, 3]]], dtype='float32')
y_data = np.array([1, 2], dtype=np.float32) y = paddle.to_tensor([1, 2], dtype='float32')
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.minimum(x, y, axis=1) res = paddle.minimum(x, y, axis=1)
print(res.numpy()) print(res.numpy())
#[[[1. 1. 1.] #[[[1. 1. 1.]
# [2. 2. 2.]]] # [2. 2. 2.]]]
x_data = np.array([2, 3, 5], dtype=np.float32) x = paddle.to_tensor([2, 3, 5], dtype='float32')
y_data = np.array([1, 4, np.nan], dtype=np.float32) y = paddle.to_tensor([1, 4, np.nan], dtype='float32')
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.minimum(x, y) res = paddle.minimum(x, y)
print(res.numpy()) print(res.numpy())
#[ 1. 3. nan] #[ 1. 3. nan]
x_data = np.array([5, 3, np.inf], dtype=np.float32) x = paddle.to_tensor([5, 3, np.inf], dtype='float32')
y_data = np.array([1, 4, 5], dtype=np.float32) y = paddle.to_tensor([1, 4, 5], dtype='float32')
x = paddle.to_variable(x_data)
y = paddle.to_variable(y_data)
res = paddle.minimum(x, y) res = paddle.minimum(x, y)
print(res.numpy()) print(res.numpy())
#[1. 3. 5.] #[1. 3. 5.]
...@@ -800,27 +766,26 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): ...@@ -800,27 +766,26 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
# x is a Tensor variable 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_data = np.array([[0.2, 0.3, 0.5, 0.9],[0.1, 0.2, 0.6, 0.7]]).astype('float32') x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
x = paddle.to_variable(x_data) [0.1, 0.2, 0.6, 0.7]])
out1 = paddle.sum(x) # [3.5] out1 = paddle.sum(x) # [3.5]
out2 = paddle.sum(x, axis=0) # [0.3, 0.5, 1.1, 1.6] out2 = paddle.sum(x, axis=0) # [0.3, 0.5, 1.1, 1.6]
out3 = paddle.sum(x, axis=-1) # [1.9, 1.6] out3 = paddle.sum(x, axis=-1) # [1.9, 1.6]
out4 = paddle.sum(x, axis=1, keepdim=True) # [[1.9], [1.6]] out4 = paddle.sum(x, axis=1, keepdim=True) # [[1.9], [1.6]]
# y is a Tensor variable with shape [2, 2, 2] and elements as below: # y is a Tensor with shape [2, 2, 2] and elements as below:
# [[[1, 2], [3, 4]], # [[[1, 2], [3, 4]],
# [[5, 6], [7, 8]]] # [[5, 6], [7, 8]]]
# Each example is followed by the corresponding output tensor. # Each example is followed by the corresponding output tensor.
y_data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]).astype('float32') y = paddle.to_tensor([[[1, 2], [3, 4]],
y = paddle.to_variable(y_data) [[5, 6], [7, 8]]])
out5 = paddle.sum(y, axis=[1, 2]) # [10, 26] out5 = paddle.sum(y, axis=[1, 2]) # [10, 26]
out6 = paddle.sum(y, axis=[0, 1]) # [16, 20] out6 = paddle.sum(y, axis=[0, 1]) # [16, 20]
""" """
...@@ -1121,9 +1086,9 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None): ...@@ -1121,9 +1086,9 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None):
paddle.disable_static() paddle.disable_static()
x = paddle.to_variable(data_x) x = paddle.to_tensor(data_x)
y = paddle.to_variable(data_y) y = paddle.to_tensor(data_y)
input = paddle.to_variable(data_input) input = paddle.to_tensor(data_input)
out = paddle.tensor.addmm( input=input, x=x, y=y, beta=0.5, alpha=5.0 ) out = paddle.tensor.addmm( input=input, x=x, y=y, beta=0.5, alpha=5.0 )
...@@ -1204,12 +1169,10 @@ def logsumexp(x, axis=None, keepdim=False, name=None): ...@@ -1204,12 +1169,10 @@ def logsumexp(x, axis=None, keepdim=False, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x = np.array([[-1.5, 0., 2.], [3., 1.2, -2.4]]) x = paddle.to_tensor([[-1.5, 0., 2.], [3., 1.2, -2.4]])
x = paddle.to_tensor(x)
out1 = paddle.logsumexp(x) # [3.4691226] out1 = paddle.logsumexp(x) # [3.4691226]
out2 = paddle.logsumexp(x, 1) # [2.15317821, 3.15684602] out2 = paddle.logsumexp(x, 1) # [2.15317821, 3.15684602]
...@@ -1260,12 +1223,10 @@ def inverse(x, name=None): ...@@ -1260,12 +1223,10 @@ def inverse(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
mat_np = np.array([[2, 0], [0, 2]]).astype("float32")
paddle.disable_static() paddle.disable_static()
mat = paddle.to_variable(mat_np)
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) # [[0.5, 0], [0, 0.5]]
...@@ -1316,16 +1277,15 @@ def max(x, axis=None, keepdim=False, name=None): ...@@ -1316,16 +1277,15 @@ def max(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
# data_x is a variable with shape [2, 4] # data_x is a variable with shape [2, 4]
# the axis is a int element # the axis is a int element
data_x = np.array([[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]])
x = paddle.to_variable(data_x)
result1 = paddle.max(x) result1 = paddle.max(x)
print(result1.numpy()) print(result1.numpy())
#[0.9] #[0.9]
...@@ -1342,9 +1302,9 @@ def max(x, axis=None, keepdim=False, name=None): ...@@ -1342,9 +1302,9 @@ def max(x, axis=None, keepdim=False, name=None):
# data_y is a variable with shape [2, 2, 2] # data_y is a variable with shape [2, 2, 2]
# the axis is list # the axis is list
data_y = np.array([[[1.0, 2.0], [3.0, 4.0]],
y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
[[5.0, 6.0], [7.0, 8.0]]]) [[5.0, 6.0], [7.0, 8.0]]])
y = paddle.to_variable(data_y)
result5 = paddle.max(y, axis=[1, 2]) result5 = paddle.max(y, axis=[1, 2])
print(result5.numpy()) print(result5.numpy())
#[4. 8.] #[4. 8.]
...@@ -1411,16 +1371,14 @@ def min(x, axis=None, keepdim=False, name=None): ...@@ -1411,16 +1371,14 @@ def min(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
# data_x is a variable with shape [2, 4] # x is a tensor with shape [2, 4]
# the axis is a int element # the axis is a int element
data_x = np.array([[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]])
x = paddle.to_variable(data_x)
result1 = paddle.min(x) result1 = paddle.min(x)
print(result1.numpy()) print(result1.numpy())
#[0.1] #[0.1]
...@@ -1435,11 +1393,10 @@ def min(x, axis=None, keepdim=False, name=None): ...@@ -1435,11 +1393,10 @@ def min(x, axis=None, keepdim=False, name=None):
#[[0.2] #[[0.2]
# [0.1]] # [0.1]]
# data_y is a variable with shape [2, 2, 2] # y is a variable with shape [2, 2, 2]
# the axis is list # the axis is list
data_y = np.array([[[1.0, 2.0], [3.0, 4.0]], y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
[[5.0, 6.0], [7.0, 8.0]]]) [[5.0, 6.0], [7.0, 8.0]]])
y = paddle.to_variable(data_y)
result5 = paddle.min(y, axis=[1, 2]) result5 = paddle.min(y, axis=[1, 2])
print(result5.numpy()) print(result5.numpy())
#[1. 5.] #[1. 5.]
...@@ -1596,11 +1553,9 @@ def clip(x, min=None, max=None, name=None): ...@@ -1596,11 +1553,9 @@ def clip(x, min=None, max=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x = np.array([[1.2,3.5], [4.5,6.4]]).astype('float32') x1 = paddle.to_tensor([[1.2, 3.5], [4.5, 6.4]], 'float32')
x1 = paddle.to_variable(x)
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) out2 = paddle.clip(x1, min=2.5)
print(out1.numpy()) print(out1.numpy())
...@@ -1701,9 +1656,9 @@ def trace(x, offset=0, axis1=0, axis2=1, name=None): ...@@ -1701,9 +1656,9 @@ def trace(x, offset=0, axis1=0, axis2=1, name=None):
paddle.disable_static() paddle.disable_static()
case1 = paddle.to_variable(case1) case1 = paddle.to_tensor(case1)
case2 = paddle.to_variable(case2) case2 = paddle.to_tensor(case2)
case3 = paddle.to_variable(case3) case3 = paddle.to_tensor(case3)
data1 = paddle.trace(case1) # data1.shape = [1] data1 = paddle.trace(case1) # data1.shape = [1]
data2 = paddle.trace(case2, offset=1, axis1=1, axis2=2) # data2.shape = [3] data2 = paddle.trace(case2, offset=1, axis1=1, axis2=2) # data2.shape = [3]
data3 = paddle.trace(case3, offset=-3, axis1=1, axis2=-1) # data2.shape = [3, 5] data3 = paddle.trace(case3, offset=-3, axis1=1, axis2=-1) # data2.shape = [3, 5]
...@@ -1894,10 +1849,8 @@ def isfinite(x, name=None): ...@@ -1894,10 +1849,8 @@ def isfinite(x, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_np = np.array([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')])
x = paddle.to_tensor(x_np)
out = paddle.tensor.isfinite(x) out = paddle.tensor.isfinite(x)
print(out.numpy()) # [False True True False True False False] print(out.numpy()) # [False True True False True False False]
""" """
...@@ -1925,10 +1878,8 @@ def isinf(x, name=None): ...@@ -1925,10 +1878,8 @@ def isinf(x, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_np = np.array([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')])
x = paddle.to_tensor(x_np)
out = paddle.tensor.isinf(x) out = paddle.tensor.isinf(x)
print(out.numpy()) # [ True False False True False False False] print(out.numpy()) # [ True False False True False False False]
""" """
...@@ -1956,10 +1907,8 @@ def isnan(x, name=None): ...@@ -1956,10 +1907,8 @@ def isnan(x, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x_np = np.array([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')])
x = paddle.to_tensor(x_np)
out = paddle.tensor.isnan(x) out = paddle.tensor.isnan(x)
print(out.numpy()) # [False False False False False True True] print(out.numpy()) # [False False False False False True True]
""" """
...@@ -2002,14 +1951,12 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None): ...@@ -2002,14 +1951,12 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
# the axis is a int element # the axis is a int element
data_x = np.array([[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]]).astype(np.float32) [0.1, 0.2, 0.6, 0.7]])
x = paddle.to_tensor(data_x)
out1 = paddle.prod(x) out1 = paddle.prod(x)
print(out1.numpy()) print(out1.numpy())
# [0.0002268] # [0.0002268]
...@@ -2035,9 +1982,8 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None): ...@@ -2035,9 +1982,8 @@ def prod(x, axis=None, keepdim=False, dtype=None, name=None):
# int64 # int64
# the axis is list # the axis is list
data_y = np.array([[[1.0, 2.0], [3.0, 4.0]], y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
[[5.0, 6.0], [7.0, 8.0]]]) [[5.0, 6.0], [7.0, 8.0]]])
y = paddle.to_tensor(data_y)
out6 = paddle.prod(y, [0, 1]) out6 = paddle.prod(y, [0, 1])
print(out6.numpy()) print(out6.numpy())
# [105. 384.] # [105. 384.]
...@@ -2070,12 +2016,10 @@ def sign(x, name=None): ...@@ -2070,12 +2016,10 @@ def sign(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
data = np.array([3.0, 0.0, -2.0, 1.7], dtype='float32')
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(data) 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] print(out) # [1.0, 0.0, -1.0, 1.0]
""" """
...@@ -2110,12 +2054,9 @@ def tanh(x, name=None): ...@@ -2110,12 +2054,9 @@ def tanh(x, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
x_data = np.array([-0.4, -0.2, 0.1, 0.3])
x = paddle.to_tensor(x_data)
out = paddle.tanh(x) out = paddle.tanh(x)
print(out.numpy()) print(out.numpy())
# [-0.37994896 -0.19737532 0.09966799 0.29131261] # [-0.37994896 -0.19737532 0.09966799 0.29131261]
......
...@@ -60,7 +60,6 @@ def bernoulli(x, name=None): ...@@ -60,7 +60,6 @@ def bernoulli(x, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
...@@ -188,7 +187,6 @@ def standard_normal(shape, dtype=None, name=None): ...@@ -188,7 +187,6 @@ def standard_normal(shape, dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
...@@ -209,8 +207,9 @@ def standard_normal(shape, dtype=None, name=None): ...@@ -209,8 +207,9 @@ def standard_normal(shape, dtype=None, name=None):
# [ 0.8086993 , 0.6868893 ]]] # random # [ 0.8086993 , 0.6868893 ]]] # random
# example 3: attr shape is a Tensor, the data type must be int64 or int32. # example 3: attr shape is a Tensor, the data type must be int64 or int32.
shape_tensor = paddle.to_tensor(np.array([2, 3])) shape_tensor = paddle.to_tensor([2, 3])
out3 = paddle.standard_normal(shape_tensor) result_3 = paddle.standard_normal(shape_tensor)
# [[-2.878077 , 0.17099959, 0.05111201] # random # [[-2.878077 , 0.17099959, 0.05111201] # random
# [-0.3761474, -1.044801 , 1.1870178 ]] # random # [-0.3761474, -1.044801 , 1.1870178 ]] # random
...@@ -258,7 +257,6 @@ def normal(mean=0.0, std=1.0, shape=None, name=None): ...@@ -258,7 +257,6 @@ def normal(mean=0.0, std=1.0, shape=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
...@@ -266,11 +264,11 @@ def normal(mean=0.0, std=1.0, shape=None, name=None): ...@@ -266,11 +264,11 @@ def normal(mean=0.0, std=1.0, shape=None, name=None):
# [[ 0.17501129 0.32364586 1.561118 ] # random # [[ 0.17501129 0.32364586 1.561118 ] # random
# [-1.7232178 1.1545963 -0.76156676]] # random # [-1.7232178 1.1545963 -0.76156676]] # random
mean_tensor = paddle.to_tensor(np.array([1.0, 2.0, 3.0])) mean_tensor = paddle.to_tensor([1.0, 2.0, 3.0])
out2 = paddle.normal(mean=mean_tensor) out2 = paddle.normal(mean=mean_tensor)
# [ 0.18644847 -1.19434458 3.93694787] # random # [ 0.18644847 -1.19434458 3.93694787] # random
std_tensor = paddle.to_tensor(np.array([1.0, 2.0, 3.0])) std_tensor = paddle.to_tensor([1.0, 2.0, 3.0])
out3 = paddle.normal(mean=mean_tensor, std=std_tensor) out3 = paddle.normal(mean=mean_tensor, std=std_tensor)
# [1.00780561 3.78457445 5.81058198] # random # [1.00780561 3.78457445 5.81058198] # random
...@@ -357,7 +355,6 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None): ...@@ -357,7 +355,6 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
...@@ -379,8 +376,7 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None): ...@@ -379,8 +376,7 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None):
# example 3: # example 3:
# attr shape is a Tensor, the data type must be int64 or int32. # attr shape is a Tensor, the data type must be int64 or int32.
shape = np.array([2, 3]) shape_tensor = paddle.to_tensor([2, 3])
shape_tensor = paddle.to_tensor(shape)
result_3 = paddle.tensor.random.uniform(shape_tensor) result_3 = paddle.tensor.random.uniform(shape_tensor)
# if shape_tensor's value is [2, 3] # if shape_tensor's value is [2, 3]
# result_3 is: # result_3 is:
...@@ -454,7 +450,6 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None): ...@@ -454,7 +450,6 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
...@@ -473,8 +468,10 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None): ...@@ -473,8 +468,10 @@ def randint(low=0, high=None, shape=[1], dtype=None, name=None):
# example 3: # example 3:
# attr shape is a Tensor # attr shape is a Tensor
shape_tensor = paddle.to_tensor(np.array([3]))
out3 = paddle.randint(low=-5, high=5, shape=shape_tensor) shape_tensor = paddle.to_tensor(3)
result_3 = paddle.randint(low=-5, high=5, shape=shape_tensor)
# [-2, 2, 3] # random # [-2, 2, 3] # random
# example 4: # example 4:
...@@ -604,7 +601,6 @@ def rand(shape, dtype=None, name=None): ...@@ -604,7 +601,6 @@ def rand(shape, dtype=None, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
# example 1: attr shape is a list which doesn't contain Tensor. # example 1: attr shape is a list which doesn't contain Tensor.
...@@ -624,8 +620,9 @@ def rand(shape, dtype=None, name=None): ...@@ -624,8 +620,9 @@ def rand(shape, dtype=None, name=None):
# [0.870881 , 0.2984597 ]]] # random # [0.870881 , 0.2984597 ]]] # random
# example 3: attr shape is a Tensor, the data type must be int64 or int32. # example 3: attr shape is a Tensor, the data type must be int64 or int32.
shape_tensor = paddle.to_tensor(np.array([2, 3])) shape_tensor = paddle.to_tensor([2, 3])
out2 = paddle.rand(shape_tensor) result_3 = paddle.rand(shape_tensor)
# [[0.22920267, 0.841956 , 0.05981819], # random # [[0.22920267, 0.841956 , 0.05981819], # random
# [0.4836288 , 0.24573246, 0.7516129 ]] # random # [0.4836288 , 0.24573246, 0.7516129 ]] # random
......
...@@ -66,16 +66,15 @@ def argsort(x, axis=-1, descending=False, name=None): ...@@ -66,16 +66,15 @@ def argsort(x, axis=-1, descending=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
input_array = np.array([[[5,8,9,5], x = paddle.to_tensor([[[5,8,9,5],
[0,0,1,7], [0,0,1,7],
[6,9,2,4]], [6,9,2,4]],
[[5,2,4,2], [[5,2,4,2],
[4,7,7,9], [4,7,7,9],
[1,7,0,6]]]).astype(np.float32) [1,7,0,6]]],
x = paddle.to_variable(input_array) dtype='float32')
out1 = paddle.argsort(x=x, axis=-1) out1 = paddle.argsort(x=x, axis=-1)
out2 = paddle.argsort(x=x, axis=0) out2 = paddle.argsort(x=x, axis=0)
out3 = paddle.argsort(x=x, axis=1) out3 = paddle.argsort(x=x, axis=1)
...@@ -148,14 +147,12 @@ def argmax(x, axis=None, keepdim=False, dtype="int64", name=None): ...@@ -148,14 +147,12 @@ def argmax(x, axis=None, keepdim=False, dtype="int64", name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
data = np.array([[5,8,9,5], x = paddle.to_tensor([[5,8,9,5],
[0,0,1,7], [0,0,1,7],
[6,9,2,4]]) [6,9,2,4]])
x = paddle.to_variable(data)
out1 = paddle.argmax(x) out1 = paddle.argmax(x)
print(out1.numpy()) # 2 print(out1.numpy()) # 2
out2 = paddle.argmax(x, axis=1) out2 = paddle.argmax(x, axis=1)
...@@ -222,14 +219,12 @@ def argmin(x, axis=None, keepdim=False, dtype="int64", name=None): ...@@ -222,14 +219,12 @@ def argmin(x, axis=None, keepdim=False, dtype="int64", name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
data = np.array([[5,8,9,5], x = paddle.to_tensor([[5,8,9,5],
[0,0,1,7], [0,0,1,7],
[6,9,2,4]]) [6,9,2,4]])
x = paddle.to_variable(data)
out1 = paddle.argmin(x) out1 = paddle.argmin(x)
print(out1.numpy()) # 4 print(out1.numpy()) # 4
out2 = paddle.argmin(x, axis=1) out2 = paddle.argmin(x, axis=1)
...@@ -300,16 +295,12 @@ def index_select(x, index, axis=0, name=None): ...@@ -300,16 +295,12 @@ def index_select(x, index, axis=0, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() # Now we are in imperative mode paddle.disable_static() # Now we are in imperative mode
data = np.array([[1.0, 2.0, 3.0, 4.0], x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0], [5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0]]) [9.0, 10.0, 11.0, 12.0]])
data_index = np.array([0, 1, 1]).astype('int32') index = paddle.to_tensor([0, 1, 1], dtype='int32')
x = paddle.to_tensor(data)
index = paddle.to_tensor(data_index)
out_z1 = paddle.index_select(x=x, index=index) out_z1 = paddle.index_select(x=x, index=index)
#[[1. 2. 3. 4.] #[[1. 2. 3. 4.]
# [5. 6. 7. 8.] # [5. 6. 7. 8.]
...@@ -363,18 +354,14 @@ def nonzero(input, as_tuple=False): ...@@ -363,18 +354,14 @@ def nonzero(input, as_tuple=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid
import numpy as np
data1 = np.array([[1.0, 0.0, 0.0], paddle.disable_static()
x1 = paddle.to_tensor([[1.0, 0.0, 0.0],
[0.0, 2.0, 0.0], [0.0, 2.0, 0.0],
[0.0, 0.0, 3.0]]) [0.0, 0.0, 3.0]])
data2 = np.array([0.0, 1.0, 0.0, 3.0]) x2 = paddle.to_tensor([0.0, 1.0, 0.0, 3.0])
data3 = np.array([0.0, 0.0, 0.0]) x3 = paddle.to_tensor([0.0, 0.0, 0.0])
with fluid.dygraph.guard():
x1 = fluid.dygraph.to_variable(data1)
x2 = fluid.dygraph.to_variable(data2)
x3 = fluid.dygraph.to_variable(data3)
out_z1 = paddle.nonzero(x1) out_z1 = paddle.nonzero(x1)
print(out_z1.numpy()) print(out_z1.numpy())
#[[0 0] #[[0 0]
...@@ -451,16 +438,15 @@ def sort(x, axis=-1, descending=False, name=None): ...@@ -451,16 +438,15 @@ def sort(x, axis=-1, descending=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
input_array = np.array([[[5,8,9,5], x = paddle.to_tensor([[[5,8,9,5],
[0,0,1,7], [0,0,1,7],
[6,9,2,4]], [6,9,2,4]],
[[5,2,4,2], [[5,2,4,2],
[4,7,7,9], [4,7,7,9],
[1,7,0,6]]]).astype(np.float32) [1,7,0,6]]],
x = paddle.to_variable(input_array) dtype='float32')
out1 = paddle.sort(x=x, axis=-1) out1 = paddle.sort(x=x, axis=-1)
out2 = paddle.sort(x=x, axis=0) out2 = paddle.sort(x=x, axis=0)
out3 = paddle.sort(x=x, axis=1) out3 = paddle.sort(x=x, axis=1)
...@@ -536,15 +522,10 @@ def where(condition, x, y, name=None): ...@@ -536,15 +522,10 @@ def where(condition, x, y, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
import paddle.fluid as fluid
x_i = np.array([0.9383, 0.1983, 3.2, 1.2]).astype("float32")
y_i = np.array([1.0, 1.0, 1.0, 1.0]).astype("float32")
with fluid.dygraph.guard(): paddle.disable_static()
x = fluid.dygraph.to_variable(x_i) x = paddle.to_tensor([0.9383, 0.1983, 3.2, 1.2])
y = fluid.dygraph.to_variable(y_i) y = paddle.to_tensor([1.0, 1.0, 1.0, 1.0])
out = paddle.where(x>1, x, y) out = paddle.where(x>1, x, y)
print(out.numpy()) print(out.numpy())
...@@ -622,26 +603,17 @@ def index_sample(x, index): ...@@ -622,26 +603,17 @@ def index_sample(x, index):
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid
import numpy as np
data = np.array([[1.0, 2.0, 3.0, 4.0], paddle.disable_static()
x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0], [5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0]]).astype('float32') [9.0, 10.0, 11.0, 12.0]], dtype='float32')
index = paddle.to_tensor([[0, 1, 2],
data_index = np.array([[0, 1, 2],
[1, 2, 3], [1, 2, 3],
[0, 0, 0]]).astype('int32') [0, 0, 0]], dtype='int32')
target = paddle.to_tensor([[100, 200, 300, 400],
target_data = np.array([[100, 200, 300, 400],
[500, 600, 700, 800], [500, 600, 700, 800],
[900, 1000, 1100, 1200]]).astype('int32') [900, 1000, 1100, 1200]], dtype='int32')
with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(data)
index = fluid.dygraph.to_variable(data_index)
target = fluid.dygraph.to_variable(target_data)
out_z1 = paddle.index_sample(x, index) out_z1 = paddle.index_sample(x, index)
print(out_z1.numpy()) print(out_z1.numpy())
#[[1. 2. 3.] #[[1. 2. 3.]
...@@ -650,7 +622,7 @@ def index_sample(x, index): ...@@ -650,7 +622,7 @@ def index_sample(x, index):
# Use the index of the maximum value by topk op # Use the index of the maximum value by topk op
# get the value of the element of the corresponding index in other tensors # get the value of the element of the corresponding index in other tensors
top_value, top_index = fluid.layers.topk(x, k=2) top_value, top_index = paddle.topk(x, k=2)
out_z2 = paddle.index_sample(target, top_index) out_z2 = paddle.index_sample(target, top_index)
print(top_value.numpy()) print(top_value.numpy())
#[[ 4. 3.] #[[ 4. 3.]
...@@ -707,18 +679,15 @@ def masked_select(x, mask, name=None): ...@@ -707,18 +679,15 @@ def masked_select(x, mask, name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
import numpy as np
paddle.disable_static() paddle.disable_static()
data = np.array([[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0]]).astype('float32')
mask_data = np.array([[True, False, False, False], x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0]])
mask = paddle.to_tensor([[True, False, False, False],
[True, True, False, False], [True, True, False, False],
[True, False, False, False]]).astype('bool') [True, False, False, False]])
x = paddle.to_tensor(data)
mask = paddle.to_tensor(mask_data)
out = paddle.masked_select(x, mask) out = paddle.masked_select(x, mask)
#[1.0 5.0 6.0 9.0] #[1.0 5.0 6.0 9.0]
""" """
...@@ -763,20 +732,17 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): ...@@ -763,20 +732,17 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None):
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import paddle
paddle.disable_static() paddle.disable_static()
data_1 = np.array([1, 4, 5, 7]) tensor_1 = paddle.to_tensor([1, 4, 5, 7])
tensor_1 = paddle.to_tensor(data_1)
value_1, indices_1 = paddle.topk(tensor_1, k=1) value_1, indices_1 = paddle.topk(tensor_1, k=1)
print(value_1.numpy()) print(value_1.numpy())
# [7] # [7]
print(indices_1.numpy()) print(indices_1.numpy())
# [3] # [3]
data_2 = np.array([[1, 4, 5, 7], [2, 6, 2, 5]]) tensor_2 = paddle.to_tensor([[1, 4, 5, 7], [2, 6, 2, 5]])
tensor_2 = paddle.to_tensor(data_2)
value_2, indices_2 = paddle.topk(tensor_2, k=1) value_2, indices_2 = paddle.topk(tensor_2, k=1)
print(value_2.numpy()) print(value_2.numpy())
# [[7] # [[7]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册