adam.py 4.3 KB
Newer Older
X
xixiaoyao 已提交
1
# -*- coding: UTF-8 -*-
X
xixiaoyao 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
"""Optimization and learning rate scheduling."""

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

import numpy as np
import paddle.fluid as fluid

X
xixiaoyao 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
class schedualer(object):
    
    def __init__(self):
        pass

    def lr(self):
        pass


def ConstantLearning():
    def __init__(self, lr):
        self._lr = lr

    def lr(self):
        return self._lr


def LinearWarmupLearning():
X
xixiaoyao 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
def linear_warmup_decay(learning_rate, warmup_steps, num_train_steps):
    """ Applies linear warmup of learning rate from 0 and decay to 0."""
    with fluid.default_main_program()._lr_schedule_guard():
        lr = fluid.layers.tensor.create_global_var(
            shape=[1],
            value=0.0,
            dtype='float32',
            persistable=True,
            name="scheduled_learning_rate")

        global_step = fluid.layers.learning_rate_scheduler._decay_step_counter()

        with fluid.layers.control_flow.Switch() as switch:
            with switch.case(global_step < warmup_steps):
                warmup_lr = learning_rate * (global_step / warmup_steps)
                fluid.layers.tensor.assign(warmup_lr, lr)
            with switch.default():
                decayed_lr = fluid.layers.learning_rate_scheduler.polynomial_decay(
                    learning_rate=learning_rate,
                    decay_steps=num_train_steps,
                    end_learning_rate=0.0,
                    power=1.0,
                    cycle=False)
                fluid.layers.tensor.assign(decayed_lr, lr)

        return lr


X
xixiaoyao 已提交
70
def optimize(loss, config, max_train_steps=None, warmup_steps=0, train_program=None):
X
xixiaoyao 已提交
71
    if warmup_steps > 0:
X
xixiaoyao 已提交
72 73
        decay_strategy = config.get('lr_scheduler', 'linear_warmup_decay')
        if decay_strategy == 'noam_decay':
X
xixiaoyao 已提交
74
            scheduled_lr = fluid.layers.learning_rate_scheduler\
X
xixiaoyao 已提交
75
             .noam_decay(1/(warmup_steps *(config['learning_rate'] ** 2)),
X
xixiaoyao 已提交
76
                         warmup_steps)
X
xixiaoyao 已提交
77 78 79
        elif decay_strategy == 'linear_warmup_decay':
            scheduled_lr = linear_warmup_decay(config['learning_rate'], warmup_steps,
                                               max_train_steps)
X
xixiaoyao 已提交
80
        else:
X
xixiaoyao 已提交
81
            raise ValueError("Unkown lr_scheduler, should be "
X
xixiaoyao 已提交
82 83 84
                             "'noam_decay' or 'linear_warmup_decay'")
        optimizer = fluid.optimizer.Adam(learning_rate=scheduled_lr)
    else:
X
xixiaoyao 已提交
85 86
        optimizer = fluid.optimizer.Adam(learning_rate=config['learning_rate'])
        scheduled_lr = config['learning_rate']
X
xixiaoyao 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    clip_norm_thres = 1.0
    # When using mixed precision training, scale the gradient clip threshold
    # by loss_scaling
    fluid.clip.set_gradient_clip(
        clip=fluid.clip.GradientClipByGlobalNorm(clip_norm=clip_norm_thres))

    def exclude_from_weight_decay(name):
        if name.find("layer_norm") > -1:
            return True
        bias_suffix = ["_bias", "_b", ".b_0"]
        for suffix in bias_suffix:
            if name.endswith(suffix):
                return True
        return False

    param_list = dict()

X
xixiaoyao 已提交
105 106 107 108 109 110
    for param in train_program.global_block().all_parameters():
        param_list[param.name] = param * 1.0
        param_list[param.name].stop_gradient = True

    _, param_grads = optimizer.minimize(loss)

X
xixiaoyao 已提交
111 112

    if config.get('weight_decay', 0) > 0:
X
xixiaoyao 已提交
113 114 115 116 117 118
        for param, grad in param_grads:
            if exclude_from_weight_decay(param.name):
                continue
            with param.block.program._optimized_guard(
                [param, grad]), fluid.framework.name_scope("weight_decay"):
                updated_param = param - param_list[
X
xixiaoyao 已提交
119
                    param.name] * config['weight_decay'] * scheduled_lr
X
xixiaoyao 已提交
120
                fluid.layers.assign(output=param, input=updated_param)
X
xixiaoyao 已提交
121