Search code examples
python-3.xpytorchpytorch-lightning

PyTorch Lightning: What's the meaning of calling self?


Consider the following function:

def training_step(self, batch, batch_idx):
    x, y = batch
    y_hat = self(x)
    loss = F.cross_entropy(y_hat, y)
    return loss

What's the meaning of calling self in this context?

How can self act as a callable anyway?

Thanks


Solution

  • Calling an object with object() is equivalent to calling the __call__ method of the given class, defined as usual with:

       def __call__(self):
    

    So self() is equivalent to obj.__call__ method of class object and since it subclasses torch.nn.Module, which will invoke _call_impl, and eventually the forward pass,