The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AI::MXNet::Symbol - Symbolic interface of MXNet.

name

Get name string from the symbol, this function only works for non-grouped symbol.

Returns
-------
value : str
    The name of this symbol, returns None for grouped symbol.

attr

Get an attribute string from the symbol, this function only works for non-grouped symbol.

Parameters
----------
key : str
    The key to get attribute from.

Returns
-------
value : str
    The attribute value of the key, returns None if attribute do not exist.

list_attr

Get all attributes from the symbol.

Returns
-------
ret : hash ref of str to str
    a dicitonary mapping attribute keys to values

attr_dict

Recursively get all attributes from the symbol and its childrens

Returns
-------
ret : hash ref of str to hash ref.
    Returns a dict whose keys are names of the symbol and its children.
    Values of the returned dict are dictionaries that map attribute keys to values.

get_internals

Get a new grouped symbol whose output contains all the internal outputs of this symbol.

Returns
-------
sgroup : AI::MXNet::Symbol
    The internal symbol of the symbol.

get_children

Get a new grouped symbol whose output contains
inputs to output nodes of the original symbol

Returns
-------
sgroup : Symbol or undef
    The children of the head node. If the symbol has no
    inputs undef will be returned.

list_arguments

List all the arguments in the symbol.

Returns
-------
args : array ref of strings

list_outputs()

List all outputs in the symbol.

Returns
-------
$out : array ref of strings.

list_auxiliary_states()

List all auxiliary states in the symbol.

Returns
-------
aux_states : array ref of string
    List the names of the auxiliary states.

Notes
-----
Auxiliary states are special states of symbols that do not corresponds to an argument,
and do not have gradient. But still be useful for the specific operations.
A common example of auxiliary state is the moving_mean and moving_variance in BatchNorm.
Most operators do not have Auxiliary states.

list_inputs

Lists all arguments and auxiliary states of this Symbol.

Returns
-------
inputs : array ref of str
List of all inputs.

Examples
--------
>>> my $bn = mx->sym->BatchNorm(name=>'bn');

infer_type

Infer the type of outputs and arguments of given known types of arguments.

User can either pass in the known types in positional way or keyword argument way.
Tuple of Nones is returned if there is not enough information passed in.
An error will be raised if there is inconsistency found in the known types passed in.

Parameters
----------
args : Array
    Provide type of arguments in a positional way.
    Unknown type can be marked as None

kwargs : Hash ref, must ne ssupplied as as sole argument to the method.
    Provide keyword arguments of known types.

Returns
-------
arg_types : array ref of Dtype or undef
    List of types of arguments.
    The order is in the same order as list_arguments()
out_types : array ref of Dtype or undef
    List of types of outputs.
    The order is in the same order as list_outputs()
aux_types : array ref of Dtype or undef
    List of types of outputs.
    The order is in the same order as list_auxiliary()

infer_shape

Infer the shape of outputs and arguments of given known shapes of arguments.

User can either pass in the known shapes in positional way or keyword argument way.
Tuple of Nones is returned if there is not enough information passed in.
An error will be raised if there is inconsistency found in the known shapes passed in.

Parameters
----------
*args :
    Provide shape of arguments in a positional way.
    Unknown shape can be marked as undef

**kwargs :
    Provide keyword arguments of known shapes.

Returns
-------
arg_shapes : array ref of Shape or undef
    List of shapes of arguments.
    The order is in the same order as list_arguments()
out_shapes : array ref of Shape or undef
    List of shapes of outputs.
    The order is in the same order as list_outputs()
aux_shapes : array ref of Shape or undef
    List of shapes of outputs.
    The order is in the same order as list_auxiliary()

infer_shape_partial

Partially infer the shape. The same as infer_shape, except that the partial
results can be returned.

debug_str

The debug string.

Returns
-------
debug_str : string
    Debug string of the symbol.

save

Save the symbol into a file.

You can also use Storable to do the job if you only work with Perl.
The advantage of load/save is the file is language agnostic.
This means the file saved using save can be loaded by other language binding of mxnet.
You also get the benefit being able to directly load/save from cloud storage(S3, HDFS)

Parameters
----------
fname : str
    The name of the file
    - s3://my-bucket/path/my-s3-symbol
    - hdfs://my-bucket/path/my-hdfs-symbol
    - /path-to/my-local-symbol

