未验证 提交 8bbedc23 编写于 作者: Q qingqing01 提交者: GitHub

Fix doc format for callbacks, metrics and Model (#28638)

* Fix doc format for callbacks, metrics and Model
* Fix code sample and doc
上级 a1486091
...@@ -180,11 +180,11 @@ class DistributedBatchSampler(BatchSampler): ...@@ -180,11 +180,11 @@ class DistributedBatchSampler(BatchSampler):
batch_size(int): sample indice number in a mini-batch indices. batch_size(int): sample indice number in a mini-batch indices.
num_replicas(int, optional): porcess number in distributed training. num_replicas(int, optional): porcess number in distributed training.
If :attr:`num_replicas` is None, :attr:`num_replicas` will be If :attr:`num_replicas` is None, :attr:`num_replicas` will be
retrieved from :code:`paddle.fluid.dygraph.parallel.ParallenEnv`. retrieved from :code:`paddle.distributed.ParallenEnv`.
Default None. Default None.
rank(int, optional): the rank of the current process among :attr:`num_replicas` rank(int, optional): the rank of the current process among :attr:`num_replicas`
processes. If :attr:`rank` is None, :attr:`rank` is retrieved from processes. If :attr:`rank` is None, :attr:`rank` is retrieved from
:code:`paddle.fluid.dygraph.parallel.ParallenEnv`. Default None. :code:`paddle.distributed.ParallenEnv`. Default None.
shuffle(bool): whther to shuffle indices order before genrating shuffle(bool): whther to shuffle indices order before genrating
batch indices. Default False. batch indices. Default False.
drop_last(bool): whether drop the last incomplete batch dataset size drop_last(bool): whether drop the last incomplete batch dataset size
......
...@@ -161,10 +161,8 @@ class Callback(object): ...@@ -161,10 +161,8 @@ class Callback(object):
- 'batch_size': an integer. Number of samples per batch. - 'batch_size': an integer. Number of samples per batch.
- 'epochs': an integer. Number of epochs. - 'epochs': an integer. Number of epochs.
- 'steps': an integer. Number of steps of one epoch. - 'steps': an integer. Number of steps of one epoch.
- 'verbose': an integer. Verbose mode is 0, 1 or 2. - 'verbose': an integer. Verbose mode is 0, 1 or 2. 0 = silent, 1 = progress bar, 2 = one line per epoch.
0 = silent, 1 = progress bar, 2 = one line per epoch. - 'metrics': a list of str. Names of metrics, including 'loss' and the names of paddle.metric.Metric.
- 'metrics': a list of str. Names of metrics, including 'loss'
and the names of paddle.metric.Metric.
""" """
self.params = params self.params = params
...@@ -298,10 +296,12 @@ class Callback(object): ...@@ -298,10 +296,12 @@ class Callback(object):
class ProgBarLogger(Callback): class ProgBarLogger(Callback):
"""Logger callback function """
Logger callback function.
Args: Args:
log_freq (int): The frequency, in number of steps, the logs such as `loss`, log_freq (int): The frequency, in number of steps,
`metrics` are printed. Default: 1. the logs such as loss, metrics are printed. Default: 1.
verbose (int): The verbosity mode, should be 0, 1, or 2. verbose (int): The verbosity mode, should be 0, 1, or 2.
0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2. 0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2.
...@@ -310,6 +310,7 @@ class ProgBarLogger(Callback): ...@@ -310,6 +310,7 @@ class ProgBarLogger(Callback):
import paddle import paddle
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST
from paddle.static import InputSpec from paddle.static import InputSpec
inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')] inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
...@@ -319,7 +320,7 @@ class ProgBarLogger(Callback): ...@@ -319,7 +320,7 @@ class ProgBarLogger(Callback):
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
]) ])
train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform) train_dataset = MNIST(mode='train', transform=transform)
lenet = paddle.vision.LeNet() lenet = paddle.vision.LeNet()
model = paddle.Model(lenet, model = paddle.Model(lenet,
...@@ -439,7 +440,9 @@ class ProgBarLogger(Callback): ...@@ -439,7 +440,9 @@ class ProgBarLogger(Callback):
class ModelCheckpoint(Callback): class ModelCheckpoint(Callback):
"""Model checkpoint callback function """
Model checkpoint callback function.
Args: Args:
save_freq(int): The frequency, in number of epochs, the model checkpoint save_freq(int): The frequency, in number of epochs, the model checkpoint
are saved. Default: 1. are saved. Default: 1.
...@@ -451,6 +454,7 @@ class ModelCheckpoint(Callback): ...@@ -451,6 +454,7 @@ class ModelCheckpoint(Callback):
import paddle import paddle
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST
from paddle.static import InputSpec from paddle.static import InputSpec
inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')] inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
...@@ -460,7 +464,7 @@ class ModelCheckpoint(Callback): ...@@ -460,7 +464,7 @@ class ModelCheckpoint(Callback):
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
]) ])
train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform) train_dataset = MNIST(mode='train', transform=transform)
lenet = paddle.vision.LeNet() lenet = paddle.vision.LeNet()
model = paddle.Model(lenet, model = paddle.Model(lenet,
...@@ -740,7 +744,9 @@ class EarlyStopping(Callback): ...@@ -740,7 +744,9 @@ class EarlyStopping(Callback):
class VisualDL(Callback): class VisualDL(Callback):
"""VisualDL callback function """
VisualDL callback function.
Args: Args:
log_dir (str): The directory to save visualdl log file. log_dir (str): The directory to save visualdl log file.
......
...@@ -808,7 +808,7 @@ class Model(object): ...@@ -808,7 +808,7 @@ class Model(object):
""" """
An Model object is network with training and inference features. An Model object is network with training and inference features.
Dynamic graph and static graph are supported at the same time, Dynamic graph and static graph are supported at the same time,
switched by `paddle.disable_static()`. The usage is as follows. switched by `paddle.enable_static()`. The usage is as follows.
But note, the switching between dynamic and static should be before But note, the switching between dynamic and static should be before
instantiating a Model. The input description, i.e, paddle.static.InputSpec, instantiating a Model. The input description, i.e, paddle.static.InputSpec,
must be required for static graph. must be required for static graph.
...@@ -1052,9 +1052,9 @@ class Model(object): ...@@ -1052,9 +1052,9 @@ class Model(object):
If `training` is set to False, only inference model will be saved. If `training` is set to False, only inference model will be saved.
Args: Args:
path (str): The file prefix to save model. The format is path (str): The file prefix to save model. The format
'dirname/file_prefix' or 'file_prefix'. if empty str. A exception is 'dirname/file_prefix' or 'file_prefix'. if empty str.
will be raised. A exception will be raised.
training (bool, optional): Whether to save for training. If not, save training (bool, optional): Whether to save for training. If not, save
for inference only. Default: True. for inference only. Default: True.
...@@ -1084,9 +1084,9 @@ class Model(object): ...@@ -1084,9 +1084,9 @@ class Model(object):
return self.net(x) return self.net(x)
dynamic = True # False dynamic = True # False
device = paddle.set_device('cpu')
# if use static graph, do not set # if use static graph, do not set
paddle.disable_static(device) if dynamic else None if not dynamic:
paddle.enable_static()
input = InputSpec([None, 784], 'float32', 'x') input = InputSpec([None, 784], 'float32', 'x')
label = InputSpec([None, 1], 'int64', 'label') label = InputSpec([None, 1], 'int64', 'label')
...@@ -1361,18 +1361,19 @@ class Model(object): ...@@ -1361,18 +1361,19 @@ class Model(object):
import paddle import paddle
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST
from paddle.static import InputSpec from paddle.static import InputSpec
dynamic = True dynamic = True
device = paddle.set_device('cpu') # or 'gpu' if not dynamic:
paddle.disable_static(device) if dynamic else None paddle.enable_static()
transform = T.Compose([ transform = T.Compose([
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
]) ])
train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform) train_dataset = MNIST(mode='train', transform=transform)
val_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform) val_dataset = MNIST(mode='test', transform=transform)
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')
...@@ -1399,22 +1400,23 @@ class Model(object): ...@@ -1399,22 +1400,23 @@ class Model(object):
import paddle import paddle
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST
from paddle.static import InputSpec from paddle.static import InputSpec
dynamic = True dynamic = True
device = paddle.set_device('cpu') # or 'gpu' if not dynamic:
paddle.disable_static(device) if dynamic else None paddle.enable_static()
transform = T.Compose([ transform = T.Compose([
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
]) ])
train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform) train_dataset = MNIST(mode='train', transform=transform)
train_loader = paddle.io.DataLoader(train_dataset, train_loader = paddle.io.DataLoader(train_dataset,
places=device, batch_size=64) batch_size=64)
val_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform) val_dataset = MNIST(mode='test', transform=transform)
val_loader = paddle.io.DataLoader(val_dataset, val_loader = paddle.io.DataLoader(val_dataset,
places=device, batch_size=64) batch_size=64)
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')
...@@ -1540,6 +1542,7 @@ class Model(object): ...@@ -1540,6 +1542,7 @@ class Model(object):
value is a scalar or numpy.array. value is a scalar or numpy.array.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
...@@ -1559,14 +1562,6 @@ class Model(object): ...@@ -1559,14 +1562,6 @@ class Model(object):
model.prepare(metrics=paddle.metric.Accuracy()) model.prepare(metrics=paddle.metric.Accuracy())
result = model.evaluate(val_dataset, batch_size=64) result = model.evaluate(val_dataset, batch_size=64)
print(result) print(result)
# imperative mode
paddle.disable_static()
model = paddle.Model(paddle.vision.models.LeNet(), input, label)
model.prepare(metrics=paddle.metric.Accuracy())
result = model.evaluate(val_dataset, batch_size=64)
print(result)
""" """
if eval_data is not None and isinstance(eval_data, Dataset): if eval_data is not None and isinstance(eval_data, Dataset):
...@@ -1637,6 +1632,7 @@ class Model(object): ...@@ -1637,6 +1632,7 @@ class Model(object):
list: output of models. list: output of models.
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
......
...@@ -39,6 +39,8 @@ class Metric(object): ...@@ -39,6 +39,8 @@ class Metric(object):
Base class for metric, encapsulates metric logic and APIs Base class for metric, encapsulates metric logic and APIs
Usage: Usage:
.. code-block:: text
m = SomeMetric() m = SomeMetric()
for prediction, label in ...: for prediction, label in ...:
m.update(prediction, label) m.update(prediction, label)
...@@ -52,6 +54,9 @@ class Metric(object): ...@@ -52,6 +54,9 @@ class Metric(object):
call :code:`update` with states in NumPy format. call :code:`update` with states in NumPy format.
Metric calculated as follows (operations in Model and Metric are Metric calculated as follows (operations in Model and Metric are
indicated with curly brackets, while data nodes not): indicated with curly brackets, while data nodes not):
.. code-block:: text
inputs & labels || ------------------ inputs & labels || ------------------
| || | ||
{model} || {model} ||
...@@ -67,6 +72,7 @@ class Metric(object): ...@@ -67,6 +72,7 @@ class Metric(object):
metric states(numpy) || numpy data metric states(numpy) || numpy data
| || | ||
{Metric.update} \/ ------------------ {Metric.update} \/ ------------------
Examples: Examples:
For :code:`Accuracy` metric, which takes :code:`pred` and :code:`label` For :code:`Accuracy` metric, which takes :code:`pred` and :code:`label`
...@@ -79,7 +85,8 @@ class Metric(object): ...@@ -79,7 +85,8 @@ class Metric(object):
prediction of each sample like follows, while the correct prediction prediction of each sample like follows, while the correct prediction
matrix shape is [N, 5]. matrix shape is [N, 5].
.. code-block:: python .. code-block:: text
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]
...@@ -92,7 +99,8 @@ class Metric(object): ...@@ -92,7 +99,8 @@ class Metric(object):
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].
:code:`update` can be define as follows: :code:`update` can be define as follows:
.. code-block:: python .. code-block:: text
def update(self, correct): def update(self, correct):
accs = [] accs = []
for i, k in enumerate(self.topk): for i, k in enumerate(self.topk):
...@@ -206,10 +214,13 @@ class Accuracy(Metric): ...@@ -206,10 +214,13 @@ class Accuracy(Metric):
import paddle import paddle
from paddle.static import InputSpec from paddle.static import InputSpec
import paddle.vision.transforms as T
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')
train_dataset = paddle.vision.datasets.MNIST(mode='train') transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
train_dataset = MNIST(mode='train', transform=transform)
model = paddle.Model(paddle.vision.LeNet(), input, label) model = paddle.Model(paddle.vision.LeNet(), input, label)
optim = paddle.optimizer.Adam( optim = paddle.optimizer.Adam(
...@@ -355,7 +366,6 @@ class Precision(Metric): ...@@ -355,7 +366,6 @@ class Precision(Metric):
def __len__(self): def __len__(self):
return self.n return self.n
paddle.disable_static()
model = paddle.Model(nn.Sequential( model = paddle.Model(nn.Sequential(
nn.Linear(10, 1), nn.Linear(10, 1),
nn.Sigmoid() nn.Sigmoid()
...@@ -489,7 +499,6 @@ class Recall(Metric): ...@@ -489,7 +499,6 @@ class Recall(Metric):
def __len__(self): def __len__(self):
return self.n return self.n
paddle.disable_static()
model = paddle.Model(nn.Sequential( model = paddle.Model(nn.Sequential(
nn.Linear(10, 1), nn.Linear(10, 1),
nn.Sigmoid() nn.Sigmoid()
...@@ -634,7 +643,6 @@ class Auc(Metric): ...@@ -634,7 +643,6 @@ class Auc(Metric):
def __len__(self): def __len__(self):
return self.n return self.n
paddle.disable_static()
model = paddle.Model(nn.Sequential( model = paddle.Model(nn.Sequential(
nn.Linear(10, 2), nn.Softmax()) nn.Linear(10, 2), nn.Softmax())
) )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册