https://d2l.ai/chapter_linear-classification/classification.html
Can someone help me understand the code within the classifier below? I would assume it should be something like Y_hat = self.forward(X). So how does X become *batch[:-1]? and why no need to call self.forward() but just use self()?
Y_hat = self(*batch[:-1])
and why no need to call self.forward() but just use self()?
It was explained before, I think. The class defines the __call__
special method to be equivalent to forward
, so doing self()
is the same as self.forward()
.
So how does X become *batch[:-1]?
batch[:-1]
gives you all but the last element of batch
. Then *
is just the unpacking operator.
Hey, I do think that in the following code:
@d2l.add_to_class(d2l.Module) #@save
def configure_optimizers(self):
return torch.optim.SGD(self.parameters(), lr=self.lr)
Instead of adding it into d2l.Module
we should add it into the Classifier
class. Because in the previous LinearRegression lecture we added the configure_optimizers
in the LinearRegressionScratch
class instead of the Module.
Of couse, at this point adding into Module or in the Classifier won’t make any difference, but just to keep consistancy, we should do that.
Suggested block:
@d2l.add_to_class(Classifier) #@save
def configure_optimizers(self):
return torch.optim.SGD(self.parameters(), lr=self.lr)
Thanks!