DEV Community

Cover image for Understanding LSTMs – Part 7: LSTM in Action with Real Data
Rijul Rajesh
Rijul Rajesh

Posted on

Understanding LSTMs – Part 7: LSTM in Action with Real Data

In the previous article, we completed all three stages of the LSTM: the Forget Gate, Input Gate, and Output Gate.

Now, let us use the LSTM with real data.


Dataset

Here we have stock prices for two companies:

  • Company A
  • Company B

On the y-axis, we have the stock value.
On the x-axis, we have the day the value was recorded.

If we overlap the data for both companies, we observe that the only difference occurs between Day 1 and Day 5.

We will sequentially pass the data from Day 1 through Day 4 into an unrolled LSTM and see whether it can correctly predict the value for Day 5 for both companies.


Initializing the LSTM

We begin by initializing the long-term and short-term memories to zero:

To maintain a clear overview as we proceed, we will use the following simplified diagram from time to time:


Company A

We now sequentially pass Company A’s values from Day 1 through Day 4 into the LSTM.

Day 1

We start by plugging in the value for Day 1, which is 0, into the input.

After performing the calculations, we obtain:

  • New long-term memory: ( C_1 = -0.20 )
  • New short-term memory: ( h_1 = -0.13 )

Our diagram updates as follows:


Day 2

Next, we plug in the value from Day 2 and repeat the same process.
This produces updated long-term and short-term memories.


Days 3 and 4

We repeat the same procedure for Day 3 and Day 4.

After processing Day 4, the final short-term memory is:

h_4 = 0

This means the output from the unrolled LSTM correctly predicts Company A’s value for Day 5.


Company B

We now repeat the exact same process using Company B’s values.

After sequentially passing Days 1 through 4 into the LSTM, we obtain:

The final short-term memory is:

h_4 = 1

This correctly predicts the value for Day 5 for Company B.

This concludes our discussion of LSTMs.

In the upcoming articles, we will move on to Word Embeddings and Word2Vec.

Looking for an easier way to install tools, libraries, or entire repositories?
Try Installerpedia: a community-driven, structured installation platform that lets you install almost anything with minimal hassle and clear, reliable guidance.

Just run:

ipm install repo-name
Enter fullscreen mode Exit fullscreen mode

… and you’re done! 🚀

Installerpedia Screenshot

🔗 Explore Installerpedia here

Top comments (0)