My autoencoder-based network met this problem: TypeError: forward() takes 2 positional arguments but 3 were given

Preformatted textI defined My auto-encoder, and when the encoder can runs well. When I put the encoder’s result into the decoder, this error occurred in decoder when I set a random (4,3,244,244) as the input of encoder and forward to my decoder, :
TypeError: forward() takes 2 positional arguments but 3 were given.

In the encoder part, I return “o4_1, o3_4, idx3, relu3_4.size(), o2_2, idx2, relu2_2.size(), o1_2, idx1, relu1_2.size()” in its forward function, and let them be the parameters of decoder’s forward function.

My code is showed as following:

import torch
import torch.nn as nn
import torchvision.models as models

import torch
import torch.nn as nn
import torchvision.models as models


class Encoder(nn.Module):
    def __init__(self, requires_grad=False):
        super(Encoder, self).__init__()
        vgg_pretrained_features = models.vgg16_bn(pretrained=True).features
        self.block1 = nn.Sequential()
        self.pool1 = nn.Sequential()
        self.block2 = nn.Sequential()
        self.pool2 = nn.Sequential()
        self.block3 = nn.Sequential()
        self.pool3 = nn.Sequential()
        self.block4 = nn.Sequential()

        for x in range(6):
            self.block1.add_module(str(x), vgg_pretrained_features[x])
        self.pool1.add_module(str(6), nn.MaxPool2d(2, 2, return_indices=True))
        for x in range(7, 13):
            self.block2.add_module(str(x), vgg_pretrained_features[x])
        self.pool2.add_module(str(13), nn.MaxPool2d(2, 2, return_indices=True))
        for x in range(14, 23):
            self.block3.add_module(str(x), vgg_pretrained_features[x])
        self.pool3.add_module(str(23), nn.MaxPool2d(2, 2, return_indices=True))
        for x in range(24, 27):
            self.block4.add_module(str(x), vgg_pretrained_features[x])

        if not requires_grad:
            for param in self.parameters():
                param.requires_grad = False

    def forward(self, X):
        h = self.block1(X)                          # [4,3,224,224]
        relu1_2 = h                                 # [4,64,224,224]
        o1_2, idx1 = self.pool1(h)                  # [4,64,112,112]

        h = self.block2(o1_2)                       # [4,128,112,112]
        relu2_2 = h
        o2_2, idx2 = self.pool2(h)                  # [4,128,56,56]

        h = self.block3(o2_2)                       # [4,256,56,56]
        relu3_4 = h
        o3_4, idx3 = self.pool3(h)                  # [4,256,28,28]

        out = self.block4(o3_4)                    # [4, 512, 28, 28]

        #return o4_1, o3_4, idx3, relu3_4.size(), o2_2, idx2, relu2_2.size(), o1_2, idx1, relu1_2.size()
        return out, idx1, relu1_2.size(), idx2, relu2_2.size(), idx3, relu3_4.size()


class Decoder(nn.Module):
    def __init__(self):
        super(Decoder, self).__init__()
        self.block5 = nn.Sequential(nn.Conv2d(512, 256, 3, 1, 1),
                                    nn.ReLU(inplace=True),)
        self.unpool1 = nn.Sequential(nn.MaxUnpool2d(2, 2),)
        self.block6 = nn.Sequential(nn.Conv2d(256, 256, 3, 1, 1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(256, 256, 3, 1, 1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(256, 256, 3, 1, 1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(256, 128, 3, 1, 1),
                                    nn.ReLU(inplace=True),
                                    )
        self.unpool2 = nn.Sequential(nn.MaxUnpool2d(2, 2),)
        self.block7 = nn.Sequential(nn.Conv2d(128, 128, 3, 1, 1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 64, 1, 1),
                                    nn.ReLU(inplace=True),
                                    )
        self.unpool3 = nn.Sequential(nn.MaxUnpool2d(2, 2),)
        self.block8 = nn.Sequential(nn.Conv2d(64, 64, 3, 1, 1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(64, 3, 1, 1),
                                    )

    def forward(self, out, idx1, relu1_2_size, idx2, relu2_2_size, idx3, relu3_4_size):
    # def forward(self, o4_1, o3_4=None, idx3=None, relu3_4_size=None, o2_2=None, idx2=None, relu2_2_size=None, o1_2=None, idx1=None, relu1_2_size=None):
        h = self.block5(out)
        h = self.unpool1(h, idx3)   #, output_size=relu3_4_size)
        h = self.block6(h)
        h = self.unpool2(h, idx2)   #, output_size=relu2_2_size)
        h = self.block7(h)
        h = self.unpool1(h, idx1)   #, output_size=relu1_2_size)
        h = self.block8(h)

        return h


ins = torch.randn(4, 3, 224, 224)
#o4_1, o3_4, idx3, relu3_4_size, o2_2, idx2, relu2_2_size, o1_2, idx1, relu1_2_size = Encoder(requires_grad=False)(ins)
out, idx1, relu1_2_size, idx2, relu2_2_size, idx3, relu3_4_size = Encoder(requires_grad=False)(ins)
print("out:", out.size())
print("idx1:", idx1.size())
print("relu1_2_size:", relu1_2_size)
print("idx2:", idx2.size())
print("relu2_2_size:", relu2_2_size)
print("idx3:", idx3.size())
print("relu3_4_size:", relu3_4_size)


# decoder = Decoder.applay(init_weight)
ot = Decoder()(out, idx1, relu1_2_size, idx2, relu2_2_size, idx3, relu3_4_size)
print("ot:", ot)





and print as following:

out: torch.Size([4, 512, 28, 28])
idx1: torch.Size([4, 64, 112, 112])
relu1_2_size: torch.Size([4, 64, 224, 224])
idx2: torch.Size([4, 128, 56, 56])
relu2_2_size: torch.Size([4, 128, 112, 112])
idx3: torch.Size([4, 256, 28, 28])
relu3_4_size: torch.Size([4, 256, 56, 56])
Traceback (most recent call last):
  File "/Users/isaac_russell/PycharmProjects/dehaze/Mynetwork.py", line 107, in <module>
    ot = Decoder()(out, idx1, relu1_2_size, idx2, relu2_2_size, idx3, relu3_4_size)
  File "/Users/isaac_russell/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/isaac_russell/PycharmProjects/dehaze/Mynetwork.py", line 83, in forward
    h = self.unpool1(h, idx3)   #, output_size=relu3_4_size)
  File "/Users/isaac_russell/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
TypeError: forward() takes 2 positional arguments but 3 were given

How can I solve this problem? and If it’s possible I want to know where I was wrong.