learning_rate.py 6.7 KB
Newer Older
S
shippingwang 已提交
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
W
WuHaobo 已提交
2
#
S
shippingwang 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
W
WuHaobo 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
S
shippingwang 已提交
9 10 11 12 13
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
W
WuHaobo 已提交
14 15 16
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
L
littletomatodonkey 已提交
17 18
from __future__ import unicode_literals
from paddle.optimizer import lr
W
WuHaobo 已提交
19 20


L
littletomatodonkey 已提交
21
class Linear(object):
W
WuHaobo 已提交
22
    """
L
littletomatodonkey 已提交
23
    Linear learning rate decay
W
WuHaobo 已提交
24
    Args:
L
littletomatodonkey 已提交
25 26 27 28 29
        lr (float): The initial learning rate. It is a python float number.
        epochs(int): The decay step size. It determines the decay cycle.
        end_lr(float, optional): The minimum final learning rate. Default: 0.0001.
        power(float, optional): Power of polynomial. Default: 1.0.
        last_epoch (int, optional):  The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
W
WuHaobo 已提交
30 31
    """

L
littletomatodonkey 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    def __init__(self,
                 learning_rate,
                 epochs,
                 step_each_epoch,
                 end_lr=0.0,
                 power=1.0,
                 warmup_epoch=0,
                 last_epoch=-1,
                 **kwargs):
        super(Linear, self).__init__()
        self.learning_rate = learning_rate
        self.epochs = epochs * step_each_epoch
        self.end_lr = end_lr
        self.power = power
        self.last_epoch = last_epoch
        self.warmup_epoch = round(warmup_epoch * step_each_epoch)
W
WuHaobo 已提交
48

L
littletomatodonkey 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    def __call__(self):
        learning_rate = lr.PolynomialDecay(
            learning_rate=self.learning_rate,
            decay_steps=self.epochs,
            end_lr=self.end_lr,
            power=self.power,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
            learning_rate = lr.LinearWarmup(
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
                end_lr=self.learning_rate,
                last_epoch=self.last_epoch)
        return learning_rate


class Cosine(object):
W
WuHaobo 已提交
67
    """
L
littletomatodonkey 已提交
68 69
    Cosine learning rate decay
    lr = 0.05 * (math.cos(epoch * (math.pi / epochs)) + 1)
W
WuHaobo 已提交
70 71 72 73
    Args:
        lr(float): initial learning rate
        step_each_epoch(int): steps each epoch
        epochs(int): total training epochs
L
littletomatodonkey 已提交
74
        last_epoch (int, optional):  The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
W
WuHaobo 已提交
75 76
    """

L
littletomatodonkey 已提交
77 78 79 80 81 82 83 84 85 86 87 88
    def __init__(self,
                 learning_rate,
                 step_each_epoch,
                 epochs,
                 warmup_epoch=0,
                 last_epoch=-1,
                 **kwargs):
        super(Cosine, self).__init__()
        self.learning_rate = learning_rate
        self.T_max = step_each_epoch * epochs
        self.last_epoch = last_epoch
        self.warmup_epoch = round(warmup_epoch * step_each_epoch)
W
WuHaobo 已提交
89

L
littletomatodonkey 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    def __call__(self):
        learning_rate = lr.CosineAnnealingDecay(
            learning_rate=self.learning_rate,
            T_max=self.T_max,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
            learning_rate = lr.LinearWarmup(
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
                end_lr=self.learning_rate,
                last_epoch=self.last_epoch)
        return learning_rate


class Step(object):
S
shippingwang 已提交
106
    """
L
littletomatodonkey 已提交
107
    Piecewise learning rate decay
S
shippingwang 已提交
108 109
    Args:
        step_each_epoch(int): steps each epoch
L
littletomatodonkey 已提交
110 111 112 113 114
        learning_rate (float): The initial learning rate. It is a python float number.
        step_size (int): the interval to update.
        gamma (float, optional): The Ratio that the learning rate will be reduced. ``new_lr = origin_lr * gamma`` .
            It should be less than 1.0. Default: 0.1.
        last_epoch (int, optional):  The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
S
shippingwang 已提交
115 116
    """

S
shippingwang 已提交
117
    def __init__(self,
L
littletomatodonkey 已提交
118 119
                 learning_rate,
                 step_size,
S
shippingwang 已提交
120
                 step_each_epoch,
L
littletomatodonkey 已提交
121 122 123
                 gamma,
                 warmup_epoch=0,
                 last_epoch=-1,
S
shippingwang 已提交
124
                 **kwargs):
L
littletomatodonkey 已提交
125 126 127 128 129 130
        super(Step, self).__init__()
        self.step_size = step_each_epoch * step_size
        self.learning_rate = learning_rate
        self.gamma = gamma
        self.last_epoch = last_epoch
        self.warmup_epoch = round(warmup_epoch * step_each_epoch)
S
shippingwang 已提交
131

L
littletomatodonkey 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    def __call__(self):
        learning_rate = lr.StepDecay(
            learning_rate=self.learning_rate,
            step_size=self.step_size,
            gamma=self.gamma,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
            learning_rate = lr.LinearWarmup(
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
                end_lr=self.learning_rate,
                last_epoch=self.last_epoch)
        return learning_rate


class Piecewise(object):
W
WuHaobo 已提交
149
    """
L
littletomatodonkey 已提交
150
    Piecewise learning rate decay
W
WuHaobo 已提交
151
    Args:
L
littletomatodonkey 已提交
152 153 154 155
        boundaries(list): A list of steps numbers. The type of element in the list is python int.
        values(list): A list of learning rate values that will be picked during different epoch boundaries.
            The type of element in the list is python float.
        last_epoch (int, optional):  The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
W
WuHaobo 已提交
156 157 158
    """

    def __init__(self,
L
littletomatodonkey 已提交
159 160 161 162 163 164 165 166 167 168 169
                 step_each_epoch,
                 decay_epochs,
                 values,
                 warmup_epoch=0,
                 last_epoch=-1,
                 **kwargs):
        super(Piecewise, self).__init__()
        self.boundaries = [step_each_epoch * e for e in decay_epochs]
        self.values = values
        self.last_epoch = last_epoch
        self.warmup_epoch = round(warmup_epoch * step_each_epoch)
W
WuHaobo 已提交
170 171

    def __call__(self):
L
littletomatodonkey 已提交
172 173 174 175 176 177 178 179 180 181 182 183
        learning_rate = lr.PiecewiseDecay(
            boundaries=self.boundaries,
            values=self.values,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
            learning_rate = lr.LinearWarmup(
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
                end_lr=self.values[0],
                last_epoch=self.last_epoch)
        return learning_rate