未验证 提交 4c3969fa 编写于 作者: N Nyakku Shigure 提交者: GitHub

fix paddle.Model en docs (#43537)

* add copy-from label for code examples, test=document_fix

* refine docs, test=document_fix

* add some output for code example, test=document_fix

* add `optional`, test=document_fix

* add missing parameters, test=document_fix

* add missing links for `ProgBarLogger` and `ModelCheckpoint`, test=document_fix

* update eval_batch example, test=document_fix

* fix typos in stack_outputs, test=document_fix

* np.random -> paddle.random, test=document_fix
上级 3d232b29
...@@ -934,89 +934,91 @@ class Model(object): ...@@ -934,89 +934,91 @@ class Model(object):
Args: Args:
network (paddle.nn.Layer): The network is an instance of network (paddle.nn.Layer): The network is an instance of
paddle.nn.Layer. paddle.nn.Layer.
inputs (InputSpec|list|tuple|dict|None): `inputs`, entry points of network, inputs (InputSpec|list|tuple|dict|None, optional): `inputs`, entry points of network,
could be a InputSpec instance, or list/tuple of InputSpec instances, could be a InputSpec instance, or list/tuple of InputSpec instances,
or dict ({name: InputSpec}), and it couldn't be None in static or dict ({name: InputSpec}), and it couldn't be None in static
graph. graph. Default: None.
labels (InputSpec|list|tuple|None): `labels`, entry points of network, labels (InputSpec|list|tuple|None, optional): `labels`, entry points of network,
could be a InputSpec instnace or list/tuple of InputSpec instances, could be a InputSpec instnace or list/tuple of InputSpec instances,
or None. For static graph, if labels is required in loss, or None. For static graph, if labels is required in loss,
labels must be set. Otherwise, it could be None. labels must be set. Otherwise, it could be None. Default: None.
Examples: Examples:
1. A common example 1. A common example
.. code-block:: python .. code-block:: python
:name: code-example1
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.static import InputSpec from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu' device = paddle.set_device('cpu') # or 'gpu'
net = nn.Sequential( net = nn.Sequential(
nn.Flatten(1), nn.Flatten(1),
nn.Linear(784, 200), nn.Linear(784, 200),
nn.Tanh(), nn.Tanh(),
nn.Linear(200, 10)) nn.Linear(200, 10))
# inputs and labels are not required for dynamic graph. # inputs and labels are not required for dynamic graph.
input = InputSpec([None, 784], 'float32', 'x') input = InputSpec([None, 784], 'float32', 'x')
label = InputSpec([None, 1], 'int64', 'label') label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(net, input, label) model = paddle.Model(net, input, label)
optim = paddle.optimizer.SGD(learning_rate=1e-3, optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameters=model.parameters()) parameters=model.parameters())
model.prepare(optim, model.prepare(optim,
paddle.nn.CrossEntropyLoss(), paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy()) paddle.metric.Accuracy())
transform = T.Compose([ transform = T.Compose([
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
]) ])
data = paddle.vision.datasets.MNIST(mode='train', transform=transform) data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
model.fit(data, epochs=2, batch_size=32, verbose=1) model.fit(data, epochs=2, batch_size=32, verbose=1)
2. An example using mixed precision training. 2. An example using mixed precision training.
.. code-block:: python .. code-block:: python
:name: code-example2
# required: gpu
import paddle
import paddle.nn as nn
import paddle.vision.transforms as T
def run_example_code(): # required: gpu
device = paddle.set_device('gpu') import paddle
import paddle.nn as nn
import paddle.vision.transforms as T
net = nn.Sequential(nn.Flatten(1), nn.Linear(784, 200), nn.Tanh(), def run_example_code():
nn.Linear(200, 10)) device = paddle.set_device('gpu')
model = paddle.Model(net) net = nn.Sequential(nn.Flatten(1), nn.Linear(784, 200), nn.Tanh(),
optim = paddle.optimizer.SGD(learning_rate=1e-3, parameters=model.parameters()) nn.Linear(200, 10))
amp_configs = { model = paddle.Model(net)
"level": "O1", optim = paddle.optimizer.SGD(learning_rate=1e-3, parameters=model.parameters())
"custom_white_list": {'conv2d'},
"use_dynamic_loss_scaling": True
}
model.prepare(optim,
paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy(),
amp_configs=amp_configs)
transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])]) amp_configs = {
data = paddle.vision.datasets.MNIST(mode='train', transform=transform) "level": "O1",
model.fit(data, epochs=2, batch_size=32, verbose=1) "custom_white_list": {'conv2d'},
"use_dynamic_loss_scaling": True
}
model.prepare(optim,
paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy(),
amp_configs=amp_configs)
# mixed precision training is only supported on GPU now. transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
if paddle.is_compiled_with_cuda(): data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
run_example_code() model.fit(data, epochs=2, batch_size=32, verbose=1)
# mixed precision training is only supported on GPU now.
if paddle.is_compiled_with_cuda():
run_example_code()
""" """
...@@ -1059,12 +1061,12 @@ class Model(object): ...@@ -1059,12 +1061,12 @@ class Model(object):
inputs (numpy.ndarray|Tensor|list): Batch of input data. It could inputs (numpy.ndarray|Tensor|list): Batch of input data. It could
be a numpy array or paddle.Tensor, or a list of arrays or be a numpy array or paddle.Tensor, or a list of arrays or
tensors (in case the model has multiple inputs). tensors (in case the model has multiple inputs).
labels (numpy.ndarray|Tensor|list): Batch of labels. It could be labels (numpy.ndarray|Tensor|list, optional): Batch of labels. It could be
a numpy array or paddle.Tensor, or a list of arrays or tensors a numpy array or paddle.Tensor, or a list of arrays or tensors
(in case the model has multiple labels). If has no labels, (in case the model has multiple labels). If has no labels,
set None. Default is None. set None. Default: None.
update (bool): Whether update parameters after loss.backward() computing. update (bool, optional): Whether update parameters after loss.backward() computing.
Using it to accumulate gradients. Default is True. Set it to False to accumulate gradients. Default: True.
Returns: Returns:
A list of scalar training loss if the model has no metrics, A list of scalar training loss if the model has no metrics,
...@@ -1074,29 +1076,30 @@ class Model(object): ...@@ -1074,29 +1076,30 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-train-batch
import numpy as np import paddle
import paddle import paddle.nn as nn
import paddle.nn as nn from paddle.static import InputSpec
from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu'
device = paddle.set_device('cpu') # or 'gpu'
net = nn.Sequential(
net = nn.Sequential( nn.Linear(784, 200),
nn.Linear(784, 200), nn.Tanh(),
nn.Tanh(), nn.Linear(200, 10))
nn.Linear(200, 10))
input = InputSpec([None, 784], 'float32', 'x')
input = InputSpec([None, 784], 'float32', 'x') label = InputSpec([None, 1], 'int64', 'label')
label = InputSpec([None, 1], 'int64', 'label') model = paddle.Model(net, input, label)
model = paddle.Model(net, input, label) optim = paddle.optimizer.SGD(learning_rate=1e-3,
optim = paddle.optimizer.SGD(learning_rate=1e-3, parameters=model.parameters())
parameters=model.parameters()) model.prepare(optim, paddle.nn.CrossEntropyLoss())
model.prepare(optim, paddle.nn.CrossEntropyLoss()) data = paddle.rand((4, 784), dtype="float32")
data = np.random.random(size=(4,784)).astype(np.float32) label = paddle.randint(0, 10, (4, 1), dtype="int64")
label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64) loss = model.train_batch([data], [label])
loss = model.train_batch([data], [label]) print(loss)
print(loss) # [array([2.192784], dtype=float32)]
""" """
loss = self._adapter.train_batch(inputs, labels, update) loss = self._adapter.train_batch(inputs, labels, update)
if fluid._non_static_mode() and self._input_info is None: if fluid._non_static_mode() and self._input_info is None:
...@@ -1112,10 +1115,10 @@ class Model(object): ...@@ -1112,10 +1115,10 @@ class Model(object):
inputs (numpy.ndarray|Tensor|list): Batch of input data. It could inputs (numpy.ndarray|Tensor|list): Batch of input data. It could
be a numpy array or paddle.Tensor, or a list of arrays or be a numpy array or paddle.Tensor, or a list of arrays or
tensors (in case the model has multiple inputs). tensors (in case the model has multiple inputs).
labels (numpy.ndarray|Tensor|list): Batch of labels. It could be labels (numpy.ndarray|Tensor|list, optional): Batch of labels. It could be
a numpy array or paddle.Tensor, or a list of arrays or tensors a numpy array or paddle.Tensor, or a list of arrays or tensors
(in case the model has multiple labels). If has no labels, (in case the model has multiple labels). If has no labels,
set None. Default is None. set None. Default: None.
Returns: Returns:
A list of scalar testing loss if the model has no metrics, A list of scalar testing loss if the model has no metrics,
...@@ -1125,30 +1128,31 @@ class Model(object): ...@@ -1125,30 +1128,31 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-eval-batch
import numpy as np
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
from paddle.static import InputSpec from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu' device = paddle.set_device('cpu') # or 'gpu'
net = nn.Sequential( net = nn.Sequential(
nn.Linear(784, 200), nn.Linear(784, 200),
nn.Tanh(), nn.Tanh(),
nn.Linear(200, 10)) nn.Linear(200, 10))
input = InputSpec([None, 784], 'float32', 'x') input = InputSpec([None, 784], 'float32', 'x')
label = InputSpec([None, 1], 'int64', 'label') label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(net, input, label) model = paddle.Model(net, input, label)
optim = paddle.optimizer.SGD(learning_rate=1e-3, optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameters=model.parameters()) parameters=model.parameters())
model.prepare(optim, model.prepare(optim,
paddle.nn.CrossEntropyLoss()) paddle.nn.CrossEntropyLoss(), metrics=paddle.metric.Accuracy())
data = np.random.random(size=(4,784)).astype(np.float32) data = paddle.rand((4, 784), dtype="float32")
label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64) label = paddle.randint(0, 10, (4, 1), dtype="int64")
loss = model.eval_batch([data], [label]) loss, acc = model.eval_batch([data], [label])
print(loss) print(loss, acc)
# [array([2.8825705], dtype=float32)] [0.0]
""" """
loss = self._adapter.eval_batch(inputs, labels) loss = self._adapter.eval_batch(inputs, labels)
if fluid._non_static_mode() and self._input_info is None: if fluid._non_static_mode() and self._input_info is None:
...@@ -1172,28 +1176,31 @@ class Model(object): ...@@ -1172,28 +1176,31 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-predict-batch
import numpy as np
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
from paddle.static import InputSpec from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu' device = paddle.set_device('cpu') # or 'gpu'
input = InputSpec([None, 784], 'float32', 'x') input = InputSpec([None, 784], 'float32', 'x')
label = InputSpec([None, 1], 'int64', 'label') label = InputSpec([None, 1], 'int64', 'label')
net = nn.Sequential( net = nn.Sequential(
nn.Linear(784, 200), nn.Linear(784, 200),
nn.Tanh(), nn.Tanh(),
nn.Linear(200, 10), nn.Linear(200, 10),
nn.Softmax()) nn.Softmax())
model = paddle.Model(net, input, label) model = paddle.Model(net, input, label)
model.prepare() model.prepare()
data = np.random.random(size=(4,784)).astype(np.float32) data = paddle.rand((1, 784), dtype="float32")
out = model.predict_batch([data]) out = model.predict_batch([data])
print(out) print(out)
# [array([[0.08189095, 0.16740078, 0.06889386, 0.05085445, 0.10729759,
# 0.02217775, 0.14518553, 0.1591538 , 0.01808308, 0.17906217]],
# dtype=float32)]
""" """
loss = self._adapter.predict_batch(inputs) loss = self._adapter.predict_batch(inputs)
if fluid._non_static_mode() and self._input_info is None: if fluid._non_static_mode() and self._input_info is None:
...@@ -1229,6 +1236,7 @@ class Model(object): ...@@ -1229,6 +1236,7 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-save
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
...@@ -1259,7 +1267,7 @@ class Model(object): ...@@ -1259,7 +1267,7 @@ class Model(object):
optim = paddle.optimizer.SGD(learning_rate=1e-3, optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameters=model.parameters()) parameters=model.parameters())
model.prepare(optim, paddle.nn.CrossEntropyLoss()) model.prepare(optim, paddle.nn.CrossEntropyLoss())
transform = T.Compose([ transform = T.Compose([
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
...@@ -1294,14 +1302,14 @@ class Model(object): ...@@ -1294,14 +1302,14 @@ class Model(object):
optimizer states. The files would be `path.pdparams` and optimizer states. The files would be `path.pdparams` and
`path.pdopt` separately, and the latter is not necessary `path.pdopt` separately, and the latter is not necessary
when no need to restore. when no need to restore.
skip_mismatch (bool): Whether to skip the loading of mismatch skip_mismatch (bool, optional): Whether to skip the loading of mismatch
parameter or raise an error when mismatch happens (not found parameter or raise an error when mismatch happens (not found
the parameter in file storing model states of or receives a the parameter in file storing model states of or receives a
mismatch shape). mismatch shape). Default: False.
reset_optimizer (bool): If True, ignore the providing file storing reset_optimizer (bool, optional): If True, ignore the providing file storing
optimizer states and initialize optimizer states from scratch. optimizer states and initialize optimizer states from scratch.
Otherwise, restore optimizer states from `path.pdopt` if Otherwise, restore optimizer states from `path.pdopt` if
a optimizer has been set to the model. Default False. a optimizer has been set to the model. Default: False.
Returns: Returns:
None None
...@@ -1309,23 +1317,24 @@ class Model(object): ...@@ -1309,23 +1317,24 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-load
import paddle
import paddle.nn as nn import paddle
from paddle.static import InputSpec import paddle.nn as nn
from paddle.static import InputSpec
device = paddle.set_device('cpu') device = paddle.set_device('cpu')
input = InputSpec([None, 784], 'float32', 'x') input = InputSpec([None, 784], 'float32', 'x')
model = paddle.Model(nn.Sequential( model = paddle.Model(nn.Sequential(
nn.Linear(784, 200), nn.Linear(784, 200),
nn.Tanh(), nn.Tanh(),
nn.Linear(200, 10), nn.Linear(200, 10),
nn.Softmax()), input) nn.Softmax()), input)
model.save('checkpoint/test') model.save('checkpoint/test')
model.load('checkpoint/test') model.load('checkpoint/test')
""" """
def _load_state_from_path(path): def _load_state_from_path(path):
...@@ -1395,19 +1404,20 @@ class Model(object): ...@@ -1395,19 +1404,20 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-parameters
import paddle
import paddle.nn as nn
from paddle.static import InputSpec
import paddle input = InputSpec([None, 784], 'float32', 'x')
import paddle.nn as nn
from paddle.static import InputSpec model = paddle.Model(nn.Sequential(
nn.Linear(784, 200),
input = InputSpec([None, 784], 'float32', 'x') nn.Tanh(),
nn.Linear(200, 10)), input)
model = paddle.Model(nn.Sequential(
nn.Linear(784, 200),
nn.Tanh(),
nn.Linear(200, 10)), input)
params = model.parameters() params = model.parameters()
""" """
return self._adapter.parameters() return self._adapter.parameters()
...@@ -1501,16 +1511,16 @@ class Model(object): ...@@ -1501,16 +1511,16 @@ class Model(object):
Configures the model before runing. Configures the model before runing.
Args: Args:
optimizer (Optimizer|None): Optimizer must be set in training optimizer (Optimizer|None, optional): Optimizer must be set in training
and should be a Optimizer instance. It can be None in eval and should be a Optimizer instance. It can be None in eval
and test mode. and test mode. Default: None.
loss (Loss|callable function|None): Loss function can loss (Loss|Callable|None, optional): Loss function can
be a `paddle.nn.Layer` instance or any callable function be a `paddle.nn.Layer` instance or any callable function
taken the predicted values and ground truth values as input. taken the predicted values and ground truth values as input.
It can be None when there is no loss. It can be None when there is no loss. Default: None.
metrics (Metric|list of Metric|None): If metrics is set, all metrics (Metric|list[Metric]|None, optional): If metrics is set, all
metrics will be calculated and output in train/eval mode. metrics will be calculated and output in train/eval mode. Default: None.
amp_configs (str|dict|None): AMP configurations. If AMP or pure amp_configs (str|dict|None, optional): AMP configurations. If AMP or pure
float16 training is used, the key 'level' of 'amp_configs' float16 training is used, the key 'level' of 'amp_configs'
should be set to 'O1' or 'O2' respectively. Otherwise, the should be set to 'O1' or 'O2' respectively. Otherwise, the
value of 'level' defaults to 'O0', which means float32 value of 'level' defaults to 'O0', which means float32
...@@ -1526,6 +1536,7 @@ class Model(object): ...@@ -1526,6 +1536,7 @@ class Model(object):
for details. For convenience, 'amp_configs' could be set to for details. For convenience, 'amp_configs' could be set to
'O1' or 'O2' if no more parameters are needed. 'amp_configs' 'O1' or 'O2' if no more parameters are needed. 'amp_configs'
could be None in float32 training. Default: None. could be None in float32 training. Default: None.
Returns: Returns:
None None
""" """
...@@ -1587,133 +1598,133 @@ class Model(object): ...@@ -1587,133 +1598,133 @@ class Model(object):
evaluation will be done at the end of each epoch. evaluation will be done at the end of each epoch.
Args: Args:
train_data (Dataset|DataLoader): An iterable data loader is used for train_data (Dataset|DataLoader, optional): An iterable data loader is used for
train. An instance of paddle paddle.io.Dataset or train. An instance of paddle paddle.io.Dataset or
paddle.io.Dataloader is recomended. Default: None. paddle.io.Dataloader is recomended. Default: None.
eval_data (Dataset|DataLoader): An iterable data loader is used for eval_data (Dataset|DataLoader, optional): An iterable data loader is used for
evaluation at the end of epoch. If None, will not do evaluation. evaluation at the end of epoch. If None, will not do evaluation.
An instance of paddle.io.Dataset or paddle.io.Dataloader An instance of paddle.io.Dataset or paddle.io.Dataloader
is recomended. Default: None. is recomended. Default: None.
batch_size (int): Integer number. The batch size of train_data batch_size (int, optional): The batch size of train_data and eval_data. When
and eval_data. When train_data and eval_data are both the train_data and eval_data are both the instance of Dataloader, this
instance of Dataloader, this parameter will be ignored. parameter will be ignored. Default: 1.
Default: 1. epochs (int, optional): The number of epochs to train the model. Default: 1.
epochs (int): Integer number. The number of epochs to train eval_freq (int, optional): The frequency, in number of epochs, an evalutation
the model. Default: 1.
eval_freq (int): The frequency, in number of epochs, an evalutation
is performed. Default: 1. is performed. Default: 1.
log_freq (int): The frequency, in number of steps, the training logs log_freq (int, optional): The frequency, in number of steps, the training logs
are printed. Default: 10. are printed. Default: 10.
save_dir(str|None): The directory to save checkpoint during training. save_dir(str|None, optional): The directory to save checkpoint during training.
If None, will not save checkpoint. Default: None. If None, will not save checkpoint. Default: None.
save_freq (int): The frequency, in number of epochs, to save save_freq (int, optional): The frequency, in number of epochs, to save
checkpoint. Default: 1. checkpoint. Default: 1.
verbose (int): The verbosity mode, should be 0, 1, or 2. 0 = silent, verbose (int, optional): The verbosity mode, should be 0, 1, or 2. 0 = silent,
1 = progress bar, 2 = one line per epoch. Default: 2. 1 = progress bar, 2 = one line per epoch. Default: 2.
drop_last (bool): Whether drop the last incomplete batch of drop_last (bool, optional): Whether drop the last incomplete batch of
train_data when dataset size is not divisible by the batch size. train_data when dataset size is not divisible by the batch size.
When train_data is an instance of Dataloader, this parameter When train_data is an instance of Dataloader, this parameter
will be ignored. Default: False. will be ignored. Default: False.
shuffle (bool): Whther to shuffle train_data. When train_data is shuffle (bool, optional): Whther to shuffle train_data. When train_data is
an instance of Dataloader, this parameter will be ignored. an instance of Dataloader, this parameter will be ignored.
Default: True. Default: True.
num_workers (int): The number of subprocess to load data, 0 for no num_workers (int, optional): The number of subprocess to load data, 0 for no
subprocess used and loading data in main process. subprocess used and loading data in main process.
When train_data and eval_data are both the instance of When train_data and eval_data are both the instance of
Dataloader, this parameter will be ignored. Default: 0. Dataloader, this parameter will be ignored. Default: 0.
callbacks (Callback|None): A list of `Callback` instances to apply callbacks (Callback|None, optional): A list of `Callback` instances to apply
during training. If None, `ProgBarLogger` and `ModelCheckpoint` during training. If None, :ref:`api_paddle_callbacks_ProgBarLogger` and
are automatically inserted. Default: None. :ref:`api_paddle_callbacks_ModelCheckpoint` are automatically inserted. Default: None.
accumulate_grad_batches (int): The number of batches to accumulate gradident accumulate_grad_batches (int, optional): The number of batches to accumulate gradident
during training process before optimizer updates. It can mimic large batch during training process before optimizer updates. It can mimic large batch
size. Default: 1. size. Default: 1.
num_iters (int|None): Integer number. The number of iterations to train num_iters (int|None, optional): The number of iterations to evaluate the model.
the model. If None, follow `epochs` to train the model, otherwise, train If None, evaluate on whole input dataset, otherwise, evaluate `num_iters` times.
the model `num_iters` times. Default: None. Default: None.
Returns: Returns:
None None
Examples: Examples:
1. An example use Dataset and set btch size, shuffle in fit. 1. An example use Dataset and set batch size, shuffle in fit.
How to make a batch is done internally. How to make a batch is done internally.
.. code-block:: python .. code-block:: python
:name: code-example-fit-1
import paddle import paddle
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST from paddle.vision.datasets import MNIST
from paddle.static import InputSpec from paddle.static import InputSpec
dynamic = True dynamic = True
if not dynamic: if not dynamic:
paddle.enable_static() 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 = MNIST(mode='train', transform=transform) train_dataset = MNIST(mode='train', transform=transform)
val_dataset = 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')
model = paddle.Model( model = paddle.Model(
paddle.vision.models.LeNet(), paddle.vision.models.LeNet(),
input, label) 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,
paddle.nn.CrossEntropyLoss(), paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy(topk=(1, 2))) paddle.metric.Accuracy(topk=(1, 2)))
model.fit(train_dataset, model.fit(train_dataset,
val_dataset, val_dataset,
epochs=2, epochs=2,
batch_size=64, batch_size=64,
save_dir='mnist_checkpoint') save_dir='mnist_checkpoint')
2. An example use DataLoader, batch size and shuffle is set in 2. An example use DataLoader, batch size and shuffle is set in
DataLoader. DataLoader.
.. code-block:: python .. code-block:: python
:name: code-example-fit-2
import paddle
import paddle.vision.transforms as T
from paddle.vision.datasets import MNIST
from paddle.static import InputSpec
import paddle dynamic = True
import paddle.vision.transforms as T if not dynamic:
from paddle.vision.datasets import MNIST paddle.enable_static()
from paddle.static import InputSpec
transform = T.Compose([
T.Transpose(),
T.Normalize([127.5], [127.5])
])
train_dataset = MNIST(mode='train', transform=transform)
train_loader = paddle.io.DataLoader(train_dataset,
batch_size=64)
val_dataset = MNIST(mode='test', transform=transform)
val_loader = paddle.io.DataLoader(val_dataset,
batch_size=64)
input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label')
dynamic = True model = paddle.Model(
if not dynamic: paddle.vision.models.LeNet(), input, label)
paddle.enable_static() optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
transform = T.Compose([ model.prepare(
T.Transpose(), optim,
T.Normalize([127.5], [127.5]) paddle.nn.CrossEntropyLoss(),
]) paddle.metric.Accuracy(topk=(1, 2)))
train_dataset = MNIST(mode='train', transform=transform) model.fit(train_loader,
train_loader = paddle.io.DataLoader(train_dataset, val_loader,
batch_size=64) epochs=2,
val_dataset = MNIST(mode='test', transform=transform) save_dir='mnist_checkpoint')
val_loader = paddle.io.DataLoader(val_dataset,
batch_size=64)
input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(
paddle.vision.models.LeNet(), input, label)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
model.prepare(
optim,
paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy(topk=(1, 2)))
model.fit(train_loader,
val_loader,
epochs=2,
save_dir='mnist_checkpoint')
""" """
assert train_data is not None, \ assert train_data is not None, \
"train_data must be given!" "train_data must be given!"
...@@ -1809,23 +1820,23 @@ class Model(object): ...@@ -1809,23 +1820,23 @@ class Model(object):
eval_data (Dataset|DataLoader): An iterable data loader is used for eval_data (Dataset|DataLoader): An iterable data loader is used for
evaluation. An instance of paddle.io.Dataset or evaluation. An instance of paddle.io.Dataset or
paddle.io.Dataloader is recomended. paddle.io.Dataloader is recomended.
batch_size (int): Integer number. The batch size of train_data batch_size (int, optional): The batch size of train_data and eval_data.
and eval_data. When eval_data is the instance of Dataloader, When eval_data is the instance of Dataloader, this argument will be
this argument will be ignored. Default: 1. ignored. Default: 1.
log_freq (int): The frequency, in number of steps, the eval logs log_freq (int, optional): The frequency, in number of steps, the eval logs
are printed. Default: 10. are printed. Default: 10.
verbose (int): The verbosity mode, should be 0, 1, or 2. 0 = silent, verbose (int, optional): The verbosity mode, should be 0, 1, or 2. 0 = silent,
1 = progress bar, 2 = one line per epoch. Default: 2. 1 = progress bar, 2 = one line per epoch. Default: 2.
num_workers (int): The number of subprocess to load data, num_workers (int, optional): The number of subprocess to load data,
0 for no subprocess used and loading data in main process. When 0 for no subprocess used and loading data in main process. When
train_data and eval_data are both the instance of Dataloader, train_data and eval_data are both the instance of Dataloader,
this parameter will be ignored. Default: 0. this parameter will be ignored. Default: 0.
callbacks (Callback|None): A list of `Callback` instances to apply callbacks (Callback|None, optional): A list of `Callback` instances to apply
during training. If None, `ProgBarLogger` and `ModelCheckpoint` during training. If None, `ProgBarLogger` and `ModelCheckpoint`
are automatically inserted. Default: None. are automatically inserted. Default: None.
num_iters (int|None): Integer number. The number of iterations to num_iters (int|None, optional): The number of iterations to evaluate the model.
evaluate the model. If None, evaluate on whole input dataset, If None, evaluate on whole input dataset, otherwise, evaluate `num_iters` times.
otherwise, evaluate `num_iters` times. Default: None. Default: None.
Returns: Returns:
dict: Result of metric. The key is the names of Metric, dict: Result of metric. The key is the names of Metric,
value is a scalar or numpy.array. value is a scalar or numpy.array.
...@@ -1833,24 +1844,26 @@ class Model(object): ...@@ -1833,24 +1844,26 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-evaluate
import paddle import paddle
import paddle.vision.transforms as T import paddle.vision.transforms as T
from paddle.static import InputSpec from paddle.static import InputSpec
# declarative mode # declarative mode
transform = T.Compose([ transform = T.Compose([
T.Transpose(), T.Transpose(),
T.Normalize([127.5], [127.5]) T.Normalize([127.5], [127.5])
]) ])
val_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform) val_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)
input = InputSpec([-1, 1, 28, 28], 'float32', 'image') input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label') label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(paddle.vision.models.LeNet(), input, label) model = paddle.Model(paddle.vision.models.LeNet(), input, label)
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)
# {'acc': 0.0699}
""" """
if eval_data is not None and isinstance(eval_data, Dataset): if eval_data is not None and isinstance(eval_data, Dataset):
...@@ -1912,21 +1925,20 @@ class Model(object): ...@@ -1912,21 +1925,20 @@ class Model(object):
test_data (Dataset|DataLoader): An iterable data loader is used for test_data (Dataset|DataLoader): An iterable data loader is used for
predict. An instance of paddle.io.Dataset or paddle.io.Dataloader predict. An instance of paddle.io.Dataset or paddle.io.Dataloader
is recomended. is recomended.
batch_size (int): Integer number. The batch size of train_data and eval_data. batch_size (int, optional): The batch size of test_data. When test_data is the
When train_data and eval_data are both the instance of Dataloader, this instance of Dataloader, this argument will be ignored. Default: 1.
argument will be ignored. Default: 1. num_workers (int, optional): The number of subprocess to load data, 0 for no subprocess
num_workers (int): The number of subprocess to load data, 0 for no subprocess used and loading data in main process. When test_data is the instance of Dataloader,
used and loading data in main process. When train_data and eval_data are this argument will be ignored. Default: 0.
both the instance of Dataloader, this argument will be ignored. Default: 0. stack_outputs (bool, optional): Whether stack output field like a batch, as for an output
stack_outputs (bool): Whether stack output field like a batch, as for an output field of a sample is in shape [X, Y], test_data contains N samples, predict
filed of a sample is in shape [X, Y], test_data contains N samples, predict
output field will be in shape [N, X, Y] if stack_output is True, and will output field will be in shape [N, X, Y] if stack_output is True, and will
be a length N list in shape [[X, Y], [X, Y], ....[X, Y]] if stack_outputs be a length N list in shape [[X, Y], [X, Y], ..., [X, Y]] if stack_outputs
is False. stack_outputs as False is used for LoDTensor output situation, is False. stack_outputs as False is used for LoDTensor output situation,
it is recommended set as True if outputs contains no LoDTensor. Default: False. it is recommended set as True if outputs contains no LoDTensor. Default: False.
verbose (int): The verbosity mode, should be 0, 1, or 2. 0 = silent, verbose (int, optional): The verbosity mode, should be 0, 1, or 2. 0 = silent,
1 = progress bar, 2 = one line per batch. Default: 1. 1 = progress bar, 2 = one line per batch. Default: 1.
callbacks(Callback): A Callback instance, default None. callbacks(Callback, optional): A Callback instance, Default: None.
Returns: Returns:
list: output of models. list: output of models.
...@@ -1934,43 +1946,46 @@ class Model(object): ...@@ -1934,43 +1946,46 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-predict
import numpy as np import numpy as np
import paddle import paddle
from paddle.static import InputSpec from paddle.static import InputSpec
class MnistDataset(paddle.vision.datasets.MNIST): class MnistDataset(paddle.vision.datasets.MNIST):
def __init__(self, mode, return_label=True): def __init__(self, mode, return_label=True):
super(MnistDataset, self).__init__(mode=mode) super(MnistDataset, self).__init__(mode=mode)
self.return_label = return_label self.return_label = return_label
def __getitem__(self, idx): def __getitem__(self, idx):
img = np.reshape(self.images[idx], [1, 28, 28]) img = np.reshape(self.images[idx], [1, 28, 28])
if self.return_label: if self.return_label:
return img, np.array(self.labels[idx]).astype('int64') return img, np.array(self.labels[idx]).astype('int64')
return img, return img,
def __len__(self): def __len__(self):
return len(self.images) return len(self.images)
test_dataset = MnistDataset(mode='test', return_label=False) test_dataset = MnistDataset(mode='test', return_label=False)
# imperative mode # imperative mode
input = InputSpec([-1, 1, 28, 28], 'float32', 'image') input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
model = paddle.Model(paddle.vision.models.LeNet(), input) model = paddle.Model(paddle.vision.models.LeNet(), input)
model.prepare() model.prepare()
result = model.predict(test_dataset, batch_size=64) result = model.predict(test_dataset, batch_size=64)
print(len(result[0]), result[0][0].shape) print(len(result[0]), result[0][0].shape)
# 157 (64, 10)
# declarative mode
device = paddle.set_device('cpu') # declarative mode
paddle.enable_static() device = paddle.set_device('cpu')
input = InputSpec([-1, 1, 28, 28], 'float32', 'image') paddle.enable_static()
model = paddle.Model(paddle.vision.models.LeNet(), input) input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
model.prepare() model = paddle.Model(paddle.vision.models.LeNet(), input)
model.prepare()
result = model.predict(test_dataset, batch_size=64)
print(len(result[0]), result[0][0].shape) result = model.predict(test_dataset, batch_size=64)
print(len(result[0]), result[0][0].shape)
# 157 (64, 10)
""" """
if test_data is not None and isinstance(test_data, Dataset): if test_data is not None and isinstance(test_data, Dataset):
...@@ -2164,23 +2179,25 @@ class Model(object): ...@@ -2164,23 +2179,25 @@ class Model(object):
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example-summary
import paddle
from paddle.static import InputSpec
input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label')
import paddle model = paddle.Model(paddle.vision.models.LeNet(),
from paddle.static import InputSpec input, label)
optim = paddle.optimizer.Adam(
input = InputSpec([None, 1, 28, 28], 'float32', 'image') learning_rate=0.001, parameters=model.parameters())
label = InputSpec([None, 1], 'int64', 'label') model.prepare(
optim,
model = paddle.Model(paddle.vision.models.LeNet(), paddle.nn.CrossEntropyLoss())
input, label)
optim = paddle.optimizer.Adam( params_info = model.summary()
learning_rate=0.001, parameters=model.parameters()) print(params_info)
model.prepare( # {'total_params': 61610, 'trainable_params': 61610}
optim,
paddle.nn.CrossEntropyLoss())
params_info = model.summary()
print(params_info)
""" """
assert (input_size is not None or self._inputs assert (input_size is not None or self._inputs
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册