mxnet.model

MXNet model module

Classes

BatchEndParam

BatchEndParams(epoch, nbatch, eval_metric, locals)

FeedForward(symbol[, ctx, num_epoch, …])

Model class of MXNet for training and predicting feedforward nets.

Functions

load_checkpoint(prefix, epoch)

Load model checkpoint from file.

load_params(prefix, epoch)

Load params from a file

save_checkpoint(prefix, epoch, symbol, …)

Checkpoint the model data into file.

mxnet.model.BatchEndParam

epoch

Alias for field number 0

eval_metric

Alias for field number 2

locals

Alias for field number 3

nbatch

Alias for field number 1

Attributes

alias of mxnet.model.BatchEndParams

class mxnet.model.FeedForward(symbol, ctx=None, num_epoch=None, epoch_size=None, optimizer='sgd', initializer=<mxnet.initializer.Uniform object>, numpy_batch_size=128, arg_params=None, aux_params=None, allow_extra_params=False, begin_epoch=0, **kwargs)[source]

Bases: object

Model class of MXNet for training and predicting feedforward nets. This class is designed for a single-data single output supervised network.

Parameters
  • symbol (Symbol) – The symbol configuration of computation network.

  • ctx (Context or list of Context, optional) – The device context of training and prediction. To use multi GPU training, pass in a list of gpu contexts.

  • num_epoch (int, optional) – Training parameter, number of training epochs(epochs).

  • epoch_size (int, optional) – Number of batches in a epoch. In default, it is set to ceil(num_train_examples / batch_size).

  • optimizer (str or Optimizer, optional) – Training parameter, name or optimizer object for training.

  • initializer (initializer function, optional) – Training parameter, the initialization scheme used.

  • numpy_batch_size (int, optional) – The batch size of training data. Only needed when input array is numpy.

  • arg_params (dict of str to NDArray, optional) – Model parameter, dict of name to NDArray of net’s weights.

  • aux_params (dict of str to NDArray, optional) – Model parameter, dict of name to NDArray of net’s auxiliary states.

  • allow_extra_params (boolean, optional) – Whether allow extra parameters that are not needed by symbol to be passed by aux_params and arg_params. If this is True, no error will be thrown when aux_params and arg_params contain more parameters than needed.

  • begin_epoch (int, optional) – The begining training epoch.

  • kwargs (dict) – The additional keyword arguments passed to optimizer.

Methods

create(symbol, X[, y, ctx, num_epoch, …])

Functional style to create a model.

fit(X[, y, eval_data, eval_metric, …])

Fit the model.

load(prefix, epoch[, ctx])

Load model checkpoint from file.

predict(X[, num_batch, return_data, reset])

Run the prediction, always only use one device.

save(prefix[, epoch, remove_amp_cast])

Checkpoint the model checkpoint into file.

score(X[, eval_metric, num_batch, …])

Run the model given an input and calculate the score as assessed by an evaluation metric.

