Either too little or too many positional arguments when using nn.Sequential

Hi,

I am new to PyTorch, so please excuse my silly question.

I define a nn.Sequential in init of my Encoder object like this:

self.list_of_blocks = [EncoderBlock(n_features, n_heads, n_hidden, dropout) for _ in range(n_blocks)]
self.blocks = nn.Sequential(*self.list_of_blocks)

The forward of EncoderBlock looks like this

def forward(self, x, mask):

Then, in the forward() of my Encoder, I try to do:

z0 = self.blocks(z0, mask)

However, I get

TypeError: forward() takes 2 positional arguments but 3 were given

When I try:

z0 = self.blocks(z0)

I get (understandably):

TypeError: forward() takes 2 positional arguments but only 1 was given

When I do not use nn.Sequential and just execute one EncoderBlock after another, it works:

for i in range(self.n_blocks):
     z0 = self.list_of_blocks[i](z0, mask)

What am I doing wrong and how do I use nn.Sequential correctly in this case?

Thank you!

nn.Sequential is used for very simple models and, if I’m not mistaken, doesn’t accept multiple inputs.
You could try this approach or write a custom model.

2 Likes