y is now a function of Variable W. Hence a simple y.backward should be able to compute the gradients of y with respect to W for you. You might want to perform net.zero_grad() before performing y.backward() so as to clear the previous gradients if any.
In your case: the code can be written in fewer lines:
x = Variable(torch.from_numpy(np.array(range(1, 6)).reshape((1, 5))).type(torch.FloatTensor))
net.zero_grad()
y = net(x)
y.backward()
Now please check the gradients.