Block objects¶
A neural network is often constructed block by block. A block could be a single function, e.g. sigmoid, a layer with parameters, e.g. a dense layer, or a combination of multiple layers and functions. The Block object in the Gluon package is the base object for such a block.
Example
Define a model that has a dense layer and return the summed results:
>>> from mxnet import np, npx, gluon
>>> npx.set_np()
>>> class MyModel(gluon.Block):
... def __init__(self, **kwargs):
... super(MyModel, self).__init__(**kwargs)
... self.dense = gluon.nn.Dense(10)
... def forward(self, x):
... return self.dense(x).sum()
Initialize this model:
>>> model = MyModel()
>>> model.initialize()
Feed in a random input to print the forward output:
>>> x = np.random.uniform(size=(10,2))
>>> print(model(x))
-0.5192252
Block([prefix, params]) |
Base class for all neural network layers and models. |
Note
Block can be accessed by either mxnet.gluon.Block and
mxnet.gluon.nn.Block. Similar for HybridBlock and SymbolBlock.
Running computations¶
Block.forward(self, \*args) |
Overrides to implement forward computation using NDArray. |
Block.__call__(self, \*args) |
Calls forward. |
Handling parameters¶
Block.initialize(self[, init, ctx, verbose, …]) |
Initializes Parameter s of this Block and its children. |
Block.save_parameters(self, filename[, …]) |
Save parameters to file. |
Block.load_parameters(self, filename[, ctx, …]) |
Load parameters from file previously saved by save_parameters. |
Block.collect_params(self[, select]) |
Returns a ParameterDict containing this Block and all of its children’s Parameters(default), also can returns the select ParameterDict which match some given regular expressions. |
Block.cast(self, dtype) |
Cast this Block to use another data type. |
Block.apply(self, fn) |
Applies fn recursively to every child block as well as self. |
Summarization¶
Block.summary(self, \*inputs) |
Print the summary of the model’s output and parameters. |
Advanced APIs for customization¶
Block.name_scope(self) |
Returns a name space object managing a child Block and parameter names. |
Block.register_child(self, block[, name]) |
Registers block as a child of self. |
Block.register_forward_hook(self, hook) |
Registers a forward hook on the block. |
Block.register_forward_pre_hook(self, hook) |
Registers a forward pre-hook on the block. |
Attributes¶
Block.name |
Name of this Block, without ‘_’ in the end. |
Block.params |
Returns this Block’s parameter dictionary (does not include its children’s parameters). |
Block.prefix |
Prefix of this Block. |
Warning
The following two APIs are deprecated since v1.2.1.
Block.save_params(self, filename) |
[Deprecated] Please use save_parameters. |
Block.load_params(self, filename[, ctx, …]) |
[Deprecated] Please use load_parameters. |
Hybridizable block¶
HybridBlock([prefix, params]) |
HybridBlock supports forwarding with both Symbol and NDArray. |
HybridBlock is a subclass of Block that provides two additional
functions
HybridBlock.hybridize(self[, active]) |
Activates or deactivates HybridBlock s recursively. |
HybridBlock.hybrid_forward(self, F, x, …) |
Overrides to construct symbolic graph for this Block. |
Block construction with Symbol¶
SymbolBlock(outputs, inputs[, params]) |
Construct block from symbol. |