I came across the following code in a neural network tutorial. The following lines works correctly although they contradict with my knowledge on Python class.
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(28 * 28, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, 10)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return F.log_softmax(x)
net = Net()
net_out = net(data)
Here some data
was passed into net.forward()
and forward()
executed.
However, to my knowledge, we have to use net.forward(data)
instead of net(data)
in order to access the function forward. Therefore, could anyone tell me why we can access forward()
without mentioning the function name? Is there some sort of rule that allows us to access a function in a class without using class_name.function_name
?