Help me understand this pytorch nn.module inheritance

class Model(torch.nn.Module):

def init(self):
“”"
In the constructor we instantiate two nn.Linear module
“”"
super(Model, self).init()
self.linear = torch.nn.Linear(1, 1) # One in and one out

def forward(self, x):
“”"
In the forward function we accept a Variable of input data and we must return
a Variable of output data. We can use Modules defined in the constructor as
well as arbitrary operators on Variables.
“”"
y_pred = self.linear(x)
return y_pred
So my question is,

  1. self.linear = torch.nn.Linear(1, 1)
    ^ How can this attribute take in a function?
  2. y_pred = self.linear(x)
    return y_pred
    ^ How can we call an attribute? I thought functions/methods were only called using (). Isn’t self.linear an attribute?

I am sorry if this sounds silly but the above two are really bothering me.

Hi,
I didn’t really understand the first question, but regarding the second question:
nn.Linear inherits from nn.Module which implements the __call__(self, *input, **kwargs): function so when using self.linear(x) it’s actually calling to __call__() function