Block¶
-
class
Block(prefix=None, params=None)¶ Base class for all neural network layers and models. Your models should subclass this class.
Blockcan be nested recursively in a tree structure. You can create and assign childBlockas 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
Blockassigned this way will be registered andcollect_params()will collect their Parameters recursively. You can also manually register child blocks withregister_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
ParameterDictfor sharing weights with the newBlock. For example, if you wantdense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20, params=dense0.collect_params())