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