Python 如何在Keras或Theano中实现具有指数衰减学习率的卷积神经网络

Python 如何在Keras或Theano中实现具有指数衰减学习率的卷积神经网络,python,machine-learning,neural-network,theano,keras,Python,Machine Learning,Neural Network,Theano,Keras,我想在Keras或Theano中实现具有指数衰减学习率的卷积神经网络(CNN)。学习速率根据以下更新规律动态变化: eta = et0*exp(LossFunction) where et0 is the initial learning rate and LossFunction is a cost function 我知道Keras允许设置SGD优化器: SGD(lr, momentum0, decay, nesterov) 衰减项仅允许在每个历元上出现固定的衰减学习率衰减 如何使用相

我想在Keras或Theano中实现具有指数衰减学习率的卷积神经网络(CNN)。学习速率根据以下更新规律动态变化:

eta = et0*exp(LossFunction)
where et0 is the initial learning rate and LossFunction is a cost function
我知道Keras允许设置SGD优化器:

SGD(lr, momentum0, decay, nesterov) 
衰减项仅允许在每个历元上出现固定的衰减学习率衰减

如何使用相对于成本函数呈指数衰减的学习率设置或编码SGD?为了供您参考,我在Keras中发布了SGD的源代码:

class SGD(Optimizer):

'''Stochastic gradient descent, with support for momentum,
learning rate decay, and Nesterov momentum.


# Arguments
    lr: float >= 0. Learning rate.
    momentum: float >= 0. Parameter updates momentum.
    decay: float >= 0. Learning rate decay over each update.
    nesterov: boolean. Whether to apply Nesterov momentum.
'''

def __init__(self, lr=0.01, momentum=0., decay=0.,

             nesterov=False, **kwargs):

    super(SGD, self).__init__(**kwargs)
    self.__dict__.update(locals())
    self.iterations = K.variable(0.)
    self.lr = K.variable(lr)
    self.momentum = K.variable(momentum)
    self.decay = K.variable(decay)
    self.inital_decay = decay

def get_updates(self, params, constraints, loss):
    grads = self.get_gradients(loss, params)
    self.updates = []

    lr = self.lr
    if self.inital_decay > 0:
        lr *= (1. / (1. + self.decay * self.iterations))
        self.updates .append(K.update_add(self.iterations, 1))

    # momentum
    shapes = [K.get_variable_shape(p) for p in params]
    moments = [K.zeros(shape) for shape in shapes]
    self.weights = [self.iterations] + moments

    for p, g, m in zip(params, grads, moments):
        v = self.momentum * m - lr * g  # velocity
        self.updates.append(K.update(m, v))

        if self.nesterov:
            new_p = p + self.momentum * v - lr * g
        else:
            new_p = p + v

        # apply constraints
        if p in constraints:
            c = constraints[p]
            new_p = c(new_p)

        self.updates.append(K.update(p, new_p))
    return self.updates

def get_config(self):
    config = {'lr': float(K.get_value(self.lr)),
              'momentum': float(K.get_value(self.momentum)),
              'decay': float(K.get_value(self.decay)),
              'nesterov': self.nesterov}

    base_config = super(SGD, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))

我认为您可以使用以下模式获得行为:

  • 使用创建一个新的学习速率控制器类
  • 当提供给
    fit
    method时,让构造器接受培训集并开始学习
  • 使其在每个历元后计算损失并更新学习率

  • Keras具有一个内置函数,用于调度学习速率。您可以从中查看Keras回调文档。以下是一个例子:

    from keras.callbacks import LearningRateScheduler
    
    LearningRateScheduler(schedule)函数接受一个称为scheduling函数的输入

    您可以定义一个函数来计划学习速率衰减。此函数将以历元作为输入参数。阶跃衰减示例:

     def step_decay(epoch):
         initial_lrate = 0.00125
         drop = 0.5
         epochs_drop = 10.0
         lrate = initial_lrate * math.pow(drop,  
                 math.floor((1+epoch)/epochs_drop))
         return lrate
    
    现在使用此函数创建学习速率计划程序

    lrScheduler=LearningRateScheduler(步骤)

    在model.compile中,将此计划程序传递给回调参数

    model.compile(...,callbacks=lrScheduler,...)
    
    类似地,对于每个历元或每个迭代的指数衰减,创建一个函数,并在学习率计划程序中调用该函数


    我希望这个解释能对你有所帮助。

    你的建议似乎是个好主意。但是我签入LearningRateScheduler(schedule),schedule函数只接受一个历元索引作为输入。问题是如何将损失信息纳入LearningRateSchedule的框架。您可以在类中创建字段来存储数据集和模型,并在每次迭代中使用它们计算适当的统计数据。