How to concat two sequential()?

Suppose I define two sequential():

a = nn.Sequential(
...
)

b = nn.Sequential(
...
)

Now I want to define another Sequential() that can concat the two Sequential() a and b above. What am I supposed to do?

1 Like

If you don’t want to stack sequentials, you could do

s_cat = torch.nn.Sequential(*(list(s1)+list(s2)))

Best regards

Thomas

1 Like

Thanks a lot!
BTW, what should I do if want to stack sequentials?

s_stacked = torch.nn.Sequential(s1, s2)

Oh, sorry, I meant the wrong thing. Let me ask you another way. Suppose the output dim of s1 is [a, b, c] and the output dim ofs2 is [a, b, c] too. Then what is the output dim of s_cat by

s_cat = torch.nn.Sequential(*(list(s1)+list(s2)))
2 Likes

Oh, you want the tensors concatenated?
I think just running the two and then concatenating the result is the best option.

Best regards

Thomas

1 Like

Thank you very much!

Hi Thomas,

What is the difference between stacking vs concatenating nn.Sequentials?

Maybe this is not the best terminology here.
You can run Sequential and concatenate/stack the resulting tensors.
You can also make a combined sequential of two of them. If you come from viewing them as functions, you might call that composing the two, or if you come from viewing them as lists, you might say concatenate.

I would probably recommend to think about what you want to do and then see how this might be achieved.

Best regards

Thomas

1 Like

Thank you very much for your detailed explanation.

1 Like

I know this thread is old, but just for reference (and because it tends to pop up again), I want to mention that you can also do

a.extend(b)

which modifies a (maybe this is okay for you), but is much simpler syntax.

Alternatively, you could do:

c = torch.nn.Sequential().extend(a).extend(b)

which does not modify a.