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

[xdoctest][task 198-199] reformat example code with google style in `tensor/*` (#56186)

* [Doctest]fix No.198-199, test=docs_preview

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

* abandon fluid

* fix ()

* fix code
上级 c65ef07c
...@@ -34,16 +34,17 @@ def array_length(array): ...@@ -34,16 +34,17 @@ def array_length(array):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
arr = paddle.tensor.create_array(dtype='float32') >>> arr = paddle.tensor.create_array(dtype='float32')
x = paddle.full(shape=[3, 3], fill_value=5, dtype="float32") >>> x = paddle.full(shape=[3, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32") >>> i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr) >>> arr = paddle.tensor.array_write(x, i, array=arr)
arr_len = paddle.tensor.array_length(arr) >>> arr_len = paddle.tensor.array_length(arr)
print(arr_len) # 1 >>> print(arr_len)
1
""" """
if in_dynamic_mode(): if in_dynamic_mode():
assert isinstance( assert isinstance(
...@@ -98,16 +99,17 @@ def array_read(array, i): ...@@ -98,16 +99,17 @@ def array_read(array, i):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
arr = paddle.tensor.create_array(dtype="float32") >>> arr = paddle.tensor.create_array(dtype="float32")
x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32") >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32") >>> i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr) >>> arr = paddle.tensor.array_write(x, i, array=arr)
item = paddle.tensor.array_read(arr, i) >>> item = paddle.tensor.array_read(arr, i)
print(item) # [[5., 5., 5.]] >>> print(item.numpy())
[[5. 5. 5.]]
""" """
if in_dynamic_mode(): if in_dynamic_mode():
assert isinstance( assert isinstance(
...@@ -158,16 +160,17 @@ def array_write(x, i, array=None): ...@@ -158,16 +160,17 @@ def array_write(x, i, array=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
arr = paddle.tensor.create_array(dtype="float32") >>> arr = paddle.tensor.create_array(dtype="float32")
x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32") >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32") >>> i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr) >>> arr = paddle.tensor.array_write(x, i, array=arr)
item = paddle.tensor.array_read(arr, i) >>> item = paddle.tensor.array_read(arr, i)
print(item) # [[5., 5., 5.]] >>> print(item.numpy())
[[5. 5. 5.]]
""" """
if in_dynamic_mode(): if in_dynamic_mode():
assert isinstance( assert isinstance(
...@@ -236,16 +239,17 @@ def create_array(dtype, initialized_list=None): ...@@ -236,16 +239,17 @@ def create_array(dtype, initialized_list=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
arr = paddle.tensor.create_array(dtype="float32") >>> arr = paddle.tensor.create_array(dtype="float32")
x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32") >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32") >>> i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr) >>> arr = paddle.tensor.array_write(x, i, array=arr)
item = paddle.tensor.array_read(arr, i) >>> item = paddle.tensor.array_read(arr, i)
print(item) # [[5., 5., 5.]] >>> print(item.numpy())
[[5. 5. 5.]]
""" """
array = [] array = []
......
...@@ -42,12 +42,12 @@ def rank(input): ...@@ -42,12 +42,12 @@ def rank(input):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
input = paddle.rand((3, 100, 100)) >>> input = paddle.rand((3, 100, 100))
rank = paddle.rank(input) >>> rank = paddle.rank(input)
print(rank) >>> print(rank.numpy())
# 3 3
""" """
check_type(input, 'input', (Variable), 'input') check_type(input, 'input', (Variable), 'input')
ndims = len(input.shape) ndims = len(input.shape)
...@@ -87,21 +87,21 @@ def shape(input): ...@@ -87,21 +87,21 @@ def shape(input):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid >>> import numpy as np
import numpy as np >>> import paddle
import paddle >>> paddle.enable_static()
paddle.enable_static()
inputs = paddle.static.data(name="x", shape=[3, 100, 100], dtype="float32") >>> inputs = paddle.static.data(name="x", shape=[3, 100, 100], dtype="float32")
output = paddle.shape(inputs) >>> output = paddle.shape(inputs)
exe = fluid.Executor(fluid.CPUPlace()) >>> exe = paddle.static.Executor(paddle.CPUPlace())
exe.run(fluid.default_startup_program()) >>> exe.run(paddle.static.default_startup_program())
img = np.ones((3, 100, 100)).astype(np.float32) >>> img = np.ones((3, 100, 100)).astype(np.float32)
res = exe.run(fluid.default_main_program(), feed={'x':img}, fetch_list=[output]) >>> res = exe.run(paddle.static.default_main_program(), feed={'x':img}, fetch_list=[output])
print(res) # [array([ 3, 100, 100], dtype=int32)] >>> print(res)
[array([ 3, 100, 100], dtype=int32)]
""" """
if in_dygraph_mode(): if in_dygraph_mode():
out = _C_ops.shape(input) out = _C_ops.shape(input)
...@@ -149,19 +149,19 @@ def is_complex(x): ...@@ -149,19 +149,19 @@ def is_complex(x):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1 + 2j, 3 + 4j]) >>> x = paddle.to_tensor([1 + 2j, 3 + 4j])
print(paddle.is_complex(x)) >>> print(paddle.is_complex(x))
# True True
x = paddle.to_tensor([1.1, 1.2]) >>> x = paddle.to_tensor([1.1, 1.2])
print(paddle.is_complex(x)) >>> print(paddle.is_complex(x))
# False False
x = paddle.to_tensor([1, 2, 3]) >>> x = paddle.to_tensor([1, 2, 3])
print(paddle.is_complex(x)) >>> print(paddle.is_complex(x))
# False False
""" """
if not isinstance(x, (paddle.Tensor, paddle.static.Variable)): if not isinstance(x, (paddle.Tensor, paddle.static.Variable)):
raise TypeError(f"Expected Tensor, but received type of x: {type(x)}") raise TypeError(f"Expected Tensor, but received type of x: {type(x)}")
...@@ -186,14 +186,14 @@ def is_floating_point(x): ...@@ -186,14 +186,14 @@ def is_floating_point(x):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(1., 5., dtype='float32') >>> x = paddle.arange(1., 5., dtype='float32')
y = paddle.arange(1, 5, dtype='int32') >>> y = paddle.arange(1, 5, dtype='int32')
print(paddle.is_floating_point(x)) >>> print(paddle.is_floating_point(x))
# True True
print(paddle.is_floating_point(y)) >>> print(paddle.is_floating_point(y))
# False False
""" """
if not isinstance(x, (paddle.Tensor, paddle.static.Variable)): if not isinstance(x, (paddle.Tensor, paddle.static.Variable)):
raise TypeError(f"Expected Tensor, but received type of x: {type(x)}") raise TypeError(f"Expected Tensor, but received type of x: {type(x)}")
...@@ -219,19 +219,19 @@ def is_integer(x): ...@@ -219,19 +219,19 @@ def is_integer(x):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([1 + 2j, 3 + 4j]) >>> x = paddle.to_tensor([1 + 2j, 3 + 4j])
print(paddle.is_integer(x)) >>> print(paddle.is_integer(x))
# False False
x = paddle.to_tensor([1.1, 1.2]) >>> x = paddle.to_tensor([1.1, 1.2])
print(paddle.is_integer(x)) >>> print(paddle.is_integer(x))
# False False
x = paddle.to_tensor([1, 2, 3]) >>> x = paddle.to_tensor([1, 2, 3])
print(paddle.is_integer(x)) >>> print(paddle.is_integer(x))
# True True
""" """
if not isinstance(x, (paddle.Tensor, paddle.static.Variable)): if not isinstance(x, (paddle.Tensor, paddle.static.Variable)):
raise TypeError(f"Expected Tensor, but received type of x: {type(x)}") raise TypeError(f"Expected Tensor, but received type of x: {type(x)}")
...@@ -261,23 +261,26 @@ def real(x, name=None): ...@@ -261,23 +261,26 @@ def real(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor( >>> x = paddle.to_tensor(
[[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]]) ... [[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]])
# Tensor(shape=[2, 3], dtype=complex64, place=CUDAPlace(0), stop_gradient=True, >>> print(x)
# [[(1+6j), (2+5j), (3+4j)], Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [(4+3j), (5+2j), (6+1j)]]) [[(1+6j), (2+5j), (3+4j)],
[(4+3j), (5+2j), (6+1j)]])
real_res = paddle.real(x)
# Tensor(shape=[2, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> real_res = paddle.real(x)
# [[1., 2., 3.], >>> print(real_res)
# [4., 5., 6.]]) Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 2., 3.],
real_t = x.real() [4., 5., 6.]])
# Tensor(shape=[2, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# [[1., 2., 3.], >>> real_t = x.real()
# [4., 5., 6.]]) >>> print(real_t)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 2., 3.],
[4., 5., 6.]])
""" """
if in_dygraph_mode(): if in_dygraph_mode():
return _C_ops.real(x) return _C_ops.real(x)
...@@ -306,23 +309,26 @@ def imag(x, name=None): ...@@ -306,23 +309,26 @@ def imag(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor( >>> x = paddle.to_tensor(
[[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]]) ... [[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]])
# Tensor(shape=[2, 3], dtype=complex64, place=CUDAPlace(0), stop_gradient=True, >>> print(x)
# [[(1+6j), (2+5j), (3+4j)], Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
# [(4+3j), (5+2j), (6+1j)]]) [[(1+6j), (2+5j), (3+4j)],
[(4+3j), (5+2j), (6+1j)]])
imag_res = paddle.imag(x)
# Tensor(shape=[2, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, >>> imag_res = paddle.imag(x)
# [[6., 5., 4.], >>> print(imag_res)
# [3., 2., 1.]]) Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[6., 5., 4.],
imag_t = x.imag() [3., 2., 1.]])
# Tensor(shape=[2, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
# [[6., 5., 4.], >>> imag_t = x.imag()
# [3., 2., 1.]]) >>> print(imag_t)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[6., 5., 4.],
[3., 2., 1.]])
""" """
if in_dygraph_mode(): if in_dygraph_mode():
return _C_ops.imag(x) return _C_ops.imag(x)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册