why not called function (forward) is called in pytorch class?

Question:

I have the following simple example code for linear regression as follows.import torch

import numpy as np
from torch.autograd import Variable
class linearRegression(torch.nn.Module):
    def __init__(self, inputSize, outputSize):
        super(linearRegression, self).__init__()
        self.linear = torch.nn.Linear(inputSize, outputSize)

    def forward(self, x):
        out = self.linear(x)
        return out

x = np.array([1], dtype = np.float32).reshape(-1, 1)
x = Variable(torch.from_numpy(x))

model = linearRegression(1, 1)
model(x)

the output is tensor([[0.4512]], grad_fn=<AddmmBackward>).

My question is how the output is made by not model.foward(x) but model(x).
In this code I have never called forward, but it seems to be called.

Asked By: Gilseung Ahn

||

Answers:

It is because the dunder method __call__ of nn.Module will internally call its user-defined forward method.

Answered By: TQCH
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.