当前位置:   article > 正文

Keras---学习率衰减_keras学习率衰减

keras学习率衰减

选择合适的学习率能够提高随机梯度下降算法的性能,并减少训练时间。为了能够使梯度下降有更好的性能,需要把学习率的值设定在合适的范围内。学习率决定了参数移动到最优值时的速度。如果learning rate过大,可能会越过最优值;过小会导致下降的过慢,长时间无法收敛。

学习率衰减的基本思想是:学习率随着训练的进行逐渐衰减。在训练开始时,使用较大的learning rate,可以加快梯度下降即快速收敛。随着训练的进行,逐步降低learning rate,即收敛速度减小。

目前比较流行的两种学习率衰减方法为:

  • 线性衰减(根据epoch逐渐衰减)
  • 指数衰减(在特定的epoch使用分数快速降低学习率)

learning rate 线性衰减

Keras中基于时间的线性learning rate衰减是通过 SGD 类中的随机梯度下降优化算法来实现的。它具有一个decay衰减率参数,该参数的线性衰减速率学习如下:

L e a r n i n g   R a t e = L e a r n i n g   R a t e ∗ 1 1 + d e c a y ∗ e p o c h Learning\ Rate = Learning\ Rate * \frac{1}{1 + decay * epoch} Learning Rate=Learning Rate1+decayepoch1

当decay衰减率为0(默认值)时,对学习率没有影响,使用非零时,学习率呈线性衰减,如下所示,decay衰减为0.001时,最初的5个epoch的学习率如下:

epoch    learning rate
1        0.1
2        0.0999000999
3        0.0997006985
4        0.09940249103
5        0.09900646517
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

下面代码例子中,SGD学习率设置为较高的0.1,decay设置为0.005,动量值设置为0.9

from sklearn import datasets
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import SGD


# 导入数据
dataset = datasets.load_iris()

x = dataset.data
Y = dataset.target

# 设定随机种子
seed = 7
np.random.seed(seed)

# 构建模型函数
def create_model(init='glorot_uniform'):
    # 构建模型
    model = Sequential()
    model.add(Dense(units=4, activation='relu', input_dim=4, kernel_initializer=init))
    model.add(Dense(units=6, activation='relu', kernel_initializer=init))
    model.add(Dense(units=3, activation='softmax', kernel_initializer=init))

    #模型优化
    learningRate = 0.1
    momentum = 0.9
    decay_rate = 0.005
    sgd = SGD(lr=learningRate, momentum=momentum, decay=decay_rate, nesterov=False)

    # 编译模型
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

    return model

epochs = 200
model = KerasClassifier(build_fn=create_model, epochs=epochs, batch_size=5, verbose=1)
model.fit(x, Y)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

learning rate 指数衰减

通常这种方法是通过固定的epoch周期将学习率降低50%来实现的。比如学习率初始为0.1,每10个epoch降低50%。前10个epoch使用0.1的学习率,接下来10个epoch使用0.05的学习率,依次类推,学习率以指数级进行衰减。在Keras中,使用LearningRateScheduler回调,来实现学习率的指数衰减。LearningRateScheduler回调使用预定义的回调函数来实现学习率的指数衰减,将epoch数作为参数,并将学习率返回得的SGD中使用。下面定义了一个step_decay()函数,实现了如下方程:

L e a r n i n g   R a t e = I n i t i a l   L e a r n i n g   R a t e ∗ D r o p   R a t e f l o o r ( 1 + E p o c h E p o c h   D r o p ) Learning\ Rate = Initial\ Learning\ Rate * Drop\ Rate^{floor(\frac{1+Epoch}{Epoch\ Drop})} Learning Rate=Initial Learning RateDrop Ratefloor(Epoch Drop1+Epoch)

from sklearn import datasets
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import SGD
from keras.callbacks import LearningRateScheduler
from math import pow, floor


# 导入数据
dataset = datasets.load_iris()

x = dataset.data
Y = dataset.target

# 设定随机种子
seed = 7
np.random.seed(seed)

# 计算学习率
def step_decay(epoch):
    init_lrate = 0.1
    drop = 0.5
    epochs_drop = 10
    lrate = init_lrate * pow(drop, floor(1 + epoch) / epochs_drop)
    return lrate

# 构建模型函数
def create_model(init='glorot_uniform'):
    # 构建模型
    model = Sequential()
    model.add(Dense(units=4, activation='relu', input_dim=4, kernel_initializer=init))
    model.add(Dense(units=6, activation='relu', kernel_initializer=init))
    model.add(Dense(units=3, activation='softmax', kernel_initializer=init))

    #模型优化
    learningRate = 0.1
    momentum = 0.9
    decay_rate = 0.0
    sgd = SGD(lr=learningRate, momentum=momentum, decay=decay_rate, nesterov=False)

    # 编译模型
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

    return model


lrate = LearningRateScheduler(step_decay)
epochs = 200
model = KerasClassifier(build_fn=create_model, epochs=epochs, batch_size=5, verbose=1, callbacks=[lrate])
model.fit(x, Y)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

learning rate 衰减使用技巧

  • 提高初始学习率
    更大的学习率在开始学习时会快速更新权重,而且随着学习率的衰减可以自动调整学习率,可以提高梯度下降的性能
  • 使用大动量
    使用较大的动量值将有助于优化算法在学习率缩小到小值时,继续向正确的方向更新权重值
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/349741
推荐阅读
相关标签
  

闽ICP备14008679号