top of page

Long Short Term Memory Networks

Updated: Jul 4, 2021

Long Short Term Memory (LSTM) are a special kind of RNN, capable of learning long-term dependencies. They work tremendously well on a large variety of problems, and are now widely used.

LSTMs are esigned to avoid the long-term dependency problem. Remembering information for long periods of time is practically their default behavior, not something they struggle to learn!

All recurrent neural networks have the form of a chain of repeating modules of neural network. In standard RNNs, this repeating module will have a very simple structure, such as a single tanh layer(neural network layer/ hidden layer).

Fig 1.

LSTMs also have this chain like structure, but the repeating module has a different structure. Instead of having a single neural network layer, there are four neural network layer , interacting in a very special way.

Fig 2.

Don’t worry we will walk through all the concept in depth to clear the concept step by step . For now, let’s just try to get comfortable with the notation we’ll be using.

Fig 3.

In the above diagram, each line carries an entire vector, from the output of one node to the inputs of others. The pink circles represent pointwise operations, like vector addition, while the yellow boxes are learned neural network layers. Lines merging denote concatenation, while a line forking denote its content being copied and the copies going to different locations.

Core Concept Behind the LSTM -

The key to LSTMs is the cell state, the horizontal line running through the top of the diagram.

The cell state is kind of like a conveyor belt. It runs straight down the entire chain, with only some minor linear interactions.

Fig 4.

The LSTM does have the ability to remove or add information to the cell state, carefully regulated by structures called gates.

Gates are a way to optionally let information through. They are composed out of a sigmoid neural layer and a pointwise multiplication operation.

Fig 5.

The output of sigmoid layer is always between 0 -1 . An LSTM has three of these gates, to protect and control the cell state.

Step By Step LSTM -

The first step in our LSTM is to decide what information we’re going to throw away from the cell state. This decision is made by a sigmoid layer called the “forget gate layer.” It looks at ht−1 and xt, and outputs a number between 0 and 1 for each number in the cell state Ct−1. A 1 represents “completely keep this” while a 0 represents “completely get rid of this.”

Let’s go back to our example of a language model trying to predict the next word based on all the previous ones. In such a problem, the cell state might include the gender of the present subject, so that the correct pronouns can be used. When we see a new subject, we want to forget the gender of the old subject.

Fig 6.

The next step is to decide what new information we’re going to store in the cell state. This has two parts. First, a sigmoid layer called the “input gate layer” decides which values we’ll update. Next, a tanh layer creates a vector of new candidate values, C~t, that could be added to the state. In the next step, we’ll combine these two to create an update to the state.

In the example of our language model, we’d want to add the gender of the new subject to the cell state, to replace the old one we’re forgetting.

Fig 7.

It’s now time to update the old cell state, Ct−1, into the new cell state Ct. The previous steps already decided what to do.

We multiply the old state by ft, forgetting the things we decided to forget earlier. Then we add it∗C~t. This is the new candidate values, scaled by how much we decided to update each state value.

In the case of the language model, this is where we’d actually drop the information about the old subject’s gender and add the new information, as we decided in the previous steps.

Fig 8.

Finally, we need to decide what we’re going to output. This output will be based on our cell state, but will be a filtered version. First, we run a sigmoid layer which decides what parts of the cell state we’re going to output. Then, we put the cell state through tanh (to push the values to be between −1 and 1) and multiply it by the output of the sigmoid gate, so that we only output the parts we decided to.

Fig 9.

Variants on Long Short Term Memory -

What I’ve described so far is a normal LSTM. But not all LSTMs are the same as the above. In fact, it seems like almost every paper involving LSTMs uses a slightly different version. The differences are minor.

1. Peephole connections -

Fig 10.

The above diagram adds peepholes to all the gates, but many papers will give some peepholes and not others.

2. Use coupled forget and input gates (at same time only) -

Instead of separately deciding what to forget and what we should add new information to, we make those decisions together. We only forget when we’re going to input something in its place. We only input new values to the state when we forget something older.

Fig 11.

This are not only the variations there are thousands of variation you can do research on it and explore as much as possible .

Your feedback is appreciated!

Did you find this Blog helpful? Any suggestions for improvement? Please let me know by filling the contact us form or ping me on LinkedIn( social handle is given at the bottom of page )


133 views0 comments

Recent Posts

See All
bottom of page