static create(symbol, X, y=None, ctx=None, num_epoch=None, epoch_size=None, optimizer='sgd', initializer=<mxnet.initializer.Uniform object>, eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', logger=None, work_load_list=None, eval_end_callback=<mxnet.callback.LogValidationMetricsCallback object>, eval_batch_end_callback=None, **kwargs)[source]

Functional style to create a model. This function is more consistent with functional languages such as R, where mutation is not allowed.

Parameters
  • symbol (Symbol) – The symbol configuration of a computation network.

  • X (DataIter) – Training data.

  • y (numpy.ndarray, optional) – If X is a numpy.ndarray, y must be set.

  • ctx (Context or list of Context, optional) – The device context of training and prediction. To use multi-GPU training, pass in a list of GPU contexts.

  • num_epoch (int, optional) – The number of training epochs(epochs).

  • epoch_size (int, optional) – Number of batches in a epoch. In default, it is set to ceil(num_train_examples / batch_size).

  • optimizer (str or Optimizer, optional) – The name of the chosen optimizer, or an optimizer object, used for training.

  • initializer (initializer function, optional) – The initialization scheme used.

  • eval_data (DataIter or numpy.ndarray pair) – If eval_set is numpy.ndarray pair, it should be (valid_data, valid_label).

  • eval_metric (metric.EvalMetric or str or callable) – The evaluation metric. Can be the name of an evaluation metric or a custom evaluation function that returns statistics based on a minibatch.

  • epoch_end_callback (callable(epoch, symbol, arg_params, aux_states)) – A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch.

  • batch_end_callback (callable(epoch)) – A callback that is invoked at end of each batch for print purposes.

  • kvstore (KVStore or str, optional) – The KVStore or a string kvstore type: ‘local’, ‘dist_sync’, ‘dis_async’. Defaults to ‘local’, often no need to change for single machine.

  • logger (logging logger, optional) – When not specified, default logger will be used.

  • work_load_list (list of float or int, optional) – The list of work load for different devices, in the same order as ctx.

fit(X, y=None, eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', logger=None, work_load_list=None, monitor=None, eval_end_callback=<mxnet.callback.LogValidationMetricsCallback object>, eval_batch_end_callback=None)[source]

Fit the model.

Parameters
  • X (DataIter, or numpy.ndarray/NDArray) – Training data. If X is a DataIter, the name or (if name not available) the position of its outputs should match the corresponding variable names defined in the symbolic graph.

  • y (numpy.ndarray/NDArray, optional) – Training set label. If X is numpy.ndarray or NDArray, y is required to be set. While y can be 1D or 2D (with 2nd dimension as 1), its first dimension must be the same as X, i.e. the number of data points and labels should be equal.

  • eval_data (DataIter or numpy.ndarray/list/NDArray pair) – If eval_data is numpy.ndarray/list/NDArray pair, it should be (valid_data, valid_label).

  • eval_metric (metric.EvalMetric or str or callable) – The evaluation metric. This could be the name of evaluation metric or a custom evaluation function that returns statistics based on a minibatch.

  • epoch_end_callback (callable(epoch, symbol, arg_params, aux_states)) – A callback that is invoked at end of each epoch. This can be used to checkpoint model each epoch.

  • batch_end_callback (callable(epoch)) – A callback that is invoked at end of each batch for purposes of printing.

  • kvstore (KVStore or str, optional) – The KVStore or a string kvstore type: ‘local’, ‘dist_sync’, ‘dist_async’ In default uses ‘local’, often no need to change for single machiine.

  • logger (logging logger, optional) – When not specified, default logger will be used.

  • work_load_list (float or int, optional) – The list of work load for different devices, in the same order as ctx.

Note

KVStore behavior - ‘local’, multi-devices on a single machine, will automatically choose best type. - ‘dist_sync’, multiple machines communicating via BSP. - ‘dist_async’, multiple machines with asynchronous communication.

static load(prefix, epoch, ctx=None, **kwargs)[source]

Load model checkpoint from file.

Parameters
  • prefix (str) – Prefix of model name.

  • epoch (int) – epoch number of model we would like to load.

  • ctx (Context or list of Context, optional) – The device context of training and prediction.

  • kwargs (dict) – Other parameters for model, including num_epoch, optimizer and numpy_batch_size.

Returns

model – The loaded model that can be used for prediction.

Return type

FeedForward

Notes

  • prefix-symbol.json will be saved for symbol.

  • prefix-epoch.params will be saved for parameters.

predict(X, num_batch=None, return_data=False, reset=True)[source]

Run the prediction, always only use one device.

Parameters
  • X (mxnet.DataIter) –

  • num_batch (int or None) – The number of batch to run. Go though all batches if None.

Returns

y – The predicted value of the output.

Return type

numpy.ndarray or a list of numpy.ndarray if the network has multiple outputs.

save(prefix, epoch=None, remove_amp_cast=True)[source]

Checkpoint the model checkpoint into file. You can also use pickle to do the job if you only work on Python. The advantage of load and save (as compared to pickle) is that the resulting file can be loaded from other MXNet language bindings. One can also directly load/save from/to cloud storage(S3, HDFS)

Parameters
  • prefix (str) – Prefix of model name.

  • remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.

Notes

  • prefix-symbol.json will be saved for symbol.

  • prefix-epoch.params will be saved for parameters.

score(X, eval_metric='acc', num_batch=None, batch_end_callback=None, reset=True)[source]

Run the model given an input and calculate the score as assessed by an evaluation metric.

Parameters
  • X (mxnet.DataIter) –

  • eval_metric (metric.metric) – The metric for calculating score.

  • num_batch (int or None) – The number of batches to run. Go though all batches if None.

Returns

s – The final score.

Return type

float

mxnet.model.load_checkpoint(prefix, epoch)[source]

Load model checkpoint from file.

Parameters
  • prefix (str) – Prefix of model name.

  • epoch (int) – Epoch number of model we would like to load.

Returns

  • symbol (Symbol) – The symbol configuration of computation network.

  • arg_params (dict of str to NDArray) – Model parameter, dict of name to NDArray of net’s weights.

  • aux_params (dict of str to NDArray) – Model parameter, dict of name to NDArray of net’s auxiliary states.

Notes

  • Symbol will be loaded from prefix-symbol.json.

  • Parameters will be loaded from prefix-epoch.params.

mxnet.model.load_params(prefix, epoch)[source]

Load params from a file

mxnet.model.save_checkpoint(prefix, epoch, symbol, arg_params, aux_params, remove_amp_cast=True)[source]

Checkpoint the model data into file.

Parameters
  • prefix (str) – Prefix of model name.

  • epoch (int) – The epoch number of the model.

  • symbol (Symbol) – The input Symbol.

  • arg_params (dict of str to NDArray) – Model parameter, dict of name to NDArray of net’s weights.

  • aux_params (dict of str to NDArray) – Model parameter, dict of name to NDArray of net’s auxiliary states.

  • remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.

Notes

  • prefix-symbol.json will be saved for symbol.

  • prefix-epoch.params will be saved for parameters.