Solved by ptrblck in post #2 
              
              
                I would stick with .clone, since .detach still uses the same underlying data. 
Your inplace unsqueeze_ will therefore also unsqueeze targets: 
x = torch.ones(10)
y = x.detach()
y.unsqueeze_(0)
print(x.shape)
> torch.Size([1, 10])
print(y.shape)
> torch.Size([1, 10])

May I ask, why you need the BCEL…