AI::MXNet::Gluon::Block - Base class for all neural network layers and models.
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. # It also allows sharing Parameters between Blocks recursively. with self.name_scope(): self.dense0 = nn.Dense(20) self.dense1 = nn.Dense(20) 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. Parameters ---------- prefix : str Prefix acts like a name space. It will be prepended to the names of all Parameters and child `Block`s in this `Block`'s `name_scope`. Prefix should be unique within one model to prevent name collisions. 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())
Returns this `Block`'s parameter dictionary (does not include its children's parameters).
Returns a `ParameterDict` containing this `Block` and all of its children's Parameters.
Save parameters to file. filename : str Path to file.
Load parameters from file. $filename : str Path to parameter file. :$ctx= : Context or list of Context Context(s) 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.
Registers block as a child of self. `Block`s assigned to self as attributes will be registered automatically.
Initializes `Parameter`s of this `Block` and its children. Equivalent to `block.collect_params().initialize(...)`
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.
Overrides to implement forward computation using `NDArray`. Only accepts positional arguments. Parameters ---------- @args : array of NDArray Input tensors.
AI::MXNet::Gluon::HybridBlock
`HybridBlock` supports forwarding with both Symbol and NDArray. Forward computation in `HybridBlock` must be static to work with `Symbol`s, i.e. you cannot call `.asnumpy()`, `.shape`, `.dtype`, 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 normal `Block`. 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 of `hybrid_forward`. Refer `Hybrid tutorial <http://mxnet.io/tutorials/gluon/hybrid.html>`_ to see the end-to-end usage.
Infers shape of Parameters from inputs.
Defines the forward computation. Arguments can be either `NDArray` or `Symbol`.
Overrides to construct symbolic graph for this `Block`. Parameters ---------- x : Symbol or NDArray The first input tensor. *args : list of Symbol or list of NDArray Additional input tensors.
AI::MXNet::Gluon::SymbolBlock - Construct block from symbol.
Construct block from symbol. This is useful for using pre-trained models as feature extractors. For example, you may want to extract get the output from fc2 layer in AlexNet. Parameters ---------- outputs : Symbol or list of Symbol The desired output for SymbolBlock. inputs : Symbol or list of Symbol The Variables in output's argument that should be used as inputs. params : ParameterDict Parameter dictionary for arguments and auxililary states of outputs that are not inputs. Examples -------- >>> # To extract the feature from fc1 and fc2 layers of AlexNet: >>> alexnet = gluon.model_zoo.vision.alexnet(pretrained=True, ctx=mx.cpu(), prefix='model_') >>> inputs = mx.sym.var('data') >>> out = alexnet(inputs) >>> internals = out.get_internals() >>> print(internals.list_outputs()) ['data', ..., 'model_dense0_relu_fwd_output', ..., 'model_dense1_relu_fwd_output', ...] >>> outputs = [internals['model_dense0_relu_fwd_output'], internals['model_dense1_relu_fwd_output']] >>> # Create SymbolBlock that shares parameters with alexnet >>> feat_model = gluon.SymbolBlock(outputs, inputs, params=alexnet.collect_params()) >>> x = mx.nd.random_normal(shape=(16, 3, 224, 224)) >>> print(feat_model(x))
To install AI::MXNet, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AI::MXNet
CPAN shell
perl -MCPAN -e shell install AI::MXNet
For more information on module installation, please visit the detailed CPAN module installation guide.