Extra parenthesis at loss function

(Ali Mirzaeyan) #1

HI,
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
loss.backward()
b = nn.MSELoss()(output_x, x_labels)
a = nn.CrossEntropyLoss()(output_y, y_labels)
loss = a + b
loss.backward()
(Juan F Montesinos) #2

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?

(Ali Mirzaeyan) #3

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 ?

(Juan F Montesinos) #4

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()
cross_entropy_loss_function=nn.CrossEntropyLoss()
b=  mse_loss_function(output_x, x_labels)
a= cross_entropy_loss_function(output_y, y_labels)
loss = a + b
loss.backward()

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