{"id":7063,"date":"2023-08-08T14:58:02","date_gmt":"2023-08-08T22:58:02","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=7063"},"modified":"2025-04-24T17:14:52","modified_gmt":"2025-04-24T17:14:52","slug":"an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/","title":{"rendered":"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM)"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\">\n\n\n\n<div class=\"fh fi fj fk fl\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png\" alt=\"\" width=\"1500\" height=\"1034\"><\/figure><div class=\"fm bg\">\n<figure class=\"fn fm bg paragraph-image\"><picture><\/picture><\/figure>\n<\/div>\n<div class=\"ab ca\">\n<div class=\"ch bg et eu ev ew\">\n<p id=\"135f\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">Human thoughts are persistent, and this enables us to understand patterns, which in turn gives us the ability to predict the next sequence of actions. Your understanding of this article will be based on the previous words that you\u2019ve read. Recurrent Neural Networks replicate this concept.<\/p>\n<p id=\"8c36\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">RNNs are a type of artificial neural network that are able to recognize and predict sequences of data such as text, genomes, handwriting, spoken word, or numerical time series data. They have loops that allow a consistent flow of information and can work on sequences of arbitrary lengths.<\/p>\n<p id=\"2968\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">Using an internal state (memory) to process a sequence of inputs, RNNs are already being used to solve a number of problems:<\/p>\n<ul class=\"\">\n<li id=\"c5db\" class=\"lx ly fs be b lz ma mb mc md me mf mg mu mi mj mk mv mm mn mo mw mq mr ms mt mx my mz bj\" data-selectable-paragraph=\"\">Language translation and modeling<\/li>\n<li id=\"e3b9\" class=\"lx ly fs be b lz na mb mc md nb mf mg mu nc mj mk mv nd mn mo mw ne mr ms mt mx my mz bj\" data-selectable-paragraph=\"\">Speech recognition<\/li>\n<li id=\"7dd9\" class=\"lx ly fs be b lz na mb mc md nb mf mg mu nc mj mk mv nd mn mo mw ne mr ms mt mx my mz bj\" data-selectable-paragraph=\"\">Image captioning<\/li>\n<li id=\"2734\" class=\"lx ly fs be b lz na mb mc md nb mf mg mu nc mj mk mv nd mn mo mw ne mr ms mt mx my mz bj\" data-selectable-paragraph=\"\">Time series data such as stock prices to tell you when to buy or sell<\/li>\n<li id=\"3e44\" class=\"lx ly fs be b lz na mb mc md nb mf mg mu nc mj mk mv nd mn mo mw ne mr ms mt mx my mz bj\" data-selectable-paragraph=\"\">Autonomous driving systems to anticipate car trajectories and help avoid accidents.<\/li>\n<\/ul>\n<p id=\"fd9b\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">I\u2019ve written this with the assumption that you have a basic understanding of neural networks. In case you need a refresher, please go through this <a class=\"af nf\" href=\"https:\/\/www.kdnuggets.com\/2016\/11\/quick-introduction-neural-networks\" target=\"_blank\" rel=\"noopener ugc nofollow\">quick Introduction to Neural Networks.<\/a><\/p>\n<h1 id=\"869c\" class=\"ng nh fs be ni nj nk nl nm nn no np nq nr ns nt nu nv nw nx ny nz oa ob oc od bj\" data-selectable-paragraph=\"\">Understanding Recurrent Neural Networks.<\/h1>\n<p id=\"e0f3\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">To understand RNNs, let\u2019s use a simple perceptron network with one hidden layer. Such a network works well with <a class=\"af nf\" href=\"https:\/\/heartbeat.comet.ml\/detecting-the-language-of-a-persons-name-using-pytorch-rnn-29a9090c20f2\" target=\"_blank\" rel=\"noopener ugc nofollow\">simple classification problems<\/a>. As more hidden layers are added, our network will be able to inference more complex sequences in our input data and increase prediction accuracy.<\/p>\n<p id=\"78e7\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><strong class=\"be oj\">RNN Structure<\/strong><\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:367\/0*5VYw4jkvm21vcnyI.\" alt=\"\" width=\"367\" height=\"178\"><\/figure><div class=\"ok ol om\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*5VYw4jkvm21vcnyI. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*5VYw4jkvm21vcnyI. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*5VYw4jkvm21vcnyI. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*5VYw4jkvm21vcnyI. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*5VYw4jkvm21vcnyI. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*5VYw4jkvm21vcnyI. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:734\/0*5VYw4jkvm21vcnyI. 734w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 367px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*5VYw4jkvm21vcnyI. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*5VYw4jkvm21vcnyI. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*5VYw4jkvm21vcnyI. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*5VYw4jkvm21vcnyI. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*5VYw4jkvm21vcnyI. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*5VYw4jkvm21vcnyI. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:734\/0*5VYw4jkvm21vcnyI. 734w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 367px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<p id=\"68f0\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">A \u2014 Neural Network.<\/p>\n<p id=\"fae1\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">Xt- Input.<\/p>\n<p id=\"92af\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">ht \u2014 Output.<\/p>\n<p id=\"7781\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">Loops ensure a consistent flow of information. <code class=\"cw os ot ou ov b\">A<\/code> (chunk of neural network) produces an output <code class=\"cw os ot ou ov b\">ht<\/code> based on the input <code class=\"cw os ot ou ov b\">Xt<\/code> .<\/p>\n<p id=\"dea9\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">RNNs can also be viewed as multiple copies of the same network, each passing information to its successor.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*WZ2tmBE5RT6vJ7d1.\" alt=\"\" width=\"700\" height=\"247\"><\/figure><div class=\"ok ol ow\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*WZ2tmBE5RT6vJ7d1. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*WZ2tmBE5RT6vJ7d1. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*WZ2tmBE5RT6vJ7d1. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*WZ2tmBE5RT6vJ7d1. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*WZ2tmBE5RT6vJ7d1. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*WZ2tmBE5RT6vJ7d1. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*WZ2tmBE5RT6vJ7d1. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*WZ2tmBE5RT6vJ7d1. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*WZ2tmBE5RT6vJ7d1. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*WZ2tmBE5RT6vJ7d1. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*WZ2tmBE5RT6vJ7d1. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*WZ2tmBE5RT6vJ7d1. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*WZ2tmBE5RT6vJ7d1. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*WZ2tmBE5RT6vJ7d1. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<p id=\"64cd\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">At each time step (<code class=\"cw os ot ou ov b\">t<\/code>), the recurrent neuron receives the input <code class=\"cw os ot ou ov b\">Xt<\/code> as well as its own output from the previous time step <code class=\"cw os ot ou ov b\">ht-1<\/code>.<\/p>\n<p id=\"f831\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">There are a number of great resources if you would like to dive deeper into RNNs, which I strongly recommend. They include:<\/p>\n<p id=\"3a1c\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><a class=\"af nf\" href=\"https:\/\/www.kdnuggets.com\/2015\/10\/recurrent-neural-networks-tutorial\" target=\"_blank\" rel=\"noopener ugc nofollow\">Introduction to Recurrent Neural Networks.<\/a><\/p>\n<p id=\"8b60\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><a class=\"af nf\" href=\"https:\/\/medium.com\/@camrongodbout\/recurrent-neural-networks-for-beginners-7aca4e933b82\" rel=\"noopener\">Recurrent Neural Networks for Beginners.<\/a><\/p>\n<p id=\"7ea6\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><a class=\"af nf\" href=\"http:\/\/www.wildml.com\/2015\/09\/recurrent-neural-networks-tutorial-part-1-introduction-to-rnns\" target=\"_blank\" rel=\"noopener ugc nofollow\">Introduction to RNNs<\/a><\/p>\n<p id=\"283a\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">RNNs have a major setback called <a class=\"af nf\" href=\"https:\/\/medium.com\/@anishsingh20\/the-vanishing-gradient-problem-48ae7f501257\" rel=\"noopener\">vanishing gradient<\/a>; that is, they have difficulties in learning long-range dependencies (relationship between entities that are several steps apart).<\/p>\n<p id=\"1591\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">Imagine we have the price of bitcoin for December 2014, which was say $350, and we want to correctly predict the bitcoin price for the months of April and May 2018. Using RNNs, our model won\u2019t be able to predict the prices for these months accurately due to the long range memory deficiency. To solve this issue, a special kind of RNN called <strong class=\"be oj\">Long Short-Term Memory cell (LSTM) <\/strong>was developed.<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fh fi fj fk fl\">\n<div class=\"ab ca\">\n<div class=\"ch bg et eu ev ew\">\n<h1 id=\"1263\" class=\"ng nh fs be ni nj pt nl nm nn pu np nq nr pv nt nu nv pw nx ny nz px ob oc od bj\" data-selectable-paragraph=\"\"><strong class=\"al\">What is a Long Short-Term Memory Cell?<\/strong><\/h1>\n<p id=\"46de\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">This is a special neuron for memorizing long-term dependencies. LSTM contains an internal state variable which is passed from one cell to the other and modified by <strong class=\"be oj\">Operation Gates <\/strong>(we\u2019ll discuss this later in our example).<\/p>\n<p id=\"4b68\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">LSTM is smart enough to determine how long to hold onto old information, when to remember and forget, and how to make connections between old memory with the new input. For an in-depth understanding of LSTMs, here is a great resource: <a class=\"af nf\" href=\"http:\/\/colah.github.io\/posts\/2015-08-Understanding-LSTMs\" target=\"_blank\" rel=\"noopener ugc nofollow\">Understanding LSTM networks.<\/a><\/p>\n<h2 id=\"914f\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Implementing LSTMs<\/strong><\/h2>\n<p id=\"c7f1\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">In our case, we\u2019re going to implement a time series analysis using LSTMs to predict the prices of bitcoin from December 2014 to May 2018. I have used the historical data from <a class=\"af nf\" href=\"http:\/\/www.cryptodatadownload.com\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">CryptoDataDownload<\/a> since I found it simple and straightforward. I used google\u2019s Colab development environment because of the simplicity in setting up the environment and the accelerated free GPU, which eases the training time for my model. If you are new to Colab, here\u2019s a <a class=\"af nf\" href=\"https:\/\/medium.com\/deep-learning-turkey\/google-colab-free-gpu-tutorial-e113627b9f5d\" rel=\"noopener\">beginner\u2019s guide.<\/a> The bitcoin <code class=\"cw os ot ou ov b\">.csv<\/code> file and the entire code for this example can be obtained from my <a class=\"af nf\" href=\"https:\/\/github.com\/brynmwangy\/predicting-bitcoin-prices-using-LSTM\" target=\"_blank\" rel=\"noopener ugc nofollow\">github profile.<\/a><\/p>\n<h2 id=\"2b83\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">What is time series analysis?<\/strong><\/h2>\n<p id=\"c396\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">This is where historical data is used to identify existing data patterns and use them to predict what will happen in the future. For a detailed understanding, refer to this <a class=\"af nf\" href=\"https:\/\/www.kdnuggets.com\/2018\/03\/time-series-dummies-3-step-process.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">guide<\/a>.<\/p>\n<h2 id=\"1f39\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Importing libraries<\/strong><\/h2>\n<p id=\"d8d9\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">We\u2019re going to work with a variety of libraries that we\u2019ll have to install first in our Colab notebook and then import into our environment.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:542\/0*BQ3ZegYxaIeJY11o.\" alt=\"\" width=\"542\" height=\"229\"><\/figure><div class=\"ok ol qp\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*BQ3ZegYxaIeJY11o. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*BQ3ZegYxaIeJY11o. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*BQ3ZegYxaIeJY11o. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*BQ3ZegYxaIeJY11o. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*BQ3ZegYxaIeJY11o. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*BQ3ZegYxaIeJY11o. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1084\/0*BQ3ZegYxaIeJY11o. 1084w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 542px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*BQ3ZegYxaIeJY11o. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*BQ3ZegYxaIeJY11o. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*BQ3ZegYxaIeJY11o. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*BQ3ZegYxaIeJY11o. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*BQ3ZegYxaIeJY11o. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*BQ3ZegYxaIeJY11o. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1084\/0*BQ3ZegYxaIeJY11o. 1084w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 542px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"8668\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Loading the dataset<\/strong><\/h2>\n<p id=\"4339\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">The <code class=\"cw os ot ou ov b\">btc.csv<\/code> dataset contains the prices and volumes of bitcoin, and we load it into our working environment using the following commands:<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*GLP7b3MnHuZrmoUj.\" alt=\"\" width=\"700\" height=\"313\"><\/figure><div class=\"ok ol qr\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*GLP7b3MnHuZrmoUj. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*GLP7b3MnHuZrmoUj. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*GLP7b3MnHuZrmoUj. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*GLP7b3MnHuZrmoUj. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*GLP7b3MnHuZrmoUj. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*GLP7b3MnHuZrmoUj. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*GLP7b3MnHuZrmoUj. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*GLP7b3MnHuZrmoUj. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*GLP7b3MnHuZrmoUj. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*GLP7b3MnHuZrmoUj. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*GLP7b3MnHuZrmoUj. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*GLP7b3MnHuZrmoUj. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*GLP7b3MnHuZrmoUj. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*GLP7b3MnHuZrmoUj. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h2 id=\"ac12\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Target Variable<\/strong><\/h2>\n<p id=\"a3cc\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">We are going to select the bitcoin closing price as our target variable to predict.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:674\/0*y8XJvbF2js5b9dmJ.\" alt=\"\" width=\"674\" height=\"105\"><\/figure><div class=\"ok ol qs\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*y8XJvbF2js5b9dmJ. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*y8XJvbF2js5b9dmJ. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*y8XJvbF2js5b9dmJ. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*y8XJvbF2js5b9dmJ. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*y8XJvbF2js5b9dmJ. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*y8XJvbF2js5b9dmJ. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1348\/0*y8XJvbF2js5b9dmJ. 1348w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 674px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*y8XJvbF2js5b9dmJ. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*y8XJvbF2js5b9dmJ. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*y8XJvbF2js5b9dmJ. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*y8XJvbF2js5b9dmJ. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*y8XJvbF2js5b9dmJ. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*y8XJvbF2js5b9dmJ. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1348\/0*y8XJvbF2js5b9dmJ. 1348w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 674px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"98c1\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Data preprocessing<\/strong><\/h2>\n<p id=\"3892\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">Sklearn contains the <a class=\"af nf\" href=\"https:\/\/heartbeat.comet.ml\/data-preprocessing-and-visualization-implications-for-your-machine-learning-model-8dfbaaa51423\" target=\"_blank\" rel=\"noopener ugc nofollow\">preprocessing<\/a> module that allows us to scale our data and then fit it in our model.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:652\/0*K9puuvVraaFxNSL3.\" alt=\"\" width=\"652\" height=\"81\"><\/figure><div class=\"ok ol qt\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*K9puuvVraaFxNSL3. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*K9puuvVraaFxNSL3. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*K9puuvVraaFxNSL3. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*K9puuvVraaFxNSL3. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*K9puuvVraaFxNSL3. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*K9puuvVraaFxNSL3. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1304\/0*K9puuvVraaFxNSL3. 1304w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 652px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*K9puuvVraaFxNSL3. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*K9puuvVraaFxNSL3. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*K9puuvVraaFxNSL3. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*K9puuvVraaFxNSL3. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*K9puuvVraaFxNSL3. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*K9puuvVraaFxNSL3. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1304\/0*K9puuvVraaFxNSL3. 1304w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 652px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"75fc\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Plotting our data<\/strong><\/h2>\n<p id=\"d239\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">Let\u2019s now take a look at how the bitcoin close price trended over the given time period.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*-nIl1ohAryLm8EvB.\" alt=\"\" width=\"700\" height=\"556\"><\/figure><div class=\"ok ol qu\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*-nIl1ohAryLm8EvB. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*-nIl1ohAryLm8EvB. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*-nIl1ohAryLm8EvB. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*-nIl1ohAryLm8EvB. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*-nIl1ohAryLm8EvB. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*-nIl1ohAryLm8EvB. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*-nIl1ohAryLm8EvB. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*-nIl1ohAryLm8EvB. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*-nIl1ohAryLm8EvB. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*-nIl1ohAryLm8EvB. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*-nIl1ohAryLm8EvB. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*-nIl1ohAryLm8EvB. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*-nIl1ohAryLm8EvB. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*-nIl1ohAryLm8EvB. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h1 id=\"48ea\" class=\"ng nh fs be ni nj nk nl nm nn no np nq nr ns nt nu nv nw nx ny nz oa ob oc od bj\" data-selectable-paragraph=\"\">Features and label dataset<\/h1>\n<p id=\"9df0\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">This function is used to create the features and labels for our data set by windowing the data.<\/p>\n<p id=\"7f6c\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">Input: data <\/code>\u2014 this is the dataset we are using .<\/p>\n<p id=\"1ba7\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">Window_size <\/code>\u2014 how many data points we are going to use to predict the next datapoint in the sequence. (Example if <code class=\"cw os ot ou ov b\">window_size=7<\/code> we are going to use the previous 7 days to predict the bitcoin price for today).<\/p>\n<p id=\"5c93\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">Outputs: X<\/code> \u2014 features split into windows of data points(if <code class=\"cw os ot ou ov b\">windows_size=1<\/code>, <code class=\"cw os ot ou ov b\">X=[len(data)-1,1]<\/code>).<\/p>\n<p id=\"ad52\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">y \u2014 labels <\/code>\u2014 this is the next number in the sequence that we\u2019re trying to predict.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:659\/0*3Wj-8r0_fZG5zAKm.\" alt=\"\" width=\"659\" height=\"208\"><\/figure><div class=\"ok ol qv\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*3Wj-8r0_fZG5zAKm. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*3Wj-8r0_fZG5zAKm. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*3Wj-8r0_fZG5zAKm. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*3Wj-8r0_fZG5zAKm. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*3Wj-8r0_fZG5zAKm. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*3Wj-8r0_fZG5zAKm. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1318\/0*3Wj-8r0_fZG5zAKm. 1318w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 659px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*3Wj-8r0_fZG5zAKm. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*3Wj-8r0_fZG5zAKm. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*3Wj-8r0_fZG5zAKm. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*3Wj-8r0_fZG5zAKm. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*3Wj-8r0_fZG5zAKm. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*3Wj-8r0_fZG5zAKm. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1318\/0*3Wj-8r0_fZG5zAKm. 1318w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 659px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"5b16\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Training and Testing dataset<\/strong><\/h2>\n<p id=\"59b3\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">Splitting the data into training and test sets is crucial for getting a realistic estimate of our model\u2019s performance. We have used 80% (1018) of the dataset as the training set and the remaining 20% (248) as the validation set.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:551\/0*tAOnnMb6NVWeM-jy.\" alt=\"\" width=\"551\" height=\"286\"><\/figure><div class=\"ok ol qw\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*tAOnnMb6NVWeM-jy. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*tAOnnMb6NVWeM-jy. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*tAOnnMb6NVWeM-jy. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*tAOnnMb6NVWeM-jy. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*tAOnnMb6NVWeM-jy. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*tAOnnMb6NVWeM-jy. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1102\/0*tAOnnMb6NVWeM-jy. 1102w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 551px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*tAOnnMb6NVWeM-jy. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*tAOnnMb6NVWeM-jy. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*tAOnnMb6NVWeM-jy. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*tAOnnMb6NVWeM-jy. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*tAOnnMb6NVWeM-jy. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*tAOnnMb6NVWeM-jy. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1102\/0*tAOnnMb6NVWeM-jy. 1102w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 551px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h1 id=\"67c2\" class=\"ng nh fs be ni nj nk nl nm nn no np nq nr ns nt nu nv nw nx ny nz oa ob oc od bj\" data-selectable-paragraph=\"\">Defining the network<\/h1>\n<h2 id=\"27ba\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Hyperparameters<\/strong><\/h2>\n<p id=\"5bb0\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\"><a class=\"af nf\" href=\"https:\/\/heartbeat.comet.ml\/tuning-machine-learning-hyperparameters-40265a35c9b8\" target=\"_blank\" rel=\"noopener ugc nofollow\">Hyperparameters <\/a>explain higher-level structural information about a model.<\/p>\n<p id=\"2592\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">batch_size<\/code> \u2014 This is the number of windows of data we are passing at once.<\/p>\n<p id=\"15c5\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">window_size<\/code> \u2014 The number of days we consider to predict the bitcoin price for our case.<\/p>\n<p id=\"526f\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">hidden_layers <\/code>\u2014 This is the number of units we use in our LSTM cell.<\/p>\n<p id=\"f170\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">clip_margin<\/code> \u2014 This is to prevent exploding the gradient \u2014 we use clipper to clip gradients below above this margin.<\/p>\n<p id=\"f7bf\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">learning_rate<\/code> \u2014 This is a an optimization method that aims to reduce the loss function.<\/p>\n<p id=\"ca1d\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\">epochs <\/code>\u2014 This is the number of iterations (forward and back propagation) our model needs to make.<\/p>\n<p id=\"c53d\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">There are a variety of hyperparameters that you can customize for your model, but for our example let\u2019s stick to those we\u2019ve defined.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:426\/0*90OvrzFz9R_K8RNZ.\" alt=\"\" width=\"426\" height=\"147\"><\/figure><div class=\"ok ol qx\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*90OvrzFz9R_K8RNZ. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*90OvrzFz9R_K8RNZ. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*90OvrzFz9R_K8RNZ. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*90OvrzFz9R_K8RNZ. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*90OvrzFz9R_K8RNZ. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*90OvrzFz9R_K8RNZ. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:852\/0*90OvrzFz9R_K8RNZ. 852w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 426px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*90OvrzFz9R_K8RNZ. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*90OvrzFz9R_K8RNZ. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*90OvrzFz9R_K8RNZ. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*90OvrzFz9R_K8RNZ. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*90OvrzFz9R_K8RNZ. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*90OvrzFz9R_K8RNZ. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:852\/0*90OvrzFz9R_K8RNZ. 852w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 426px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"1450\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Placeholders<\/strong><\/h2>\n<p id=\"0d31\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">Placeholders allows us to send different data within our network with the <code class=\"cw os ot ou ov b\">tf.placeholder()<\/code> command.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:650\/0*wW_9D5XG_iY1VesO.\" alt=\"\" width=\"650\" height=\"88\"><\/figure><div class=\"ok ol qy\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*wW_9D5XG_iY1VesO. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*wW_9D5XG_iY1VesO. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*wW_9D5XG_iY1VesO. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*wW_9D5XG_iY1VesO. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*wW_9D5XG_iY1VesO. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*wW_9D5XG_iY1VesO. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1300\/0*wW_9D5XG_iY1VesO. 1300w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 650px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*wW_9D5XG_iY1VesO. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*wW_9D5XG_iY1VesO. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*wW_9D5XG_iY1VesO. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*wW_9D5XG_iY1VesO. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*wW_9D5XG_iY1VesO. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*wW_9D5XG_iY1VesO. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1300\/0*wW_9D5XG_iY1VesO. 1300w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 650px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"adc3\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">LSTM Weights<\/strong><\/h2>\n<p id=\"a2c9\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">LSTM weights are determined by Operation Gates which include: Forget, Input and Output gates.<\/p>\n<h2 id=\"5f9a\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Forget Gate<\/strong><\/h2>\n<p id=\"d759\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\"><strong class=\"be oj\">ft =\u03c3(Wf[ht-1,Xt]+bf)<\/strong><\/code><\/p>\n<p id=\"5b7f\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">This is a sigmoid layer that takes the output at <code class=\"cw os ot ou ov b\">t-1<\/code> and the current input at time <code class=\"cw os ot ou ov b\">t <\/code>and then combines them into a single tensor. It then applies linear transformation followed by a sigmoid.<\/p>\n<p id=\"1474\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">The output of the gate is between 0 and 1 due to the sigmoid. This number is then multiplied with the internal state, and that is why the gate is called forget gate. If <code class=\"cw os ot ou ov b\">ft =0<\/code> ,then the previous internal state is completely forgotten, while if <code class=\"cw os ot ou ov b\">ft =1<\/code>, it will be passed unaltered.<\/p>\n<h2 id=\"47cd\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Input Gate<\/strong><\/h2>\n<p id=\"9c7c\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\"><strong class=\"be oj\">it=\u03c3(Wi[ht-1,Xt]+bi)<\/strong><\/code><\/p>\n<p id=\"622f\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">This state takes the previous output together with the new input and passes them through another sigmoid layer. This gate returns a value between 0 and 1. The value of the input gate is then multiplied with the output of the candidate layer.<\/p>\n<p id=\"43a9\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\"><strong class=\"be oj\">Ct=tanh(Wi[ht-1,Xt]+bi)<\/strong><\/code><\/p>\n<p id=\"f605\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">This layer applies hyperbolic tangent to the mix of the input and previous output, returning the candidate vector. The candidate vector is then added to the internal state, which is updated with this rule:<\/p>\n<p id=\"5462\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\"><strong class=\"be oj\">Ct=ft *Ct-1+it*Ct<\/strong><\/code><\/p>\n<p id=\"1ef0\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">The previous state is multiplied by the forget gate, and then added to the fraction of the new candidate allowed by the output gate.<\/p>\n<h2 id=\"af87\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Output Gate<\/strong><\/h2>\n<p id=\"8933\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\"><strong class=\"be oj\">Ot=\u03c3(Wo[ht-1,Xt]+bo)<\/strong><\/code><\/p>\n<p id=\"6ffc\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><code class=\"cw os ot ou ov b\"><strong class=\"be oj\">ht=Ot*tanh Ct<\/strong><\/code><\/p>\n<p id=\"a9f7\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">This gate controls how much of the internal state is passed to the output and works in a similar manner to the other gates.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*XWgQKNGtrmLdoeh_.\" alt=\"\" width=\"700\" height=\"355\"><\/figure><div class=\"ok ol qz\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*XWgQKNGtrmLdoeh_. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*XWgQKNGtrmLdoeh_. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*XWgQKNGtrmLdoeh_. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*XWgQKNGtrmLdoeh_. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*XWgQKNGtrmLdoeh_. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*XWgQKNGtrmLdoeh_. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*XWgQKNGtrmLdoeh_. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*XWgQKNGtrmLdoeh_. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*XWgQKNGtrmLdoeh_. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*XWgQKNGtrmLdoeh_. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*XWgQKNGtrmLdoeh_. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*XWgQKNGtrmLdoeh_. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*XWgQKNGtrmLdoeh_. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*XWgQKNGtrmLdoeh_. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h1 id=\"8bd2\" class=\"ng nh fs be ni nj nk nl nm nn no np nq nr ns nt nu nv nw nx ny nz oa ob oc od bj\" data-selectable-paragraph=\"\">Network loop<\/h1>\n<p id=\"aca5\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">A loop for the network is created which iterates through every window in the batch creating the <code class=\"cw os ot ou ov b\">batch_states<\/code> as all zeros .The output is the used for predicting the bitcoin price.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*wBuBHc4Sc3R9JnM0.\" alt=\"\" width=\"700\" height=\"251\"><\/figure><div class=\"ok ol ra\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*wBuBHc4Sc3R9JnM0. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*wBuBHc4Sc3R9JnM0. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*wBuBHc4Sc3R9JnM0. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*wBuBHc4Sc3R9JnM0. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*wBuBHc4Sc3R9JnM0. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*wBuBHc4Sc3R9JnM0. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*wBuBHc4Sc3R9JnM0. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*wBuBHc4Sc3R9JnM0. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*wBuBHc4Sc3R9JnM0. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*wBuBHc4Sc3R9JnM0. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*wBuBHc4Sc3R9JnM0. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*wBuBHc4Sc3R9JnM0. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*wBuBHc4Sc3R9JnM0. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*wBuBHc4Sc3R9JnM0. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h2 id=\"3f81\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Defining the loss<\/strong><\/h2>\n<p id=\"3dc3\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">Here we will use the <code class=\"cw os ot ou ov b\">mean_squared_error<\/code> function for the loss to minimize the errors.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*Vgmr2vA2YwdyjvZ9.\" alt=\"\" width=\"700\" height=\"106\"><\/figure><div class=\"ok ol rb\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*Vgmr2vA2YwdyjvZ9. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*Vgmr2vA2YwdyjvZ9. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*Vgmr2vA2YwdyjvZ9. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*Vgmr2vA2YwdyjvZ9. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*Vgmr2vA2YwdyjvZ9. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*Vgmr2vA2YwdyjvZ9. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*Vgmr2vA2YwdyjvZ9. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*Vgmr2vA2YwdyjvZ9. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*Vgmr2vA2YwdyjvZ9. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*Vgmr2vA2YwdyjvZ9. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*Vgmr2vA2YwdyjvZ9. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*Vgmr2vA2YwdyjvZ9. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*Vgmr2vA2YwdyjvZ9. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*Vgmr2vA2YwdyjvZ9. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h2 id=\"4779\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Training the network<\/strong><\/h2>\n<p id=\"1704\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">We now train the network with the number of epochs (200), which we had initialized, and then observe the change in our loss through time. The current loss decreases with the increase in the epochs as observed, increasing our model accuracy in predicting the bitcoin prices.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*P4ll-BB3uSW89q-q.\" alt=\"\" width=\"700\" height=\"338\"><\/figure><div class=\"ok ol rc\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*P4ll-BB3uSW89q-q. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*P4ll-BB3uSW89q-q. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*P4ll-BB3uSW89q-q. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*P4ll-BB3uSW89q-q. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*P4ll-BB3uSW89q-q. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*P4ll-BB3uSW89q-q. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*P4ll-BB3uSW89q-q. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*P4ll-BB3uSW89q-q. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*P4ll-BB3uSW89q-q. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*P4ll-BB3uSW89q-q. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*P4ll-BB3uSW89q-q. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*P4ll-BB3uSW89q-q. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*P4ll-BB3uSW89q-q. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*P4ll-BB3uSW89q-q. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h1 id=\"d290\" class=\"ng nh fs be ni nj nk nl nm nn no np nq nr ns nt nu nv nw nx ny nz oa ob oc od bj\" data-selectable-paragraph=\"\">Plotting the predictions<\/h1>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:573\/0*N_l3wQLCjWbJXO4b.\" alt=\"\" width=\"573\" height=\"167\"><\/figure><div class=\"ok ol rd\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*N_l3wQLCjWbJXO4b. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*N_l3wQLCjWbJXO4b. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*N_l3wQLCjWbJXO4b. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*N_l3wQLCjWbJXO4b. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*N_l3wQLCjWbJXO4b. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*N_l3wQLCjWbJXO4b. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1146\/0*N_l3wQLCjWbJXO4b. 1146w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 573px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*N_l3wQLCjWbJXO4b. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*N_l3wQLCjWbJXO4b. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*N_l3wQLCjWbJXO4b. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*N_l3wQLCjWbJXO4b. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*N_l3wQLCjWbJXO4b. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*N_l3wQLCjWbJXO4b. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1146\/0*N_l3wQLCjWbJXO4b. 1146w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 573px\" data-testid=\"og\"><\/picture><\/div>\n<\/figure>\n<h2 id=\"fece\" class=\"py nh fs be ni pz qa qb nm qc qd qe nq mh qf qg qh ml qi qj qk mp ql qm qn qo bj\" data-selectable-paragraph=\"\"><strong class=\"al\">Output<\/strong><\/h2>\n<p id=\"a999\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">Our model has been able to accurately predict the bitcoin prices based on the original data by implementing LSTMs cells. We could improve the model performance by reducing the window length from 7 days to 3 days. You can tweak the full code to optimize your model performance.<\/p>\n<figure class=\"on oo op oq or fm ok ol paragraph-image\">\n<div class=\"ox oy eb oz bg pa\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg fo fp c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/0*tx0qokTwxHdor2VJ.\" alt=\"\" width=\"700\" height=\"324\"><\/figure><div class=\"ok ol re\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*tx0qokTwxHdor2VJ. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*tx0qokTwxHdor2VJ. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*tx0qokTwxHdor2VJ. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*tx0qokTwxHdor2VJ. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*tx0qokTwxHdor2VJ. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*tx0qokTwxHdor2VJ. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*tx0qokTwxHdor2VJ. 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/0*tx0qokTwxHdor2VJ. 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/0*tx0qokTwxHdor2VJ. 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/0*tx0qokTwxHdor2VJ. 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/0*tx0qokTwxHdor2VJ. 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/0*tx0qokTwxHdor2VJ. 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/0*tx0qokTwxHdor2VJ. 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/0*tx0qokTwxHdor2VJ. 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<\/figure>\n<h1 id=\"45b2\" class=\"ng nh fs be ni nj nk nl nm nn no np nq nr ns nt nu nv nw nx ny nz oa ob oc od bj\" data-selectable-paragraph=\"\">Conclusion.<\/h1>\n<p id=\"c6d4\" class=\"pw-post-body-paragraph lx ly fs be b lz oe mb mc md of mf mg mh og mj mk ml oh mn mo mp oi mr ms mt fh bj\" data-selectable-paragraph=\"\">I was inspired by this <a class=\"af nf\" href=\"https:\/\/dashee87.github.io\/deep%20learning\/python\/predicting-cryptocurrency-prices-with-deep-learning\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">blog<\/a> on using LSTMs for time series analysis. I would recommend it to gain more insights. Also check out <a class=\"af nf\" href=\"https:\/\/heartbeat.comet.ml\/using-a-keras-long-shortterm-memory-lstm-model-to-predict-stock-prices-a08c9f69aa74\" target=\"_blank\" rel=\"noopener ugc nofollow\">this look<\/a> at using an LSTM as a foundation for predicting stock prices over time<\/p>\n<p id=\"aa38\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\">I hope this article has given you a head start in understanding LSTMs. Feel free to comment, clap, and share.<\/p>\n<p id=\"0438\" class=\"pw-post-body-paragraph lx ly fs be b lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms mt fh bj\" data-selectable-paragraph=\"\"><strong class=\"be oj\">Discuss this post on <\/strong><a class=\"af nf\" href=\"https:\/\/news.ycombinator.com\/item?id=17179737\" target=\"_blank\" rel=\"noopener ugc nofollow\"><strong class=\"be oj\">Hacker News.<\/strong><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Human thoughts are persistent, and this enables us to understand patterns, which in turn gives us the ability to predict the next sequence of actions. Your understanding of this article will be based on the previous words that you\u2019ve read. Recurrent Neural Networks replicate this concept. RNNs are a type of artificial neural network that [&hellip;]<\/p>\n","protected":false},"author":71,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"coauthors":[169],"class_list":["post-7063","post","type-post","status-publish","format-standard","hentry","category-machine-learning"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM) - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM)\" \/>\n<meta property=\"og:description\" content=\"Human thoughts are persistent, and this enables us to understand patterns, which in turn gives us the ability to predict the next sequence of actions. Your understanding of this article will be based on the previous words that you\u2019ve read. Recurrent Neural Networks replicate this concept. RNNs are a type of artificial neural network that [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-08T22:58:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:14:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png\" \/>\n<meta name=\"author\" content=\"Brian Mwangi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brian Mwangi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM) - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/","og_locale":"en_US","og_type":"article","og_title":"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM)","og_description":"Human thoughts are persistent, and this enables us to understand patterns, which in turn gives us the ability to predict the next sequence of actions. Your understanding of this article will be based on the previous words that you\u2019ve read. Recurrent Neural Networks replicate this concept. RNNs are a type of artificial neural network that [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-08-08T22:58:02+00:00","article_modified_time":"2025-04-24T17:14:52+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png","type":"","width":"","height":""}],"author":"Brian Mwangi","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Brian Mwangi","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/"},"author":{"name":"Brian Mwangi","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/11e566851d7c80669e55ab10e6191a68"},"headline":"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM)","datePublished":"2023-08-08T22:58:02+00:00","dateModified":"2025-04-24T17:14:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/"},"wordCount":1470,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png","articleSection":["Machine Learning"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/","url":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/","name":"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM) - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png","datePublished":"2023-08-08T22:58:02+00:00","dateModified":"2025-04-24T17:14:52+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:1500\/1*oqgA65tVM9LgfSm-J866dQ.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/an-intro-tutorial-for-implementing-long-short-term-memory-networks-lstm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"An Intro Tutorial for Implementing Long Short-Term Memory Networks (LSTM)"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/11e566851d7c80669e55ab10e6191a68","name":"Brian Mwangi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/d5b4daae2a6d50ff0f7f08cf468e8eec","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1632239369714-96x96.jpg","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1632239369714-96x96.jpg","caption":"Brian Mwangi"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/brynmwangygmail-com\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=7063"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7063\/revisions"}],"predecessor-version":[{"id":15585,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/7063\/revisions\/15585"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=7063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=7063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=7063"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=7063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}