Skip to content
Advertisement

can anyone explain what “out = self(images)” do in below code

I am not able to understand, if prediction is calculated in forward method, then why there is need “out = self(images)” and what it will do. I am bit confuse about this code.

JavaScript

model = MnistModel()

Advertisement

Answer

In Python, self refers to the instance that you have created from a class (similar to this in Java and C++). An instance is callable, which means it may be called like a function itself, if method __call__ have been overridden.

Example:

JavaScript

In your case, __call__ method is implemented in super class nn.Module. As it is a neural network module it needs an input placeholder. “out” is the placeholder for the data that is going to be forward the output of the module to the next layer or module of your model.

In the case of nn.Module class instances (and those that inherit from the class) the forward method is what is used as the __call__ method. At least where it is defined with respect to the nn.Module class.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement