Keras (link)에 대한 탄력적 인 backpropagation 최적화 프로그램을 구현하려고했지만 도전적인 부분은 해당하는 그래디언트가 양수인지 여부에 따라 개별 매개 변수를 업데이트 할 수 있었지만, 음 또는 0입니다. 아래 코드를 Rprop 최적화 프로그램을 구현하기위한 시작으로 작성했습니다. 그러나 매개 변수에 개별적으로 액세스하는 방법을 찾지 못하는 것 같습니다. params
(아래 코드와 같이) 이상의 루핑은 모든 행렬 인 각 반복마다 p, g, g_old, s, wChangeOld
을 반환합니다.Keras - Rprop 알고리즘을 구현할 때 발생하는 문제
개별 매개 변수를 반복하고 업데이트 할 수있는 방법이 있습니까? 그래디언트의 부호에 따라 매개 변수 벡터를 인덱싱 할 수도 있습니다. 감사!
class Rprop(Optimizer):
def __init__(self, init_step=0.01, **kwargs):
super(Rprop, self).__init__(**kwargs)
self.init_step = K.variable(init_step, name='init_step')
self.iterations = K.variable(0., name='iterations')
self.posStep = 1.2
self.negStep = 0.5
self.minStep = 1e-6
self.maxStep = 50.
def get_updates(self, params, constraints, loss):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
shapes = [K.get_variable_shape(p) for p in params]
stepList = [K.ones(shape)*self.init_step for shape in shapes]
wChangeOldList = [K.zeros(shape) for shape in shapes]
grads_old = [K.zeros(shape) for shape in shapes]
self.weights = stepList + grads_old + wChangeOldList
self.updates = []
for p, g, g_old, s, wChangeOld in zip(params, grads, grads_old,
stepList, wChangeOldList):
change = K.sign(g * g_old)
if change > 0:
s_new = K.minimum(s * self.posStep, self.maxStep)
wChange = s_new * K.sign(g)
g_new = g
elif change < 0:
s_new = K.maximum(s * self.posStep, self.maxStep)
wChange = - wChangeOld
g_new = 0
else:
s_new = s
wChange = s_new * K.sign(g)
g_new = p
self.updates.append(K.update(g_old, g_new))
self.updates.append(K.update(wChangeOld, wChange))
self.updates.append(K.update(s, s_new))
new_p = p - wChange
# 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 = {'init_step': float(K.get_value(self.init_step))}
base_config = super(Rprop, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
은 (0) ... K.equal (변경) 여기가 아닌 경우/ELIF/다른 사람을 K.switch 필요하지 않습니다? – gkcn