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
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 ,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.