Confusion in model creation and calling it and passing parameters

When we create the Logistic Regression model, we pass ‘input_size’ and ‘num_classes’ as parameters (model = nn.Linear(input_size, num_classes)). But during training the model, we pass only ‘images’ as a parameter ( outputs = model(images)). How does the model train since we have defined both the parameters of the model when creating it and how can ‘images’ relate ?

nn.Linear is a class with __init__ method (in other languages called constructor). what you do by calling it, you actually construct an instance of this class.

This class has an method named __call__ which is invoked whenever you use model(someparams)

So you’re kinda confusing 2 different things over here - constructing an object and using it’s __call__ method.

The parameters passed into __init__ are used only to construct the model, initialize the weights/bias etc.
The parameters passed into __call__ (in this case images) have nothing to do with input_size or num_classes.

1 Like

input_size and num_classes are used to initialize the model with a weights matrix of size num_classes x input_size and bias matrix of size 1 x num_classes at this point model is at a trainable state.

But training happens when you pass images of shape no_of_sample x input_size where the forward method is called where the initialized weights and bias matrix is used along with the input (images) to do some computation based on your forward method (here the linear transformation y = x @ w.t() + b)