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

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

概述我想在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, de 我想在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 = decaydef 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.updatesdef 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()))
解决方法 我认为你可以使用以下架构获得行为:

>使用this创建一个新的学习速率控制器类.>使其成为接受训练集并在提供适合方法时开始学习率的构造函数.>让它计算每个纪元后的损失并更新学习率.

总结

以上是内存溢出为你收集整理的如何在Keras或Theano中实现具有指数衰减学习率的卷积神经网络全部内容,希望文章能够帮你解决如何在Keras或Theano中实现具有指数衰减学习率的卷积神经网络所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1197741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存