learning_rate.py 6.64 KB
Newer Older
WenmuZhou's avatar
WenmuZhou committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# 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
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

WenmuZhou's avatar
WenmuZhou committed
20
from paddle.optimizer import lr
WenmuZhou's avatar
WenmuZhou committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34


class Linear(object):
    """
    Linear learning rate decay
    Args:
        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.
    """

    def __init__(self,
WenmuZhou's avatar
WenmuZhou committed
35
                 learning_rate,
WenmuZhou's avatar
WenmuZhou committed
36
37
38
39
40
41
42
43
                 epochs,
                 step_each_epoch,
                 end_lr=0.0,
                 power=1.0,
                 warmup_epoch=0,
                 last_epoch=-1,
                 **kwargs):
        super(Linear, self).__init__()
WenmuZhou's avatar
WenmuZhou committed
44
        self.learning_rate = learning_rate
WenmuZhou's avatar
WenmuZhou committed
45
46
47
48
49
50
51
        self.epochs = epochs * step_each_epoch
        self.end_lr = end_lr
        self.power = power
        self.last_epoch = last_epoch
        self.warmup_epoch = warmup_epoch * step_each_epoch

    def __call__(self):
WenmuZhou's avatar
WenmuZhou committed
52
53
        learning_rate = lr.PolynomialDecay(
            learning_rate=self.learning_rate,
WenmuZhou's avatar
WenmuZhou committed
54
55
56
57
58
            decay_steps=self.epochs,
            end_lr=self.end_lr,
            power=self.power,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
WenmuZhou's avatar
WenmuZhou committed
59
            learning_rate = lr.LinearWarmup(
WenmuZhou's avatar
WenmuZhou committed
60
61
62
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
WenmuZhou's avatar
WenmuZhou committed
63
                end_lr=self.learning_rate,
WenmuZhou's avatar
WenmuZhou committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                last_epoch=self.last_epoch)
        return learning_rate


class Cosine(object):
    """
    Cosine learning rate decay
    lr = 0.05 * (math.cos(epoch * (math.pi / epochs)) + 1)
    Args:
        lr(float): initial learning rate
        step_each_epoch(int): steps each epoch
        epochs(int): total training epochs
        last_epoch (int, optional):  The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
    """

    def __init__(self,
WenmuZhou's avatar
WenmuZhou committed
80
                 learning_rate,
WenmuZhou's avatar
WenmuZhou committed
81
82
83
84
85
86
                 step_each_epoch,
                 epochs,
                 warmup_epoch=0,
                 last_epoch=-1,
                 **kwargs):
        super(Cosine, self).__init__()
WenmuZhou's avatar
WenmuZhou committed
87
        self.learning_rate = learning_rate
WenmuZhou's avatar
WenmuZhou committed
88
89
90
91
92
        self.T_max = step_each_epoch * epochs
        self.last_epoch = last_epoch
        self.warmup_epoch = warmup_epoch * step_each_epoch

    def __call__(self):
WenmuZhou's avatar
WenmuZhou committed
93
94
95
96
        learning_rate = lr.CosineAnnealingDecay(
            learning_rate=self.learning_rate,
            T_max=self.T_max,
            last_epoch=self.last_epoch)
WenmuZhou's avatar
WenmuZhou committed
97
        if self.warmup_epoch > 0:
WenmuZhou's avatar
WenmuZhou committed
98
            learning_rate = lr.LinearWarmup(
WenmuZhou's avatar
WenmuZhou committed
99
100
101
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
WenmuZhou's avatar
WenmuZhou committed
102
                end_lr=self.learning_rate,
WenmuZhou's avatar
WenmuZhou committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
                last_epoch=self.last_epoch)
        return learning_rate


class Step(object):
    """
    Piecewise learning rate decay
    Args:
        step_each_epoch(int): steps each epoch
        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.
    """

    def __init__(self,
WenmuZhou's avatar
WenmuZhou committed
120
                 learning_rate,
WenmuZhou's avatar
WenmuZhou committed
121
122
123
124
125
126
127
128
                 step_size,
                 step_each_epoch,
                 gamma,
                 warmup_epoch=0,
                 last_epoch=-1,
                 **kwargs):
        super(Step, self).__init__()
        self.step_size = step_each_epoch * step_size
WenmuZhou's avatar
WenmuZhou committed
129
        self.learning_rate = learning_rate
WenmuZhou's avatar
WenmuZhou committed
130
131
132
133
134
        self.gamma = gamma
        self.last_epoch = last_epoch
        self.warmup_epoch = warmup_epoch * step_each_epoch

    def __call__(self):
WenmuZhou's avatar
WenmuZhou committed
135
136
        learning_rate = lr.StepDecay(
            learning_rate=self.learning_rate,
WenmuZhou's avatar
WenmuZhou committed
137
138
139
140
            step_size=self.step_size,
            gamma=self.gamma,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
WenmuZhou's avatar
WenmuZhou committed
141
            learning_rate = lr.LinearWarmup(
WenmuZhou's avatar
WenmuZhou committed
142
143
144
                learning_rate=learning_rate,
                warmup_steps=self.warmup_epoch,
                start_lr=0.0,
WenmuZhou's avatar
WenmuZhou committed
145
                end_lr=self.learning_rate,
WenmuZhou's avatar
WenmuZhou committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                last_epoch=self.last_epoch)
        return learning_rate


class Piecewise(object):
    """
    Piecewise learning rate decay
    Args:
        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.
    """

    def __init__(self,
                 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 = warmup_epoch * step_each_epoch

    def __call__(self):
WenmuZhou's avatar
WenmuZhou committed
174
        learning_rate = lr.PiecewiseDecay(
WenmuZhou's avatar
WenmuZhou committed
175
176
177
178
            boundaries=self.boundaries,
            values=self.values,
            last_epoch=self.last_epoch)
        if self.warmup_epoch > 0:
WenmuZhou's avatar
WenmuZhou committed
179
            learning_rate = lr.LinearWarmup(
WenmuZhou's avatar
WenmuZhou committed
180
181
182
183
184
185
                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