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

[xdoctest][task 272-273,282-283] reformat example code with google style...

[xdoctest][task 272-273,282-283] reformat example code with google style in`utils/*`, `tensor/*` (#56438)

* [Doctest]fix No.272-273,282-283, test=docs_preview

* fix number

* fix numpy

* fix xd style
上级 2d345148
...@@ -56,28 +56,32 @@ def mean(x, axis=None, keepdim=False, name=None): ...@@ -56,28 +56,32 @@ def mean(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[[1., 2., 3., 4.], >>> x = paddle.to_tensor([[[1., 2., 3., 4.],
[5., 6., 7., 8.], ... [5., 6., 7., 8.],
[9., 10., 11., 12.]], ... [9., 10., 11., 12.]],
[[13., 14., 15., 16.], ... [[13., 14., 15., 16.],
[17., 18., 19., 20.], ... [17., 18., 19., 20.],
[21., 22., 23., 24.]]]) ... [21., 22., 23., 24.]]])
out1 = paddle.mean(x) >>> out1 = paddle.mean(x)
# 12.5 >>> print(out1.numpy())
out2 = paddle.mean(x, axis=-1) 12.5
# [[ 2.5 6.5 10.5] >>> out2 = paddle.mean(x, axis=-1)
# [14.5 18.5 22.5]] >>> print(out2.numpy())
out3 = paddle.mean(x, axis=-1, keepdim=True) [[ 2.5 6.5 10.5]
# [[[ 2.5] [14.5 18.5 22.5]]
# [ 6.5] >>> out3 = paddle.mean(x, axis=-1, keepdim=True)
# [10.5]] >>> print(out3.numpy())
# [[14.5] [[[ 2.5]
# [18.5] [ 6.5]
# [22.5]]] [10.5]]
out4 = paddle.mean(x, axis=[0, 2]) [[14.5]
# [ 8.5 12.5 16.5] [18.5]
[22.5]]]
>>> out4 = paddle.mean(x, axis=[0, 2])
>>> print(out4.numpy())
[ 8.5 12.5 16.5]
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.mean(x, axis, keepdim) return _C_ops.mean(x, axis, keepdim)
...@@ -138,13 +142,15 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None): ...@@ -138,13 +142,15 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]]) >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]])
out1 = paddle.var(x) >>> out1 = paddle.var(x)
# 2.66666667 >>> print(out1.numpy())
out2 = paddle.var(x, axis=1) 2.6666667
# [1. 4.33333333] >>> out2 = paddle.var(x, axis=1)
>>> print(out2.numpy())
[1. 4.3333335]
""" """
if not in_dynamic_mode(): if not in_dynamic_mode():
check_variable_and_dtype( check_variable_and_dtype(
...@@ -203,15 +209,18 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None): ...@@ -203,15 +209,18 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]]) >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]])
out1 = paddle.std(x) >>> out1 = paddle.std(x)
# 1.63299316 >>> print(out1.numpy())
out2 = paddle.std(x, unbiased=False) 1.6329932
# 1.49071205 >>> out2 = paddle.std(x, unbiased=False)
out3 = paddle.std(x, axis=1) >>> print(out2.numpy())
# [1. 2.081666] 1.490712
>>> out3 = paddle.std(x, axis=1)
>>> print(out3.numpy())
[1. 2.081666]
""" """
if not in_dynamic_mode(): if not in_dynamic_mode():
...@@ -237,10 +246,12 @@ def numel(x, name=None): ...@@ -237,10 +246,12 @@ def numel(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.full(shape=[4, 5, 7], fill_value=0, dtype='int32') >>> x = paddle.full(shape=[4, 5, 7], fill_value=0, dtype='int32')
numel = paddle.numel(x) # 140 >>> numel = paddle.numel(x)
>>> print(numel.numpy())
140
""" """
...@@ -285,20 +296,24 @@ def nanmedian(x, axis=None, keepdim=False, name=None): ...@@ -285,20 +296,24 @@ def nanmedian(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[float('nan'), 2. , 3. ], [0. , 1. , 2. ]]) >>> x = paddle.to_tensor([[float('nan'), 2. , 3. ], [0. , 1. , 2. ]])
y1 = x.nanmedian() >>> y1 = x.nanmedian()
# y1 is 2. >>> print(y1.numpy())
2.0
y2 = x.nanmedian(0) >>> y2 = x.nanmedian(0)
# y2 is [0., 1.5, 2.5] >>> print(y2.numpy())
[0. 1.5 2.5]
y3 = x.nanmedian(0, keepdim=True) >>> y3 = x.nanmedian(0, keepdim=True)
# y3 is [[0., 1.5, 2.5]] >>> print(y3.numpy())
[[0. 1.5 2.5]]
y4 = x.nanmedian((0, 1)) >>> y4 = x.nanmedian((0, 1))
# y4 is 2. >>> print(y4.numpy())
2.0
""" """
if not isinstance(x, Variable): if not isinstance(x, Variable):
raise TypeError("In median, the input x should be a Tensor.") raise TypeError("In median, the input x should be a Tensor.")
...@@ -360,29 +375,34 @@ def median(x, axis=None, keepdim=False, name=None): ...@@ -360,29 +375,34 @@ def median(x, axis=None, keepdim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.arange(12).reshape([3, 4]) >>> x = paddle.arange(12).reshape([3, 4])
# Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, >>> print(x)
# [[0 , 1 , 2 , 3 ], Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
# [4 , 5 , 6 , 7 ], [[0 , 1 , 2 , 3 ],
# [8 , 9 , 10, 11]]) [4 , 5 , 6 , 7 ],
[8 , 9 , 10, 11]])
y1 = paddle.median(x) >>> y1 = paddle.median(x)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, >>> print(y1)
# 5.50000000) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
5.50000000)
y2 = paddle.median(x, axis=0) >>> y2 = paddle.median(x, axis=0)
# Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, >>> print(y2)
# [4., 5., 6., 7.]) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[4., 5., 6., 7.])
y3 = paddle.median(x, axis=1) >>> y3 = paddle.median(x, axis=1)
# Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, >>> print(y3)
# [1.50000000, 5.50000000, 9.50000000]) Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.50000000, 5.50000000, 9.50000000])
y4 = paddle.median(x, axis=0, keepdim=True) >>> y4 = paddle.median(x, axis=0, keepdim=True)
# Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True, >>> print(y4)
# [[4., 5., 6., 7.]]) Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[4., 5., 6., 7.]])
""" """
if not isinstance(x, Variable): if not isinstance(x, Variable):
...@@ -607,35 +627,40 @@ def quantile(x, q, axis=None, keepdim=False): ...@@ -607,35 +627,40 @@ def quantile(x, q, axis=None, keepdim=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
y = paddle.arange(0, 8 ,dtype="float32").reshape([4, 2]) >>> y = paddle.arange(0, 8 ,dtype="float32").reshape([4, 2])
# Tensor(shape=[4, 2], dtype=float32, place=Place(cpu), stop_gradient=True, >>> print(y)
# [[0., 1.], Tensor(shape=[4, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [2., 3.], [[0., 1.],
# [4., 5.], [2., 3.],
# [6., 7.]]) [4., 5.],
[6., 7.]])
y1 = paddle.quantile(y, q=0.5, axis=[0, 1])
# Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, >>> y1 = paddle.quantile(y, q=0.5, axis=[0, 1])
# 3.50000000) >>> print(y1)
Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
y2 = paddle.quantile(y, q=0.5, axis=1) 3.50000000)
# Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=True,
# [0.50000000, 2.50000000, 4.50000000, 6.50000000]) >>> y2 = paddle.quantile(y, q=0.5, axis=1)
>>> print(y2)
y3 = paddle.quantile(y, q=[0.3, 0.5], axis=0) Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=True,
# Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, [0.50000000, 2.50000000, 4.50000000, 6.50000000])
# [[1.80000000, 2.80000000],
# [3. , 4. ]]) >>> y3 = paddle.quantile(y, q=[0.3, 0.5], axis=0)
>>> print(y3)
y[0,0] = float("nan") Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
y4 = paddle.quantile(y, q=0.8, axis=1, keepdim=True) [[1.80000000, 2.80000000],
# Tensor(shape=[4, 1], dtype=float64, place=Place(cpu), stop_gradient=True, [3. , 4. ]])
# [[nan ],
# [2.80000000], >>> y[0,0] = float("nan")
# [4.80000000], >>> y4 = paddle.quantile(y, q=0.8, axis=1, keepdim=True)
# [6.80000000]]) >>> print(y4)
Tensor(shape=[4, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
[[nan ],
[2.80000000],
[4.80000000],
[6.80000000]])
""" """
return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=False) return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=False)
...@@ -670,37 +695,42 @@ def nanquantile(x, q, axis=None, keepdim=False): ...@@ -670,37 +695,42 @@ def nanquantile(x, q, axis=None, keepdim=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor( >>> x = paddle.to_tensor(
[[0, 1, 2, 3, 4], ... [[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]], ... [5, 6, 7, 8, 9]],
dtype="float32") ... dtype="float32")
x[0,0] = float("nan") >>> x[0,0] = float("nan")
y1 = paddle.nanquantile(x, q=0.5, axis=[0, 1]) >>> y1 = paddle.nanquantile(x, q=0.5, axis=[0, 1])
# Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, >>> print(y1)
# 5.) Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
5.)
y2 = paddle.nanquantile(x, q=0.5, axis=1)
# Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, >>> y2 = paddle.nanquantile(x, q=0.5, axis=1)
# [2.50000000, 7. ]) >>> print(y2)
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
y3 = paddle.nanquantile(x, q=[0.3, 0.5], axis=0) [2.50000000, 7. ])
# Tensor(shape=[2, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
# [[5. , 2.50000000, 3.50000000, 4.50000000, 5.50000000], >>> y3 = paddle.nanquantile(x, q=[0.3, 0.5], axis=0)
# [5. , 3.50000000, 4.50000000, 5.50000000, 6.50000000]]) >>> print(y3)
Tensor(shape=[2, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
y4 = paddle.nanquantile(x, q=0.8, axis=1, keepdim=True) [[5. , 2.50000000, 3.50000000, 4.50000000, 5.50000000],
# Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=True, [5. , 3.50000000, 4.50000000, 5.50000000, 6.50000000]])
# [[3.40000000],
# [8.20000000]]) >>> y4 = paddle.nanquantile(x, q=0.8, axis=1, keepdim=True)
>>> print(y4)
nan = paddle.full(shape=[2, 3], fill_value=float("nan")) Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
y5 = paddle.nanquantile(nan, q=0.8, axis=1, keepdim=True) [[3.40000000],
# Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=True, [8.20000000]])
# [[nan],
# [nan]]) >>> nan = paddle.full(shape=[2, 3], fill_value=float("nan"))
>>> y5 = paddle.nanquantile(nan, q=0.8, axis=1, keepdim=True)
>>> print(y5)
Tensor(shape=[2, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
[[nan],
[nan]])
""" """
return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=True) return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=True)
...@@ -55,23 +55,20 @@ def set_printoptions( ...@@ -55,23 +55,20 @@ def set_printoptions(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.seed(10) >>> paddle.seed(10)
a = paddle.rand([10, 20]) >>> a = paddle.rand([10, 20])
paddle.set_printoptions(4, 100, 3) >>> paddle.set_printoptions(4, 100, 3)
print(a) >>> print(a)
Tensor(shape=[10, 20], dtype=float32, place=Place(cpu), stop_gradient=True,
''' [[0.2727, 0.5489, 0.8655, ..., 0.2916, 0.8525, 0.9000],
Tensor(shape=[10, 20], dtype=float32, place=CUDAPlace(0), stop_gradient=True, [0.3806, 0.8996, 0.0928, ..., 0.9535, 0.8378, 0.6409],
[[0.0002, 0.8503, 0.0135, ..., 0.9508, 0.2621, 0.6661], [0.1484, 0.4038, 0.8294, ..., 0.0148, 0.6520, 0.4250],
[0.9710, 0.2605, 0.9950, ..., 0.4427, 0.9241, 0.9363], ...,
[0.0948, 0.3226, 0.9955, ..., 0.1198, 0.0889, 0.9231], [0.3426, 0.1909, 0.7240, ..., 0.4218, 0.2676, 0.5679],
..., [0.5561, 0.2081, 0.0676, ..., 0.9778, 0.3302, 0.9559],
[0.7206, 0.0941, 0.5292, ..., 0.4856, 0.1379, 0.0351], [0.2665, 0.8483, 0.5389, ..., 0.4956, 0.6862, 0.9178]])
[0.1745, 0.5621, 0.3602, ..., 0.2998, 0.4011, 0.1764],
[0.0728, 0.7786, 0.0314, ..., 0.2583, 0.1654, 0.0637]])
'''
""" """
kwargs = {} kwargs = {}
......
...@@ -84,10 +84,10 @@ def get_weights_path_from_url(url, md5sum=None): ...@@ -84,10 +84,10 @@ def get_weights_path_from_url(url, md5sum=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
from paddle.utils.download import get_weights_path_from_url >>> from paddle.utils.download import get_weights_path_from_url
resnet18_pretrained_weight_url = 'https://paddle-hapi.bj.bcebos.com/models/resnet18.pdparams' >>> resnet18_pretrained_weight_url = 'https://paddle-hapi.bj.bcebos.com/models/resnet18.pdparams'
local_weight_path = get_weights_path_from_url(resnet18_pretrained_weight_url) >>> local_weight_path = get_weights_path_from_url(resnet18_pretrained_weight_url)
""" """
path = get_path_from_url(url, WEIGHTS_HOME, md5sum) path = get_path_from_url(url, WEIGHTS_HOME, md5sum)
......
...@@ -218,15 +218,15 @@ def run_check(): ...@@ -218,15 +218,15 @@ def run_check():
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.utils.run_check() >>> paddle.utils.run_check()
# Running verify PaddlePaddle program ... >>> # doctest: +SKIP('the output will change in different run')
# W1010 07:21:14.972093 8321 device_context.cc:338] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 11.0, Runtime API Version: 10.1 Running verify PaddlePaddle program ...
# W1010 07:21:14.979770 8321 device_context.cc:346] device: 0, cuDNN Version: 7.6. I0818 15:35:08.335391 30540 program_interpreter.cc:173] New Executor is Running.
# PaddlePaddle works well on 1 GPU. I0818 15:35:08.398319 30540 interpreter_util.cc:529] Standalone Executor is Used.
# PaddlePaddle works well on 8 GPUs. PaddlePaddle works well on 1 CPU.
# PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now. PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
""" """
print("Running verify PaddlePaddle program ... ") print("Running verify PaddlePaddle program ... ")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册