Model

class paddle. Model ( network, inputs=None, labels=None ) [source]

An Model object is network with training and inference features. Dynamic graph and static graph are supported at the same time, 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.

When training on GPU, auto mixed precision (AMP O1) and pure float16 (AMP O2) training are both supported in static graph mode and dynamic mode. In static graph mode, before training with pure float16 (AMP O2), multi_precision could be set to True when creating optimizer, which can avoid poor accuracy or slow convergence in a way, and inputs of dtype float should be cast to float16 by users. paddle.static.amp.fp16_guard API should be also used to limit the range of pure float16 training, otherwise, ‘use_fp16_guard’ should be set to False by users. However, limiting the range of is not supported during training using AMP.

Parameters
  • network (paddle.nn.Layer) – The network is an instance of paddle.nn.Layer.

  • inputs (InputSpec|list|tuple|dict|None, optional) – inputs, entry points of network, could be a InputSpec instance, or list/tuple of InputSpec instances, or dict ({name: InputSpec}), and it couldn’t be None in static graph. Default: None.

  • labels (InputSpec|list|tuple|None, optional) – labels, entry points of network, could be a InputSpec instnace or list/tuple of InputSpec instances, or None. For static graph, if labels is required in loss, labels must be set. Otherwise, it could be None. Default: None.

Examples

  1. A common example

>>> import paddle
>>> import paddle.nn as nn
>>> import paddle.vision.transforms as T
>>> from paddle.static import InputSpec

>>> device = paddle.set_device('cpu') # or 'gpu'

>>> net = nn.Sequential(
...     nn.Flatten(1),
...     nn.Linear(784, 200),
...     nn.Tanh(),
...     nn.Linear(200, 10))
...
>>> # inputs and labels are not required for dynamic graph.
>>> input = InputSpec([None, 784], 'float32', 'x')
>>> label = InputSpec([None, 1], 'int64', 'label')

>>> model = paddle.Model(net, input, label)
>>> optim = paddle.optimizer.SGD(learning_rate=1e-3,
...     parameters=model.parameters())
...
>>> model.prepare(optim,
...             paddle.nn.CrossEntropyLoss(),
...             paddle.metric.Accuracy())
...
>>> transform = T.Compose([
...     T.Transpose(),
...     T.Normalize([127.5], [127.5])
>>> ])
>>> data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
>>> model.fit(data, epochs=2, batch_size=32, verbose=1)
  1. An example using mixed precision training.

>>> 
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> import paddle.nn as nn
>>> import paddle.vision.transforms as T

