AI-MXNet-Gluon-Contrib

 view release on metacpan or  search on metacpan

lib/AI/MXNet/Gluon/Contrib/NN/BasicLayers.pm  view on Meta::CPAN

    Lays Blocks concurrently.

    This block feeds its input to all children blocks, and
    produces the output by concatenating all the children blocks' outputs
    on the specified axis.

    Example:

        $net = nn->Concurrent();
        # use net's name_scope to give children blocks appropriate names.
        $net->name_scope(sub {
            $net->add(nn->Dense(10, activation=>'relu'));
            $net->add(nn->Dense(20));
            $net->add(nn->Identity());
        });

    Parameters
    ----------
    axis : int, default -1
        The axis on which to concatenate the outputs.
=cut

lib/AI/MXNet/Gluon/Contrib/NN/BasicLayers.pm  view on Meta::CPAN

    Lays HybridBlocks concurrently.

    This block feeds its input to all children blocks, and
    produces the output by concatenating all the children blocks' outputs
    on the specified axis.

    Example:

        $net = nn->HybridConcurrent();
        # use net's name_scope to give children blocks appropriate names.
        $net->name_scope(sub {
            $net->add(nn->Dense(10, activation=>'relu'));
            $net->add(nn->Dense(20));
            $net->add(nn->Identity());
        });

    Parameters
    ----------
    axis : int, default -1
        The axis on which to concatenate the outputs.
=cut

lib/AI/MXNet/Gluon/Contrib/NN/BasicLayers.pm  view on Meta::CPAN


    Block that passes through the input directly.

    This block can be used in conjunction with HybridConcurrent
    block for residual connection.

    Example:

        $net = nn->HybridConcurrent();
        # use net's name_scope to give child Blocks appropriate names.
        $net->name_scope(sub {
            $net->add(nn->Dense(10, activation=>'relu'));
            $net->add(nn->Dense(20));
            $net->add(nn->Identity());
        });
=cut

method hybrid_forward(GluonClass $F, GluonInput $x)
{
    return $x;
}

lib/AI/MXNet/Gluon/Contrib/NN/BasicLayers.pm  view on Meta::CPAN

    weight_initializer : Initializer
        Initializer for the embeddings matrix.
=cut

has 'input_dim'          => (is => 'ro', isa => 'Int', required => 1);
has 'output_dim'         => (is => 'ro', isa => 'Int', required => 1);
has 'dtype'              => (is => 'ro', isa => 'Dtype', default => 'float32');
has 'weight_initializer' => (is => 'ro', isa => 'Maybe[Initializer]');
method python_constructor_arguments() { [qw/input_dim output_dim dtype weight_initializer/] }

sub BUILD
{
    my $self = shift;
    $self->_kwargs({
        input_dim => $self->input_dim, 
        output_dim => $self->output_dim,
        dtype => $self->dtype,
        sparse_grad => 1
    });
    $self->weight($self->params->get('weight', shape=>[$self->input_dim, $self->output_dim],
                                      init=>$self->weight_initializer, dtype=>$self->dtype,
                                      grad_stype=>'row_sparse', stype=>'row_sparse'));
}

method forward(GluonInput $x)
{
    my $weight = $self->weight->row_sparse_data($x);
    return AI::MXNet::NDArray->Embedding($x, $weight, { name=>'fwd', %{ $self->_kwargs } });
}

use overload '""' => sub {
    my $self = shift;
    $self->_class_name.'('.$self->input_dim.' -> '.$self->input_dim.', '.$self->dtype.')';
};

__PACKAGE__->register('AI::MXNet::Gluon::NN');

1;



( run in 0.643 second using v1.01-cache-2.11-cpan-a5abf4f5562 )