Seems like you might need to use .gather rather than the [] operator? you can check with code like:
import torch
from torch import nn
from torch.autograd import Variable


batch_size = 4
out_reg1 = Variable(torch.rand(batch_size, 3), requires_grad=True)
valid_index = torch.LongTensor([1, 0, 2, 0])
print('out_reg1', out_reg1)
out_reg2 = out_reg1.gather(1, Variable(valid_index.view(-1, 1)))
print('out_reg2', out_reg2)
mse_loss = nn.MSELoss()
loss_reg = mse_loss(out_reg2, Variable(torch.rand(batch_size, 1)))
loss_reg.backward()
print('out_reg1.grad', out_reg1.grad)

Result:
out_reg1 Variable containing:
 0.7507  0.2493  0.4223
 0.6270  0.9968  0.5263
 0.9014  0.5669  0.3850
 0.3649  0.1115  0.2633
[torch.FloatTensor of size 4x3]

out_reg2 Variable containing:
 0.2493
 0.6270
 0.3850
 0.3649
[torch.FloatTensor of size 4x1]

out_reg1.grad Variable containing:
 0.0000 -0.3239  0.0000
-0.0951  0.0000  0.0000
 0.0000  0.0000  0.1379
 0.0450  0.0000  0.0000
[torch.FloatTensor of size 4x3]