NAME

AI::MXNet::Gluon::Parameter - A Container holding parameters (weights) of AI::MXNEt::Gluon::Block(s).

DESCRIPTION

AI::MXNet::Gluon::Parameter holds a copy of the parameter on each AI::MXNet::Context after
it is initialized with AI::MXNet::Gluon::Parameter->initialize(...)`. If grad_req is
not 'null', it will also hold a gradient array on each AI::MXNet::Context

    $ctx = mx->gpu(0);
    $x = mx->nd->zeros([16, 100], ctx=>$ctx);
    $w = mx->gluon->Parameter('fc_weight', shape=>[64, 100], init=>mx->init->Xavier());
    $b = mx->gluon->Parameter('fc_bias', shape=>[64], init=>mx->init->Zero());
    $w->initialize(ctx=>$ctx);
    $b->initialize(ctx=>ctx);
    $out = mx->nd->FullyConnected($x, $w->data($ctx), $b->data($ctx), num_hidden=>64);

Parameters
----------
name : str
    Name of this parameter.
grad_req : {'write', 'add', 'null'}, default 'write'
    Specifies how to update gradient to grad arrays.

    - 'write' means everytime gradient is written to grad NDArray.
    - 'add' means everytime gradient is added to the grad NDArray. You need
      to manually call zero_grad() to clear the gradient buffer before each
      iteration when using this option.
    - 'null' means gradient is not requested for this parameter. gradient arrays
      will not be allocated.
shape : array ref of int, default None
    Shape of this parameter. By default shape is not specified. Parameter with
    unknown shape can be used for `Symbol` API, but `init` will throw an error
    when using `NDArray` API.
dtype : Dtype, default 'float32'
    Data type of this parameter. For example, 'float64'.
lr_mult : float, default 1.0
    Learning rate multiplier. Learning rate will be multiplied by lr_mult
    when updating this parameter with optimizer.
wd_mult : float, default 1.0
    Weight decay multiplier (L2 regularizer coefficient). Works similar to lr_mult.
init : Initializer, default None
    Initializer of this parameter. Will use the global initializer by default.

Attributes
----------
grad_req : {'write', 'add', 'null'}
    This can be set before or after initialization. Setting grad_req to null
    with $x->grad_req = 'null' saves memory and computation when you don't
    need gradient w.r.t x.

initialize

Initializes parameter and gradient arrays. Only used for `NDArray` API.

Parameters
----------
:$init : Initializer
    The initializer to use. Overrides AI::MXNet::Gluon::Parameter->init and default_init.
:$ctx : AI::MXNet::Context or array ref of AI::MXNet::Context, defaults to AI::MXNet::Context->current_ctx().
    Initialize Parameter on given context. If ctx is a list of Context, a
    copy will be made for each context.
    Copies are independent arrays. User is responsible for keeping
    their values consistent when updating. Normally gluon->Trainer does this for you.
:$default_init : Initializer
    Default initializer is used when both 'init' and AI::MXNet::Gluon::Parameter->init are undefined.
:$force_reinit : bool, default False
    Whether to force re-initialization if parameter is already initialized.

Examples
--------
>>> $weight = mx->gluon->Parameter('weight', shape=>[2, 2]);
>>> $weight->initialize(ctx=>mx->cpu(0));
>>> print $weight->data
[[-0.01068833  0.01729892]
 [ 0.02042518 -0.01618656]]
<NDArray 2x2 @cpu(0)>
>>> print $weight->grad()
[[ 0.  0.]
 [ 0.  0.]]
<NDArray 2x2 @cpu(0)>
>>> $weight->initialize(ctx=>[mx->gpu(0), mx->gpu(1)]);
>>> print $weight->data(mx->gpu(0));
[[-0.00873779 -0.02834515]
 [ 0.05484822 -0.06206018]]
<NDArray 2x2 @gpu(0)>
>>> print $weight->data(mx->gpu(1))
[[-0.00873779 -0.02834515]
 [ 0.05484822 -0.06206018]]
<NDArray 2x2 @gpu(1)>

reset_ctx

Re-assign Parameter to other contexts.

:$ctx : AI::MXNet::Context or array ref of AI::MXNet::Context, default AI::MXNet::Context->current_ctx.
Assign Parameter to given context. If ctx is a list of Context, a
copy will be made for each context.

set_data

Sets this parameter's value on all contexts to data.

data

Returns a copy of this parameter on one context. Must have been
initialized on this context before.

Parameters
----------
ctx : Context
    Desired context.

Returns
-------
NDArray on ctx

list_data

Returns copies of this parameter on all contexts, in the same order
as creation.

grad

Returns a gradient buffer for this parameter on one context.

Parameters
----------
ctx : Context
    Desired context.

list_grad

Returns gradient buffers on all contexts, in the same order
as 'values'.

list_ctx

Returns a list of contexts this parameter is initialized on.

zero_grad

Sets gradient buffer on all contexts to 0. No action is taken if
parameter is uninitialized or doesn't require gradient.

var

Returns a symbol representing this parameter.

NAME

AI::MXNet::Gluon::ParameterDict - A dictionary managing a set of parameters.

DESCRIPTION

Parameters
----------
prefix : str, default ''
    The prefix to be prepended to all Parameters' names created by this dict.
shared : ParameterDict or undef
    If not undef, when this dict's `get` method creates a new parameter, will
    first try to retrieve it from `shared` dict. Usually used for sharing
    parameters with another `Block`.

get

Retrieves a 'AI::MXNet::Gluon::Parameter' with name '$self->prefix.$name'. If not found,
'get' will first try to retrieve it from 'shared' dict. If still not
found, 'get' will create a new 'AI::MXNet::Gluon::Parameter' with key-word arguments and
insert it to self.

Parameters
----------
name : str
    Name of the desired Parameter. It will be prepended with this dictionary's
    prefix.
%kwargs : hash
    The rest of key-word arguments for the created `Parameter`.

Returns
-------
Parameter
    The created or retrieved `Parameter`.

update

Copies all Parameters in $other to self.

initialize

Initializes all Parameters managed by this dictionary to be used for 'NDArray'
API. It has no effect when using 'Symbol' API.

Parameters
----------
:$init : Initializer
    Global default Initializer to be used when AI::MXNet::Gluon::Parameter->init is undef.
    Otherwise, AI::MXNet::Gluon::Parameter->init takes precedence.
:$ctx : AI::MXNet::Context or array ref of AI::MXNet::Context objects
    Keeps a copy of Parameters on one or many context(s).
:$force_reinit : bool, default False
    Whether to force re-initialization if parameter is already initialized.

zero_grad

Sets all Parameters' gradient buffer to 0.

reset_ctx

Re-assign all Parameters to other contexts.

$ctx : AI::MXNet::Context or array ref of AI::MXNet::Context objects, defaults to AI::MXNet::Context->current_ctx().
        Assign Parameter to given context. If $ctx is an array ref of AI::MXNet::Context objects, a
        copy will be made for each context.

setattr

Set an attribute to a new value for all Parameters.

For example, set grad_req to null if you don't need gradient w.r.t a
model's Parameters::

    $model->collect_params()->setattr(grad_req => 'null');

or change the learning rate multiplier::

    $model->collect_params()->setattr(lr_mult => 0.5);

Parameters
----------
$name : str
    Name of the attribute.
$value : valid type for attribute name
    The new value for the attribute.

save

Save parameters to file.

$filename : str
    Path to parameter file.
$strip_prefix : str, default ''
Strip prefix from parameter names before saving.

Load parameters from file.

$filename : str
    Path to parameter file.
:$ctx : AI::MXNet::Context or array ref of AI::MXNet::Context objects
    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 ParameterDict.
:$restore_prefix : str, default ''
    prepend prefix to names of stored parameters before loading.