>>> def run_example_code():
...     device = paddle.set_device('gpu')
...
...     net = nn.Sequential(nn.Flatten(1), nn.Linear(784, 200), nn.Tanh(),
...                         nn.Linear(200, 10))
...
...     model = paddle.Model(net)
...     optim = paddle.optimizer.SGD(learning_rate=1e-3, parameters=model.parameters())
...
...     amp_configs = {
...         "level": "O1",
...         "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])])
...     data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
...     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()
...
train_batch ( inputs, labels=None, update=True )

train_batch

Run one training step on one batch of data. And using update indicates whether optimizer update gradients computing by this batch.

Parameters
  • inputs (numpy.ndarray|Tensor|list) – Batch of input data. It could be a numpy array or paddle.Tensor, or a list of arrays or tensors (in case the model has multiple inputs).

  • 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 (in case the model has multiple labels). If has no labels, set None. Default: None.

  • update (bool, optional) – Whether update parameters after loss.backward() computing. Set it to False to accumulate gradients. Default: True.

Returns

A list of scalar training loss if the model has no metrics, or a tuple (list of scalar loss, list of metrics) if the model set metrics.

Examples

>>> import paddle
>>> import paddle.nn as nn
>>> from paddle.static import InputSpec
>>> paddle.seed(2023)

>>> device = paddle.set_device('cpu') # or 'gpu'

>>> net = nn.Sequential(
...     nn.Linear(784, 200),
...     nn.Tanh(),
...     nn.Linear(200, 10))
...
>>> input = InputSpec([None, 784], 'float32', 'x')
>>> label = InputSpec([None, 1], 'int64', 'label')
>>> model = paddle.Model(net, input, label)
>>> optim = paddle.optimizer.SGD(learning_rate=1e-3,
...     parameters=model.parameters())
>>> model.prepare(optim, paddle.nn.CrossEntropyLoss())
>>> data = paddle.rand((4, 784), dtype="float32")
>>> label = paddle.randint(0, 10, (4, 1), dtype="int64")
>>> loss = model.train_batch([data], [label])
>>> print(loss)
[array(3.0039132, dtype=float32)]
eval_batch ( inputs, labels=None )

eval_batch

Run one evaluating step on a batch of data.

Parameters
  • inputs (numpy.ndarray|Tensor|list) – Batch of input data. It could be a numpy array or paddle.Tensor, or a list of arrays or tensors (in case the model has multiple inputs).

  • 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 (in case the model has multiple labels). If has no labels, set None. Default: None.

Returns

A list of scalar testing loss if the model has no metrics, or a tuple (list of scalar loss, list of metrics) if the model set metrics.

Examples

>>> import paddle
>>> import paddle.nn as nn
>>> from paddle.static import InputSpec
>>> paddle.seed(2023)

>>> device = paddle.set_device('cpu') # or 'gpu'

>>> net = nn.Sequential(
...     nn.Linear(784, 200),
...     nn.Tanh(),
...     nn.Linear(200, 10))
...
>>> input = InputSpec([None, 784], 'float32', 'x')
>>> label = InputSpec([None, 1], 'int64', 'label')
>>> model = paddle.Model(net, input, label)
>>> optim = paddle.optimizer.SGD(learning_rate=1e-3,
...     parameters=model.parameters())
>>> model.prepare(optim,
...               paddle.nn.CrossEntropyLoss(),
...               metrics=paddle.metric.Accuracy())
>>> data = paddle.rand((4, 784), dtype="float32")
>>> label = paddle.randint(0, 10, (4, 1), dtype="int64")
>>> loss, acc = model.eval_batch([data], [label])
>>> print(loss, acc)
[array(3.0039132, dtype=float32)] [0.0]
predict_batch ( inputs )

predict_batch

Run one predicting step on a batch of data.

Parameters

inputs (numpy.ndarray|Tensor|list) – Batch of input data. It could be a numpy array or paddle.Tensor, or a list of arrays or tensors (in case the model has multiple inputs).

Returns

A list of numpy.ndarray of predictions, that is the outputs of Model forward.

Examples

>>> import paddle
>>> import paddle.nn as nn
>>> from paddle.static import InputSpec
>>> paddle.seed(2023)

>>> device = paddle.set_device('cpu') # or 'gpu'

>>> input = InputSpec([None, 784], 'float32', 'x')
>>> label = InputSpec([None, 1], 'int64', 'label')

>>> net = nn.Sequential(
...     nn.Linear(784, 200),
...     nn.Tanh(),
...     nn.Linear(200, 10),
...     nn.Softmax())
...
>>> model = paddle.Model(net, input, label)
>>> model.prepare()
>>> data = paddle.rand((1, 784), dtype="float32")
>>> out = model.predict_batch([data])
>>> print(out)
[array([[0.10844935, 0.04650883, 0.11790176, 0.04962315, 0.10899059,
         0.08197589, 0.03125402, 0.03232312, 0.3786293 , 0.04434395]],
      dtype=float32)]
save ( path, training=True ) [source]

save

This function saves parameters, optimizer information or model and paramters only for inference to path. It depends on the parameter training.

If training is set to True, the parameters saved contain all the trainable Variable, will save to a file with suffix “.pdparams”. The optimizer information contains all the variable used by optimizer. For Adam optimizer, contains beta1, beta2, momentum etc. All the information will save to a file with suffix “.pdopt”. (If the optimizer have no variable need to save (like SGD), the fill will not generated). This function will silently overwrite existing file at the target location.

If training is set to False, only inference model will be saved.

Parameters
  • 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.

Returns

None

Examples

>>> import paddle
>>> import paddle.nn as nn
>>> import paddle.vision.transforms as T
>>> from paddle.static import InputSpec
>>> from paddle.vision.datasets import MNIST

>>> dynamic = True  # False
>>> # If use static graph, do not set
>>> if not dynamic:
...     paddle.enable_static()

>>> 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')

>>> 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, verbose=0)
>>> model.save('checkpoint/test')  # save for training
>>> model.save('inference_model', False)  # save for inference
load ( path, skip_mismatch=False, reset_optimizer=False ) [source]

load

Load from files storing the model states and optimizer states. The file for optimizer states is not necessary if no need to restore the optimizer.

