gluon.loss¶
Gluon provides pre-defined loss functions in the mxnet.gluon.loss
module.
losses for training neural networks
Classes
|
Base class for loss. |
|
Calculates the mean squared error between label and pred. |
|
Calculates the mean absolute error between label and pred. |
The cross-entropy loss for binary classification. |
|
The cross-entropy loss for binary classification. |
|
|
Computes the softmax cross entropy loss. |
Computes the softmax cross entropy loss. |
|
|
The Kullback-Leibler divergence loss. |
|
Connectionist Temporal Classification Loss. |
|
Calculates smoothed L1 loss that is equal to L1 loss if absolute error exceeds rho but is equal to L2 loss otherwise. |
|
Calculates the hinge loss function often used in SVMs: |
|
Calculates the soft-margin loss function used in SVMs: |
|
Calculates the logistic loss (for binary losses only): |
|
Calculates triplet loss given three input tensors and a positive margin. |
|
For a target (Random Variable) in a Poisson distribution, the function calculates the Negative Log likelihood loss. |
|
For a target label 1 or -1, vectors input1 and input2, the function computes the cosine distance between the vectors. |
|
Calculates Batchwise Smoothed Deep Metric Learning (SDML) Loss given two input tensors and a smoothing weight SDM Loss learns similarity between paired samples by using unpaired samples in the minibatch as potential negative examples. |
-
class
Loss
(weight, batch_axis, **kwargs)[source]¶ Bases:
mxnet.gluon.block.HybridBlock
Base class for loss.
- Parameters
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(x, *args)Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
L2Loss
(weight=1.0, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates the mean squared error between label and pred.
L=12∑i|labeli−predi|2.Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).label and pred can have arbitrary shape as long as they have the same number of elements.
- Parameters
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape
label: target tensor with the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
L1Loss
(weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates the mean absolute error between label and pred.
L=∑i|labeli−predi|.Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).label and pred can have arbitrary shape as long as they have the same number of elements.
- Parameters
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape
label: target tensor with the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
SigmoidBinaryCrossEntropyLoss
(from_sigmoid=False, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
The cross-entropy loss for binary classification. (alias: SigmoidBCELoss)
BCE loss is useful when training logistic regression. If from_sigmoid is False (default), this loss computes:
prob=11+exp(−pred)L=−∑ilabeli∗log(probi)∗pos_weight+(1−labeli)∗log(1−probi)Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight, pos_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).If from_sigmoid is True, this loss computes:
L=−∑ilabeli∗log(predi)∗pos_weight+(1−labeli)∗log(1−predi)A tensor pos_weight > 1 decreases the false negative count, hence increasing the recall. Conversely setting pos_weight < 1 decreases the false positive count and increases the precision.
pred and label can have arbitrary shape as long as they have the same number of elements.
- Parameters
from_sigmoid (bool, default is False) – Whether the input is from the output of sigmoid. Set this to false will make the loss calculate sigmoid and BCE together, which is more numerically stable through log-sum-exp trick.
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape
label: target tensor with values in range [0, 1]. Must have the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
pos_weight: a weighting tensor of positive examples. Must be a vector with length equal to the number of classes.For example, if pred has shape (64, 10), pos_weight should have shape (1, 10).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None, pos_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
SigmoidBCELoss
¶
|
Returns this |
Methods
|
Applies |
|
Returns a |
|
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface. |
|
Overrides to construct symbolic graph for this Block. |
|
Activates or deactivates |
|
Infers shape of Parameters from inputs. |
|
Infers data type of Parameters from inputs. |
|
Initializes |
|
Load a model saved using the save API |
|
Load parameters from dict |
|
Load parameters from file previously saved by save_parameters. |
|
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. |
|
Registers a forward hook on the block. |
|
Registers a forward pre-hook on the block. |
|
Install op hook for block recursively. |
|
This function has been deprecated. |
|
Re-assign all Parameters to other devices. |
|
Save the model architecture and parameters to load again later |
|
Save parameters to file. |
|
Set an attribute to a new value for all Parameters. |
|
Share parameters recursively inside the model. |
|
Print the summary of the model’s output and parameters. |
|
Sets all Parameters’ gradient buffer to 0. |
Attributes
-
class
SoftmaxCrossEntropyLoss
(axis=-1, sparse_label=True, from_logits=False, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Computes the softmax cross entropy loss. (alias: SoftmaxCELoss)
If sparse_label is True (default), label should contain integer category indicators:
p=softmax(pred)L=−∑ilogpi,labeliMethods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).label’s shape should be pred’s shape with the axis dimension removed. i.e. for pred with shape (1,2,3,4) and axis = 2, label’s shape should be (1,2,4).
If sparse_label is False, label should contain probability distribution and label’s shape should be the same with pred:
p=softmax(pred)L=−∑i∑jlabeljlogpij- Parameters
axis (int, default -1) – The axis to sum over when computing softmax and entropy.
sparse_label (bool, default True) – Whether label is an integer array instead of probability distribution.
from_logits (bool, default False) – Whether input is a log probability (usually from log_softmax) instead of unnormalized numbers.
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: the prediction tensor, where the batch_axis dimension ranges over batch size and axis dimension ranges over the number of classes.
label: the truth tensor. When sparse_label is True, label’s shape should be pred’s shape with the axis dimension removed. i.e. for pred with shape (1,2,3,4) and axis = 2, label’s shape should be (1,2,4) and values should be integers between 0 and 2. If sparse_label is False, label’s shape must be the same as pred and values should be floats in the range [0, 1].
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
SoftmaxCELoss
¶
|
Returns this |
Methods
|
Applies |
|
Returns a |
|
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface. |
|
Overrides to construct symbolic graph for this Block. |
|
Activates or deactivates |
|
Infers shape of Parameters from inputs. |
|
Infers data type of Parameters from inputs. |
|
Initializes |
|
Load a model saved using the save API |
|
Load parameters from dict |
|
Load parameters from file previously saved by save_parameters. |
|
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. |
|
Registers a forward hook on the block. |
|
Registers a forward pre-hook on the block. |
|
Install op hook for block recursively. |
|
This function has been deprecated. |
|
Re-assign all Parameters to other devices. |
|
Save the model architecture and parameters to load again later |
|
Save parameters to file. |
|
Set an attribute to a new value for all Parameters. |
|
Share parameters recursively inside the model. |
|
Print the summary of the model’s output and parameters. |
|
Sets all Parameters’ gradient buffer to 0. |
Attributes
-
class
KLDivLoss
(from_logits=True, axis=-1, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
The Kullback-Leibler divergence loss.
KL divergence measures the distance between contiguous distributions. It can be used to minimize information loss when approximating a distribution. If from_logits is True (default), loss is defined as:
L=∑ilabeli∗[log(labeli)−predi]Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).If from_logits is False, loss is defined as:
prob=softmax(pred)L=∑ilabeli∗[log(labeli)−log(probi)]label and pred can have arbitrary shape as long as they have the same number of elements.
- Parameters
from_logits (bool, default is True) – Whether the input is log probability (usually from log_softmax) instead of unnormalized numbers.
axis (int, default -1) – The dimension along with to compute softmax. Only used when from_logits is False.
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape. If from_logits is True, pred should be log probabilities. Otherwise, it should be unnormalized predictions, i.e. from a dense layer.
label: truth tensor with values in range (0, 1). Must have the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
References
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
CTCLoss
(layout='NTC', label_layout='NT', weight=None, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Connectionist Temporal Classification Loss.
- Parameters
layout (str, default 'NTC') – Layout of prediction tensor. ‘N’, ‘T’, ‘C’ stands for batch size, sequence length, and alphabet_size respectively.
label_layout (str, default 'NT') – Layout of the labels. ‘N’, ‘T’ stands for batch size, and sequence length respectively.
weight (float or None) – Global scalar weight for loss.
Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, pred_lengths, …])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).- Inputs:
pred: unnormalized prediction tensor (before softmax). Its shape depends on layout. If layout is ‘TNC’, pred should have shape (sequence_length, batch_size, alphabet_size). Note that in the last dimension, index alphabet_size-1 is reserved for internal use as blank label. So alphabet_size is one plus the actual alphabet size.
label: zero-based label tensor. Its shape depends on label_layout. If label_layout is ‘TN’, label should have shape (label_sequence_length, batch_size).
pred_lengths: optional (default None), used for specifying the length of each entry when different pred entries in the same batch have different lengths. pred_lengths should have shape (batch_size,).
label_lengths: optional (default None), used for specifying the length of each entry when different label entries in the same batch have different lengths. label_lengths should have shape (batch_size,).
- Outputs:
loss: output loss has shape (batch_size,).
Example: suppose the vocabulary is [a, b, c], and in one batch we have three sequences ‘ba’, ‘cbb’, and ‘abac’. We can index the labels as {‘a’: 0, ‘b’: 1, ‘c’: 2, blank: 3}. Then alphabet_size should be 4, where label 3 is reserved for internal use by CTCLoss. We then need to pad each sequence with -1 to make a rectangular label tensor:
[[1, 0, -1, -1], [2, 1, 1, -1], [0, 1, 0, 2]]
References
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, pred_lengths=None, label_lengths=None, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
HuberLoss
(rho=1, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates smoothed L1 loss that is equal to L1 loss if absolute error exceeds rho but is equal to L2 loss otherwise. Also called SmoothedL1 loss.
L=∑i{12rho(labeli−predi)2 if |labeli−predi|<rho|labeli−predi|−rho2 otherwiseMethods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).label and pred can have arbitrary shape as long as they have the same number of elements.
- Parameters
rho (float, default 1) – Threshold for trimmed mean estimator.
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape
label: target tensor with the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
HingeLoss
(margin=1, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates the hinge loss function often used in SVMs:
L=∑imax(0,margin−predi⋅labeli)Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).where pred is the classifier prediction and label is the target tensor containing values -1 or 1. label and pred must have the same number of elements.
- Parameters
margin (float) – The margin in hinge loss. Defaults to 1.0
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape.
label: truth tensor with values -1 or 1. Must have the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
SquaredHingeLoss
(margin=1, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates the soft-margin loss function used in SVMs:
L=∑imax(0,margin−predi⋅labeli)2Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).where pred is the classifier prediction and label is the target tensor containing values -1 or 1. label and pred can have arbitrary shape as long as they have the same number of elements.
- Parameters
margin (float) – The margin in hinge loss. Defaults to 1.0
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape
label: truth tensor with values -1 or 1. Must have the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
LogisticLoss
(weight=None, batch_axis=0, label_format='signed', **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates the logistic loss (for binary losses only):
L=∑ilog(1+exp(−predi⋅labeli))Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).where pred is the classifier prediction and label is the target tensor containing values -1 or 1 (0 or 1 if label_format is binary). label and pred can have arbitrary shape as long as they have the same number of elements.
- Parameters
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
label_format (str, default 'signed') – Can be either ‘signed’ or ‘binary’. If the label_format is ‘signed’, all label values should be either -1 or 1. If the label_format is ‘binary’, all label values should be either 0 or 1.
Inputs –
pred: prediction tensor with arbitrary shape.
label: truth tensor with values -1/1 (label_format is ‘signed’) or 0/1 (label_format is ‘binary’). Must have the same size as pred.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
Outputs –
loss: loss tensor with shape (batch_size,). Dimenions other than batch_axis are averaged out.
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
TripletLoss
(margin=1, weight=None, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates triplet loss given three input tensors and a positive margin. Triplet loss measures the relative similarity between a positive example, a negative example, and prediction:
L=∑imaxMethods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, positive, negative[, …])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).positive, negative, and ‘pred’ can have arbitrary shape as long as they have the same number of elements.
- Parameters
margin (float) – Margin of separation between correct and incorrect pair.
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
- Inputs:
pred: prediction tensor with arbitrary shape
positive: positive example tensor with arbitrary shape. Must have the same size as pred.
negative: negative example tensor with arbitrary shape Must have the same size as pred.
- Outputs:
loss: loss tensor with shape (batch_size,).
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, positive, negative, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
PoissonNLLLoss
(weight=None, from_logits=True, batch_axis=0, compute_full=False, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
For a target (Random Variable) in a Poisson distribution, the function calculates the Negative Log likelihood loss. PoissonNLLLoss measures the loss accrued from a poisson regression prediction made by the model.
L = \text{pred} - \text{target} * \log(\text{pred}) +\log(\text{target!})Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(pred, target[, sample_weight, epsilon])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).target, ‘pred’ can have arbitrary shape as long as they have the same number of elements.
- Parameters
from_logits (boolean, default True) – indicating whether log(predicted) value has already been computed. If True, the loss is computed as \exp(\text{pred}) - \text{target} * \text{pred}, and if False, then loss is computed as \text{pred} - \text{target} * \log(\text{pred}+\text{epsilon}).The default value
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
compute_full (boolean, default False) – Indicates whether to add an approximation(Stirling factor) for the Factorial term in the formula for the loss. The Stirling factor is: \text{target} * \log(\text{target}) - \text{target} + 0.5 * \log(2 * \pi * \text{target})
epsilon (float, default 1e-08) – This is to avoid calculating log(0) which is not defined.
- Inputs:
pred: Predicted value
target: Random variable(count or number) which belongs to a Poisson distribution.
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as pred. For example, if pred has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: Average loss (shape=(1,1)) of the loss tensor with shape (batch_size,).
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(pred, target, sample_weight=None, epsilon=1e-08)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
CosineEmbeddingLoss
(weight=None, batch_axis=0, margin=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
For a target label 1 or -1, vectors input1 and input2, the function computes the cosine distance between the vectors. This can be interpreted as how similar/dissimilar two input vectors are.
\begin{split}L = \sum_i \begin{cases} 1 - {cos\_sim({input1}_i, {input2}_i)} & \text{ if } {label}_i = 1\\ {cos\_sim({input1}_i, {input2}_i)} & \text{ if } {label}_i = -1 \end{cases}\\ cos\_sim(input1, input2) = \frac{{input1}_i.{input2}_i}{||{input1}_i||.||{input2}_i||}\end{split}Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(input1, input2, label[, sample_weight])Overrides to construct symbolic graph for this Block.
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).input1, input2 can have arbitrary shape as long as they have the same number of elements.
- Parameters
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
margin (float) – Margin of separation between correct and incorrect pair.
- Inputs:
input1: a tensor with arbitrary shape
input2: another tensor with same shape as pred to which input1 is compared for similarity and loss calculation
label: A 1-D tensor indicating for each pair input1 and input2, target label is 1 or -1
sample_weight: element-wise weighting tensor. Must be broadcastable to the same shape as input1. For example, if input1 has shape (64, 10) and you want to weigh each sample in the batch separately, sample_weight should have shape (64, 1).
- Outputs:
loss: The loss tensor with shape (batch_size,).
-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(input1, input2, label, sample_weight=None)[source]¶ Overrides to construct symbolic graph for this Block.
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.
-
class
SDMLLoss
(smoothing_parameter=0.3, weight=1.0, batch_axis=0, **kwargs)[source]¶ Bases:
mxnet.gluon.loss.Loss
Calculates Batchwise Smoothed Deep Metric Learning (SDML) Loss given two input tensors and a smoothing weight SDM Loss learns similarity between paired samples by using unpaired samples in the minibatch as potential negative examples.
The loss is described in greater detail in “Large Scale Question Paraphrase Retrieval with Smoothed Deep Metric Learning.” - by Bonadiman, Daniele, Anjishnu Kumar, and Arpit Mittal. arXiv preprint arXiv:1905.12786 (2019). URL: https://arxiv.org/pdf/1905.12786.pdf
According to the authors, this loss formulation achieves comparable or higher accuracy to Triplet Loss but converges much faster. The loss assumes that the items in both tensors in each minibatch are aligned such that x1[0] corresponds to x2[0] and all other datapoints in the minibatch are unrelated. x1 and x2 are minibatches of vectors.
- Parameters
smoothing_parameter (float) – Probability mass to be distributed over the minibatch. Must be < 1.0.
weight (float or None) – Global scalar weight for loss.
batch_axis (int, default 0) – The axis that represents mini-batch.
Inputs –
x1: Minibatch of data points with shape (batch_size, vector_dim)
x2: Minibatch of data points with shape (batch_size, vector_dim) Each item in x2 is a positive sample for the same index in x1. That is, x1[0] and x2[0] form a positive pair, x1[1] and x2[1] form a positive pair - and so on. All data points in different rows should be decorrelated
Outputs –
loss: loss tensor with shape (batch_size,).
Methods
apply
(fn)Applies
fn
recursively to every child block as well as self.collect_params
([select])Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.export
(path[, epoch, remove_amp_cast])Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
forward
(x1, x2)the function computes the kl divergence between the negative distances
hybridize
([active, partition_if_dynamic, …])Activates or deactivates
HybridBlock
s recursively.infer_shape
(*args)Infers shape of Parameters from inputs.
infer_type
(*args)Infers data type of Parameters from inputs.
initialize
([init, device, verbose, force_reinit])Initializes
Parameter
s of thisBlock
and its children.load
(prefix)Load a model saved using the save API
load_dict
(param_dict[, device, …])Load parameters from dict
load_parameters
(filename[, device, …])Load parameters from file previously saved by save_parameters.
optimize_for
(x, *args[, backend, clear, …])Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass.
register_forward_hook
(hook)Registers a forward hook on the block.
Registers a forward pre-hook on the block.
register_op_hook
(callback[, monitor_all])Install op hook for block recursively.
reset_ctx
(ctx)This function has been deprecated.
reset_device
(device)Re-assign all Parameters to other devices.
save
(prefix)Save the model architecture and parameters to load again later
save_parameters
(filename[, deduplicate])Save parameters to file.
setattr
(name, value)Set an attribute to a new value for all Parameters.
share_parameters
(shared)Share parameters recursively inside the model.
summary
(*inputs)Print the summary of the model’s output and parameters.
Sets all Parameters’ gradient buffer to 0.
Attributes
Returns this
Block
’s parameter dictionary (does not include its children’s parameters).-
apply
(fn)¶ Applies
fn
recursively to every child block as well as self.- Parameters
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Returns
- Return type
this block
-
collect_params
(select=None)¶ Returns a
Dict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectDict
which match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters
select (str) – regular expressions
- Returns
- Return type
The selected
Dict
-
export
(path, epoch=0, remove_amp_cast=True)¶ Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports or the C++ interface.
Note
When there are only one input, it will have name data. When there Are more than one inputs, they will be named as data0, data1, etc.
- Parameters
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
-
forward
(x1, x2)[source]¶ the function computes the kl divergence between the negative distances (internally it compute a softmax casting into probabilities) and the identity matrix.
This assumes that the two batches are aligned therefore the more similar vector should be the one having the same id.
Batch1 Batch2
President of France French President President of US American President
Given the question president of France in batch 1 the model will learn to predict french president comparing it with all the other vectors in batch 2
-
hybridize
(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶ Activates or deactivates
HybridBlock
s recursively. Has no effect on non-hybrid children.- Parameters
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
-
infer_shape
(*args)¶ Infers shape of Parameters from inputs.
-
infer_type
(*args)¶ Infers data type of Parameters from inputs.
-
initialize
(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children.- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
-
load
(prefix)¶ Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
-
load_dict
(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from dict
- Parameters
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
-
load_parameters
(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to initialize loaded parameters on.
allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this Block.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
-
optimize_for
(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶ Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
-
property
params
¶ Returns this
Block
’s parameter dictionary (does not include its children’s parameters).
-
register_forward_hook
(hook)¶ Registers a forward hook on the block.
The hook function is called immediately after
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_forward_pre_hook
(hook)¶ Registers a forward pre-hook on the block.
The hook function is called immediately before
forward()
. It should not modify the input or output.- Parameters
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Returns
- Return type
mxnet.gluon.utils.HookHandle
-
register_op_hook
(callback, monitor_all=False)¶ Install op hook for block recursively.
- Parameters
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
-
reset_ctx
(ctx)¶ This function has been deprecated. Please refer to
HybridBlock.reset_device
.
-
reset_device
(device)¶ Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters
device (Device or list of Device, default
device.current_device()
.) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
-
save
(prefix)¶ Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
-
save_parameters
(filename, deduplicate=False)¶ Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export()
.- Parameters
filename (str) – Path to file.
deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.
References
-
setattr
(name, value)¶ Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1
to sharedense0
’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters
shared (Dict) – Dict of the shared parameters.
- Returns
- Return type
this block
-
summary
(*inputs)¶ Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArray
is supported.
-
zero_grad
()¶ Sets all Parameters’ gradient buffer to 0.