Pytorchのクラスclass A(nn.Module)内のforward関数について、
デフォルトでは
def forward(self, x):
のように書かれていますが、
引数を増やして、
def forward(self, x, y, z):
と書き換え、x, y, zの引数を与えて実行すると
result = self.forward(*input, **kwargs)
TypeError: forward() takes 2 positional arguments but 4 were given
というエラーが出ます。
forwardの引数は、必ず(self, x)のままでないといけないのでしょうか?
class ShakeBlock(nn.Module): def __init__(self, in_ch, out_ch, stride=1): super(ShakeBlock, self).__init__() self.equal_io = in_ch == out_ch self.shortcut = self.equal_io and None or Shortcut(in_ch, out_ch, stride=stride) self.branch1 = self._make_branch(in_ch, out_ch, stride) self.branch2 = self._make_branch(in_ch, out_ch, stride) def forward(self, x, y): h1 = self.branch1(x) h2 = self.branch2(x) h = ShakeShake.apply(h1, h2, self.training) h0 = x if self.equal_io else self.shortcut(x) return h + h0 def _make_branch(self, in_ch, out_ch, stride=1): return nn.Sequential( nn.ReLU(inplace=False), nn.Conv2d(in_ch, out_ch, 3, padding=1, stride=stride, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(inplace=False), nn.Conv2d(out_ch, out_ch, 3, padding=1, stride=1, bias=False), nn.BatchNorm2d(out_ch)) class ShakeResNet(nn.Module): def __init__(self, depth, num_classes): super(ShakeResNet, self).__init__() n_units = (depth - 2) / 6 w_base = 32 in_chs = [16, w_base, w_base * 2, w_base * 4] self.in_chs = in_chs self.c_in = nn.Conv2d(3, in_chs[0], 3, padding=1) self.layer1 = self._make_layer(n_units, in_chs[0], in_chs[1]) self.layer2 = self._make_layer(n_units, in_chs[1], in_chs[2], 2) self.layer3 = self._make_layer(n_units, in_chs[2], in_chs[3], 2) self.fc_out = nn.Linear(in_chs[3], num_classes) # Initialize paramters for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): m.bias.data.zero_() def forward(self, x): h = self.c_in(x) h = self.layer1(h, h) h = self.layer2(h) h = self.layer3(h) h = F.relu(h) h = F.avg_pool2d(h, 8) h = h.view(-1, self.in_chs[3]) h = self.fc_out(h) return h def _make_layer(self, n_units, in_ch, out_ch, stride=1): layers = [] for i in range(int(n_units)): layers.append(ShakeBlock(in_ch, out_ch, stride=stride)) in_ch, stride = out_ch, 1 return nn.Sequential(*layers)
仮にh = self.layer1(h, h)として
ShakeBlock内のforward(self, x, y)を呼び出していますが、
実行すると、
result = self.forward(*input, **kwargs)
TypeError: forward() takes 2 positional arguments but 3 were given
というエラーが出ます。
コードは
https://github.com/owruby/shake-shake_pytorch
から借りています。
ご指摘の点について
ShakeBlock内のforwardを
def forward(self, x, y): h1 = self.branch1(x) h2 = self.branch2(x) h = ShakeShake.apply(h1, h2, self.training) h0 = x if self.equal_io else self.shortcut(x) return h + h0, h + h0
ShakeResNet内のforwardを
def forward(self, x): h = self.c_in(x) h, h = self.layer1(h, h) h, h = self.layer2(h, h) h, h = self.layer3(h, h) h = F.relu(h) h = F.avg_pool2d(h, 8) h = h.view(-1, self.in_chs[3]) h = self.fc_out(h) return h
とそれぞれ変更しましたが、同じエラーが出ます。
回答1件
あなたの回答
tips
プレビュー