The following code runs ok for me:
import torch
from torch import Tensor
from torch.autograd import Variable
from torch.nn import functional as F

target = Variable(torch.LongTensor([2, 1]))

v_a = Variable(Tensor([[1,2,3], [3,4,5]]),requires_grad=True)
v_b = v_a * 2
loss = F.cross_entropy(v_b, target)
loss.backward()

print('v_a.grad.data', v_a.grad.data)

A few points:


Crit doesnt exist, as far as I can tell. replaced by cross_entropy


torch.nn contains functors, but you can find functional equivalents in torch.nn.functional, which I’ve used here
you need to provide everything in mini-batches, so:
– the input to cross_entropy should be two dimensional, (N, C), where N is size of minibatch, and C is numberof classes
– in practice, this meant I had to make your v_a two dimensional too, so it was a minibatch

target, for cross_entropy is a onedimensional LongTesnor, of class labels, one per minibatch example

everything should be Variables. Forget that Tensors exist