【问题标题】:How Adagrad works in Keras? What does self.weights mean in Keras Optimizer?Adagrad 如何在 Keras 中工作? Keras Optimizer 中的 self.weights 是什么意思?
【发布时间】:2017-06-06 20:56:00
【问题描述】:

例如,Keras 的 Adagrad 的实现是:

class Adagrad(Optimizer):
"""Adagrad optimizer.
It is recommended to leave the parameters of this optimizer
at their default values.
# Arguments
    lr: float >= 0. Learning rate.
    epsilon: float >= 0.
    decay: float >= 0. Learning rate decay over each update.
# References
    - [Adaptive Subgradient Methods for Online Learning and Stochastic Optimization](http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf)
"""

def __init__(self, lr=0.01, epsilon=1e-8, decay=0., **kwargs):
    super(Adagrad, self).__init__(**kwargs)
    self.lr = K.variable(lr)
    self.epsilon = epsilon
    self.decay = K.variable(decay)
    self.initial_decay = decay
    self.iterations = K.variable(0.)

def get_updates(self, params, constraints, loss):
    grads = self.get_gradients(loss, params)
    shapes = [K.get_variable_shape(p) for p in params]
    accumulators = [K.zeros(shape) for shape in shapes]
    self.weights = accumulators
    self.updates = []

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

    for p, g, a in zip(params, grads, accumulators):
        new_a = a + K.square(g)  # update accumulator
        self.updates.append(K.update(a, new_a))
        new_p = p - lr * g / (K.sqrt(new_a) + self.epsilon)
        # 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

函数“get_update()”似乎是一步更新。但是,累加器应该存储历史信息吗?为什么它在每一步都被初始化为零?怎么能在整个训练过程中成为累加器?

这条线有什么作用?

self.weights = accumulators

似乎 self.weights 再也没有被调用过。

【问题讨论】:

    标签: python machine-learning tensorflow theano keras


    【解决方案1】:

    你是对的.. 对于 Keras 中的所有优化器,get_updates() 实现了张量逻辑以进行一步更新。对于_make_train_function() here 中的每个model.fit() 调用一次此函数,用于通过将更新规则传递为update= here 来创建张量函数。该更新规则用于迭代更新模型参数和其他参数。

    self.weights 优化器类是它的内部参数。这不用于训练。它只是用于保持优化器的状态(指向参数/累加器张量的指针列表),当调用 model.save 时,它们也会通过调用 get_weights() here 来保存,并在调用 model.load 时加载回来set_weights()here

    【讨论】:

    • 我明白了。非常感谢。
    猜你喜欢
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2020-01-21
    • 2020-03-04
    相关资源
    最近更新 更多