Sequential LSTM II

I have the same question as the one unanswered in a previous tread.

Is it ever possible to do this:

model = nn.Sequential(
nn.LSTM(…),
# since LSTM will return a tuple, anything here in the middle to make this whole thing work?
nn.Linear(…)
)

2 Likes

I haven’t tried but I don’t see why it doesn’t work.

Because the LSTM gives out a tuple and the next linear needs one input. Basically, I am hoping to be able to define which output of the LSTM sequential should use.

There isn’t a layer for that in pytorch.
Typically you would not use nn.Sequential but nn.Module and spell out the forward.
You could also put the things (taking items from the tuple, .view or so) between LSTM and Linear in a layer, but that would feel (to me) a bit artificial in pytorch.
That said, don’t believe my style advice, but use what works best for your project.

Best regards

Thomas

Thanks to all, so basically this is still impossible. The sequential code needs a slight modification in it’s forward to consider mutli-output modules:

def forward(self, input): 
    for module in self._modules.values():
        input = module(input)
        if isinstance(input, tuple):
            input = input[0] 
    return input

I filed this issue as well.

This is not a bug at all. You are able to use nn.Sequential to acheive this. My previous reply meant that you can indeed insert something in the middle to make it work. Just write a custom nn.Module.

1 Like

I am still confused, would you please give an example?

Multiple options:

  1. custom module:
class TakeFirst(nn.Module):
  def forward(self, x):
    return x[0]
  1. Activate nn.Sequential(..., LSTM) first, then feed the first result into a second nn.Sequential.

  2. As @tom said, don’t use nn.Sequential altogether. Spell out the steps in a custom module.

The reason that this is not a bug is because ``nn.Squential` is just supposed to put the output as the next’s input. Automatically taking the first is something that it never promised to do.

nn.Sequential is just a convenient wrapper class. If it doesn’t work the best for you, just use other more suitable approaches.

3 Likes

Thanks Simon, I was trying to avoid creating a new class and make the code simpler.

Cannot really see the harm though in making Sequential deal with tuples in that it just takes the first element - would make using Sequential simpler and less surprising because this is what everyone would expect it to do.
The error one gets is also not very useful, the Sequential layer could at least complain properly.

1 Like

I agree that the error message could be improved, but I think it is important to have a well defined, strict sequential wrapper. One can always create a custom module (be it LSTM or anything else) with the desired specifications, and add it to nn.Sequential.