Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix compatibility issues with newer keras version #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions keras/weightnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# adapted from keras.optimizers.SGD
class SGDWithWeightnorm(SGD):
def get_updates(self, params, constraints, loss):
def get_updates(self, params, loss):
grads = self.get_gradients(loss, params)
self.updates = []

Expand Down Expand Up @@ -47,9 +47,8 @@ def get_updates(self, params, constraints, loss):
new_V_param = V + v_v

# if there are constraints we apply them to V, not W
if p in constraints:
c = constraints[p]
new_V_param = c(new_V_param)
if getattr(p, 'constraint', None) is not None:
new_V_param = p.constraint(new_V_param)

# wn param updates --> W updates
add_weightnorm_param_updates(self.updates, new_V_param, new_g_param, p, V_scaler)
Expand All @@ -64,24 +63,23 @@ def get_updates(self, params, constraints, loss):
new_p = p + v

# apply constraints
if p in constraints:
c = constraints[p]
new_p = c(new_p)
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

# adapted from keras.optimizers.Adam
class AdamWithWeightnorm(Adam):
def get_updates(self, params, constraints, loss):
def get_updates(self, params, loss):
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 * self.iterations))

t = self.iterations + 1
t = tf.to_float(self.iterations + 1)
lr_t = lr * K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))

shapes = [K.get_variable_shape(p) for p in params]
Expand Down Expand Up @@ -119,9 +117,8 @@ def get_updates(self, params, constraints, loss):
self.updates.append(K.update(v, v_t))

# if there are constraints we apply them to V, not W
if p in constraints:
c = constraints[p]
new_V_param = c(new_V_param)
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)

# wn param updates --> W updates
add_weightnorm_param_updates(self.updates, new_V_param, new_g_param, p, V_scaler)
Expand All @@ -136,9 +133,8 @@ def get_updates(self, params, constraints, loss):

new_p = p_t
# apply constraints
if p in constraints:
c = constraints[p]
new_p = c(new_p)
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

Expand Down