NOTE: parameters are retrieved out from the file storing model states accoring to their structured names.

For fine-tuning or transfer-learning models where some of the layers have changed, keep parameters needed to restore have same structured names in the pre-trained model and fine-tuning model.

Parameters
  • path (str) – The prefix of files storing the model states and optimizer states. The files would be path.pdparams and path.pdopt separately, and the latter is not necessary when no need to restore.

  • skip_mismatch (bool, optional) – Whether to skip the loading of mismatch parameter or raise an error when mismatch happens (not found the parameter in file storing model states of or receives a mismatch shape). Default: False.

  • reset_optimizer (bool, optional) – If True, ignore the providing file storing optimizer states and initialize optimizer states from scratch. Otherwise, restore optimizer states from path.pdopt if a optimizer has been set to the model. Default: False.

Returns

None

Examples

>>> import paddle
>>> import paddle.nn as nn
>>> from paddle.static import InputSpec

>>> device = paddle.set_device('cpu')

>>> input = InputSpec([None, 784], 'float32', 'x')

>>> model = paddle.Model(nn.Sequential(
...     nn.Linear(784, 200),
...     nn.Tanh(),
...     nn.Linear(200, 10),
...     nn.Softmax()), input)
...
>>> model.save('checkpoint/test')
>>> model.load('checkpoint/test')
parameters ( *args, **kwargs )

parameters

Returns a list of parameters of the model.

Returns

A list of Parameter in static graph. A list of ParamBase in dynamic graph.

Examples

>>> import paddle
>>> import paddle.nn as nn
>>> from paddle.static import InputSpec
>>> paddle.seed(2023)
>>> input = InputSpec([None, 784], 'float32', 'x')

>>> model = paddle.Model(nn.Sequential(
...     nn.Linear(784, 200),
...     nn.Tanh(),
...     nn.Linear(200, 10)), input)
...
>>> params = model.parameters()
>>> print(params)
[Parameter containing:
Tensor(shape=[784, 200], dtype=float32, place=Place(cpu), stop_gradient=False,
[[ 0.05713400,  0.00314646, -0.03754271, ..., -0.02529256,
   0.04872842, -0.06670858],
 ...,
 [ 0.06268418,  0.06550254, -0.02103353, ...,  0.06395906,
   0.05509177, -0.06355451]]), Parameter containing:
Tensor(shape=[200], dtype=float32, place=Place(cpu), stop_gradient=False,
[0., 0., 0., ..., 0., 0.]), Parameter containing:
Tensor(shape=[200, 10], dtype=float32, place=Place(cpu), stop_gradient=False,
[[ 0.12933084,  0.07726504,  0.05336720, ...,  0.10865459,
   0.06605886,  0.13684085],
 ...,
 [-0.10171061, -0.01649965, -0.13420501, ...,  0.11190581,
  -0.12700224,  0.02916957]]), Parameter containing:
Tensor(shape=[10], dtype=float32, place=Place(cpu), stop_gradient=False,
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])]
prepare ( optimizer=None, loss=None, metrics=None, amp_configs=None )

prepare

Configures the model before runing.

Parameters
  • optimizer (Optimizer|None, optional) – Optimizer must be set in training and should be a Optimizer instance. It can be None in eval and test mode. Default: None.

  • loss (Loss|Callable|None, optional) – Loss function can be a paddle.nn.Layer instance or any callable function taken the predicted values and ground truth values as input. It can be None when there is no loss. Default: None.

  • metrics (Metric|list[Metric]|None, optional) – If metrics is set, all metrics will be calculated and output in train/eval mode. Default: None.

  • amp_configs (str|dict|None, optional) – AMP configurations. If AMP or pure float16 training is used, the key ‘level’ of ‘amp_configs’ should be set to ‘O1’ or ‘O2’ respectively. Otherwise, the value of ‘level’ defaults to ‘O0’, which means float32 training. In addition to ‘level’, parameters consistent with mixed precision API could also be passed in. The supported keys are: ‘init_loss_scaling’, ‘incr_ratio’, ‘decr_ratio’, ‘incr_every_n_steps’, ‘decr_every_n_nan_or_inf’, ‘use_dynamic_loss_scaling’, ‘custom_white_list’, ‘custom_black_list’, and ‘custom_black_varnames’or ‘use_fp16_guard’ is only supported in static graph mode. Mixed precision API documentations auto_cast and GradScaler could be referenced for details. For convenience, ‘amp_configs’ could be set to ‘O1’ or ‘O2’ if no more parameters are needed. ‘amp_configs’ could be None in float32 training. Default: None.

