Extend nn.Module class

While extending the class it is mentioned that :-
’Note that the model no longer has .weight and .bias attributes (as they are now inside the .linear attribute), but it does have a .parameters method that returns a list containing the weights and bias.'

What is the meaning of this ?

Could you Notebook link where it shows this?

In part 2, model is defined as model = nn.Linear(3, 2).
we can access weight and bias using model.weight and model.bias.

In part 3, model is defined as class MnistModel(nn.Module) (Not nn.Linear anymore).
You can still access weight and bias by model.linear.weight & model.linear.bias.

1 Like

Because the new class probably contains a field named linear that is an instance of nn.Linear.

First version:
model = nn.Linear(x, y)
You can access weights and bias.

Second version:
model = MnistModel
Probably somewhere inside def __init__ you’ll see a line like this:
self.linear = nn.Linear(x, y).
So now your model is an instance of different class, but it has a field linear which you can access. And since this field is an instance of nn.Linear you can access it’s weights and bias fields.

1 Like