I am getting “there are no graph nodes that require computing gradients”. Is it because this operation is not differentiable?
def distance(input, means):
df = 1
dist = torch.zeros(self.means.size(0), input.size(0))
for i in range(self.means.size()[0]):
dist[i] = torch.pow(1 + torch.sum(torch.pow(input - self.means[i], 2), 1) / df, -(1 + df)/2).data
return dist.t()
If not, can you please show me what is thecorrect way to make this operation differentiable?
richard
2
How did you get “there are no graph nodes that require computing gradients”?
But anyways, I tweaked the code a little and you can call backwards on it:
import torch
from torch.autograd import Variable
def distance(input, means):
df = 1
dist = Variable(torch.zeros(means.size(0), input.size(0)))
for i in range(means.size()[0]):
dist[i] = torch.pow(1 + torch.sum(torch.pow(input - means[i], 2), 1) / df, -(1 + df)/2)
return dist.t()
x = Variable(torch.randn(2, 2), requires_grad=True)
means = Variable(torch.randn(2), requires_grad=False)
out = distance(input, means).sum()
out.backward()
x.grad # prints something
You have to wrap your tensors in Variables so that autograd can keep track of them to compute backwards.
2 Likes
That worked, Thank you !!!