Lasagne / Recipes

Lasagne recipes: examples, IPython notebooks, ...
MIT License
914 stars 418 forks source link

Problem with op.grad in OpFromGraph - Guided Backpropagation #119

Open HATEM-ELAZAB opened 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. So i tried the code explained in this link. 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. What is the diffrence between the "op.grad" and "grad_overrides"? Do they have the same effect on computation or there is a diffrence?
4. If this is an unresolved issue, what's the easiest way to override gradient for partial layers in a model?