未验证 提交 9084a034 编写于 作者: L LoneRanger 提交者: GitHub

[xdoctest] reformat example code with google style in No.1-No.5 (#55804)

上级 7ce0f9e1
...@@ -35,21 +35,20 @@ def batch(reader, batch_size, drop_last=False): ...@@ -35,21 +35,20 @@ def batch(reader, batch_size, drop_last=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
def reader(): >>> def reader():
for i in range(10): ... for i in range(10):
yield i ... yield i
batch_reader = paddle.batch(reader, batch_size=2) >>> batch_reader = paddle.batch(reader, batch_size=2)
for data in batch_reader(): >>> for data in batch_reader():
print(data) ... print(data)
...
# Output is [0, 1]
# [0, 1] [2, 3]
# [2, 3] [4, 5]
# [4, 5] [6, 7]
# [6, 7] [8, 9]
# [8, 9]
""" """
def batch_reader(): def batch_reader():
......
...@@ -123,9 +123,9 @@ def load_image_bytes(bytes, is_color=True): ...@@ -123,9 +123,9 @@ def load_image_bytes(bytes, is_color=True):
.. code-block:: python .. code-block:: python
with open('cat.jpg') as f: >>> with open('cat.jpg') as f:
im = load_image_bytes(f.read()) ... im = load_image_bytes(f.read())
...
:param bytes: the input image bytes array. :param bytes: the input image bytes array.
:type bytes: str :type bytes: str
:param is_color: If set is_color True, it will load and :param is_color: If set is_color True, it will load and
...@@ -148,7 +148,7 @@ def load_image(file, is_color=True): ...@@ -148,7 +148,7 @@ def load_image(file, is_color=True):
.. code-block:: python .. code-block:: python
im = load_image('cat.jpg') >>> im = load_image('cat.jpg')
:param file: the input image path. :param file: the input image path.
:type file: string :type file: string
...@@ -178,8 +178,8 @@ def resize_short(im, size): ...@@ -178,8 +178,8 @@ def resize_short(im, size):
.. code-block:: python .. code-block:: python
im = load_image('cat.jpg') >>> im = load_image('cat.jpg')
im = resize_short(im, 256) >>> im = resize_short(im, 256)
:param im: the input image with HWC layout. :param im: the input image with HWC layout.
:type im: ndarray :type im: ndarray
...@@ -208,9 +208,9 @@ def to_chw(im, order=(2, 0, 1)): ...@@ -208,9 +208,9 @@ def to_chw(im, order=(2, 0, 1)):
.. code-block:: python .. code-block:: python
im = load_image('cat.jpg') >>> im = load_image('cat.jpg')
im = resize_short(im, 256) >>> im = resize_short(im, 256)
im = to_chw(im) >>> im = to_chw(im)
:param im: the input image with HWC layout. :param im: the input image with HWC layout.
:type im: ndarray :type im: ndarray
...@@ -230,7 +230,8 @@ def center_crop(im, size, is_color=True): ...@@ -230,7 +230,8 @@ def center_crop(im, size, is_color=True):
.. code-block:: python .. code-block:: python
im = center_crop(im, 224) >>> im = load_image('cat.jpg')
>>> im = center_crop(im, 224)
:param im: the input image with HWC layout. :param im: the input image with HWC layout.
:type im: ndarray :type im: ndarray
...@@ -258,7 +259,8 @@ def random_crop(im, size, is_color=True): ...@@ -258,7 +259,8 @@ def random_crop(im, size, is_color=True):
.. code-block:: python .. code-block:: python
im = random_crop(im, 224) >>> im = load_image('cat.jpg')
>>> im = random_crop(im, 224)
:param im: the input image with HWC layout. :param im: the input image with HWC layout.
:type im: ndarray :type im: ndarray
...@@ -287,7 +289,8 @@ def left_right_flip(im, is_color=True): ...@@ -287,7 +289,8 @@ def left_right_flip(im, is_color=True):
.. code-block:: python .. code-block:: python
im = left_right_flip(im) >>> im = load_image('cat.jpg')
>>> im = left_right_flip(im)
:param im: input image with HWC layout or HW layout for gray image :param im: input image with HWC layout or HW layout for gray image
:type im: ndarray :type im: ndarray
...@@ -311,7 +314,8 @@ def simple_transform( ...@@ -311,7 +314,8 @@ def simple_transform(
.. code-block:: python .. code-block:: python
im = simple_transform(im, 256, 224, True) >>> im = load_image('cat.jpg')
>>> im = simple_transform(im, 256, 224, True)
:param im: The input image with HWC layout. :param im: The input image with HWC layout.
:type im: ndarray :type im: ndarray
...@@ -365,7 +369,7 @@ def load_and_transform( ...@@ -365,7 +369,7 @@ def load_and_transform(
.. code-block:: python .. code-block:: python
im = load_and_transform('cat.jpg', 256, 224, True) >>> im = load_and_transform('cat.jpg', 256, 224, True)
:param filename: The file name of input image. :param filename: The file name of input image.
:type filename: string :type filename: string
......
此差异已折叠。
...@@ -85,13 +85,13 @@ class Metric(metaclass=abc.ABCMeta): ...@@ -85,13 +85,13 @@ class Metric(metaclass=abc.ABCMeta):
.. code-block:: python .. code-block:: python
:name: code-compute-example :name: code-compute-example
def compute(pred, label): >>> def compute(pred, label):
# sort prediction and slice the top-5 scores ... # sort prediction and slice the top-5 scores
pred = paddle.argsort(pred, descending=True)[:, :5] ... pred = paddle.argsort(pred, descending=True)[:, :5]
# calculate whether the predictions are correct ... # calculate whether the predictions are correct
correct = pred == label ... correct = pred == label
return paddle.cast(correct, dtype='float32') ... return paddle.cast(correct, dtype='float32')
...
With the :code:`compute`, we split some calculations to OPs (which With the :code:`compute`, we split some calculations to OPs (which
may run on GPU devices, will be faster), and only fetch 1 tensor with may run on GPU devices, will be faster), and only fetch 1 tensor with
shape as [N, 5] instead of 2 tensors with shapes as [N, 10] and [N, 1]. shape as [N, 5] instead of 2 tensors with shapes as [N, 10] and [N, 1].
...@@ -100,15 +100,15 @@ class Metric(metaclass=abc.ABCMeta): ...@@ -100,15 +100,15 @@ class Metric(metaclass=abc.ABCMeta):
.. code-block:: python .. code-block:: python
:name: code-update-example :name: code-update-example
def update(self, correct): >>> def update(self, correct):
accs = [] ... accs = []
for i, k in enumerate(self.topk): ... for i, k in enumerate(self.topk):
num_corrects = correct[:, :k].sum() ... num_corrects = correct[:, :k].sum()
num_samples = len(correct) ... num_samples = len(correct)
accs.append(float(num_corrects) / num_samples) ... accs.append(float(num_corrects) / num_samples)
self.total[i] += num_corrects ... self.total[i] += num_corrects
self.count[i] += num_samples ... self.count[i] += num_samples
return accs ... return accs
""" """
def __init__(self): def __init__(self):
...@@ -201,44 +201,45 @@ class Accuracy(Metric): ...@@ -201,44 +201,45 @@ class Accuracy(Metric):
.. code-block:: python .. code-block:: python
:name: code-standalone-example :name: code-standalone-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
x = paddle.to_tensor(np.array([ >>> x = paddle.to_tensor(np.array([
[0.1, 0.2, 0.3, 0.4], ... [0.1, 0.2, 0.3, 0.4],
[0.1, 0.4, 0.3, 0.2], ... [0.1, 0.4, 0.3, 0.2],
[0.1, 0.2, 0.4, 0.3], ... [0.1, 0.2, 0.4, 0.3],
[0.1, 0.2, 0.3, 0.4]])) ... [0.1, 0.2, 0.3, 0.4]]))
y = paddle.to_tensor(np.array([[0], [1], [2], [3]])) >>> y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
m = paddle.metric.Accuracy() >>> m = paddle.metric.Accuracy()
correct = m.compute(x, y) >>> correct = m.compute(x, y)
m.update(correct) >>> m.update(correct)
res = m.accumulate() >>> res = m.accumulate()
print(res) # 0.75 >>> print(res)
0.75
.. code-block:: python .. code-block:: python
:name: code-model-api-example :name: code-model-api-example
import paddle >>> import paddle
from paddle.static import InputSpec >>> from paddle.static import InputSpec
import paddle.vision.transforms as T >>> import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST >>> from paddle.vision.datasets import MNIST
input = InputSpec([None, 1, 28, 28], 'float32', 'image') >>> input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label') >>> label = InputSpec([None, 1], 'int64', 'label')
transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])]) >>> transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
train_dataset = MNIST(mode='train', transform=transform) >>> train_dataset = MNIST(mode='train', transform=transform)
model = paddle.Model(paddle.vision.models.LeNet(), input, label) >>> model = paddle.Model(paddle.vision.models.LeNet(), input, label)
optim = paddle.optimizer.Adam( >>> optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters()) ... learning_rate=0.001, parameters=model.parameters())
model.prepare( >>> model.prepare(
optim, ... optim,
loss=paddle.nn.CrossEntropyLoss(), ... loss=paddle.nn.CrossEntropyLoss(),
metrics=paddle.metric.Accuracy()) ... metrics=paddle.metric.Accuracy())
...
model.fit(train_dataset, batch_size=64) >>> model.fit(train_dataset, batch_size=64)
""" """
...@@ -353,51 +354,52 @@ class Precision(Metric): ...@@ -353,51 +354,52 @@ class Precision(Metric):
.. code-block:: python .. code-block:: python
:name: code-standalone-example :name: code-standalone-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
x = np.array([0.1, 0.5, 0.6, 0.7]) >>> x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([0, 1, 1, 1]) >>> y = np.array([0, 1, 1, 1])
m = paddle.metric.Precision() >>> m = paddle.metric.Precision()
m.update(x, y) >>> m.update(x, y)
res = m.accumulate() >>> res = m.accumulate()
print(res) # 1.0 >>> print(res)
1.0
.. code-block:: python .. code-block:: python
:name: code-model-api-example :name: code-model-api-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
import paddle.nn as nn >>> import paddle.nn as nn
class Data(paddle.io.Dataset): >>> class Data(paddle.io.Dataset):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self.n = 1024 ... self.n = 1024
self.x = np.random.randn(self.n, 10).astype('float32') ... self.x = np.random.randn(self.n, 10).astype('float32')
self.y = np.random.randint(2, size=(self.n, 1)).astype('float32') ... self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')
...
def __getitem__(self, idx): ... def __getitem__(self, idx):
return self.x[idx], self.y[idx] ... return self.x[idx], self.y[idx]
...
def __len__(self): ... def __len__(self):
return self.n ... return self.n
...
model = paddle.Model(nn.Sequential( >>> model = paddle.Model(nn.Sequential(
nn.Linear(10, 1), ... nn.Linear(10, 1),
nn.Sigmoid() ... nn.Sigmoid()
)) ... ))
optim = paddle.optimizer.Adam( >>> optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters()) ... learning_rate=0.001, parameters=model.parameters())
model.prepare( >>> model.prepare(
optim, ... optim,
loss=nn.BCELoss(), ... loss=nn.BCELoss(),
metrics=paddle.metric.Precision()) ... metrics=paddle.metric.Precision())
...
data = Data() >>> data = Data()
model.fit(data, batch_size=16) >>> model.fit(data, batch_size=16)
""" """
def __init__(self, name='precision', *args, **kwargs): def __init__(self, name='precision', *args, **kwargs):
...@@ -484,51 +486,52 @@ class Recall(Metric): ...@@ -484,51 +486,52 @@ class Recall(Metric):
.. code-block:: python .. code-block:: python
:name: code-standalone-example :name: code-standalone-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
x = np.array([0.1, 0.5, 0.6, 0.7]) >>> x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([1, 0, 1, 1]) >>> y = np.array([1, 0, 1, 1])
m = paddle.metric.Recall() >>> m = paddle.metric.Recall()
m.update(x, y) >>> m.update(x, y)
res = m.accumulate() >>> res = m.accumulate()
print(res) # 2.0 / 3.0 >>> print(res)
0.6666666666666666
.. code-block:: python .. code-block:: python
:name: code-model-api-example :name: code-model-api-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
import paddle.nn as nn >>> import paddle.nn as nn
class Data(paddle.io.Dataset): >>> class Data(paddle.io.Dataset):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self.n = 1024 ... self.n = 1024
self.x = np.random.randn(self.n, 10).astype('float32') ... self.x = np.random.randn(self.n, 10).astype('float32')
self.y = np.random.randint(2, size=(self.n, 1)).astype('float32') ... self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')
...
def __getitem__(self, idx): ... def __getitem__(self, idx):
return self.x[idx], self.y[idx] ... return self.x[idx], self.y[idx]
...
def __len__(self): ... def __len__(self):
return self.n ... return self.n
...
model = paddle.Model(nn.Sequential( >>> model = paddle.Model(nn.Sequential(
nn.Linear(10, 1), ... nn.Linear(10, 1),
nn.Sigmoid() ... nn.Sigmoid()
)) ... ))
optim = paddle.optimizer.Adam( >>> optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters()) ... learning_rate=0.001, parameters=model.parameters())
model.prepare( >>> model.prepare(
optim, ... optim,
loss=nn.BCELoss(), ... loss=nn.BCELoss(),
metrics=[paddle.metric.Precision(), paddle.metric.Recall()]) ... metrics=[paddle.metric.Precision(), paddle.metric.Recall()])
...
data = Data() >>> data = Data()
model.fit(data, batch_size=16) >>> model.fit(data, batch_size=16)
""" """
def __init__(self, name='recall', *args, **kwargs): def __init__(self, name='recall', *args, **kwargs):
...@@ -624,56 +627,56 @@ class Auc(Metric): ...@@ -624,56 +627,56 @@ class Auc(Metric):
.. code-block:: python .. code-block:: python
:name: code-standalone-example :name: code-standalone-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
m = paddle.metric.Auc() >>> m = paddle.metric.Auc()
n = 8 >>> n = 8
class0_preds = np.random.random(size = (n, 1)) >>> class0_preds = np.random.random(size = (n, 1))
class1_preds = 1 - class0_preds >>> class1_preds = 1 - class0_preds
preds = np.concatenate((class0_preds, class1_preds), axis=1) >>> preds = np.concatenate((class0_preds, class1_preds), axis=1)
labels = np.random.randint(2, size = (n, 1)) >>> labels = np.random.randint(2, size = (n, 1))
m.update(preds=preds, labels=labels) >>> m.update(preds=preds, labels=labels)
res = m.accumulate() >>> res = m.accumulate()
.. code-block:: python .. code-block:: python
:name: code-model-api-example :name: code-model-api-example
import numpy as np >>> import numpy as np
import paddle >>> import paddle
import paddle.nn as nn >>> import paddle.nn as nn
class Data(paddle.io.Dataset): >>> class Data(paddle.io.Dataset):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self.n = 1024 ... self.n = 1024
self.x = np.random.randn(self.n, 10).astype('float32') ... self.x = np.random.randn(self.n, 10).astype('float32')
self.y = np.random.randint(2, size=(self.n, 1)).astype('int64') ... self.y = np.random.randint(2, size=(self.n, 1)).astype('int64')
...
def __getitem__(self, idx): ... def __getitem__(self, idx):
return self.x[idx], self.y[idx] ... return self.x[idx], self.y[idx]
...
def __len__(self): ... def __len__(self):
return self.n ... return self.n
...
model = paddle.Model(nn.Sequential( >>> model = paddle.Model(nn.Sequential(
nn.Linear(10, 2), nn.Softmax()) ... nn.Linear(10, 2), nn.Softmax())
) ... )
optim = paddle.optimizer.Adam( >>> optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters()) ... learning_rate=0.001, parameters=model.parameters())
...
def loss(x, y): >>> def loss(x, y):
return nn.functional.nll_loss(paddle.log(x), y) ... return nn.functional.nll_loss(paddle.log(x), y)
...
model.prepare( >>> model.prepare(
optim, ... optim,
loss=loss, ... loss=loss,
metrics=paddle.metric.Auc()) ... metrics=paddle.metric.Auc())
data = Data() >>> data = Data()
model.fit(data, batch_size=16) >>> model.fit(data, batch_size=16)
""" """
def __init__( def __init__(
...@@ -789,12 +792,14 @@ def accuracy(input, label, k=1, correct=None, total=None, name=None): ...@@ -789,12 +792,14 @@ def accuracy(input, label, k=1, correct=None, total=None, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32') >>> predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32')
label = paddle.to_tensor([[2], [0]], dtype="int64") >>> label = paddle.to_tensor([[2], [0]], dtype="int64")
result = paddle.metric.accuracy(input=predictions, label=label, k=1) >>> result = paddle.metric.accuracy(input=predictions, label=label, k=1)
# 0.5 >>> print(result)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.50000000)
""" """
if label.dtype == paddle.int32: if label.dtype == paddle.int32:
label = paddle.cast(label, paddle.int64) label = paddle.cast(label, paddle.int64)
......
...@@ -46,45 +46,46 @@ def export(layer, path, input_spec=None, opset_version=9, **configs): ...@@ -46,45 +46,46 @@ def export(layer, path, input_spec=None, opset_version=9, **configs):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
class LinearNet(paddle.nn.Layer): >>> class LinearNet(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self._linear = paddle.nn.Linear(128, 10) ... self._linear = paddle.nn.Linear(128, 10)
...
def forward(self, x): ... def forward(self, x):
return self._linear(x) ... return self._linear(x)
...
# Export model with 'InputSpec' to support dynamic input shape. >>> # Export model with 'InputSpec' to support dynamic input shape.
def export_linear_net(): >>> def export_linear_net():
model = LinearNet() ... model = LinearNet()
x_spec = paddle.static.InputSpec(shape=[None, 128], dtype='float32') ... x_spec = paddle.static.InputSpec(shape=[None, 128], dtype='float32')
paddle.onnx.export(model, 'linear_net', input_spec=[x_spec]) ... paddle.onnx.export(model, 'linear_net', input_spec=[x_spec])
...
export_linear_net() >>> # doctest: +SKIP('raise ImportError')
>>> export_linear_net()
class Logic(paddle.nn.Layer):
def __init__(self): >>> class Logic(paddle.nn.Layer):
super().__init__() ... def __init__(self):
... super().__init__()
def forward(self, x, y, z): ...
if z: ... def forward(self, x, y, z):
return x ... if z:
else: ... return x
return y ... else:
... return y
# Export model with 'Tensor' to support pruned model by set 'output_spec'. ...
def export_logic(): >>> # Export model with 'Tensor' to support pruned model by set 'output_spec'.
model = Logic() >>> def export_logic():
x = paddle.to_tensor([1]) ... model = Logic()
y = paddle.to_tensor([2]) ... x = paddle.to_tensor([1])
# Static and run model. ... y = paddle.to_tensor([2])
paddle.jit.to_static(model) ... # Static and run model.
out = model(x, y, z=True) ... paddle.jit.to_static(model)
paddle.onnx.export(model, 'pruned', input_spec=[x], output_spec=[out]) ... out = model(x, y, z=True)
... paddle.onnx.export(model, 'pruned', input_spec=[x], output_spec=[out])
export_logic() ...
>>> export_logic()
""" """
p2o = try_import('paddle2onnx') p2o = try_import('paddle2onnx')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册