Difference between functional.mse_loss and nn.MSELoss (stateless loss functions)

Is there any difference between calling

functional.mse_loss(input, target)


nn.MSELoss(input, target)?

Same question applies for l1_loss and any other stateless loss function.

1 Like

Hi Night!

Roughly speaking, there is no difference.

Note, as written, this won’t work. This calls the MSELoss constructor
with invalid arguments. You need:

loss_value = torch.nn.MSELoss() (input, target)
# or
loss_function = torch.nn.MSELoss()
loss_value = loss_function (input, target)

That is, you have to construct an MSELoss object first, and then call
(apply) it.

MSELoss objects (and similar loss-function objects) are “stateless” in
the sense that they don’t remember anything from one application
(loss_function (input, target)) to the next. But they do have
state, in that they are constructed with certain “control” parameters
set (for example, the reduction property of MSELoss and other
loss-function objects) that are stored internally, and used repeatedly
from one application to the next. (These properties can be changed
after construction, but I don’t think that that’s common practice.)


K. Frank


Thanks a lot for your answer!