UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([1, 1]))
I have this code:
    actual_loes_score_g = actual_loes_score_t.to(self.device, non_blocking=True)

    predicted_loes_score_g = self.model(input_g)

    loss_func = nn.L1Loss()
    loss_g = loss_func(
        predicted_loes_score_g,
        actual_loes_score_g,
    )

where predicted_loes_score_g is tensor([[-24.9374]], grad_fn=&lt;AddmmBackward0&gt;) and actual_loes_score_g is tensor([20.], dtype=torch.float64).  (I am using a batch size of 1 for debugging purposes.)
I am getting this warning:
torch/nn/modules/loss.py:96: UserWarning: Using a target size (torch.Size([1])) that is 
different to the input size (torch.Size([1, 1])). This will likely lead to incorrect 
results due to broadcasting. Please ensure they have the same size.

How do I correctly ensure they have the same size?
I thought this might be the answer:
    predicted_loes_score = predicted_loes_score_g.detach()[0]
    loss_g = loss_func(
        predicted_loes_score,
        actual_loes_score_g,
    )

but then I get this error later:
torch/autograd/__init__.py&quot;, line 154, in backward
    Variable._execution_engine.run_backward(
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn


predicted_loes_score_g = tensor([[-24.9374]], grad_fn=&lt;AddmmBackward0&gt;)

which is size [1,1]
actual_loes_score_g = tensor([20.], dtype=torch.float64)

which is size [1]
You need to either remove a dimension from your prediction or add a dimension to your target. I would recommend the latter because that extra dimension corresponds to your batch size. Try this:
actual_loes_score_g =  actual_loes_score_g.unsqueeze(1)

