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