How to use multiple LSTM?

I want to create model like the picture.
Is it okay to write code like this?

def __init__(self):
    super(Feature_extracter, self).__init__()
    self.lstm = nn.LSTM(input_dim, 50, batch_first=True)
    self.lstm2= nn.LSTM(50, 50, batch_first=True)
    self.lstm3= nn.LSTM(50, 20, batch_first=True)
    self.dropout = nn.Dropout(0.2)

def forward(self, input):
    h0 = torch.zeros(1,1,50)
    c0 = torch.zeros(1,1,50)
    output, (h, c) = self.lstm(input, (h0, c0))
    output  = self.dropout(output)
    output, (h, c) = self.lstm2(output, (h, c))
    output  = self.dropout(output)
    output, (h, c) = self.lstm3(output, (h, c))
    output = self.dropout(output)

Please help a newbie developer :frowning:


nn.LSTM has a num_layers argument. You could just set that to 3. It also has a dropout argument, which just specifies the p value for dropout applied to each layer.

1 Like

I love you ! Thank you ~~

1 Like