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