| 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 | |||
| 21 | package AI::MXNet::Gluon::Data::Sampler; | ||
| 22 | use AI::MXNet::Function::Parameters; | ||
| 23 | use Mouse; | ||
| 24 | around BUILDARGS => \&AI::MXNet::Base::process_arguments; | ||
| 25 | |||
| 26 | method _class_name() | ||
| 27 | { | ||
| 28 | my $class = ref $self || $self; | ||
| 29 | $class =~ s/^.+:://; | ||
| 30 | $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | method register(Str $container) | ||
| 34 | { | ||
| 35 | my $sub_name = $self->_class_name; | ||
| 36 | no strict 'refs'; | ||
| 37 | *{$container.'::'.$sub_name} = sub { shift; $self->new(@_) }; | ||
| 38 | } | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | AI::MXNet::Gluon::Data::Sampler | ||
| 43 | =cut | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | Base class for samplers. | ||
| 48 | |||
| 49 | All samplers should subclass AI::MXNet::Gluon::Data::Sampler | ||
| 50 | and define method 'len' and 'next' | ||
| 51 | methods. | ||
| 52 | =cut | ||
| 53 | |||
| 54 | use overload '<>' => sub { shift->next }, | ||
| 55 | '@{}' => sub { shift->list }; | ||
| 56 | |||
| 57 | method list() | ||
| 58 | { | ||
| 59 | my @ret; | ||
| 60 | while(defined(my $data = <$self>)) | ||
| 61 | { | ||
| 62 | push @ret, $data; | ||
| 63 | } | ||
| 64 | return \@ret; | ||
| 65 | } | ||
| 66 | |||
| 67 | method len() { confess('Not Implemented') } | ||
| 68 | method next() { confess('Not Implemented') } | ||
| 69 | |||
| 70 | package AI::MXNet::Gluon::Data::Sampler::SequentialSampler; | ||
| 71 | use Mouse; | ||
| 72 | extends 'AI::MXNet::Gluon::Data::Sampler'; | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | AI::MXNet::Gluon::Data::Sampler::SequentialSampler | ||
| 77 | =cut | ||
| 78 | |||
| 79 | =head1 DESCRIPTION | ||
| 80 | |||
| 81 | Samples elements from [0, length) sequentially. | ||
| 82 | |||
| 83 | Parameters | ||
| 84 | ---------- | ||
| 85 | length : int | ||
| 86 | Length of the sequence. | ||
| 87 | =cut | ||
| 88 | has 'length' => (is => 'ro', isa => 'Int', required => 1); | ||
| 89 | has '_current' => (is => 'rw', init_arg => undef, default => 0); | ||
| 90 | method python_constructor_arguments() { ['length'] } | ||
| 91 | |||
| 92 | method next() | ||
| 93 | { | ||
| 94 | my $current = $self->_current; | ||
| 95 | if($self->_current == $self->length) | ||
| 96 | { | ||
| 97 | $self->reset; | ||
| 98 | return undef; | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | $self->_current($self->_current + 1); | ||
| 103 | return $current; | ||
| 104 | } | ||
| 105 | }; | ||
| 106 | |||
| 107 | method reset() { $self->_current(0) } | ||
| 108 | method len() { $self->length } | ||
| 109 | |||
| 110 | __PACKAGE__->register('AI::MXNet::Gluon::Data'); | ||
| 111 | |||
| 112 | package AI::MXNet::Gluon::Data::Sampler::RandomSampler; | ||
| 113 | use Mouse; | ||
| 114 | use List::Util qw(shuffle); | ||
| 115 | extends 'AI::MXNet::Gluon::Data::Sampler'; | ||
| 116 | |||
| 117 | =head1 NAME | ||
| 118 | |||
| 119 | AI::MXNet::Gluon::Data::Sampler::RandomSampler | ||
| 120 | =cut | ||
| 121 | |||
| 122 | =head1 DESCRIPTION | ||
| 123 | |||
| 124 | Samples elements from [0, length) randomly without replacement. | ||
| 125 | |||
| 126 | Parameters | ||
| 127 | ---------- | ||
| 128 | length : int | ||
| 129 | Length of the sequence. | ||
| 130 | =cut | ||
| 131 | has 'length' => (is => 'ro', isa => 'Int', required => 1); | ||
| 132 | has '_current' => (is => 'rw', init_arg => undef, default => 0); | ||
| 133 | has '_indices' => (is => 'rw', init_arg => undef); | ||
| 134 | method python_constructor_arguments() { ['length'] } | ||
| 135 | |||
| 136 | sub BUILD | ||
| 137 | { | ||
| 138 | my $self = shift; | ||
| 139 | $self->_indices([shuffle(0..$self->length-1)]); | ||
| 140 | } | ||
| 141 | |||
| 142 | method next() | ||
| 143 | { | ||
| 144 | my $current = $self->_current; | ||
| 145 | if($self->_current == $self->length) | ||
| 146 | { | ||
| 147 | $self->reset; | ||
| 148 | return undef; | ||
| 149 | } | ||
| 150 | else | ||
| 151 | { | ||
| 152 | $self->_current($self->_current + 1); | ||
| 153 | return $self->_indices->[$current]; | ||
| 154 | } | ||
| 155 | }; | ||
| 156 | |||
| 157 | method reset() { @{ $self->_indices } = shuffle(@{ $self->_indices }); $self->_current(0) } | ||
| 158 | method len() { $self->length } | ||
| 159 | |||
| 160 | __PACKAGE__->register('AI::MXNet::Gluon::Data'); | ||
| 161 | |||
| 162 | package AI::MXNet::Gluon::Data::Sampler::BatchSampler; | ||
| 163 | use Mouse; | ||
| 164 | use List::Util qw(shuffle); | ||
| 165 | extends 'AI::MXNet::Gluon::Data::Sampler'; | ||
| 166 | |||
| 167 | =head1 NAME | ||
| 168 | |||
| 169 | AI::MXNet::Gluon::Data::Sampler::BatchSampler | ||
| 170 | =cut | ||
| 171 | |||
| 172 | =head1 DESCRIPTION | ||
| 173 | |||
| 174 | Wraps over another AI::MXNet::Gluon::Data::Sampler and return mini-batches of samples. | ||
| 175 | |||
| 176 | Parameters | ||
| 177 | ---------- | ||
| 178 | sampler : AI::MXNet::Gluon::Data::Sampler | ||
| 179 | The source Sampler. | ||
| 180 | batch_size : int | ||
| 181 | Size of mini-batch. | ||
| 182 | last_batch : {'keep', 'discard', 'rollover'} | ||
| 183 | Specifies how the last batch is handled if batch_size does not evenly | ||
| 184 | divide sequence length. | ||
| 185 | |||
| 186 | If 'keep', the last batch will be returned directly, but will contain | ||
| 187 | less element than `batch_size` requires. | ||
| 188 | |||
| 189 | If 'discard', the last batch will be discarded. | ||
| 190 | |||
| 191 | If 'rollover', the remaining elements will be rolled over to the next | ||
| 192 | iteration. | ||
| 193 | |||
| 194 | Examples | ||
| 195 | -------- | ||
| 196 | >>> $sampler = gluon->data->SequentialSampler(10) | ||
| 197 | >>> $batch_sampler = gluon->data->BatchSampler($sampler, batch_size => 3, last_batch => 'keep'); | ||
| 198 | >>> @{ $batch_sampler } | ||
| 199 | [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] | ||
| 200 | =cut | ||
| 201 | has 'batch_size' => (is => 'ro', isa => 'Int', required => 1); | ||
| 202 | has 'sampler' => (is => 'ro', isa => 'AI::MXNet::Gluon::Data::Sampler', required => 1); | ||
| 203 | has 'last_batch' => (is => 'ro', isa => 'Str', default => 'keep'); | ||
| 204 | has '_prev' => (is => 'rw', init_arg => undef); | ||
| 205 | has '_kept' => (is => 'rw', init_arg => undef); | ||
| 206 | method python_constructor_arguments() { ['sampler', 'batch_size', 'last_batch'] } | ||
| 207 | |||
| 208 | sub BUILD | ||
| 209 | { | ||
| 210 | my $self = shift; | ||
| 211 | $self->_prev([]); | ||
| 212 | } | ||
| 213 | |||
| 214 | method next() | ||
| 215 | { | ||
| 216 | if($self->_kept) | ||
| 217 | { | ||
| 218 | $self->_kept(0); | ||
| 219 | return undef; | ||
| 220 | } | ||
| 221 | $self->_kept(0); | ||
| 222 | my $batch = $self->_prev; | ||
| 223 | $self->_prev([]); | ||
| 224 | my $sampler = $self->sampler; | ||
| 225 | while(defined(my $i = <$sampler>)) | ||
| 226 | { | ||
| 227 | push @{ $batch }, $i; | ||
| 228 | if(@{ $batch } == $self->batch_size) | ||
| 229 | { | ||
| 230 | return $batch; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | if(@{ $batch }) | ||
| 234 | { | ||
| 235 | if($self->last_batch eq 'keep') | ||
| 236 | { | ||
| 237 | $self->_kept(1); | ||
| 238 | return $batch; | ||
| 239 | } | ||
| 240 | elsif($self->last_batch eq 'discard') | ||
| 241 | { | ||
| 242 | return undef; | ||
| 243 | } | ||
| 244 | elsif($self->last_batch eq 'rollover') | ||
| 245 | { | ||
| 246 | $self->_prev($batch); | ||
| 247 | return undef; | ||
| 248 | } | ||
| 249 | else | ||
| 250 | { | ||
| 251 | confess( | ||
| 252 | "last_batch must be one of 'keep', 'discard', or 'rollover', ". | ||
| 253 | "but got ${\ $self->last_batch }" | ||
| 254 | ); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | return undef; | ||
| 258 | } | ||
| 259 | |||
| 260 | method len() | ||
| 261 | { | ||
| 262 | if($self->last_batch eq 'keep') | ||
| 263 | { | ||
| 264 | return int(($self->sampler->len + $self->batch_size - 1) / $self->batch_size); | ||
| 265 | } | ||
| 266 | elsif($self->last_batch eq 'discard') | ||
| 267 | { | ||
| 268 | return int($self->sampler->len/$self->batch_size); | ||
| 269 | } | ||
| 270 | elsif($self->last_batch eq 'rollover') | ||
| 271 | { | ||
| 272 | return int((@{ $self->_prev } + $self->sampler->len) / $self->batch_size); | ||
| 273 | } | ||
| 274 | else | ||
| 275 | { | ||
| 276 | confess( | ||
| 277 | "last_batch must be one of 'keep', 'discard', or 'rollover', ". | ||
| 278 | "but got ${\ $self->last_batch }" | ||
| 279 | ); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | __PACKAGE__->register('AI::MXNet::Gluon::Data'); | ||
| 284 | |||
| 285 | 1; |