Contrib/NN/BasicLayers.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 66 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 use strict;
19 use warnings;
20 package AI::MXNet::Gluon::Contrib::NN::BasicLayers;
21  
22 =head1 NAME
23  
24 AI::MXNet::Gluon::Contrib::NN::BasicLayers - An additional collection of Gluon's building blocks.
25 =cut
26  
27 use AI::MXNet::Function::Parameters;
28 package AI::MXNet::Gluon::NN::Concurrent;
29 use AI::MXNet::Gluon::Mouse;
30 extends 'AI::MXNet::Gluon::NN::Sequential';
31  
32 =head1 NAME
33  
34 AI::MXNet::Gluon::NN::Concurrent - Lays Blocks concurrently.
35 =cut
36  
37 =head1 DESCRIPTION
38  
39 Lays Blocks concurrently.
40  
41 This block feeds its input to all children blocks, and
42 produces the output by concatenating all the children blocks' outputs
43 on the specified axis.
44  
45 Example:
46  
47 $net = nn->Concurrent();
48 # use net's name_scope to give children blocks appropriate names.
49 $net->name_scope(sub {
50 $net->add(nn->Dense(10, activation=>'relu'));
51 $net->add(nn->Dense(20));
52 $net->add(nn->Identity());
53 });
54  
55 Parameters
56 ----------
57 axis : int, default -1
58 The axis on which to concatenate the outputs.
59 =cut
60 has 'axis' => (is => 'rw', isa => 'Int', default => -1);
61 method python_constructor_arguments() { ['axis'] }
62  
63 method forward(GluonInput $x)
64 {
65 return AI::MXNet::NDArray->concat((map { $_->($x) } $self->_children->values), dim=>$self->axis);
66 }
67  
68 __PACKAGE__->register('AI::MXNet::Gluon::NN');
69  
70 package AI::MXNet::Gluon::NN::HybridConcurrent;
71 use AI::MXNet::Gluon::Mouse;
72 extends 'AI::MXNet::Gluon::NN::HybridSequential';
73  
74 =head1 NAME
75  
76 AI::MXNet::Gluon::NN::HybridConcurrent - Lays HubridBlocks concurrently.
77 =cut
78  
79 =head1 DESCRIPTION
80  
81 Lays HybridBlocks concurrently.
82  
83 This block feeds its input to all children blocks, and
84 produces the output by concatenating all the children blocks' outputs
85 on the specified axis.
86  
87 Example:
88  
89 $net = nn->HybridConcurrent();
90 # use net's name_scope to give children blocks appropriate names.
91 $net->name_scope(sub {
92 $net->add(nn->Dense(10, activation=>'relu'));
93 $net->add(nn->Dense(20));
94 $net->add(nn->Identity());
95 });
96  
97 Parameters
98 ----------
99 axis : int, default -1
100 The axis on which to concatenate the outputs.
101 =cut
102 has 'axis' => (is => 'rw', isa => 'Int', default => -1);
103 method python_constructor_arguments() { ['axis'] }
104  
105 method hybrid_forward(GluonClass $F, GluonInput $x)
106 {
107 return $F->concat((map { $_->($x) } $self->_children->values), dim=>$self->axis);
108 }
109  
110 __PACKAGE__->register('AI::MXNet::Gluon::NN');
111  
112 package AI::MXNet::Gluon::NN::Identity;
113 use AI::MXNet::Gluon::Mouse;
114 extends 'AI::MXNet::Gluon::HybridBlock';
115  
116 =head1 NAME
117  
118 AI::MXNet::Gluon::NN::Identity - Block that passes through the input directly.
119 =cut
120  
121 =head1 DESCRIPTION
122  
123 Block that passes through the input directly.
124  
125 This block can be used in conjunction with HybridConcurrent
126 block for residual connection.
127  
128 Example:
129  
130 $net = nn->HybridConcurrent();
131 # use net's name_scope to give child Blocks appropriate names.
132 $net->name_scope(sub {
133 $net->add(nn->Dense(10, activation=>'relu'));
134 $net->add(nn->Dense(20));
135 $net->add(nn->Identity());
136 });
137 =cut
138  
139 method hybrid_forward(GluonClass $F, GluonInput $x)
140 {
141 return $x;
142 }
143  
144 __PACKAGE__->register('AI::MXNet::Gluon::NN');
145  
146 package AI::MXNet::Gluon::NN::SparseEmbedding;
147 use AI::MXNet::Gluon::Mouse;
148 extends 'AI::MXNet::Gluon::Block';
149  
150 =head1 NAME
151  
152 AI::MXNet::Gluon::NN::SparseEmbedding - Turns non-negative integers (indexes/tokens) into dense vectors.
153 =cut
154  
155 =head1 DESCRIPTION
156  
157 Turns non-negative integers (indexes/tokens) into dense vectors
158 of fixed size. eg. [4, 20] -> [[0.25, 0.1], [0.6, -0.2]]
159  
160 This SparseBlock is designed for distributed training with extremely large
161 input dimension. Both weight and gradient w.r.t. weight are AI::MXNet::NDArray::RowSparse.
162  
163 Parameters
164 ----------
165 input_dim : int
166 Size of the vocabulary, i.e. maximum integer index + 1.
167 output_dim : int
168 Dimension of the dense embedding.
169 dtype : Dtype, default 'float32'
170 Data type of output embeddings.
171 weight_initializer : Initializer
172 Initializer for the embeddings matrix.
173 =cut
174  
175 has 'input_dim' => (is => 'ro', isa => 'Int', required => 1);
176 has 'output_dim' => (is => 'ro', isa => 'Int', required => 1);
177 has 'dtype' => (is => 'ro', isa => 'Dtype', default => 'float32');
178 has 'weight_initializer' => (is => 'ro', isa => 'Maybe[Initializer]');
179 method python_constructor_arguments() { [qw/input_dim output_dim dtype weight_initializer/] }
180  
181 sub BUILD
182 {
183 my $self = shift;
184 $self->_kwargs({
185 input_dim => $self->input_dim,
186 output_dim => $self->output_dim,
187 dtype => $self->dtype,
188 sparse_grad => 1
189 });
190 $self->weight($self->params->get('weight', shape=>[$self->input_dim, $self->output_dim],
191 init=>$self->weight_initializer, dtype=>$self->dtype,
192 grad_stype=>'row_sparse', stype=>'row_sparse'));
193 }
194  
195 method forward(GluonInput $x)
196 {
197 my $weight = $self->weight->row_sparse_data($x);
198 return AI::MXNet::NDArray->Embedding($x, $weight, { name=>'fwd', %{ $self->_kwargs } });
199 }
200  
201 use overload '""' => sub {
202 my $self = shift;
203 $self->_class_name.'('.$self->input_dim.' -> '.$self->input_dim.', '.$self->dtype.')';
204 };
205  
206 __PACKAGE__->register('AI::MXNet::Gluon::NN');
207  
208 1;