Table Of Contents
Table Of Contents

Block

class mxnet.gluon.nn.Block(prefix=None, params=None)[source]

Base class for all neural network layers and models. Your models should subclass this class.

Block can be nested recursively in a tree structure. You can create and assign child Block as regular attributes:

from mxnet.gluon import Block, nn
from mxnet import ndarray as F

class Model(Block):
    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 forward(self, x):
        x = F.relu(self.dense0(x))
        return F.relu(self.dense1(x))

model = Model()
model.initialize(ctx=mx.cpu(0))
model(F.zeros((10, 10), ctx=mx.cpu(0)))

Child Block assigned this way will be registered and collect_params() will collect their Parameters recursively. You can also manually register child blocks with register_child().

Parameters:
  • prefix (str) – Prefix acts like a name space. All children blocks created in parent block’s name_scope() will have parent block’s prefix in their name. Please refer to naming tutorial for more info on prefix and naming.
  • params (ParameterDict or None) –

    ParameterDict for sharing weights with the new Block. For example, if you want dense1 to share dense0’s weights, you can do:

    dense0 = nn.Dense(20)
    dense1 = nn.Dense(20, params=dense0.collect_params())
    

Handle model parameters:

Block.initialize([init, ctx, verbose, …]) Initializes Parameter s of this Block and its children.
Block.save_parameters(filename) Save parameters to file.
Block.load_parameters(filename[, ctx, …]) Load parameters from file previously saved by save_parameters.
Block.collect_params([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(dtype) Cast this Block to use another data type.
Block.apply(fn) Applies fn recursively to every child block as well as self.

Run computation

Block.forward(*args) Overrides to implement forward computation using NDArray.

Debugging

Block.summary(*inputs) Print the summary of the model’s output and parameters.

Advanced API for customization

Block.name_scope() Returns a name space object managing a child Block and parameter names.
Block.register_child(block[, name]) Registers block as a child of self.
Block.register_forward_hook(hook) Registers a forward hook on the block.
Block.register_forward_pre_hook(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(filename) [Deprecated] Please use save_parameters.
Block.load_params(filename[, ctx, …]) [Deprecated] Please use load_parameters.