Learning - Why does .unsqueeze(-1) magically work?

There’s nothing magic about this. What you got is a warning and not an error. If you look at the documentation for MSELoss it says the target should have the same shape as the input which is what the unsqueeze operation ensures by adding an extra dimension. Hope this helps.