AI-MXNet
view release on metacpan or search on metacpan
lib/AI/MXNet/Module/Base.pm view on Meta::CPAN
{
my $params = AI::MXNet::BatchEndParam->new(
epoch => $epoch,
nbatch => $actual_num_batch,
eval_metric => $eval_metric,
);
for my $callback (@{ _as_list($score_end_callback) })
{
&{callback}($params);
}
}
return $eval_metric->get_name_value;
}
=head2 iter_predict
Iterate over predictions.
Parameters
----------
$eval_data : AI::MXNet::DataIter
:$num_batch= : Maybe[Int]
Default is undef, indicating running all the batches in the data iterator.
:$reset=1 : bool
Default is 1, indicating whether we should reset the data iter before start
doing prediction.
=cut
method iter_predict(AI::MXNet::DataIter $eval_data, Maybe[Int] :$num_batch=, Bool :$reset=1)
{
assert($self->binded and $self->params_initialized);
if($reset)
{
$eval_data->reset;
}
my $nbatch = 0;
my @out;
while(my $eval_batch = <$eval_data>)
{
last if defined $num_batch and $nbatch == $num_batch;
$self->forward($eval_batch, is_train => 0);
my $pad = $eval_batch->pad;
my $outputs = [
map { $_->slice([0, $_->shape->[0] - ($pad//0) - 1]) } @{ $self->get_outputs() }
];
push @out, [$outputs, $nbatch, $eval_batch];
$nbatch++;
}
return @out;
}
=head2 predict
Run prediction and collect the outputs.
Parameters
----------
$eval_data : AI::MXNet::DataIter
:$num_batch= : Maybe[Int]
Default is undef, indicating running all the batches in the data iterator.
:$merge_batches=1 : Bool
Default is 1.
:$reset=1 : Bool
Default is 1, indicating whether we should reset the data iter before start
doing prediction.
:$always_output_list=0 : Bool
Default is 0, see the doc for return values.
Returns
-------
When $merge_batches is 1 (by default), the return value will be an array ref
[$out1, $out2, $out3] where each element is concatenation of the outputs for
all the mini-batches. If $always_output_list` also is 0 (by default),
then in the case of a single output, $out1 is returned in stead of [$out1].
When $merge_batches is 0, the return value will be a nested array ref like
[[$out1_batch1, $out2_batch1], [$out1_batch2], ...]. This mode is useful because
in some cases (e.g. bucketing), the module does not necessarily produce the same
number of outputs.
The objects in the results are AI::MXNet::NDArray`s. If you need to work with pdl array,
just call ->aspdl() on each AI::MXNet::NDArray.
=cut
method predict(
AI::MXNet::DataIter $eval_data,
Maybe[Int] :$num_batch=, Bool :$merge_batches=1, Bool :$reset=1, Bool :$always_output_list=0
)
{
assert($self->binded and $self->params_initialized);
$eval_data->reset() if $reset;
my @output_list;
my $nbatch = 0;
while(my $eval_batch = <$eval_data>)
{
last if defined $num_batch and $nbatch == $num_batch;
$self->forward($eval_batch, is_train => 0);
my $pad = $eval_batch->pad;
my $outputs = [map { $_->slice([0, $_->shape->[0]-($pad//0)-1])->copy } @{ $self->get_outputs }];
push @output_list, $outputs;
}
return () unless @output_list;
if($merge_batches)
{
my $num_outputs = @{ $output_list[0] };
for my $out (@output_list)
{
unless(@{ $out } == $num_outputs)
{
confess('Cannot merge batches, as num of outputs is not the same '
.'in mini-batches. Maybe bucketing is used?');
}
}
my @output_list2;
for my $i (0..$num_outputs-1)
{
push @output_list2,
AI::MXNet::NDArray->concatenate([map { $_->[$i] } @output_list]);
}
if($num_outputs == 1 and not $always_output_list)
{
return $output_list2[0];
}
return @output_list2;
}
return @output_list;
}
=head2 fit
Train the module parameters.
Parameters
----------
$train_data : AI::MXNet::DataIter
:$eval_data= : Maybe[AI::MXNet::DataIter]
If not undef, it will be used as a validation set to evaluate the performance
after each epoch.
:$eval_metric='acc' : str or AI::MXNet::EvalMetric subclass object.
Default is 'accuracy'. The performance measure used to display during training.
Other possible predefined metrics are:
'ce' (CrossEntropy), 'f1', 'mae', 'mse', 'rmse', 'top_k_accuracy'
:$epoch_end_callback= : Maybe[Callback]|ArrayRef[Callback] function or array ref of functions.
Each callback will be called with the current $epoch, $symbol, $arg_params
and $aux_params.
:$batch_end_callback= : Maybe[Callback]|ArrayRef[Callback] function or array ref of functions.
Each callback will be called with a AI::MXNet::BatchEndParam.
:$kvstore='local' : str or AI::MXNet::KVStore
Default is 'local'.
:$optimizer : str or AI::MXNet::Optimizer
Default is 'sgd'
:$optimizer_params : hash ref
Default { learning_rate => 0.01 }.
The parameters for the optimizer constructor.
:$eval_end_callback= : Maybe[Callback]|ArrayRef[Callback] function or array ref of functions
These will be called at the end of each full evaluation, with the metrics over
the entire evaluation set.
:$eval_batch_end_callback : Maybe[Callback]|ArrayRef[Callback] function or array ref of functions
These will be called at the end of each minibatch during evaluation
:$initializer= : Initializer
Will be called to initialize the module parameters if not already initialized.
:$arg_params= : hash ref
Default undef, if not undef, must be an existing parameters from a trained
model or loaded from a checkpoint (previously saved model). In this case,
the value here will be used to initialize the module parameters, unless they
are already initialized by the user via a call to init_params or fit.
$arg_params have higher priority than the $initializer.
:$aux_params= : hash ref
Default is undef. This is similar to the $arg_params, except for auxiliary states.
:$allow_missing=0 : Bool
lib/AI/MXNet/Module/Base.pm view on Meta::CPAN
method save_params(
Str $fname,
Maybe[HashRef[AI::MXNet::NDArray]] $arg_params=,
Maybe[HashRef[AI::MXNet::NDArray]] $aux_params=
)
{
($arg_params, $aux_params) = $self->get_params
unless (defined $arg_params and defined $aux_params);
my %save_dict;
while(my ($k, $v) = each %{ $arg_params })
{
$save_dict{"arg:$k"} = $v->as_in_context(AI::MXNet::Context->cpu);
}
while(my ($k, $v) = each %{ $aux_params })
{
$save_dict{"aux:$k"} = $v->as_in_context(AI::MXNet::Context->cpu);
}
AI::MXNet::NDArray->save($fname, \%save_dict);
}
=head2 load_params
Load model parameters from file.
Parameters
----------
$fname : str
Path to input param file.
=cut
method load_params(Str $fname)
{
my %save_dict = %{ AI::MXNet::NDArray->load($fname) };
my %arg_params;
my %aux_params;
while(my ($k, $v) = each %save_dict)
{
my ($arg_type, $name) = split(/:/, $k, 2);
if($arg_type eq 'arg')
{
$arg_params{ $name } = $v;
}
elsif($arg_type eq 'aux')
{
$aux_params{ $name } = $v;
}
else
{
confess("Invalid param file $fname");
}
}
$self->set_params(\%arg_params, \%aux_params);
}
=head2 get_states
The states from all devices
Parameters
----------
$merge_multi_context=1 : Bool
Default is true (1). In the case when data-parallelism is used, the states
will be collected from multiple devices. A true value indicate that we
should merge the collected results so that they look like from a single
executor.
Returns
-------
If $merge_multi_context is 1, it is like [$out1, $out2]. Otherwise, it
is like [[$out1_dev1, $out1_dev2], [$out2_dev1, $out2_dev2]]. All the output
elements are AI::MXNet::NDArray.
=cut
method get_states(Bool $merge_multi_context=1)
{
assert($self->binded and $self->params_initialized);
assert(not $merge_multi_context);
return [];
}
=head2 set_states
Set value for states. You can specify either $states or $value, not both.
Parameters
----------
$states= : Maybe[ArrayRef[ArrayRef[AI::MXNet::NDArray]]]
source states arrays formatted like [[$state1_dev1, $state1_dev2],
[$state2_dev1, $state2_dev2]].
$value= : Maybe[Num]
a single scalar value for all state arrays.
=cut
method set_states(Maybe[ArrayRef[ArrayRef[AI::MXNet::NDArray]]] $states=, Maybe[Num] $value=)
{
assert($self->binded and $self->params_initialized);
assert(not $states and not $value);
}
=head2 install_monitor
Install monitor on all executors
Parameters
----------
$mon : AI::MXNet::Monitor
=cut
method install_monitor(AI::MXNet::Monitor $mon) { confess("NotImplemented") }
=head2 prepare
Prepare the module for processing a data batch.
Usually involves switching a bucket and reshaping.
Parameters
----------
$data_batch : AI::MXNet::DataBatch
=cut
method prepare(AI::MXNet::DataBatch $data_batch){}
################################################################################
# Computations
################################################################################
=head2 forward
Forward computation. It supports data batches with different shapes, such as
different batch sizes or different image sizes.
If reshaping of data batch relates to modification of symbol or module, such as
changing image layout ordering or switching from training to predicting, module
rebinding is required.
Parameters
----------
$data_batch : DataBatch
Could be anything with similar API implemented.
:$is_train= : Bool
Default is undef, which means is_train takes the value of $self->for_training.
=cut
method forward(AI::MXNet::DataBatch $data_batch, Bool :$is_train=) { confess("NotImplemented") }
=head2 backward
Backward computation.
Parameters
----------
$out_grads : Maybe[AI::MXNet::NDArray|ArrayRef[AI::MXNet::NDArray]], optional
Gradient on the outputs to be propagated back.
This parameter is only needed when bind is called
on outputs that are not a loss function.
=cut
method backward(Maybe[AI::MXNet::NDArray|ArrayRef[AI::MXNet::NDArray]] $out_grads=)
{
confess("NotImplemented")
}
=head2 get_outputs
The outputs of the previous forward computation.
Parameters
----------
$merge_multi_context=1 : Bool
=cut
method get_outputs(Bool $merge_multi_context=1) { confess("NotImplemented") }
=head2 get_input_grads
The gradients to the inputs, computed in the previous backward computation.
Parameters
----------
$merge_multi_context=1 : Bool
=cut
method get_input_grads(Bool $merge_multi_context=1) { confess("NotImplemented") }
=head2 update
Update parameters according to the installed optimizer and the gradients computed
in the previous forward-backward batch.
=cut
method update() { confess("NotImplemented") }
=head2 update_metric
Evaluate and accumulate evaluation metric on outputs of the last forward computation.
Parameters
----------
$eval_metric : EvalMetric
$labels : ArrayRef[AI::MXNet::NDArray]
Typically $data_batch->label.
=cut
method update_metric(EvalMetric $eval_metric, ArrayRef[AI::MXNet::NDArray] $labels)
{
confess("NotImplemented")
}
################################################################################
# module setup
################################################################################
=head2 bind
Binds the symbols in order to construct the executors. This is necessary
before the computations can be performed.
Parameters
----------
$data_shapes : ArrayRef[AI::MXNet::DataDesc]
Typically is $data_iter->provide_data.
:$label_shapes= : Maybe[ArrayRef[AI::MXNet::DataDesc]]
Typically is $data_iter->provide_label.
:$for_training=1 : Bool
Default is 1. Whether the executors should be bind for training.
:$inputs_need_grad=0 : Bool
Default is 0. Whether the gradients to the input data need to be computed.
Typically this is not needed. But this might be needed when implementing composition
of modules.
:$force_rebind=0 : Bool
Default is 0. This function does nothing if the executors are already
binded. But with this as 1, the executors will be forced to rebind.
:$shared_module= : A subclass of AI::MXNet::Module::Base
Default is undef. This is used in bucketing. When not undef, the shared module
essentially corresponds to a different bucket -- a module with different symbol
but with the same sets of parameters (e.g. unrolled RNNs with different lengths).
:$grad_req='write' : Str|ArrayRef[Str]|HashRef[Str]
Requirement for gradient accumulation. Can be 'write', 'add', or 'null'
(defaults to 'write').
Can be specified globally (str) or for each argument (array ref, hash ref).
=cut
method bind(
( run in 1.504 second using v1.01-cache-2.11-cpan-54e63673c56 )