Getting gradient of model output w.r.t weights using Keras Getting gradient of model output w.r.t weights using Keras python python

Getting gradient of model output w.r.t weights using Keras


To get the gradients of model output with respect to weights using Keras you have to use the Keras backend module. I created this simple example to illustrate exactly what to do:

from keras.models import Sequentialfrom keras.layers import Dense, Activationfrom keras import backend as kmodel = Sequential()model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))model.add(Dense(8, init='uniform', activation='relu'))model.add(Dense(1, init='uniform', activation='sigmoid'))model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

To calculate the gradients we first need to find the output tensor. For the output of the model (what my initial question asked) we simply call model.output. We can also find the gradients of outputs for other layers by calling model.layers[index].output

outputTensor = model.output #Or model.layers[index].output

Then we need to choose the variables that are in respect to the gradient.

  listOfVariableTensors = model.trainable_weights  #or variableTensors = model.trainable_weights[0]

We can now calculate the gradients. It is as easy as the following:

gradients = k.gradients(outputTensor, listOfVariableTensors)

To actually run the gradients given an input, we need to use a bit of Tensorflow.

trainingExample = np.random.random((1,8))sess = tf.InteractiveSession()sess.run(tf.initialize_all_variables())evaluated_gradients = sess.run(gradients,feed_dict={model.input:trainingExample})

And thats it!


The below answer is with the cross entropy function, feel free to change it your function.

outputTensor = model.outputlistOfVariableTensors = model.trainable_weightsbce = keras.losses.BinaryCrossentropy()loss = bce(outputTensor, labels)gradients = k.gradients(loss, listOfVariableTensors)sess = tf.InteractiveSession()sess.run(tf.global_variables_initializer())evaluated_gradients = sess.run(gradients,feed_dict={model.input:training_data1})print(evaluated_gradients)