Code Monkey home page Code Monkey logo

autograd-hacks's People

Contributors

yaroslavvb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

autograd-hacks's Issues

UserWarning: Using a non-full backward hook when the forward contains multiple autograd Nodes is deprecated and will be removed in future versions. This hook will be missing some grad_input. Please use register_full_backward_hook to get the documented behavior.

Getting this warning:

UserWarning: Using a non-full backward hook when the forward contains multiple autograd Nodes is deprecated and will be removed in future versions. This hook will be missing some grad_input. Please use register_full_backward_hook to get the documented behavior.

Can compute_grad1 support multiple backprops?

Hi Yaroslav,
First of all, I want to say your tricks here are really neat and have helped me a lot!

I'm currently trying to compute the per-example gradient over two backprops in a typical Hessian vector product calculation, e.g.

output = model(data)
loss = loss_fn(output, targets)
grad = torch.autograd.grad(loss, model.parameters(), create_graph=True)
grad = flat_grad(grad)
grad_p = (grad * p).sum()
hessian_p= torch.autograd.grad(grad_p , actor.parameters())

Is it possible to apply compute_grad1 to this procedure to get the per-example Hessian vector product? Naively applying it fails because currently compute_grad1 only supports a single backprop.

Thanks!
Jack

ResNet support

Hi! My question is about computing gradients for each sample in the batch. I reproduced your example with simple neural net, which works exactly fine. However, when using ResNet50 (which has batchnorm, for example) the code produces the following error:
AttributeError: 'Parameter' object has no attribute 'grad1'

Imprecision?

This assertion failed at some point during training...

hack_grads = torch.cat([param.grad1.flatten(start_dim=1) for param in model.parameters()
                                       if hasattr(param, 'grad1')], dim=1)

grads = torch.cat([param.grad.flatten() for param in model.parameters() if hasattr(param, 'grad1')], dim=0)
                
assert torch.allclose(torch.mean(hack_grads, dim=0), grads)

Extend to support Conv1d and Conv3d

I suspect supporting Conv1d and Conv3d is straight forward, but einsum() probably needs to change? Does anyone know the correct values here for the computation string?

A One-Line Bug That Could be Easily Fixed

Hi,
First, thanks a lot for this library!

Recently I found a bug in this line of your code:

A = torch.nn.functional.unfold(A, layer.kernel_size)

The bug makes the code not compatible with non-default padding and stride in Conv2d.
Hence, this should be corrected to
A = torch.nn.functional.unfold(A, layer.kernel_size,padding=layer.padding,stride=layer.stride)

Best regards,
Haoxiang

How to run calculate the gradient per sample in a loop?

In a nutshell, my code looks like this:

autograd_hacks.add_hooks(model)
all_params = []
for i in range(20):
      epoch_params = []
      train_loss = training_function(model, data, lr)
      autograd_hacks.compute_grad1(model)
      for name, params in model.named_parameters():
            sample_grads = params.grad1.clone().cpu().detach().numpy()
            epoch_params.append(sample_grads)
      all_params.append(epoch_params)
      autograd_hacks.disable_hooks(model)

all_params should contain different values as the gradients are changing every epoch, but it outputs always the same array. I tried using remove_hooks and clear_backprop but either it gave me errors or it does nothing. The training function has the usual loss, step, etc. I'd imagine the solution to this is easy. If it is not, I can write a minimal reproducible example.

Replace module backward hook with tensor hook

Module backward hooks may get deprecated eventually, can replace them with tensor hook as follows.

def tensor_hook_adder(module, input, output):
    def tensor_backwards(grad):
        setattr(module, 'backprops', grad)
    output.register_hook(tensor_backwards)

layer.register_forward_hook(tensor_hook_adder)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.