Skip to content
Advertisement

Specifying number of cells in LSTM layer in PyTorch

I don’t fully understand the LSTM layer in PyTorch. When I instantiate an LSTM layer how can I specify the number of LSTM cells inside the layer? My first thought was that it was the “num_layers” argument, if we assume that LSTM cells are connected vertically. But if that is the case how can we implement stacked LSTM with for example two layers with 8 cells each?

Advertisement

Answer

The amount of cells of an LSTM (or RNN or GRU) is the amount of timesteps your input has/needs. For example, when you want to run the word „hello“ through the LSTM function in Pytorch, you can just convert the word to a vector (with one-hot encoding or embeddings) and then pass that vector though the LSTM function. It will then, in the background, iterate through all the embedded characters („h“, „e“, „l“, …). And each input can even have a different amount of timesteps/cells, for example when you want to pass „hello“ and after that „Joe“ the LSTM will need different amount of iterations (5 for hello, 3 for Joe). So as you can see, there is no need to give an amount of cells! Hope that answer satisfied you. :)

Edit

An example:

JavaScript

So what happens in this (outputs, hidden = lstm(input, hidden)) line? Again pseudo code:

JavaScript

Is it clear now how what the LSTM function does and how to use it?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement