Marco Birck
Marco Birck

Reputation: 21

Multi-input gradients calculation on keras with K.function

This model.input is a four head placeholder with size (3, 227, 227)

input_imgs = model.input
loss = K.max(layer_dict[layer_name].output[p[0]])

compute the gradient of the input picture wrt this loss

grads = K.gradients(loss, input_imgs)[0]

this function returns the loss and grads given the input picture

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

Answers (1)

indraforyou
indraforyou

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

Related Questions