See Also
--------
load : Used to load symbol from file.

tojson

Save the symbol into a JSON string.

See Also
--------
load_json : Used to load symbol from JSON string.

simple_bind

Bind current symbol to get an executor, allocate all the ndarrays needed.
Allows specifying data types.

This function will ask user to pass in ndarray of position
they like to bind to, and it will automatically allocate the ndarray
for arguments and auxiliary states that user did not specify explicitly.

Parameters
----------
:$ctx : AI::MXNet::Context
    The device context the generated executor to run on.

:$grad_req: string
    {'write', 'add', 'null'}, or list of str or dict of str to str, optional
    Specifies how we should update the gradient to the args_grad.
        - 'write' means everytime gradient is write to specified args_grad NDArray.
        - 'add' means everytime gradient is add to the specified NDArray.
        - 'null' means no action is taken, the gradient may not be calculated.

:$type_dict  : hash ref of str->Dtype
    Input type map, name->dtype

:$type_dict  : hash ref of str->Stype
    Storage type map, name->stype (for sparse operations)

:$group2ctx : hash ref of string to AI::MXNet::Context
    The mapping of the ctx_group attribute to the context assignment.

:$shapes : hash ref of str->Shape
    Input shape map, name->shape

:$shared_arg_names : Maybe[ArrayRef[Str]]
    The argument names whose 'NDArray' of shared_exec can be reused for initializing
    the current executor.

:$shared_exec : Maybe[AI::MXNet::Executor]
    The executor whose arg_arrays, arg_arrays, grad_arrays, and aux_arrays can be
    reused for initializing the current executor.

:$shared_buffer : Maybe[HashRef[AI::MXNet::NDArray]]
    The dict mapping argument names to the `NDArray` that can be reused for initializing
    the current executor. This buffer will be checked for reuse if one argument name
    of the current executor is not found in `shared_arg_names`.

Returns
-------
$executor : AI::MXNet::Executor
    The generated Executor

bind

Bind current symbol to get an executor.

Parameters
----------
:$ctx : AI::MXNet::Context
    The device context the generated executor to run on.

:$args : HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray]
    Input arguments to the symbol.
        - If type is array ref of NDArray, the position is in the same order of list_arguments.
        - If type is hash ref of str to NDArray, then it maps the name of arguments
            to the corresponding NDArray.
        - In either case, all the arguments must be provided.

:$args_grad : Maybe[HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray]]
    When specified, args_grad provide NDArrays to hold
    the result of gradient value in backward.
        - If type is array ref of NDArray, the position is in the same order of list_arguments.
        - If type is hash ref of str to NDArray, then it maps the name of arguments
            to the corresponding NDArray.
        - When the type is hash ref of str to NDArray, users only need to provide the dict
            for needed argument gradient.
    Only the specified argument gradient will be calculated.

:$grad_req : {'write', 'add', 'null'}, or array ref of str or hash ref of str to str, optional
    Specifies how we should update the gradient to the args_grad.
        - 'write' means everytime gradient is write to specified args_grad NDArray.
        - 'add' means everytime gradient is add to the specified NDArray.
        - 'null' means no action is taken, the gradient may not be calculated.

:$aux_states : array ref of NDArray, or hash ref of str to NDArray, optional
    Input auxiliary states to the symbol, only need to specify when
    list_auxiliary_states is not empty.
        - If type is array ref of NDArray, the position is in the same order of list_auxiliary_states
        - If type is hash ref of str to NDArray, then it maps the name of auxiliary_states
            to the corresponding NDArray,
        - In either case, all the auxiliary_states need to be provided.

:$group2ctx : hash ref of string to AI::MXNet::Context
    The mapping of the ctx_group attribute to the context assignment.

:$shared_exec : AI::MXNet::Executor
    Executor to share memory with. This is intended for runtime reshaping, variable length
    sequences, etc. The returned executor shares state with shared_exec, and should not be
    used in parallel with it.

Returns
-------
$executor : AI::MXNet::Executor
    The generated Executor

Notes
-----
Auxiliary states are special states of symbols that do not corresponds to an argument,
and do not have gradient. But still be useful for the specific operations.
A common example of auxiliary state is the moving_mean and moving_variance in BatchNorm.
Most operators do not have auxiliary states and this parameter can be safely ignored.

User can give up gradient by using a hash ref in args_grad and only specify
the gradient they're interested in.

eval

Evaluate a symbol given arguments

The `eval` method combines a call to `bind` (which returns an executor)
with a call to `forward` (executor method).
For the common use case, where you might repeatedly evaluate with same arguments,
eval is slow.
In that case, you should call `bind` once and then repeatedly call forward.
Eval allows simpler syntax for less cumbersome introspection.

Parameters
----------
:$ctx : Context
The device context the generated executor to run on.
Optional, defaults to cpu(0)

:$args array ref of NDArray or hash ref of NDArray

    - If the type is an array ref of NDArray, the position is in the same order of list_arguments.
    - If the type is a hash of str to NDArray, then it maps the name of the argument
        to the corresponding NDArray.
    - In either case, all arguments must be provided.

Returns
----------
result :  an array ref of NDArrays corresponding to the values
    taken by each symbol when evaluated on given args.
    When called on a single symbol (not a group),
    the result will be an array ref with one element.

Examples:
my $result = $symbol->eval(ctx => mx->gpu, args => {data => mx->nd->ones([5,5])});
my $result = $symbol->eval(args => {data => mx->nd->ones([5,5])});

grad

Get the autodiff of current symbol.
This function can only be used if current symbol is a loss function.

Parameters
----------
$wrt : Array of String
    keyword arguments of the symbol that the gradients are taken.

Returns
-------
grad : AI::MXNet::Symbol
    A gradient Symbol with returns to be the corresponding gradients.

Variable

Create a symbolic variable with specified name.

Parameters
----------
name : str
    Name of the variable.
attr : hash ref of string -> string
    Additional attributes to set on the variable.
shape : array ref of positive integers
    Optionally, one can specify the shape of a variable. This will be used during
    shape inference. If user specified a different shape for this variable using
    keyword argument when calling shape inference, this shape information will be ignored.
lr_mult : float
    Specify learning rate muliplier for this variable.
wd_mult : float
    Specify weight decay muliplier for this variable.
dtype : Dtype
    Similar to shape, we can specify dtype for this variable.
init : initializer (mx->init->*)
    Specify initializer for this variable to override the default initializer
kwargs : hash ref
    other additional attribute variables
Returns
-------
variable : Symbol
    The created variable symbol.

var

A synonym to Variable.

Group

Create a symbol that groups symbols together.

Parameters
----------
symbols : array ref
    List of symbols to be grouped.

Returns
-------
sym : Symbol
    The created group symbol.

load

Load symbol from a JSON file.

You can also use Storable to do the job if you only work with Perl.
The advantage of load/save is the file is language agnostic.
This means the file saved using save can be loaded by other language binding of mxnet.
You also get the benefit being able to directly load/save from cloud storage(S3, HDFS)

Parameters
----------
fname : str
    The name of the file, examples:

    - `s3://my-bucket/path/my-s3-symbol`
    - `hdfs://my-bucket/path/my-hdfs-symbol`
    - `/path-to/my-local-symbol`

Returns
-------
sym : Symbol
    The loaded symbol.

See Also
--------
AI::MXNet::Symbol->save : Used to save symbol into file.

load_json

Load symbol from json string.

Parameters
----------
json_str : str
    A json string.

Returns
-------
sym : Symbol
    The loaded symbol.

See Also
--------
AI::MXNet::Symbol->tojson : Used to save symbol into json string.

arange

Simlar function in the MXNet ndarray as numpy.arange
    See Also https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html.

Parameters
----------
:$start=0 : number
    Start of interval. The interval includes this value. The default start value is 0.
:$stop= : number, optional
    End of interval. The interval does not include this value.
:$step=1.0 : number, optional
    Spacing between values
:$repeat=1 : int, optional
    "The repeating time of all elements.
    E.g repeat=3, the element a will be repeated three times --> a, a, a.
:$infer_range=0 : Bool
    When set to 1, infer stop position from start, step, repeat, and
    output tensor size.
:$dtype='float32' : type, optional
    The value type of the NDArray, default to np.float32

Returns
-------
out : Symbol
    The created Symbol