未验证 提交 d64deaae 编写于 作者: 小飞猪 提交者: GitHub

[xdoctest][task 200] reformat example code with google style in...

[xdoctest][task 200] reformat example code with google style in `python/paddle/tensor/creation.py` (#56685)

* [Doctest]fix No.200, test=docs_preview

* fix output

* add retain_grads

* fix style
上级 5583f277
...@@ -87,10 +87,10 @@ def create_global_var( ...@@ -87,10 +87,10 @@ def create_global_var(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.enable_static() >>> paddle.enable_static()
var = paddle.static.create_global_var(shape=[2,3], value=1.0, dtype='float32', >>> var = paddle.static.create_global_var(shape=[2,3], value=1.0, dtype='float32',
persistable=True, force_cpu=True, name='new_var') ... persistable=True, force_cpu=True, name='new_var')
""" """
check_type(shape, 'shape', (list, tuple, np.ndarray), 'create_global_var') check_type(shape, 'shape', (list, tuple, np.ndarray), 'create_global_var')
for item in shape: for item in shape:
...@@ -172,9 +172,9 @@ def create_parameter( ...@@ -172,9 +172,9 @@ def create_parameter(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.enable_static() >>> paddle.enable_static()
W = paddle.create_parameter(shape=[784, 200], dtype='float32') >>> W = paddle.create_parameter(shape=[784, 200], dtype='float32')
""" """
check_type(shape, 'shape', (list, tuple, np.ndarray), 'create_parameter') check_type(shape, 'shape', (list, tuple, np.ndarray), 'create_parameter')
for item in shape: for item in shape:
...@@ -243,8 +243,8 @@ def create_tensor(dtype, name=None, persistable=False): ...@@ -243,8 +243,8 @@ def create_tensor(dtype, name=None, persistable=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
tensor = paddle.tensor.create_tensor(dtype='float32') >>> tensor = paddle.tensor.create_tensor(dtype='float32')
""" """
check_dtype( check_dtype(
dtype, dtype,
...@@ -290,9 +290,13 @@ def linspace(start, stop, num, dtype=None, name=None): ...@@ -290,9 +290,13 @@ def linspace(start, stop, num, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.linspace(0, 10, 5, 'float32') # [0.0, 2.5, 5.0, 7.5, 10.0] >>> data = paddle.linspace(0, 10, 5, 'float32')
data = paddle.linspace(0, 10, 1, 'float32') # [0.0] >>> print(data.numpy())
[0. 2.5 5. 7.5 10.]
>>> data = paddle.linspace(0, 10, 1, 'float32')
>>> print(data.numpy())
[0.]
""" """
if dtype is None: if dtype is None:
...@@ -418,11 +422,13 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None): ...@@ -418,11 +422,13 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.logspace(0, 10, 5, 2, 'float32') >>> data = paddle.logspace(0, 10, 5, 2, 'float32')
# [1. , 5.65685415 , 32. , 181.01933289, 1024. ] >>> print(data.numpy())
data = paddle.logspace(0, 10, 1, 2, 'float32') [1.0000000e+00 5.6568542e+00 3.2000000e+01 1.8101933e+02 1.0240000e+03]
# [1.] >>> data = paddle.logspace(0, 10, 1, 2, 'float32')
>>> print(data.numpy())
[1.]
""" """
if dtype is None: if dtype is None:
dtype = 'float32' dtype = 'float32'
...@@ -739,38 +745,38 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True): ...@@ -739,38 +745,38 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
Tensor: A Tensor constructed from ``data`` . Tensor: A Tensor constructed from ``data`` .
Examples: Examples:
.. code-block:: python
.. code-block:: python >>> import paddle
import paddle
type(paddle.to_tensor(1)) >>> type(paddle.to_tensor(1))
# <class 'paddle.Tensor'> <class 'paddle.Tensor'>
paddle.to_tensor(1) >>> paddle.to_tensor(1)
# Tensor(shape=[], dtype=int64, place=CPUPlace, stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 1) 1)
x = paddle.to_tensor(1, stop_gradient=False) >>> x = paddle.to_tensor(1, stop_gradient=False)
# Tensor(shape=[], dtype=int64, place=CPUPlace, stop_gradient=False, >>> print(x)
# 1) Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=False,
1)
paddle.to_tensor(x) # A new tensor will be created with default stop_gradient=True >>> paddle.to_tensor(x) # A new tensor will be created with default stop_gradient=True
# Tensor(shape=[], dtype=int64, place=CPUPlace, stop_gradient=True, Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
# 1) 1)
paddle.to_tensor([[0.1, 0.2], [0.3, 0.4]], place=paddle.CPUPlace(), stop_gradient=False) >>> paddle.to_tensor([[0.1, 0.2], [0.3, 0.4]], place=paddle.CPUPlace(), stop_gradient=False)
# Tensor(shape=[2, 2], dtype=float32, place=CPUPlace, stop_gradient=False, Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
# [[0.10000000, 0.20000000], [[0.10000000, 0.20000000],
# [0.30000001, 0.40000001]]) [0.30000001, 0.40000001]])
type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64')) >>> type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64'))
# <class 'paddle.Tensor'> <class 'paddle.Tensor'>
paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64') >>> paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64')
# Tensor(shape=[2, 2], dtype=complex64, place=CPUPlace, stop_gradient=True, Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [[(1+1j), (2+0j)], [[(1+1j), (2+0j)],
# [(3+2j), (4+0j)]]) [(3+2j), (4+0j)]])
""" """
place = _get_paddle_place(place) place = _get_paddle_place(place)
if place is None: if place is None:
...@@ -808,12 +814,13 @@ def full_like(x, fill_value, dtype=None, name=None): ...@@ -808,12 +814,13 @@ def full_like(x, fill_value, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.full(shape=[2, 3], fill_value=0.0, dtype='float32', name='input') >>> input = paddle.full(shape=[2, 3], fill_value=0.0, dtype='float32', name='input')
output = paddle.full_like(input, 2.0) >>> output = paddle.full_like(input, 2.0)
# [[2. 2. 2.] >>> print(output.numpy())
# [2. 2. 2.]] [[2. 2. 2.]
[2. 2. 2.]]
""" """
if dtype is None: if dtype is None:
dtype = x.dtype dtype = x.dtype
...@@ -984,27 +991,30 @@ def ones(shape, dtype=None, name=None): ...@@ -984,27 +991,30 @@ def ones(shape, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# shape is a list/tuple >>> # shape is a list/tuple
data1 = paddle.ones(shape=[3, 2]) >>> data1 = paddle.ones(shape=[3, 2])
# [[1. 1.] >>> print(data1.numpy())
# [1. 1.] [[1. 1.]
# [1. 1.]] [1. 1.]
[1. 1.]]
# shape is a Tensor
shape = paddle.to_tensor([3, 2]) >>> # shape is a Tensor
data2 = paddle.ones(shape=shape) >>> shape = paddle.to_tensor([3, 2])
# [[1. 1.] >>> data2 = paddle.ones(shape=shape)
# [1. 1.] >>> print(data2.numpy())
# [1. 1.]] [[1. 1.]
[1. 1.]
# shape is a Tensor List [1. 1.]]
shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
data3 = paddle.ones(shape=shape) >>> # shape is a Tensor List
# [[1. 1.] >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
# [1. 1.] >>> data3 = paddle.ones(shape=shape)
# [1. 1.]] >>> print(data3.numpy())
[[1. 1.]
[1. 1.]
[1. 1.]]
""" """
if dtype is None: if dtype is None:
dtype = core.VarDesc.VarType.FP32 dtype = core.VarDesc.VarType.FP32
...@@ -1032,11 +1042,15 @@ def ones_like(x, dtype=None, name=None): ...@@ -1032,11 +1042,15 @@ def ones_like(x, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1,2,3]) >>> x = paddle.to_tensor([1,2,3])
out1 = paddle.ones_like(x) # [1., 1., 1.] >>> out1 = paddle.ones_like(x)
out2 = paddle.ones_like(x, dtype='int32') # [1, 1, 1] >>> print(out1.numpy())
[1 1 1]
>>> out2 = paddle.ones_like(x, dtype='int32')
>>> print(out2.numpy())
[1 1 1]
""" """
return full_like(x=x, fill_value=1, dtype=dtype, name=name) return full_like(x=x, fill_value=1, dtype=dtype, name=name)
...@@ -1061,27 +1075,30 @@ def zeros(shape, dtype=None, name=None): ...@@ -1061,27 +1075,30 @@ def zeros(shape, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# shape is a list/tuple >>> # shape is a list/tuple
data1 = paddle.zeros(shape=[3, 2]) >>> data1 = paddle.zeros(shape=[3, 2])
# [[0. 0.] >>> print(data1.numpy())
# [0. 0.] [[0. 0.]
# [0. 0.]] [0. 0.]
[0. 0.]]
# shape is a Tensor
shape = paddle.to_tensor([3, 2]) >>> # shape is a Tensor
data2 = paddle.zeros(shape=shape) >>> shape = paddle.to_tensor([3, 2])
# [[0. 0.] >>> data2 = paddle.zeros(shape=shape)
# [0. 0.] >>> print(data2.numpy())
# [0. 0.]] [[0. 0.]
[0. 0.]
# shape is a Tensor List [0. 0.]]
shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
data3 = paddle.zeros(shape=shape) >>> # shape is a Tensor List
# [[0. 0.] >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
# [0. 0.] >>> data3 = paddle.zeros(shape=shape)
# [0. 0.]] >>> print(data3.numpy())
[[0. 0.]
[0. 0.]
[0. 0.]]
""" """
if dtype is None: if dtype is None:
dtype = 'float32' dtype = 'float32'
...@@ -1110,11 +1127,15 @@ def zeros_like(x, dtype=None, name=None): ...@@ -1110,11 +1127,15 @@ def zeros_like(x, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1, 2, 3]) >>> x = paddle.to_tensor([1, 2, 3])
out1 = paddle.zeros_like(x) # [0., 0., 0.] >>> out1 = paddle.zeros_like(x)
out2 = paddle.zeros_like(x, dtype='int32') # [0, 0, 0] >>> print(out1.numpy())
[0 0 0]
>>> out2 = paddle.zeros_like(x, dtype='int32')
>>> print(out2.numpy())
[0 0 0]
""" """
return full_like(x=x, fill_value=0, dtype=dtype, name=name) return full_like(x=x, fill_value=0, dtype=dtype, name=name)
...@@ -1140,15 +1161,17 @@ def eye(num_rows, num_columns=None, dtype=None, name=None): ...@@ -1140,15 +1161,17 @@ def eye(num_rows, num_columns=None, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.eye(3, dtype='int32') >>> data = paddle.eye(3, dtype='int32')
# [[1 0 0] >>> print(data.numpy())
# [0 1 0] [[1 0 0]
# [0 0 1]] [0 1 0]
data = paddle.eye(2, 3, dtype='int32') [0 0 1]]
# [[1 0 0] >>> data = paddle.eye(2, 3, dtype='int32')
# [0 1 0]] >>> print(data.numpy())
[[1 0 0]
[0 1 0]]
""" """
def _check_attr(attr, message): def _check_attr(attr, message):
...@@ -1219,34 +1242,38 @@ def full(shape, fill_value, dtype=None, name=None): ...@@ -1219,34 +1242,38 @@ def full(shape, fill_value, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# shape is a list/tuple >>> # shape is a list/tuple
data1 = paddle.full(shape=[3, 2], fill_value=1.) >>> data1 = paddle.full(shape=[3, 2], fill_value=1.)
# [[1. 1.] >>> print(data1.numpy())
# [1. 1.] [[1. 1.]
# [1. 1.]] [1. 1.]
[1. 1.]]
# shape is a Tensor
shape = paddle.to_tensor([3, 2]) >>> # shape is a Tensor
data2 = paddle.full(shape=shape, fill_value=2.) >>> shape = paddle.to_tensor([3, 2])
# [[2. 2.] >>> data2 = paddle.full(shape=shape, fill_value=2.)
# [2. 2.] >>> print(data2.numpy())
# [2. 2.]] [[2. 2.]
[2. 2.]
# shape is a Tensor List [2. 2.]]
shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
data3 = paddle.full(shape=shape, fill_value=3.) >>> # shape is a Tensor List
# [[3. 3.] >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
# [3. 3.] >>> data3 = paddle.full(shape=shape, fill_value=3.)
# [3. 3.]] >>> print(data3.numpy())
[[3. 3.]
# fill_value is a Tensor. [3. 3.]
val = paddle.full([], 2.0, "float32") [3. 3.]]
data5 = paddle.full(shape=[3, 2], fill_value=val)
# [[2. 2.] >>> # fill_value is a Tensor.
# [2. 2.] >>> val = paddle.full([], 2.0, "float32")
# [2. 2.]] >>> data5 = paddle.full(shape=[3, 2], fill_value=val)
>>> print(data5.numpy())
[[2. 2.]
[2. 2.]
[2. 2.]]
""" """
if dtype is None: if dtype is None:
...@@ -1292,21 +1319,25 @@ def arange(start=0, end=None, step=1, dtype=None, name=None): ...@@ -1292,21 +1319,25 @@ def arange(start=0, end=None, step=1, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
out1 = paddle.arange(5) >>> out1 = paddle.arange(5)
# [0, 1, 2, 3, 4] >>> print(out1.numpy())
[0 1 2 3 4]
out2 = paddle.arange(3, 9, 2.0) >>> out2 = paddle.arange(3, 9, 2.0)
# [3, 5, 7] >>> print(out2.numpy())
[3. 5. 7.]
# use 4.999 instead of 5.0 to avoid floating point rounding errors >>> # use 4.999 instead of 5.0 to avoid floating point rounding errors
out3 = paddle.arange(4.999, dtype='float32') >>> out3 = paddle.arange(4.999, dtype='float32')
# [0., 1., 2., 3., 4.] >>> print(out3.numpy())
[0. 1. 2. 3. 4.]
start_var = paddle.to_tensor(3) >>> start_var = paddle.to_tensor(3)
out4 = paddle.arange(start_var, 7) >>> out4 = paddle.arange(start_var, 7)
# [3, 4, 5, 6] >>> print(out4.numpy())
[3 4 5 6]
""" """
if end is None: if end is None:
...@@ -1444,33 +1475,37 @@ def tril(x, diagonal=0, name=None): ...@@ -1444,33 +1475,37 @@ def tril(x, diagonal=0, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.arange(1, 13, dtype="int64").reshape([3,-1]) >>> data = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> print(data)
# [[1 , 2 , 3 , 4 ], Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [5 , 6 , 7 , 8 ], [[1 , 2 , 3 , 4 ],
# [9 , 10, 11, 12]]) [5 , 6 , 7 , 8 ],
[9 , 10, 11, 12]])
tril1 = paddle.tril(data)
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> tril1 = paddle.tril(data)
# [[1 , 0 , 0 , 0 ], >>> print(tril1)
# [5 , 6 , 0 , 0 ], Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [9 , 10, 11, 0 ]]) [[1 , 0 , 0 , 0 ],
[5 , 6 , 0 , 0 ],
# example 2, positive diagonal value [9 , 10, 11, 0 ]])
tril2 = paddle.tril(data, diagonal=2)
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> # example 2, positive diagonal value
# [[1 , 2 , 3 , 0 ], >>> tril2 = paddle.tril(data, diagonal=2)
# [5 , 6 , 7 , 8 ], >>> print(tril2)
# [9 , 10, 11, 12]]) Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1 , 2 , 3 , 0 ],
# example 3, negative diagonal value [5 , 6 , 7 , 8 ],
tril3 = paddle.tril(data, diagonal=-1) [9 , 10, 11, 12]])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0 , 0 , 0 , 0 ], >>> # example 3, negative diagonal value
# [5 , 0 , 0 , 0 ], >>> tril3 = paddle.tril(data, diagonal=-1)
# [9 , 10, 0 , 0 ]]) >>> print(tril3)
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 0 , 0 , 0 ],
[5 , 0 , 0 , 0 ],
[9 , 10, 0 , 0 ]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.tril(x, diagonal) return _C_ops.tril(x, diagonal)
...@@ -1515,34 +1550,38 @@ def triu(x, diagonal=0, name=None): ...@@ -1515,34 +1550,38 @@ def triu(x, diagonal=0, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(1, 13, dtype="int64").reshape([3,-1]) >>> x = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> print(x)
# [[1 , 2 , 3 , 4 ], Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [5 , 6 , 7 , 8 ], [[1 , 2 , 3 , 4 ],
# [9 , 10, 11, 12]]) [5 , 6 , 7 , 8 ],
[9 , 10, 11, 12]])
# example 1, default diagonal
triu1 = paddle.tensor.triu(x) >>> # example 1, default diagonal
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> triu1 = paddle.tensor.triu(x)
# [[1 , 2 , 3 , 4 ], >>> print(triu1)
# [0 , 6 , 7 , 8 ], Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [0 , 0 , 11, 12]]) [[1 , 2 , 3 , 4 ],
[0 , 6 , 7 , 8 ],
# example 2, positive diagonal value [0 , 0 , 11, 12]])
triu2 = paddle.tensor.triu(x, diagonal=2)
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> # example 2, positive diagonal value
# [[0, 0, 3, 4], >>> triu2 = paddle.tensor.triu(x, diagonal=2)
# [0, 0, 0, 8], >>> print(triu2)
# [0, 0, 0, 0]]) Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0, 3, 4],
# example 3, negative diagonal value [0, 0, 0, 8],
triu3 = paddle.tensor.triu(x, diagonal=-1) [0, 0, 0, 0]])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1 , 2 , 3 , 4 ], >>> # example 3, negative diagonal value
# [5 , 6 , 7 , 8 ], >>> triu3 = paddle.tensor.triu(x, diagonal=-1)
# [0 , 10, 11, 12]]) >>> print(triu3)
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1 , 2 , 3 , 4 ],
[5 , 6 , 7 , 8 ],
[0 , 10, 11, 12]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
...@@ -1578,20 +1617,19 @@ def meshgrid(*args, **kwargs): ...@@ -1578,20 +1617,19 @@ def meshgrid(*args, **kwargs):
Tensor: k tensors. The shape of each tensor is (N1, N2, ..., Nk) Tensor: k tensors. The shape of each tensor is (N1, N2, ..., Nk)
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
x = paddle.randint(low=0, high=100, shape=[100]) >>> import paddle
y = paddle.randint(low=0, high=100, shape=[200])
grid_x, grid_y = paddle.meshgrid(x, y) >>> x = paddle.randint(low=0, high=100, shape=[100])
>>> y = paddle.randint(low=0, high=100, shape=[200])
print(grid_x.shape) >>> grid_x, grid_y = paddle.meshgrid(x, y)
print(grid_y.shape)
#the shape of res_1 is (100, 200) >>> print(grid_x.shape)
#the shape of res_2 is (100, 200) [100, 200]
>>> print(grid_y.shape)
[100, 200]
""" """
...@@ -1655,63 +1693,63 @@ def diagflat(x, offset=0, name=None): ...@@ -1655,63 +1693,63 @@ def diagflat(x, offset=0, name=None):
.. code-block:: python .. code-block:: python
:name: code-example-1 :name: code-example-1
import paddle >>> import paddle
x = paddle.to_tensor([1, 2, 3]) >>> x = paddle.to_tensor([1, 2, 3])
y = paddle.diagflat(x) >>> y = paddle.diagflat(x)
print(y) >>> print(y)
# Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 0, 0], [[1, 0, 0],
# [0, 2, 0], [0, 2, 0],
# [0, 0, 3]]) [0, 0, 3]])
y = paddle.diagflat(x, offset=1) >>> y = paddle.diagflat(x, offset=1)
print(y) >>> print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 1, 0, 0], [[0, 1, 0, 0],
# [0, 0, 2, 0], [0, 0, 2, 0],
# [0, 0, 0, 3], [0, 0, 0, 3],
# [0, 0, 0, 0]]) [0, 0, 0, 0]])
y = paddle.diagflat(x, offset=-1) >>> y = paddle.diagflat(x, offset=-1)
print(y) >>> print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 0, 0, 0], [[0, 0, 0, 0],
# [1, 0, 0, 0], [1, 0, 0, 0],
# [0, 2, 0, 0], [0, 2, 0, 0],
# [0, 0, 3, 0]]) [0, 0, 3, 0]])
.. code-block:: python .. code-block:: python
:name: code-example-2 :name: code-example-2
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [3, 4]]) >>> x = paddle.to_tensor([[1, 2], [3, 4]])
y = paddle.diagflat(x) >>> y = paddle.diagflat(x)
print(y) >>> print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 0, 0, 0], [[1, 0, 0, 0],
# [0, 2, 0, 0], [0, 2, 0, 0],
# [0, 0, 3, 0], [0, 0, 3, 0],
# [0, 0, 0, 4]]) [0, 0, 0, 4]])
y = paddle.diagflat(x, offset=1) >>> y = paddle.diagflat(x, offset=1)
print(y) >>> print(y)
# Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 1, 0, 0, 0], [[0, 1, 0, 0, 0],
# [0, 0, 2, 0, 0], [0, 0, 2, 0, 0],
# [0, 0, 0, 3, 0], [0, 0, 0, 3, 0],
# [0, 0, 0, 0, 4], [0, 0, 0, 0, 4],
# [0, 0, 0, 0, 0]]) [0, 0, 0, 0, 0]])
y = paddle.diagflat(x, offset=-1) >>> y = paddle.diagflat(x, offset=-1)
print(y) >>> print(y)
# Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 0, 0, 0, 0], [[0, 0, 0, 0, 0],
# [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],
# [0, 2, 0, 0, 0], [0, 2, 0, 0, 0],
# [0, 0, 3, 0, 0], [0, 0, 3, 0, 0],
# [0, 0, 0, 4, 0]]) [0, 0, 0, 4, 0]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
if len(x.shape) <= 1: if len(x.shape) <= 1:
...@@ -1788,53 +1826,53 @@ def diag(x, offset=0, padding_value=0, name=None): ...@@ -1788,53 +1826,53 @@ def diag(x, offset=0, padding_value=0, name=None):
.. code-block:: python .. code-block:: python
:name: code-example-1 :name: code-example-1
import paddle >>> import paddle
paddle.disable_static() >>> paddle.disable_static()
x = paddle.to_tensor([1, 2, 3]) >>> x = paddle.to_tensor([1, 2, 3])
y = paddle.diag(x) >>> y = paddle.diag(x)
print(y) >>> print(y)
# Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 0, 0], [[1, 0, 0],
# [0, 2, 0], [0, 2, 0],
# [0, 0, 3]]) [0, 0, 3]])
y = paddle.diag(x, offset=1) >>> y = paddle.diag(x, offset=1)
print(y) >>> print(y)
# Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[0, 1, 0, 0], [[0, 1, 0, 0],
# [0, 0, 2, 0], [0, 0, 2, 0],
# [0, 0, 0, 3], [0, 0, 0, 3],
# [0, 0, 0, 0]]) [0, 0, 0, 0]])
y = paddle.diag(x, padding_value=6) >>> y = paddle.diag(x, padding_value=6)
print(y) >>> print(y)
# Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
# [[1, 6, 6], [[1, 6, 6],
# [6, 2, 6], [6, 2, 6],
# [6, 6, 3]]) [6, 6, 3]])
.. code-block:: python .. code-block:: python
:name: code-example-2 :name: code-example-2
import paddle >>> import paddle
paddle.disable_static() >>> paddle.disable_static()
x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
y = paddle.diag(x) >>> y = paddle.diag(x)
print(y) >>> print(y)
# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [1, 5]) [1, 5])
y = paddle.diag(x, offset=1) >>> y = paddle.diag(x, offset=1)
print(y) >>> print(y)
# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
# [2, 6]) [2, 6])
y = paddle.diag(x, offset=-1) >>> y = paddle.diag(x, offset=-1)
print(y) >>> print(y)
# Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
# [4]) [4])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.diag(x, offset, padding_value) return _C_ops.diag(x, offset, padding_value)
...@@ -1890,27 +1928,33 @@ def empty(shape, dtype=None, name=None): ...@@ -1890,27 +1928,33 @@ def empty(shape, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# shape is a list/tuple >>> # shape is a list/tuple
data1 = paddle.empty(shape=[3, 2]) >>> data1 = paddle.empty(shape=[3, 2])
# [[1. 1.] >>> print(data1.numpy())
# [1. 1.] >>> # doctest: +SKIP('change everytime')
# [1. 1.]] [[1. 1.]
[1. 1.]
# shape is a Tensor [1. 1.]]
shape = paddle.to_tensor([3, 2])
data2 = paddle.empty(shape=shape) >>> # shape is a Tensor
# [[1. 1.] >>> shape = paddle.to_tensor([3, 2])
# [1. 1.] >>> data2 = paddle.empty(shape=shape)
# [1. 1.]] >>> print(data2.numpy())
>>> # doctest: +SKIP('change everytime')
# shape is a Tensor List [[1. 1.]
shape = [paddle.to_tensor(3), paddle.to_tensor(2)] [1. 1.]
data3 = paddle.empty(shape=shape) [1. 1.]]
# [[1. 1.]
# [1. 1.] >>> # shape is a Tensor List
# [1. 1.]] >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
>>> data3 = paddle.empty(shape=shape)
>>> print(data3.numpy())
>>> # doctest: +SKIP('change everytime')
[[1. 1.]
[1. 1.]
[1. 1.]]
""" """
if dtype is None: if dtype is None:
...@@ -1985,14 +2029,16 @@ def empty_like(x, dtype=None, name=None): ...@@ -1985,14 +2029,16 @@ def empty_like(x, dtype=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.set_device("cpu") # and use cpu device >>> paddle.set_device("cpu") # and use cpu device
x = paddle.randn([2, 3], 'float32') >>> x = paddle.randn([2, 3], 'float32')
output = paddle.empty_like(x) >>> output = paddle.empty_like(x)
#[[1.8491974e+20 1.8037303e+28 1.7443726e+28] # uninitialized >>> print(output)
# [4.9640171e+28 3.0186127e+32 5.6715899e-11]] # uninitialized >>> # doctest: +SKIP('change everytime')
[[1.8491974e+20 1.8037303e+28 1.7443726e+28]
[4.9640171e+28 3.0186127e+32 5.6715899e-11]]
""" """
if dtype is None: if dtype is None:
...@@ -2075,16 +2121,32 @@ def assign(x, output=None): ...@@ -2075,16 +2121,32 @@ def assign(x, output=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import numpy as np >>> import numpy as np
data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64') # [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] >>> data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64')
array = np.array([[1, 1], >>> print(data.numpy())
[3, 4], [[2.5 2.5]
[1, 3]]).astype(np.int64) [2.5 2.5]
result1 = paddle.zeros(shape=[3, 3], dtype='float32') [2.5 2.5]]
paddle.assign(array, result1) # result1 = [[1, 1], [3 4], [1, 3]] >>> array = np.array([[1, 1],
result2 = paddle.assign(data) # result2 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] ... [3, 4],
result3 = paddle.assign(np.array([[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]], dtype='float32')) # result3 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] ... [1, 3]]).astype(np.int64)
>>> result1 = paddle.zeros(shape=[3, 3], dtype='float32')
>>> paddle.assign(array, result1)
>>> print(result1.numpy())
[[1 1]
[3 4]
[1 3]]
>>> result2 = paddle.assign(data)
>>> print(result2.numpy())
[[2.5 2.5]
[2.5 2.5]
[2.5 2.5]]
>>> result3 = paddle.assign(np.array([[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]], dtype='float32'))
>>> print(result3.numpy())
[[2.5 2.5]
[2.5 2.5]
[2.5 2.5]]
""" """
# speed up # speed up
if x is output and isinstance(x, Variable): if x is output and isinstance(x, Variable):
...@@ -2245,16 +2307,21 @@ def clone(x, name=None): ...@@ -2245,16 +2307,21 @@ def clone(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
>>> import numpy as np
x = paddle.ones([2])
x.stop_gradient = False >>> x = paddle.ones([2])
clone_x = paddle.clone(x) >>> x.stop_gradient = False
>>> x.retain_grads()
y = clone_x**3 >>> clone_x = paddle.clone(x)
y.backward() >>> clone_x.retain_grads()
print(clone_x.grad) # [3]
print(x.grad) # [3] >>> y = clone_x**3
>>> y.backward()
>>> print(clone_x.grad.numpy())
[3. 3.]
>>> print(x.grad.numpy())
[3. 3.]
""" """
return x.clone() return x.clone()
...@@ -2278,10 +2345,19 @@ def _memcpy(input, place=None, output=None): ...@@ -2278,10 +2345,19 @@ def _memcpy(input, place=None, output=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64') # [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] >>> data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64')
result = paddle._memcpy(data, place=paddle.CPUPlace()) # result2 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] >>> print(data.numpy())
[[2.5 2.5]
[2.5 2.5]
[2.5 2.5]]
>>> # doctest: +SKIP('NOTE(zhiqiu): not public')
>>> result = paddle._memcpy(data, place=paddle.CPUPlace())
>>> print(result2)
[[2.5 2.5]
[2.5 2.5]
[2.5 2.5]]
""" """
helper = LayerHelper('memcpy', **locals()) helper = LayerHelper('memcpy', **locals())
check_type(input, 'input', (Variable), 'memcpy') check_type(input, 'input', (Variable), 'memcpy')
...@@ -2351,14 +2427,14 @@ def complex(real, imag, name=None): ...@@ -2351,14 +2427,14 @@ def complex(real, imag, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(2, dtype=paddle.float32).unsqueeze(-1) >>> x = paddle.arange(2, dtype=paddle.float32).unsqueeze(-1)
y = paddle.arange(3, dtype=paddle.float32) >>> y = paddle.arange(3, dtype=paddle.float32)
z = paddle.complex(x, y) >>> z = paddle.complex(x, y)
print(z) >>> print(z)
# Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True, Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [[0j , 1j , 2j ], [[0j , 1j , 2j ],
# [(1+0j), (1+1j), (1+2j)]]) [(1+0j), (1+1j), (1+2j)]])
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.complex(real, imag) return _C_ops.complex(real, imag)
...@@ -2409,25 +2485,28 @@ def tril_indices(row, col, offset=0, dtype='int64'): ...@@ -2409,25 +2485,28 @@ def tril_indices(row, col, offset=0, dtype='int64'):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# example 1, default offset value >>> # example 1, default offset value
data1 = paddle.tril_indices(4,4,0) >>> data1 = paddle.tril_indices(4,4,0)
print(data1) >>> print(data1)
# [[0, 1, 1, 2, 2, 2, 3, 3, 3, 3], Tensor(shape=[2, 10], dtype=int64, place=Place(cpu), stop_gradient=True,
# [0, 0, 1, 0, 1, 2, 0, 1, 2, 3]] [[0, 1, 1, 2, 2, 2, 3, 3, 3, 3],
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]])
# example 2, positive offset value
data2 = paddle.tril_indices(4,4,2) >>> # example 2, positive offset value
print(data2) >>> data2 = paddle.tril_indices(4,4,2)
# [[0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], >>> print(data2)
# [0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]] Tensor(shape=[2, 15], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
# example 3, negative offset value [0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]])
data3 = paddle.tril_indices(4,4,-1)
print(data3) >>> # example 3, negative offset value
# [[ 1, 2, 2, 3, 3, 3], >>> data3 = paddle.tril_indices(4,4,-1)
# [ 0, 0, 1, 0, 1, 2]] >>> print(data3)
Tensor(shape=[2, 6], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1, 2, 2, 3, 3, 3],
[0, 0, 1, 0, 1, 2]])
""" """
if not isinstance(dtype, core.VarDesc.VarType): if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype) dtype = convert_np_dtype_to_dtype_(dtype)
...@@ -2491,22 +2570,22 @@ def triu_indices(row, col=None, offset=0, dtype='int64'): ...@@ -2491,22 +2570,22 @@ def triu_indices(row, col=None, offset=0, dtype='int64'):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
# example 1, default offset value >>> # example 1, default offset value
data1 = paddle.triu_indices(4,4,0) >>> data1 = paddle.triu_indices(4,4,0)
print(data1) >>> print(data1.numpy())
# [[0, 0, 0, 0, 1, 1, 1, 2, 2, 3], [[0 0 0 0 1 1 1 2 2 3]
# [0, 1, 2, 3, 1, 2, 3, 2, 3, 3]] [0 1 2 3 1 2 3 2 3 3]]
# example 2, positive offset value >>> # example 2, positive offset value
data2 = paddle.triu_indices(4,4,2) >>> data2 = paddle.triu_indices(4,4,2)
print(data2) >>> print(data2.numpy())
# [[0, 0, 1], [[0 0 1]
# [2, 3, 3]] [2 3 3]]
# example 3, negative offset value >>> # example 3, negative offset value
data3 = paddle.triu_indices(4,4,-1) >>> data3 = paddle.triu_indices(4,4,-1)
print(data3) >>> print(data3.numpy())
# [[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3], [[0 0 0 0 1 1 1 1 2 2 2 3 3]
# [0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 2, 3]] [0 1 2 3 0 1 2 3 1 2 3 2 3]]
""" """
if not isinstance(dtype, core.VarDesc.VarType): if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype) dtype = convert_np_dtype_to_dtype_(dtype)
...@@ -2563,16 +2642,16 @@ def polar(abs, angle, name=None): ...@@ -2563,16 +2642,16 @@ def polar(abs, angle, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import numpy as np >>> import numpy as np
abs = paddle.to_tensor([1, 2], dtype=paddle.float64) >>> abs = paddle.to_tensor([1, 2], dtype=paddle.float64)
angle = paddle.to_tensor([np.pi / 2, 5 * np.pi / 4], dtype=paddle.float64) >>> angle = paddle.to_tensor([np.pi / 2, 5 * np.pi / 4], dtype=paddle.float64)
out = paddle.polar(abs, angle) >>> out = paddle.polar(abs, angle)
print(out) >>> print(out)
# Tensor(shape=[2], dtype=complex128, place=Place(cpu), stop_gradient=True, Tensor(shape=[2], dtype=complex128, place=Place(cpu), stop_gradient=True,
# [ (6.123233995736766e-17+1j) , [ (6.123233995736766e-17+1j) ,
# (-1.4142135623730954-1.414213562373095j)]) (-1.4142135623730954-1.414213562373095j)])
""" """
check_variable_and_dtype(abs, 'abs', ['float32', 'float64'], 'paddle.polar') check_variable_and_dtype(abs, 'abs', ['float32', 'float64'], 'paddle.polar')
check_variable_and_dtype( check_variable_and_dtype(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册