Giter Club home page Giter Club logo

Comments (3)

KimiHsieh avatar KimiHsieh commented on May 30, 2024

I have the same issue

from adahessian.

KimiHsieh avatar KimiHsieh commented on May 30, 2024

Environment: adahessian_tf/environment.yml

I think the issue is caused by grads = gradients.gradients(loss, params) in get_gradients_hessian(self, loss, params)
if you check the return of grads = gradients.gradients(loss, params), it will be None.
But I don't know how to fix this issue.

@tf.function
def step(x, y, training):
with tf.GradientTape() as tape:
r_loss = tf.add_n(model.losses)
outs = model(x, training)
c_loss = loss_fn(y, outs)
loss = c_loss + r_loss
if training:
if optim_method != 'adahessian':
gradients = tape.gradient(loss, model.trainable_weights)
optimizer.apply_gradients(zip(gradients, model.trainable_weights))
else:
gradients, Hessian = optimizer.get_gradients_hessian(loss, model.trainable_weights)
optimizer.apply_gradients_hessian(zip(gradients, Hessian, model.trainable_weights))

from adahessian.

KimiHsieh avatar KimiHsieh commented on May 30, 2024

Environment: adahessian_tf/environment.yml

I think the issue is caused by grads = gradients.gradients(loss, params) in get_gradients_hessian(self, loss, params)
if you check the return of grads = gradients.gradients(loss, params), it will be None.
But I don't know how to fix this issue.

@tf.function
def step(x, y, training):
with tf.GradientTape() as tape:
r_loss = tf.add_n(model.losses)
outs = model(x, training)
c_loss = loss_fn(y, outs)
loss = c_loss + r_loss
if training:
if optim_method != 'adahessian':
gradients = tape.gradient(loss, model.trainable_weights)
optimizer.apply_gradients(zip(gradients, model.trainable_weights))
else:
gradients, Hessian = optimizer.get_gradients_hessian(loss, model.trainable_weights)
optimizer.apply_gradients_hessian(zip(gradients, Hessian, model.trainable_weights))

def get_gradients_hessian(self, loss, params):
"""Returns gradients and Hessian of `loss` with respect to `params`.
Arguments:
loss: Loss tensor.
params: List of variables.
Returns:
List of gradient and Hessian tensors.
Raises:
ValueError: In case any gradient cannot be computed (e.g. if gradient
function not implemented).
"""
params = nest.flatten(params)
with backend.get_graph().as_default(), backend.name_scope(self._name +
"/gradients"):
grads = gradients.gradients(loss, params)
for grad, param in zip(grads, params):
if grad is None:
raise ValueError("Variable {} has `None` for gradient. "
"Please make sure that all of your ops have a "
"gradient defined (i.e. are differentiable). "
"Common ops without gradient: "
"K.argmax, K.round, K.eval.".format(param))
# WARNING: for now we do not support gradient clip
# grads = self._clip_gradients(grads)
v = [np.random.uniform(0, 1, size = p.shape) for p in params]
for vi in v:
vi[ vi < 0.5] = -1
vi[ vi >= 0.5] = 1
v = [tf.convert_to_tensor(vi, dtype = tf.dtypes.float32) for vi in v]
vprod = tf.reduce_sum([ tf.reduce_sum(vi * grad) for vi, grad in zip(v, grads)])
Hv = gradients.gradients(vprod, params)
Hd = [ tf.abs(Hvi * vi) for Hvi, vi in zip(Hv, v)]
return grads, Hd

from adahessian.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.