Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the gradients in keras?

Tags:

python

keras

I am attempting to debug a keras model that I have built. It seems that my gradients are exploding, or there is a division by 0 or some such. It would be convenient to be able to inspect the various gradients as they back-propagate through the network. Something like the following would be ideal:

model.evaluate(np.array([[1,2]]), np.array([[1]])) #gives the loss
model.evaluate_gradient(np.array([[1,2]]), np.array([[1]]), layer=2) #gives the doutput/dloss at layer 2 for the given input
model.evaluate_weight_gradient(np.array([[1,2]]), np.array([[1]]), layer=2) #gives the dweight/dloss at layer 2 for the given input
like image 389
Him Avatar asked Jul 02 '18 17:07

Him


People also ask

How do you find the gradient in TensorFlow?

If you want to access the gradients that are computed for the optimizer, you can call optimizer. compute_gradients() and optimizer. apply_gradients() manually, instead of calling optimizer.

What is gradient in Keras?

GradientTape is a brand-new function in TensorFlow 2.0. And it can be used to write custom training loops (both for Keras models and models implemented in “pure” TensorFlow)

How do you find the gradient in Python?

We will use numdifftools to find Gradient of a function. Examples: Input : x^4+x+1 Output :Gradient of x^4+x+1 at x=1 is 4.99 Input :(1-x)^2+(y-x^2)^2 Output :Gradient of (1-x^2)+(y-x^2)^2 at (1, 2) is [-4.

What is TF GradientTape ()?

TensorFlow provides the tf. GradientTape API for automatic differentiation; that is, computing the gradient of a computation with respect to some inputs, usually tf. Variable s. TensorFlow "records" relevant operations executed inside the context of a tf. GradientTape onto a "tape".


1 Answers

You need to create a symbolic Keras function, taking the input/output as inputs and returning the gradients. Here is a working example :

import numpy as np import keras from keras import backend as K  model = keras.Sequential() model.add(keras.layers.Dense(20, input_shape = (10, ))) model.add(keras.layers.Dense(5)) model.compile('adam', 'mse')  dummy_in = np.ones((4, 10)) dummy_out = np.ones((4, 5)) dummy_loss = model.train_on_batch(dummy_in, dummy_out)  def get_weight_grad(model, inputs, outputs):     """ Gets gradient of model for given inputs and outputs for all weights"""     grads = model.optimizer.get_gradients(model.total_loss, model.trainable_weights)     symb_inputs = (model._feed_inputs + model._feed_targets + model._feed_sample_weights)     f = K.function(symb_inputs, grads)     x, y, sample_weight = model._standardize_user_data(inputs, outputs)     output_grad = f(x + y + sample_weight)     return output_grad   def get_layer_output_grad(model, inputs, outputs, layer=-1):     """ Gets gradient a layer output for given inputs and outputs"""     grads = model.optimizer.get_gradients(model.total_loss, model.layers[layer].output)     symb_inputs = (model._feed_inputs + model._feed_targets + model._feed_sample_weights)     f = K.function(symb_inputs, grads)     x, y, sample_weight = model._standardize_user_data(inputs, outputs)     output_grad = f(x + y + sample_weight)     return output_grad   weight_grads = get_weight_grad(model, dummy_in, dummy_out) output_grad = get_layer_output_grad(model, dummy_in, dummy_out) 

The first function I wrote returns all the gradients in the model but it wouldn't be difficult to extend it so it supports layer indexing. However, it's probably dangerous because any layer without weights in the model will be ignored by this indexing and you would end up with different layer indexing in the model and the gradients.
The second function I wrote returns the gradient at a given layer's output and there, the indexing is the same as in the model, so it's safe to use it.

Note : This works with Keras 2.2.0, not under, as this release included a major refactoring of keras.engine

like image 195
mpariente Avatar answered Sep 21 '22 18:09

mpariente