这个超级函数在哪里寻找 __init__()?
Where is this super function looking for __init__()?
我正在查看这个 python class 并且我试图弄清楚为什么超级函数有任何参数。如果我的理解是正确的,在这种情况下,不带参数的 super() 将完成相同的工作。我说得对吗?
这是代码
class Net(torch.nn.Module):
def __init__(self, input_size, hidden_size):
super(Net, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size)
self.relu = torch.nn.ReLU()
self.fc2 = torch.nn.Linear(self.hidden_size, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
hidden = self.fc1(x)
relu = self.relu(hidden)
output = self.fc2(relu)
output = self.sigmoid(output)
return output
在 Python 2 中,参数不是可选的。在 Python 3 中,有特殊的编译器魔法来确定要使用的参数是它们被省略了。在 Net.__init__
的上下文中,super()
等同于 super(Net, self)
.
我正在查看这个 python class 并且我试图弄清楚为什么超级函数有任何参数。如果我的理解是正确的,在这种情况下,不带参数的 super() 将完成相同的工作。我说得对吗?
这是代码
class Net(torch.nn.Module):
def __init__(self, input_size, hidden_size):
super(Net, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size)
self.relu = torch.nn.ReLU()
self.fc2 = torch.nn.Linear(self.hidden_size, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
hidden = self.fc1(x)
relu = self.relu(hidden)
output = self.fc2(relu)
output = self.sigmoid(output)
return output
在 Python 2 中,参数不是可选的。在 Python 3 中,有特殊的编译器魔法来确定要使用的参数是它们被省略了。在 Net.__init__
的上下文中,super()
等同于 super(Net, self)
.