Home > Back-end >  why not called function (forward) is called in pytorch class?
why not called function (forward) is called in pytorch class?

Time:12-13

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.

CodePudding user response:

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

  • Related