HybridBlock¶
-
class
mxnet.gluon.nn.
HybridBlock
(prefix=None, params=None)[source]¶ HybridBlock supports forwarding with both Symbol and NDArray.
HybridBlock is similar to Block, with a few differences:
import mxnet as mx from mxnet.gluon import HybridBlock, nn class Model(HybridBlock): def __init__(self, **kwargs): super(Model, self).__init__(**kwargs) # use name_scope to give child Blocks appropriate names. with self.name_scope(): self.dense0 = nn.Dense(20) self.dense1 = nn.Dense(20) def hybrid_forward(self, F, x): x = F.relu(self.dense0(x)) return F.relu(self.dense1(x)) model = Model() model.initialize(ctx=mx.cpu(0)) model.hybridize() model(mx.nd.zeros((10, 10), ctx=mx.cpu(0)))
Forward computation in
HybridBlock
must be static to work withSymbol
s, i.e. you cannot callNDArray.asnumpy()
,NDArray.shape
,NDArray.dtype
, NDArray indexing (x[i]) etc on tensors. Also, you cannot use branching or loop logic that bases on non-constant expressions like random numbers or intermediate results, since they change the graph structure for each iteration.Before activating with
hybridize()
,HybridBlock
works just like normalBlock
. After activation,HybridBlock
will create a symbolic graph representing the forward computation and cache it. On subsequent forwards, the cached graph will be used instead ofhybrid_forward()
.Please see references for detailed tutorial.
References
Hybrid - Faster training and easy deployment
HybridBlock inherits all methods and attributes from
mxnet.gluon.nn.Block
. It adds the following additional methods:Symbol related methods
HybridBlock.hybridize
([active])Activates or deactivates
HybridBlock
s recursively.HybridBlock.hybrid_forward
(F, x, *args, **kwargs)Overrides to construct symbolic graph for this Block.
HybridBlock.export
(path[, epoch])Export HybridBlock to json format that can be loaded by SymbolBlock.imports, mxnet.mod.Module or the C++ interface.
HybridBlock.infer_shape
(*args)Infers shape of Parameters from inputs.
HybridBlock.infer_type
(*args)Infers data type of Parameters from inputs.
-
register_child
(block, name=None)[source]¶ Registers block as a child of self.
Block
s assigned to self as attributes will be registered automatically.
-
hybridize
(active=True, **kwargs)[source]¶ 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.
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.
-
cast
(dtype)[source]¶ Cast this Block to use another data type.
- Parameters
dtype (str or numpy.dtype) – The new data type.
-
export
(path, epoch=0)[source]¶ Export HybridBlock to json format that can be loaded by SymbolBlock.imports, mxnet.mod.Module 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) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number.
epoch (int) – Epoch number of saved model.
-
forward
(x, *args)[source]¶ Defines the forward computation. Arguments can be either
NDArray
orSymbol
.
-
hybrid_forward
(F, x, *args, **kwargs)[source]¶ Overrides to construct symbolic graph for this Block.
-
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
ParameterDict
containing thisBlock
and all of its children’s Parameters(default), also can returns the selectParameterDict
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
ParameterDict
-
initialize
(init=<mxnet.initializer.Uniform object>, ctx=None, verbose=False, force_reinit=False)¶ Initializes
Parameter
s of thisBlock
and its children. Equivalent toblock.collect_params().initialize(...)
- Parameters
init (Initializer) – Global default Initializer to be used when
Parameter.init()
isNone
. Otherwise,Parameter.init()
takes precedence.ctx (Context or list of Context) – Keeps a copy of Parameters on one or many context(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_parameters
(filename, ctx=None, allow_missing=False, ignore_extra=False)¶ Load parameters from file previously saved by save_parameters.
- Parameters
filename (str) – Path to parameter file.
ctx (Context or list of Context, default cpu()) – Context(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.
References
-
load_params
(filename, ctx=None, allow_missing=False, ignore_extra=False)¶ [Deprecated] Please use load_parameters.
Load parameters from file.
- filenamestr
Path to parameter file.
- ctxContext or list of Context, default cpu()
Context(s) to initialize loaded parameters on.
- allow_missingbool, default False
Whether to silently skip loading parameters not represents in the file.
- ignore_extrabool, default False
Whether to silently ignore parameters from the file that are not present in this Block.
-
name_scope
()¶ Returns a name space object managing a child
Block
and parameter names. Should be used within awith
statement:with self.name_scope(): self.dense = nn.Dense(20)
Please refer to naming tutorial for more info on prefix and naming.
-
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
-
save_parameters
(filename)¶ 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.
References
-
save_params
(filename)¶ [Deprecated] Please use save_parameters. Note that if you want load from SymbolBlock later, please use export instead.
Save parameters to file.
- filenamestr
Path to file.
-
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.
-