Returns

None

fit ( train_data=None, eval_data=None, batch_size=1, epochs=1, eval_freq=1, log_freq=10, save_dir=None, save_freq=1, verbose=2, drop_last=False, shuffle=True, num_workers=0, callbacks=None, accumulate_grad_batches=1, num_iters=None )

fit

Trains the model for a fixed number of epochs. If eval_data is set, evaluation will be done at the end of each epoch.

Parameters
  • train_data (Dataset|DataLoader, optional) – An iterable data loader is used for train. An instance of paddle paddle.io.Dataset or paddle.io.Dataloader is recomended. Default: None.

  • eval_data (Dataset|DataLoader, optional) – An iterable data loader is used for evaluation at the end of epoch. If None, will not do evaluation. An instance of paddle.io.Dataset or paddle.io.Dataloader is recomended. Default: None.

  • batch_size (int|list, optional) – The batch size of train_data and eval_data. When train_data and eval_data are both the instance of Dataloader, this parameter will be ignored. Default: 1.

  • epochs (int, optional) – The number of epochs to train the model. Default: 1.

  • eval_freq (int, optional) – The frequency, in number of epochs, an evalutation is performed. Default: 1.

  • log_freq (int, optional) – The frequency, in number of steps, the training logs are printed. Default: 10.

  • save_dir (str|None, optional) – The directory to save checkpoint during training. If None, will not save checkpoint. Default: None.

  • save_freq (int, optional) – The frequency, in number of epochs, to save checkpoint. Default: 1.

  • verbose (int, optional) – The verbosity mode, should be 0, 1, or 2. 0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2.

  • drop_last (bool, optional) – Whether drop the last incomplete batch of train_data when dataset size is not divisible by the batch size. When train_data is an instance of Dataloader, this parameter will be ignored. Default: False.

  • shuffle (bool, optional) – Whther to shuffle train_data. When train_data is an instance of Dataloader, this parameter will be ignored. Default: True.

  • num_workers (int, optional) – The number of subprocess to load data, 0 for no subprocess used and loading data in main process. When train_data and eval_data are both the instance of Dataloader, this parameter will be ignored. Default: 0.

  • callbacks (Callback|None, optional) – A list of Callback instances to apply during training. If None, ProgBarLogger and ModelCheckpoint are automatically inserted. Default: None.

  • accumulate_grad_batches (int, optional) – The number of batches to accumulate gradident during training process before optimizer updates. It can mimic large batch size. Default: 1.

  • num_iters (int|None, optional) – The number of iterations to evaluate the model. If None, evaluate on whole input dataset, otherwise, evaluate num_iters times. Default: None.

Returns

None

Examples

  1. An example use Dataset and set batch size, shuffle in fit. How to make a batch is done internally.

>>> 
>>> import paddle
>>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import MNIST
>>> from paddle.static import InputSpec

>>> dynamic = True
>>> if not dynamic:
...     paddle.enable_static()
...
>>> transform = T.Compose([T.Transpose(),
...                        T.Normalize([127.5], [127.5])])
>>> 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')

>>> 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_dataset,
...             val_dataset,
...             epochs=2,
...             batch_size=64,
...             save_dir='mnist_checkpoint')
...
  1. An example use DataLoader, batch size and shuffle is set in DataLoader.

>>> 
>>> import paddle
>>> import paddle.vision.transforms as T
>>> from paddle.vision.datasets import MNIST
>>> from paddle.static import InputSpec

>>> dynamic = True
>>> if not dynamic:
...     paddle.enable_static()
...
>>> 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')

>>> 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')
...
evaluate ( eval_data, batch_size=1, log_freq=10, verbose=2, num_workers=0, callbacks=None, num_iters=None )

evaluate

Evaluate the loss and metrics of the model on input dataset.

