NAME

AI::MXNet::Gluon::Block - Base class for all neural network layers and models.

DESCRIPTION

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

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

use AI::MXNet::Gluon::NN qw(nn);
use AI::MXNet qw(mx);

package Model;
use AI::MXNet::Gluon::Mouse;
use AI::MXNet::Function::Parameters;
extends 'AI::MXNet::Gluon::Block';

sub BUILD
{
    my $self = shift;
    $self->name_scope(sub {
        $self->dense0(nn->Dense(5, in_units=>5));
        $self->dense1(nn->Dense(5, in_units=>5));
    });
}

method forward($x)
{
    return $self->dense1->($self->dense0->($x));
}

my $model = Model->new()
$model->initialize(ctx=>mx->cpu(0))
$model->(nd->zeros([10, 10], ctx=>mx->cpu(0)));


Child AI::MXNet::Gluon::Block assigned this way will be registered and ->collect_params
will collect their Parameters recursively.

Parameters
----------
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 http://mxnet.incubator.apache.org/tutorials/gluon/naming.html
for more info on prefix and naming.

params : AI::MXNet::Gluon::ParameterDict or undef
    AI::MXNet::Gluon::ParameterDict for sharing weights with the new AI::MXNet::Gluon::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());

params

Returns this `Block`'s parameter dictionary (does not include its
children's parameters).

collect_params

Returns a AI::MXNet::Gluon::ParameterDict containing this AI::MXNet::Gluon::Block and all of its
children's Parameters(default), also can returns the ParameterDict
with parameters that match a regular expression.

For example, collects parameters specified in ['conv1_weight', 'conv1_bias', 'fc_weight',
'fc_bias'

    $model->collect_params('conv1_weight|conv1_bias|fc_weight|fc_bias')

or collects all parameters that have the name end with 'weight' or 'bias', this can be done
using regular expressions.

    $model->collect_params('.*weight|.*bias')

save_parameters

Save parameters to file.

filename : str
    Path to file.

load_parameters

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.

register_child

Registers block as a child of self. `Block`s assigned to self as
attributes will be registered automatically.

register_forward_pre_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 : CodeRef or callable object
    The forward hook function of form $hook->($block, $input).

Returns
-------
AI::MXNet::Gluon::Utils::HookHandle

register_forward_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 : CodeRef or callable object
    The forward hook function of form $hook->($block, $input).

Returns
-------
AI::MXNet::Gluon::Utils::HookHandle

apply

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
-------
this block

initialize

Initializes AI::MXNet::Gluon::Parameters of this AI::MXNet::Gluon::Block and its children.
Equivalent to $block->collect_params()->initialize(...)

Parameters
----------
$init : Initializer
    Global default Initializer to be used when Parameter->init is undefined`.
    Otherwise, Parameter->init takes precedence.
ctx : Context or array ref 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.

hybridize

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

Cast this Block to use another data type.

Parameters
----------
dtype : Dtype
    The new data type.

forward

Overrides to implement forward computation using `NDArray`. Only
accepts positional arguments.

Parameters
----------
@args : array of NDArray
    Input tensors.

summary

Print the summary of the model's output and parameters.

The network must have been initialized, and must not have been hybridized.

Parameters
----------
@inputs : objects
    Any inputs that the model supports. For any tensor in the input, only
    AI::MXNet::NDArray is supported.

NAME

AI::MXNet::Gluon::HybridBlock

DESCRIPTION

HybridBlock supports forwarding with both Symbol and NDArray.

Forward computation in HybridBlock must be static to work with Symbols,
i.e. you cannot call aspdl, 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 L<http://mxnet.io/tutorials/gluon/hybrid.html> to see
the end-to-end usage.

infer_shape

Infers shape of Parameters from inputs.

forward

Defines the forward computation. Arguments can be either
NDArray or Symbol

hybrid_forward

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.

export

Export HybridBlock to json format that can be loaded by AI::MXNet::Module
or the C++ interface.

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=0 : Int
    Epoch number of saved model.

NAME

AI::MXNet::Gluon::SymbolBlock - Construct block from symbol.

DESCRIPTION

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=>1, 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->slice('model_dense0_relu_fwd_output'),
               $internals->slice('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));

imports

Import model previously saved by HybridBlock->export or
Module->save_checkpoint as a SymbolBlock for use in Gluon.

Parameters
----------
$symbol_file : Str
    Path to symbol file.
$input_names : Str|ArrayRef[Str]
    List of input variable names
:$param_file : Str, optional
    Path to parameter file.
$ctx : Context, default undef
    The context to initialize SymbolBlock on.

Returns
-------
SymbolBlock
    SymbolBlock loaded from symbol and parameter files.