Pytorch 自动编码器MaxUnpol2D缺失';指数';论点
以下模型返回错误: TypeError:forward()缺少1个必需的位置参数:“index” 我已经列举了许多在线示例,它们看起来都与我的代码相似。我的maxpool层返回unpol层的输入和索引。有什么问题吗Pytorch 自动编码器MaxUnpol2D缺失';指数';论点,pytorch,Pytorch,以下模型返回错误: TypeError:forward()缺少1个必需的位置参数:“index” 我已经列举了许多在线示例,它们看起来都与我的代码相似。我的maxpool层返回unpol层的输入和索引。有什么问题吗 class autoencoder(nn.Module): def __init__(self): super(autoencoder, self).__init__() self.encoder = nn.Sequential( ...
class autoencoder(nn.Module):
def __init__(self):
super(autoencoder, self).__init__()
self.encoder = nn.Sequential(
...
nn.MaxPool2d(2, stride=1, return_indices=True)
)
self.decoder = nn.Sequential(
nn.MaxUnpool2d(2, stride=1),
...
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
与问题类似,解决方案似乎是将maxUnpol层与解码器分离,并显式传递其所需参数<代码>nn。顺序只接受一个参数
class SimpleConvAE(nn.Module):
def __init__(self):
super().__init__()
# input: batch x 3 x 32 x 32 -> output: batch x 16 x 16 x 16
self.encoder = nn.Sequential(
...
nn.MaxPool2d(2, stride=2, return_indices=True),
)
self.unpool = nn.MaxUnpool2d(2, stride=2, padding=0)
self.decoder = nn.Sequential(
...
)
def forward(self, x):
encoded, indices = self.encoder(x)
out = self.unpool(encoded, indices)
out = self.decoder(out)
return (out, encoded)