I want to compute the gradient between two tensors in a net. The input X tensor is sent through a set of convolutional layers which give me back and output Y tensor.
I’m creating a new loss and I would like to know the MSE between gradient of norm(Y) w.r.t. each element of X. Here the code:
# Staring tensors
X = torch.rand(40, requires_grad=True)
Y = torch.rand(40, requires_grad=True)
# Define loss
loss_fn = nn.MSELoss()
#Make some calculations
V = Y*X+2
# Compute the norm
V_norm = V.norm()
# Computing gradient to calculate the loss
for i in range(len(V)):
if i == 0:
grad_tensor = torch.autograd.grad(outputs=V_norm, inputs=X[i])
else:
grad_tensor_ = torch.autograd.grad(outputs=V_norm, inputs=X[i])
grad_tensor = torch.cat((grad_tensor, grad_tensor_), dim=0)
# Grund truth
gt = grad_tensor * 0 + 1
#Loss
loss_g = loss_fn(grad_tensor, gt)
print(loss_g)
Unfortunately, I’ve been making tests with torch.autograd.grad(), but I could not figure out how to do it. I get teh following error: 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.
Setting allow_unused=True
gives me back None
which is not an option. Not sure how to compute the loss between the gradients and the norm. Any idea about how to code this loss?