Solved by ptrblck in post #2 
              
              
                Variables are deprecated since PyTorch 0.4 so you should use tensors in newer versions. 
That being said, I would just slice y directly and pass it to the loss function: 
y = l1(x)
first_two = y[:, :2]
loss = dummy_loss(first_two)
loss.backward()