Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch LSTM vs LSTMCell

What is the difference between LSTM and LSTMCell in Pytorch (currently version 1.1)? It seems that LSTMCell is a special case of LSTM (i.e. with only one layer, unidirectional, no dropout).

Then, what's the purpose of having both implementations? Unless I'm missing something, it's trivial to use an LSTM object as an LSTMCell (or alternatively, it's pretty easy to use multiple LSTMCells to create the LSTM object)

like image 768
dkv Avatar asked Jul 15 '19 23:07

dkv


People also ask

What is the difference between LSTM and LSTMCell?

Both things are almost the same. An LSTM layer is a RNN layer using an LSTMCell, as you can check out in the source code. About the number of cells: Although it seems, because of its name, that LSTMCell is a single cell, it is actually an object that manages all the units/cells as we may think.

Does Pytorch have LSTM?

We'll then intuitively describe the mechanics that allow an LSTM to “remember.” With this approximate understanding, we can implement a Pytorch LSTM using a traditional model class structure inheriting from nn. Module , and write a forward method for it.

How is LSTM implemented in Pytorch?

Next stepsClean up the data by removing non-letter characters. Increase the model capacity by adding more Linear or LSTM layers. Split the dataset into train, test, and validation sets. Add checkpoints so you don't have to train the model every time you want to run prediction.

What is hidden state and cell state in LSTM?

A key idea in LSTM is the (star)Gate.h is the hidden state, representing short term memory. C is the cell state, representing long term memory and x is the input. The gates perform only few matrices transformations, sigmoid and tanh activation in order to magically solve all the RNN problems.


1 Answers

Yes, you can emulate one by another, the reason for having them separate is efficiency.

LSTMCell is a cell that takes arguments:

  • Input of shape batch × input dimension;
  • A tuple of LSTM hidden states of shape batch x hidden dimensions.

It is a straightforward implementation of the equations.

LSTM is a layer applying an LSTM cell (or multiple LSTM cells) in a "for loop", but the loop is heavily optimized using cuDNN. Its input is

  • A three-dimensional tensor of inputs of shape batch × input length × input dimension;
  • Optionally, an initial state of the LSTM, i.e., a tuple of hidden states of shape batch × hidden dim (or tuple of such tuples if the LSTM is bidirectional)

You often might want to use the LSTM cell in a different context than apply it over a sequence, i.e. make an LSTM that operates over a tree-like structure. When you write a decoder in sequence-to-sequence models, you also call the cell in a loop and stop the loop when the end-of-sequence symbol is decoded.

like image 56
Jindřich Avatar answered Nov 11 '22 12:11

Jindřich