Difference in forward() impl

whats the difference between

def forward(self):
        y = self.lin1.sigmoid(x)
        return y

and

def forward(self, x):
        y = x.sigmoid(x)
        return y

in the first one, it is using the sigmoid method from lin1 and in the second one, it is using sigmoid from x. We can help you more if you put a more complete sample code.
I didn’t see any x with sigmoid. It should be a tensor. usually you define a sigmoid in your init function and call it in forward:

...
def __init__(...):
   ...
   self.sigmoid = nn.Sigmoid()

def forward(self, x):
   self.sigmoid(x)
   ...

ya sorry, I should have included the whole code. So my actual nn class looks like this for example

class LogReg(nn.Module):
    def __init__(self):
        super(LogReg, self).__init__()
        self.lin1 = nn.Linear(4, 2)

I was browsing through examples on how to implement the forward(), and I was seeing both versions on different models, but was confused about the difference

I don’t think you can use sigmoid without defining it. In this implementation, you don’t have sigmoid.

ok i see, then would this be a more correct impl?

class LogReg(nn.Module):
    def __init__(self):
        super(LogReg, self).__init__()
        self.lin1 = nn.Linear(4, 2)
        self.sig1 = nn.Sigmoid()
    
    def forward(self, x):
        x = self.lin1(x)
        y = self.sig1(x)
        return y

yes this is ok and should work.

ok great, thanks for the help, appreciate it!

1 Like