0
votes

model is my trained Keras residual model. I'm trying to calculate the gradient of the loss w.r.t an input tensor, however:

tf.gradients(mse(model.predict(x), y), x[0])

(gradient of loss w.r.t an input tensor), gives me:

[None].

What does None here mean and how can I compute these gradients?

1
It means there is no gradient, to get gradients you need to fully use TensorFlow symbolic operations for it to work (so no model.predict). - Dr. Snoopy

1 Answers

0
votes

To compute the gradient you must use symbolic tensors and operations:

from keras import backend as K
from keras.losses import the_loss_function   # import the suitable loss function

y = Input(shape=labels_shape)

# this is the gradient of loss with respect to inputs given some input data
grads = K.gradients(the_loss_function(y, model.output), model.inputs)
func = K.function(model.inputs + [y, K.learning_phase()], grads)

# usage in test mode = 0
out = func([input_data_array, input_labels_array, 0])

# usage in train mode = 1
out = func([input_data_array, input_labels_array, 1])