Combining an LSTM with multiple fully connected layers
Sometimes, it may be valuable to combine different types of neural networks in a single model. In this recipe, you’ll learn how to combine an LSTM module with a fully connected layer that is the basis of feedforward neural networks.
Getting ready
In this section, we’ll use a hybrid model that combines an LSTM layer with multiple fully connected (also known as dense) layers. This allows us to further abstract features from the sequence, and then learn complex mappings to the output space.
We continue using the reshaped train and test sets from the previous sections.
How to do it…
To construct this hybrid model in PyTorch, we add two fully connected layers after the LSTM layer:
class HybridLSTM(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim=1, num_layers=1): &...