1

I use network InceptionV3, I want to compute the gradients of the model output w.r.t. the input layer. I have the following code:

origin_image = load_img("dog.jpg", target_size=(299, 299))
origin_image = img_to_array(origin_image)
origin_image = (origin_image - 127.5) / 127.5
origin_image = np.expand_dims(origin_image, axis=0)

model = tensorflow.keras.applications.inception_v3.InceptionV3()

with tf.GradientTape() as gtape:
   output = model(origin_image)
   y_pred = output[0, 346]

gradient = gtape.gradient(y_pred, model.get_layer("input_1").trainable_variables)
print(gradient) # return []

but this code is running:

from keras import backend as K

model_input_layer = model.layers[0].input
model_output_layer = model.layers[-1].output

cost_function = model_output_layer[0, 346]
gradient_function = K.gradients(cost_function, model_input_layer)
grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()],
                                                [cost_function, gradient_function])

cost, gradients = grab_cost_and_gradients_from_model([origin_image, 0])

How can tf.GradientTape be used to calculate the gradient of the output with respect to the input

1 Answers1

0

You can perform this in TF2 by the solution offered here: How to compute gradient of output wrt input in Tensorflow 2.0

input = tf.Variable(np.random.normal(size=(25, 120)), dtype=tf.float32) 

with tf.GradientTape() as tape:
   preds = model(inp) 

grads = tape.gradient(preds, input)

Do not forget to change the input shape in size=(25, 120) depending on the number of input columns you have. The model(input) and model.predict(input) do not do the same, predict works with numpy arrays, while model(input) does a symbolic computation that tensorflow can differentiate.

But looking into your code, it looks like you are trying to do this on a specific layer. By that, you can refer to this link: how to compute the gradients of a network output wrt a specific layer? This may help.

TF_Support
  • 1,794
  • 1
  • 7
  • 16