| 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 | package AI::MXNet::LRScheduler; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Mouse; | ||
| 22 | use AI::MXNet::Function::Parameters; | ||
| 23 | use AI::MXNet::Logging; | ||
| 24 | use overload "&{}" => sub { my $self = shift; sub { $self->call(@_) } }, | ||
| 25 | fallback => 1; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | AI::MXNet::LRScheduler - The adaptive scheduler of the learning rate. | ||
| 30 | =cut | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Learning rate scheduler, which adaptively changes the learning rate based on the | ||
| 35 | progress. | ||
| 36 | =cut | ||
| 37 | |||
| 38 | =head2 new | ||
| 39 | |||
| 40 | base_lr : float (optional, default 0.01) | ||
| 41 | the initial learning rate | ||
| 42 | =cut | ||
| 43 | |||
| 44 | has 'base_lr' => (is => 'rw', isa => 'Num', default => 0.01); | ||
| 45 | |||
| 46 | =head2 call | ||
| 47 | |||
| 48 | Call to schedule current learning rate | ||
| 49 | |||
| 50 | The training progress is presented by num_update, which can be roughly | ||
| 51 | viewed as the number of minibatches executed so far. Its value is | ||
| 52 | non-decreasing, and increases at most by one. | ||
| 53 | |||
| 54 | The exact value is the upper bound of the number of updates applied to | ||
| 55 | a weight/index | ||
| 56 | |||
| 57 | See more details in https://github.com/apache/mxnet/issues/625 | ||
| 58 | |||
| 59 | Parameters | ||
| 60 | ---------- | ||
| 61 | $num_update: Int | ||
| 62 | the maximal number of updates applied to a weight. | ||
| 63 | =cut | ||
| 64 | |||
| 65 | package AI::MXNet::FactorScheduler; | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | AI::MXNet::FactorScheduler - Reduces the learning rate by a factor. | ||
| 70 | |||
| 71 | =head1 DESCRIPTION | ||
| 72 | |||
| 73 | Reduces the learning rate by a factor each step. | ||
| 74 | Assume the weight has been updated by n times, then the learning rate will | ||
| 75 | be base_lr * factor^(floor(n/step)) | ||
| 76 | |||
| 77 | Parameters | ||
| 78 | ---------- | ||
| 79 | step: Int | ||
| 80 | schedule the learning rate update after n updates | ||
| 81 | factor: Num | ||
| 82 | the factor by which to reduce the learning rate. | ||
| 83 | =cut | ||
| 84 | use Mouse; | ||
| 85 | extends 'AI::MXNet::LRScheduler'; | ||
| 86 | |||
| 87 | has 'step' => (is => 'ro', isa => 'Int', required => 1); | ||
| 88 | has 'factor' => (is => 'ro', isa => 'Num', default => 1); | ||
| 89 | has 'count' => (is => 'rw', isa => 'Int', default => 1); | ||
| 90 | has 'stop_factor_lr' => (is => 'ro', isa => 'Num', default => 1e-8); | ||
| 91 | |||
| 92 | sub BUILD | ||
| 93 | { | ||
| 94 | my $self = shift; | ||
| 95 | confess("Schedule step must be greater or equal than 1") | ||
| 96 | if $self->step < 1; | ||
| 97 | confess("Factor must be no more than 1 to make lr reduce") | ||
| 98 | if $self->factor > 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | method call(Int $num_update) | ||
| 102 | { | ||
| 103 | # NOTE: use while rather than if (for continuing training via load_epoch) | ||
| 104 | while($num_update > $self->count + $self->step) | ||
| 105 | { | ||
| 106 | $self->count($self->count + $self->step); | ||
| 107 | $self->base_lr($self->base_lr * $self->factor); | ||
| 108 | if($self->base_lr < $self->stop_factor_lr) | ||
| 109 | { | ||
| 110 | $self->base_lr($self->stop_factor_lr); | ||
| 111 | AI::MXNet::Logging->info( | ||
| 112 | "Update[%d]: now learning rate arrived at %0.5e, will not " | ||
| 113 | ."change in the future", $num_update, $self->base_lr | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | else | ||
| 117 | { | ||
| 118 | AI::MXNet::Logging->info( | ||
| 119 | "Update[%d]: Changed learning rate to %0.5e", | ||
| 120 | $num_update, $self->base_lr | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | return $self->base_lr; | ||
| 125 | } | ||
| 126 | |||
| 127 | package AI::MXNet::MultiFactorScheduler; | ||
| 128 | |||
| 129 | =head1 NAME | ||
| 130 | |||
| 131 | AI::MXNet::MultiFactorScheduler - Reduces the learning rate by an array ref of factors. | ||
| 132 | |||
| 133 | =head1 DESCRIPTION | ||
| 134 | |||
| 135 | Reduces a learning rate in factor at steps specified in an array ref. | ||
| 136 | Assume the weight has been updated by n times, then the learning rate will | ||
| 137 | be base_lr * factor^(sum((step/n)<=1)) # step is an array. | ||
| 138 | |||
| 139 | Parameters | ||
| 140 | ---------- | ||
| 141 | step: ArrayRef[Int] | ||
| 142 | schedule learning rate after n updates | ||
| 143 | factor: Num | ||
| 144 | the factor for reducing the learning rate | ||
| 145 | =cut | ||
| 146 | |||
| 147 | use Mouse; | ||
| 148 | extends 'AI::MXNet::LRScheduler'; | ||
| 149 | has 'step' => (is => 'ro', isa => 'ArrayRef[Int]', required => 1); | ||
| 150 | has 'factor' => (is => 'ro', isa => 'Num', default => 1); | ||
| 151 | has 'cur_step_ind' => (is => 'rw', isa => 'Int', default => 0); | ||
| 152 | has 'count' => (is => 'rw', isa => 'Int', default => 0); | ||
| 153 | |||
| 154 | sub BUILD | ||
| 155 | { | ||
| 156 | my $self = shift; | ||
| 157 | confess("step array must have at least one member") | ||
| 158 | unless @{ $self->step } >=1 ; | ||
| 159 | for (my $i = 0; $i < @{ $self->step }; $i++) | ||
| 160 | { | ||
| 161 | confess("Schedule step must be an increasing integer list") | ||
| 162 | if($i and $self->step->[$i] <= $self->step->[$i-1]); | ||
| 163 | confess("Schedule step must be greater or equal than 1") | ||
| 164 | if $self->step->[$i] < 1; | ||
| 165 | } | ||
| 166 | confess("Factor must be no more than 1 to make lr reduce") | ||
| 167 | if $self->factor > 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | method call(Int $num_update) | ||
| 171 | { | ||
| 172 | # NOTE: use while rather than if (for continuing training via load_epoch) | ||
| 173 | while($self->cur_step_ind < @{ $self->step }) | ||
| 174 | { | ||
| 175 | if($num_update > $self->step->[$self->cur_step_ind]) | ||
| 176 | { | ||
| 177 | $self->count($self->step->[$self->cur_step_ind]); | ||
| 178 | $self->cur_step_ind($self->cur_step_ind + 1); | ||
| 179 | $self->base_lr($self->base_lr * $self->factor); | ||
| 180 | AI::MXNet::Logging->info( | ||
| 181 | "Update[%d]: Changed learning rate to %0.5e", | ||
| 182 | $num_update, $self->base_lr | ||
| 183 | ); | ||
| 184 | } | ||
| 185 | else | ||
| 186 | { | ||
| 187 | return $self->base_lr; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | return $self->base_lr; | ||
| 191 | } | ||
| 192 | |||
| 193 | 1; |