mxnet.executor

Symbolic Executor component of MXNet.

Classes

Executor(sym, device, args, args_grad, …)

Executor is the object providing efficient symbolic and imperative graph execution and optimization.

class Executor(sym, device, args, args_grad, grad_req, aux_states, static_alloc=False)[source]

Bases: object

Executor is the object providing efficient symbolic and imperative graph execution and optimization.

Examples

Attributes

arg_arrays

the argument array

arg_dict

Get dictionary representation of argument arrrays.

aux_arrays

the auxilary argument array

aux_dict

Get dictionary representation of auxiliary states arrays.

grad_arrays

the gradient array

grad_dict

Get dictionary representation of gradient arrays.

output_dict

Get dictionary representation of output arrays.

Methods

backward([out_grads])

Do backward pass to get the gradient of arguments.

copy_params_from(arg_params[, aux_params, …])

Copy parameters from arg_params, aux_params into executor’s internal array.

forward([is_train])

Calculate the outputs specified by the bound symbol.

get_optimized_symbol()

Get an optimized version of the symbol from the executor.

>>> # typical approach to create an executor is to bind symbol
>>> a = mx.sym.var('a')
>>> b = mx.sym.var('b')
>>> c = 2 * a + b
>>> texec = c._bind(mx.cpu(), {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])})
property arg_arrays

the argument array

property arg_dict

Get dictionary representation of argument arrrays.

Returns

arg_dict – The dictionary that maps the names of arguments to NDArrays.

Return type

dict of str to NDArray

:raises ValueError : if there are duplicated names in the arguments.:

property aux_arrays

the auxilary argument array

property aux_dict

Get dictionary representation of auxiliary states arrays.

Returns

aux_dict – The dictionary that maps name of auxiliary states to NDArrays.

Return type

dict of str to NDArray

:raises ValueError : if there are duplicated names in the auxiliary states.:

backward(out_grads=None)[source]

Do backward pass to get the gradient of arguments.

Parameters
  • out_grads (NDArray or list of NDArray or dict of str to NDArray, optional) – Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function.

  • is_train (bool, default True) – Whether this backward is for training or inference. Note that in rare cases you want to call backward with is_train=False to get gradient during inference.

copy_params_from(arg_params, aux_params=None, allow_extra_params=False)[source]

Copy parameters from arg_params, aux_params into executor’s internal array.

Parameters
  • arg_params (dict of str to NDArray) – Parameters, dict of name to NDArray of arguments.

  • aux_params (dict of str to NDArray, optional) – Parameters, dict of name to NDArray of auxiliary states.

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

Raises

ValueError – If there is additional parameters in the dict but allow_extra_params=False.

Examples

>>> # set parameters with existing model checkpoint
>>> model_prefix = 'mx_mlp'
>>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 0)
>>> texec.copy_params_from(arg_params, aux_params)
forward(is_train=False, **kwargs)[source]

Calculate the outputs specified by the bound symbol.

Parameters
  • is_train (bool, optional) – Whether this forward is for evaluation purpose. If True, a backward call is expected to follow.

  • **kwargs – Additional specification of input arguments.

Examples

>>> # doing forward by specifying data
>>> texec.forward(is_train=True, data=mydata)
>>> # doing forward by not specifying things, but copy to the executor before hand
>>> mydata.copyto(texec.arg_dict['data'])
>>> texec.forward(is_train=True)
>>> # doing forward by specifying data and get outputs
>>> outputs = texec.forward(is_train=True, data=mydata)
>>> print(outputs[0].asnumpy())
get_optimized_symbol()[source]

Get an optimized version of the symbol from the executor.

Returns

symbol – Optimized symbol from the executor.

Return type

Symbol

property grad_arrays

the gradient array

property grad_dict

Get dictionary representation of gradient arrays.

Returns

grad_dict – The dictionary that maps name of arguments to gradient arrays.

Return type

dict of str to NDArray

property output_dict

Get dictionary representation of output arrays.

Returns

output_dict – The dictionary that maps name of output names to NDArrays.

Return type

dict of str to NDArray

:raises ValueError : if there are duplicated names in the outputs.: