AI-MXNet
view release on metacpan or search on metacpan
lib/AI/MXNet/Symbol.pm view on Meta::CPAN
The attribute value of the key, returns None if attribute do not exist.
=cut
method attr(Str $key)
{
my ($attr, $success) = check_call(
AI::MXNetCAPI::SymbolGetAttr($self->handle, $key)
);
return $success ? $attr : undef;
}
=head2 list_attr
Get all attributes from the symbol.
Returns
-------
ret : hash ref of str to str
a dicitonary mapping attribute keys to values
=cut
method list_attr()
{
my %ret;
my @attrs = @{ check_call(AI::MXNetCAPI::SymbolListAttrShallow($self->handle)) };
while(@attrs)
{
my $k = shift(@attrs);
my $v = shift(@attrs);
$ret{ $k } = $v;
}
return \%ret;
}
=head2 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.
=cut
method attr_dict()
{
my %ret;
my @attrs = @{ check_call(AI::MXNetCAPI::SymbolListAttr($self->handle)) };
my $size = @attrs/2;
for (my $i = 0; $i < $size; $i++)
{
my ($name, $key) = split(/\$/, $attrs[$i*2]);
my $val = $attrs[$i*2+1];
$ret{ $name }{ $key } = $val;
}
return \%ret;
}
method _set_attr(Str @args)
{
my %kwargs = @args;
while(my ($key, $val) = each(%kwargs))
{
check_call(
AI::MXNetCAPI::SymbolSetAttr(
$self->handle, $key, $val
)
);
}
}
=head2 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.
=cut
method get_internals()
{
my $handle = check_call(AI::MXNetCAPI::SymbolGetInternals($self->handle));
return __PACKAGE__->new(handle => $handle);
}
=head2 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.
=cut
method get_children()
{
my $handle = check_call(AI::MXNetCAPI::SymbolGetChildren($self->handle));
my $ret = __PACKAGE__->new(handle => $handle);
return undef unless @{ $ret->list_outputs };
return $ret;
}
=head2 list_arguments
List all the arguments in the symbol.
Returns
-------
args : array ref of strings
=cut
method list_arguments()
{
lib/AI/MXNet/Symbol.pm view on Meta::CPAN
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.
=cut
method bind(
AI::MXNet::Context :$ctx,
HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray] :$args,
Maybe[HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray]] :$args_grad=,
Str|HashRef[Str]|ArrayRef[Str] :$grad_req='write',
Maybe[HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray]] :$aux_states=,
Maybe[HashRef[AI::MXNet::Context]] :$group2ctx=,
Maybe[AI::MXNet::Executor] :$shared_exec=
)
{
$grad_req //= 'write';
my $listed_arguments = $self->list_arguments();
my ($args_handle, $args_grad_handle, $aux_args_handle) = ([], [], []);
($args_handle, $args) = $self->_get_ndarray_inputs('args', $args, $listed_arguments);
if(not defined $args_grad)
{
@$args_grad_handle = ((undef) x (@$args));
}
else
{
($args_grad_handle, $args_grad) = $self->_get_ndarray_inputs(
'args_grad', $args_grad, $listed_arguments, 1
);
}
if(not defined $aux_states)
{
$aux_states = [];
}
($aux_args_handle, $aux_states) = $self->_get_ndarray_inputs(
'aux_states', $aux_states, $self->list_auxiliary_states()
);
# setup requirements
my $req_map = { null => 0, write => 1, add => 3 };
my $req_array = [];
if(not ref $grad_req)
{
confess('grad_req must be one of "null,write,add"')
unless exists $req_map->{ $grad_req };
@{ $req_array } = (($req_map->{ $grad_req }) x @{ $listed_arguments });
}
elsif(ref $grad_req eq 'ARRAY')
{
@{ $req_array } = map { $req_map->{ $_ } } @{ $grad_req };
}
else
{
for my $name (@{ $listed_arguments })
{
if(exists $grad_req->{ $name })
{
push @{ $req_array }, $req_map->{ $grad_req->{ $name } };
}
else
{
push @{ $req_array }, 0;
}
}
}
my $ctx_map_keys = [];
my $ctx_map_dev_types = [];
my $ctx_map_dev_ids = [];
if(defined $group2ctx)
{
while(my ($key, $val) = each %{ $group2ctx })
{
push @{ $ctx_map_keys } , $key;
push @{ $ctx_map_dev_types }, $val->device_type_id;
push @{ $ctx_map_dev_ids }, $val->device_id;
}
}
my $shared_handle = $shared_exec->handle if $shared_exec;
my $handle = check_call(AI::MXNetCAPI::ExecutorBindEX(
$self->handle,
$ctx->device_type_id,
$ctx->device_id,
scalar(@{ $ctx_map_keys }),
$ctx_map_keys,
$ctx_map_dev_types,
$ctx_map_dev_ids,
scalar(@{ $args }),
$args_handle,
$args_grad_handle,
$req_array,
scalar(@{ $aux_states }),
$aux_args_handle,
$shared_handle
)
);
my $executor = AI::MXNet::Executor->new(
handle => $handle,
lib/AI/MXNet/Symbol.pm view on Meta::CPAN
- 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])});
=cut
method eval(:$ctx=AI::MXNet::Context->cpu, HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray] :$args)
{
return $self->bind(ctx => $ctx, args => $args)->forward;
}
=head2 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.
=cut
method grad(ArrayRef[Str] $wrt)
{
my $handle = check_call(AI::MXNetCAPI::SymbolGrad(
$self->handle,
scalar(@$wrt),
$wrt
)
);
return __PACKAGE__->new(handle => $handle);
}
=head2 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.
=cut
method Variable(
Str $name,
HashRef[Str] :$attr={},
Maybe[Shape] :$shape=,
Maybe[Num] :$lr_mult=,
Maybe[Num] :$wd_mult=,
Maybe[Dtype] :$dtype=,
Maybe[Initializer] :$init=,
HashRef[Str] :$kwargs={},
Maybe[Str] :$__layout__=
)
{
my $handle = check_call(AI::MXNetCAPI::SymbolCreateVariable($name));
my $ret = __PACKAGE__->new(handle => $handle);
$attr = AI::MXNet::Symbol::AttrScope->current->get($attr);
$attr->{__shape__} = "(".join(',', @{ $shape }).")" if $shape;
$attr->{__lr_mult__} = $lr_mult if defined $lr_mult;
$attr->{__wd_mult__} = $wd_mult if defined $wd_mult;
$attr->{__dtype__} = DTYPE_STR_TO_MX->{ $dtype } if $dtype;
$attr->{__init__} = "$init" if defined $init;
$attr->{__layout__} = $__layout__ if defined $__layout__;
while(my ($k, $v) = each %{ $kwargs })
{
if($k =~ /^__/ and $k =~ /__$/)
{
$attr->{$k} = "$v";
}
else
{
confess("Attribute name=$k is not supported.".
' Additional attributes must start and end with double underscores,'.
' e.g, __yourattr__'
);
}
}
$ret->_set_attr(%{ $attr });
return $ret;
}
=head2 var
A synonym to Variable.
=cut
*var = \&Variable;
=head2 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.
=cut
method Group(ArrayRef[AI::MXNet::Symbol] $symbols)
{
my @handles = map { $_->handle } @{ $symbols };
my $handle = check_call(AI::MXNetCAPI::SymbolCreateGroup(scalar(@handles), \@handles));
return __PACKAGE__->new(handle => $handle);
}
=head2 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.
=cut
( run in 0.997 second using v1.01-cache-2.11-cpan-39bf76dae61 )