Parameters
  • eval_data (Dataset|DataLoader) – An iterable data loader is used for evaluation. An instance of paddle.io.Dataset or paddle.io.Dataloader is recomended.

  • batch_size (int, optional) – The batch size of train_data and eval_data. When eval_data is the instance of Dataloader, this argument will be ignored. Default: 1.

  • log_freq (int, optional) – The frequency, in number of steps, the eval logs are printed. Default: 10.

  • verbose (int, optional) – The verbosity mode, should be 0, 1, or 2. 0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2.

  • num_workers (int, optional) – The number of subprocess to load data, 0 for no subprocess used and loading data in main process. When train_data and eval_data are both the instance of Dataloader, this parameter will be ignored. Default: 0.

  • callbacks (Callback|None, optional) – A list of Callback instances to apply during training. If None, ProgBarLogger and ModelCheckpoint are automatically inserted. Default: None.

  • num_iters (int|None, optional) – The number of iterations to evaluate the model. If None, evaluate on whole input dataset, otherwise, evaluate num_iters times. Default: None.

Returns

Result of metric. The key is the names of Metric,

value is a scalar or numpy.array.

Return type

dict

Examples

>>> 
>>> import paddle
>>> import paddle.vision.transforms as T
>>> from paddle.static import InputSpec

>>> # declarative mode
>>> transform = T.Compose([T.Transpose(),
...                        T.Normalize([127.5], [127.5])])
>>> val_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)

>>> input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
>>> label = InputSpec([None, 1], 'int64', 'label')
>>> 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)
{'acc': 0.0699}
predict ( test_data, batch_size=1, num_workers=0, stack_outputs=False, verbose=1, callbacks=None )

predict

Compute the output predictions on testing data.

Parameters
  • test_data (Dataset|DataLoader) – An iterable data loader is used for predict. An instance of paddle.io.Dataset or paddle.io.Dataloader is recomended.

  • batch_size (int, optional) – The batch size of test_data. When test_data is the instance of Dataloader, this argument will be ignored. Default: 1.

  • num_workers (int, optional) – 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, this argument will be ignored. Default: 0.

  • stack_outputs (bool, optional) – 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 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 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.

  • verbose (int, optional) – The verbosity mode, should be 0, 1, or 2. 0 = silent, 1 = progress bar, 2 = one line per batch. Default: 1.

  • callbacks (Callback, optional) – A Callback instance, Default: None.

Returns

output of models.

Return type

list

Examples

>>> import numpy as np
>>> import paddle
>>> from paddle.static import InputSpec

>>> class MnistDataset(paddle.vision.datasets.MNIST):
...     def __init__(self, mode, return_label=True):
...         super().__init__(mode=mode)
...         self.return_label = return_label
...
...     def __getitem__(self, idx):
...         img = np.reshape(self.images[idx], [1, 28, 28])
...         if self.return_label:
...             return img, np.array(self.labels[idx]).astype('int64')
...         return img
...
...     def __len__(self):
...         return len(self.images)
...
>>> test_dataset = MnistDataset(mode='test', return_label=False)

>>> # imperative mode
>>> input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
>>> 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)
157 (64, 10)
>>> # declarative mode
>>> device = paddle.set_device('cpu')
>>> paddle.enable_static()
>>> input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
>>> 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)
157 (64, 10)
summary ( input_size=None, dtype=None ) [source]

summary

Prints a string summary of the network.

Parameters
  • input_size (tuple|InputSpec|list[tuple|InputSpec], optional) – Size of input tensor. if not set, input_size will get from self._inputs if network only have one input, input_size can be tuple or InputSpec. if model have multiple input, input_size must be a list which contain every input’s shape. Default: None.

  • dtype (str, optional) – If dtype is None, ‘float32’ will be used, Default: None.

Returns

A summary of the network including total params and total trainable params.

Return type

Dict

Examples

>>> import paddle
>>> from paddle.static import InputSpec

>>> 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())
>>> params_info = model.summary()
>>> print(params_info)
---------------------------------------------------------------------------
Layer (type)       Input Shape          Output Shape         Param #
===========================================================================
  Conv2D-1       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
    ReLU-1        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
  MaxPool2D-1     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
  Conv2D-2       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
    ReLU-2       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
  MaxPool2D-2    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
  Linear-1          [[1, 400]]            [1, 120]           48,120
  Linear-2          [[1, 120]]            [1, 84]            10,164
  Linear-3          [[1, 84]]             [1, 10]              850
===========================================================================
Total params: 61,610
Trainable params: 61,610
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.11
Params size (MB): 0.24
Estimated Total Size (MB): 0.35
---------------------------------------------------------------------------
{'total_params': 61610, 'trainable_params': 61610}