Extra parenthesis at loss function

can anyone explain what is the difference between these two pieces of code:

b = nn.MSELoss(output_x, x_labels)
a = nn.CrossEntropyLoss(output_y, y_labels)
loss = a + b
b = nn.MSELoss()(output_x, x_labels)
a = nn.CrossEntropyLoss()(output_y, y_labels)
loss = a + b

In the first case you are instantiating the class passing those parameters as arguments.
In the 2nd case you are instantiating the class, calling the instance with two inputs and assigning output to b and a.

Does the 1st case even run?

No the first one is not working. I’m a little bit confused with the “constructor” which will be used by init and the “forward” function in the pytorch classes. whenever we instantiate a class the constructor will run automatically. and when we call an instance of a class the “forward” function will run automatically. am I right ?

It’s a pythonic thing.

A class is a scheme of an object. When you instantiate a class, you are creating an object (an instance) which has specific properties. Constructor is a function called automatically during that process in which you can set up some variables, paramters etcetera…

Once you have that object, which has been defined in the init method, you can call it. By calling, I mean to use it as a function itself.

You could rewrite second funtion as

mse_loss_function = nn.MSELoss()
b=  mse_loss_function(output_x, x_labels)
a= cross_entropy_loss_function(output_y, y_labels)
loss = a + b

This does exactly the same than you snippet but following the logical way of coding. You instantiate the class “MSE” and you use the object called mse_loss_function as a function.

But yeh, you are right, init is the constructor which instantiates and when you call the instance it runs forward