Hi, I have some issues about autograd. I try to implement a network whose nodes in the hidden layer takes diverse noisy inputs. However, it hindered when computing the gradients w.r.t all the parameters.
Few things: You should not use Variable anymore. All the Tensors can require gradients now.
And if the output of a function does not have requires_grad=True it is because no input were requiring gradients. So you should make sure that the inputs you want the gradients for are set properly.
Also you should not use .data as it has many side effects that you most likely don’t want (and will lead to silently wrong gradients in some cases).
If you want to set the weights of your net you can do:
Really appreciate your help. I have removed Variable().
I kind of initial nodes in the hidden layer accordingly, but when I compute the gradients w.r.t net.parameters(), all the gradients are None. However, if w.r.t [w1,w1_bias,w2,w2_bias], it works properly.
Is there any way to solve it?
As is clear with the torch.no_grad(). setting the value in the net is not a differentiable operation and w1 is not the same as net.body.weight. (you previous code was breaking this as well but in a more subtle manner by using .data).
I am afraid you cannot assign these in a differentiable manner. You can set them before and use these to compute the output though if you want the gradient wrt to these paramters to be computed.