0

I am working on a project where I am getting some funny error with the automatic differentiator in Pytorch.

I am trying to minimize a function with respect to x values. To do so, I use the code at the bottom of this post. As I understand it, I should be able to make an initial guess, set the requires_grad flag to true and run the forward pass (scores = alpha(Xsamples, model, robustness)) and then get the gradients with scores.backward() and then update my initial guess accordingly with optimizer.step. However, when I try running this I get the following error 'RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn' which I don't understand because I have set my initial guess to requires gradient. I tried looking on forums for help but most of the answers were regarding training neural networks so their fixes did not work in this case. Any guidance on this would be greatly appreciated, thank you.

epoch = 100
learning_rate = 0.01
N = 1
Xsamples = torch.randn(1,2)
Xsamples.requires_grad = True
optimizer = torch.optim.SGD([Xsamples], lr = learning_rate)
for i in range(epoch):
    scores = alpha(Xsamples, model, robustness)
    scores.backward() #dscore/ dx
    optimizer.step()
    optimizer.zero_grad()
return Xsamples 
  • What are `alpha`, `models`, and `robustness`? – jodag Mar 15 '21 at 21:00
  • @jodag alpha is basically like a loss function. It takes in an a point Xsamples, and a gpytorch model as well as a flag called robustness and returns a single element tensor value. – Neel Shah Mar 15 '21 at 21:34
  • can u provide the content/logic of the model and alpha function? and also show how you instanciate your model and optimizer ? It seems like there's some 'discontinuity' in your computation graph – Alka Mar 16 '21 at 11:57
  • @Alka The model is a Gaussian process model which follows the steps outlined here: https://docs.gpytorch.ai/en/latest/examples/01_Exact_GPs/Simple_GP_Regression.html The optimizer is instantiated like this: optimizer = torch.optim.SGD([Xsamples], lr = 0.01) alpha has a sample output like this: tensor([21.8907]) – Neel Shah Mar 16 '21 at 16:06
  • It's little bit difficult to catch the error without knowing what is happening exactly inside your alpha function. Nevertheless if you can't share this function for whatever reason, one way of debugging is to print the value of `output.requires_grad` at every line that changes an input to an output inside your alpha function. Maybe you can get which line broke your computation graph – Alka Mar 17 '21 at 08:50
  • @Alka If I print scores.requires_grad, its returns false. So does this mean my Xsamples.requires_grad = True is being lost in my alpha function? – Neel Shah Mar 17 '21 at 15:33
  • Yes it probably is. That's why I suggested you print requires_grad for all intermediate results inside your alpha function in order to see exactly which operation breaks the computation graph – Alka Mar 17 '21 at 17:28
  • Oh I see! Ok I printed requires_grad for the intermediate results and it seems to have a disconnect here: f_preds = model(Xsamples + noise, sample[i][:] += f_preds.sample(). where model here is a GPytorch model. When I print sample.requires_grad I get false. Do you know how to communicate the requires_grad command through the model? Is that the problem? – Neel Shah Mar 17 '21 at 21:28
  • Difficult to know beforehand how to fix it without digging into code. If you can post your code here I can try to check it. Or if your code is stored on github you can post the link – Alka Mar 18 '21 at 11:31
  • @Alka Would it be possible for me to email you my code? – Neel Shah Mar 18 '21 at 21:24
  • Sure. You can To alkasalissou[at]hotmail.com – Alka Mar 18 '21 at 22:49
  • @Alka Ok I sent it, thanks so much for your help! – Neel Shah Mar 19 '21 at 16:22

0 Answers0