Reputation: 21
input_imgs = model.input
loss = K.max(layer_dict[layer_name].output[p[0]])
grads = K.gradients(loss, input_imgs)[0]
iterate = K.function(input_imgs, [grads])
I want to compute gradients for a siamese NN with fout head inputs, but when I apply the above K function I get a tensor of gradient with shape (1, 3, 227, 227)
, when what I want is (4, 3, 227, 227)
.
What can I do to get the correct gradients?
Upvotes: 2
Views: 1281
Reputation: 9099
I see you are trying to generate saliency maps.
Check whats the shape of input_imgs tensor. Thats what determines the shape your gradient. My guess will be input_imgs
is a list of tensors and in the grads = K.gradients(loss, input_imgs)[0]
since you are indexing to 0
you are dropping all other gradients in the list. So probably this will fix your issue:
grads = K.gradients(loss, input_imgs)
iterate = K.function(input_imgs, grads)
Now when you use iterate
it will give you a list of 4 each of shape (1, 3, 227, 227)
Upvotes: 0