Last active
May 13, 2019 14:21
-
-
Save kashif/3eddc3c90e23d84975451f43f6e917da to your computer and use it in GitHub Desktop.
Keras implementation of AMSGrad optimizer from "On the Convergence of Adam and Beyond" paper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AMSgrad(Optimizer): | |
"""AMSGrad optimizer. | |
Default parameters follow those provided in the Adam paper. | |
# Arguments | |
lr: float >= 0. Learning rate. | |
beta_1: float, 0 < beta < 1. Generally close to 1. | |
beta_2: float, 0 < beta < 1. Generally close to 1. | |
epsilon: float >= 0. Fuzz factor. | |
decay: float >= 0. Learning rate decay over each update. | |
# References | |
- [On the Convergence of Adam and Beyond](https://openreview.net/forum?id=ryQu7f-RZ) | |
- [Adam - A Method for Stochastic Optimization](http://arxiv.org/abs/1412.6980v8) | |
""" | |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, | |
epsilon=1e-8, decay=0., **kwargs): | |
super(AMSgrad, self).__init__(**kwargs) | |
with K.name_scope(self.__class__.__name__): | |
self.iterations = K.variable(0, dtype='int64', name='iterations') | |
self.lr = K.variable(lr, name='lr') | |
self.beta_1 = K.variable(beta_1, name='beta_1') | |
self.beta_2 = K.variable(beta_2, name='beta_2') | |
self.decay = K.variable(decay, name='decay') | |
self.epsilon = epsilon | |
self.initial_decay = decay | |
@interfaces.legacy_get_updates_support | |
def get_updates(self, loss, params): | |
grads = self.get_gradients(loss, params) | |
self.updates = [K.update_add(self.iterations, 1)] | |
lr = self.lr | |
if self.initial_decay > 0: | |
lr *= (1. / (1. + self.decay * K.cast(self.iterations, | |
K.dtype(self.decay)))) | |
t = K.cast(self.iterations, K.floatx()) + 1 | |
lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) / | |
(1. - K.pow(self.beta_1, t))) | |
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] | |
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] | |
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] | |
self.weights = [self.iterations] + ms + vs + vhats | |
for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): | |
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g | |
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) | |
vhat_t = K.maximum(vhat, v_t) | |
p_t = p - lr_t * m_t / (K.sqrt(vhat_t) + self.epsilon) | |
self.updates.append(K.update(m, m_t)) | |
self.updates.append(K.update(v, v_t)) | |
self.updates.append(K.update(vhat, vhat_t)) | |
new_p = p_t | |
# Apply constraints. | |
if getattr(p, 'constraint', None) is not None: | |
new_p = p.constraint(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)), | |
'beta_1': float(K.get_value(self.beta_1)), | |
'beta_2': float(K.get_value(self.beta_2)), | |
'decay': float(K.get_value(self.decay)), | |
'epsilon': self.epsilon} | |
base_config = super(AMSgrad, self).get_config() | |
return dict(list(base_config.items()) + list(config.items())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. How does this compare (implementation-wise and performance-wise) to AMSGrad built-in in Keras optimizer class?
https://github.com/keras-team/keras/blob/083a41cc6be7b1796e3817df198d1557bb8557b8/keras/optimizers.py#L419
Should we use this or start using the version in Keras core? Thank you.
Edit: Ignore the above. I have just noticed that you have submitted the gist as a PR to Keras team: keras-team/keras#8693