keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.71k stars 19.43k forks source link

Guided Backpropagation in Theano/Lasagne #16153

Closed HATEM-ELAZAB closed 2 years ago

HATEM-ELAZAB commented 2 years ago

I have a model, for which i need to compute the gradients of output w.r.t the model's input. But I want to apply some custom gradients for some of the layers in my model, which can be tedious to build from scratch. So i tried the code explained in this link [https://github.com/Lasagne/Recipes/blob/master/examples/Saliency%20Maps%20and%20Guided%20Backpropagation.ipynb]. I added the following two classes:

relu = nn.nonlinearities.rectify relu_layers = [layer for layer in nn.layers.get_all_layers(net['l_out']) if getattr(layer, 'nonlinearity', None) is relu] modded_relu = GuidedBackprop(relu)

for layer in relu_layers: layer.nonlinearity = modded_relu

prop = nn.layers.get_output( net['l_out'], model_in, deterministic=True)

for sample in range(ini, batch_len):
model_out = prop[sample, 'z'] # get prop for label 'z' gradients = theano.gradient.jacobian(model_out, wrt=model_in)

gradients = theano.grad(model_out, wrt=model_in)

get_gradients = theano.function(inputs=[model_in],
                                    outputs=gradients)
grads = get_gradients(X_batch) # gradient dimension: X_batch == model_in(64, 20, 32) 
grads = np.array(grads)
grads = grads[sample]


Now when i run the code, it works without any error, and the shape of the output is also correct. But that's because it executes the default theano.grad function and not the one supposed to override it. In other words, the grad() function in the class GuidedBackprop never been invoked.

1. I can't understand what is the issue?
2. is there's a solution?
3. If this is an unresolved issue, what's the easiest way to override gradient for partial layers in a model?
tilakrayal commented 2 years ago

@HATEM-ELAZAB , This issue is not related to Keras repo.Please find the correct repo and post the issue to resolve the issue.Thanks!

google-ml-butler[bot] commented 2 years ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] commented 2 years ago

Closing as stale. Please reopen if you'd like to work on this further.

google-ml-butler[bot] commented 2 years ago

Are you satisfied with the resolution of your issue? Yes No