Solved by ptrblck in post #2 
              
              
                The problem is with your shapes. 
Currently you are just using tensors of the shape [batch_size] for the output and target. 
This works fine in your manual approach, as you are just defining the weight matrix as torch.randn(dim_in) (the output dimension is missing). 
However, if you use the nn.Linea…