Let’s say I have a function Psi with a 4-dimensional vector output, that takes a 3-dimensional vector u as input. I would like to compute the gradient of the first three components of Psi w.r.t. the respective three components of u:
import torch
u = torch.tensor([1.,2.,3.], requires_grad=True)
psi = torch.zeros(4)
psi[0] = 2*u[0]
psi[1] = 2*u[1]
psi[2] = 2*u[2]
psi[3] = torch.dot(u,u)
grad_Psi_0 = torch.autograd.grad(psi[0], u[0])
grad_Psi_1 = torch.autograd.grad(psi[1], u[1])
grad_Psi_2 = torch.autograd.grad(psi[2], u[2])
And I get the error that u[0],u[1], and u[2] are not used in the graph:
---> 19 grad_Psi_0 = torch.autograd.grad(psi[0], u[0])
20 grad_Psi_1 = torch.autograd.grad(psi[1], u[1])
21 grad_Psi_2 = torch.autograd.grad(psi[2], u[2])
File ~/.local/lib/python3.10/site-packages/torch/autograd/__init__.py:275, in grad(outputs, inputs, grad_outputs, retain_graph, create_graph, only_inputs, allow_unused, is_grads_batched)
273 return _vmap_internals._vmap(vjp, 0, 0, allow_none_pass_through=True)(grad_outputs)
274 else:
--> 275 return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
276 outputs, grad_outputs_, retain_graph, create_graph, inputs,
277 allow_unused, accumulate_grad=False)
RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.
Why is this the case? I am using u[i] to build psi…