MangLion
MangLion

Reputation: 153

Why is K.gradients is returning none for gradient of loss wrt input

I am wondering why I am getting none for my grads in the following code:

import tensorflow.keras.losses as losses
loss = losses.squared_hinge(y_true, y_pred)

from tensorflow.keras import backend as K
grads = K.gradients(loss, CNN_model.input)[0]
iterate = K.function([CNN_model.input], [loss, grads])

my CNN_model.input is: <tf.Tensor 'conv2d_3_input:0' shape=(?, 28, 28, 1) dtype=float32>

my loss is: <tf.Tensor 'Mean_3:0' shape=(1,) dtype=float64>

Note: I am passing the predicted output of an SVM as y_pred for my application if that is of importance.

Upvotes: 2

Views: 1351

Answers (1)

Davide Giordano
Davide Giordano

Reputation: 68

As far as I understood from my previous experience, Tensorflow needs to use GradientTape in order to record the activity of a certain variable and so to compute its gradients. In your case should be something like that:

x = np.random.rand(10) #your input variable
x = tf.Variable(x) #to be evaluated by GradientTape the input should be a tensor
with tf.GradientTape() as tape:
    tape.watch(x) #with this method you can observe your variable
    proba = model(x) #get the prediction of the input
    loss = your_loss_function(y_true, proba) #compute the loss

gradient = tape.gradient(loss, x) #compute the gradients, this must be done outside the recording

Upvotes: 2

Related Questions