Your code works for me:
>>> centroids = [Variable(torch.rand(46,46)) for i in range(10)]
>>> net = CentroidDistances(centroids)
>>> x = Variable(torch.rand(128,46,46), requires_grad=True)
>>> y = net(x)
>>> z = y.sum()
>>> z.backward()
>>> x.grad
Variable containing:
( 0 ,.,.) = 
  1.2523e+00  1.1620e+00 -3.7774e+00  ...   4.9212e-01 -7.9828e+00 -6.6876e+00
  ...

Which version of Pytorch are you using ?