<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sagar</title>
    <description>The latest articles on DEV Community by Sagar (@sagarmainkar).</description>
    <link>https://dev.to/sagarmainkar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319600%2F6f75de34-461c-4e5d-a4ab-2ca6330b1251.jpeg</url>
      <title>DEV Community: Sagar</title>
      <link>https://dev.to/sagarmainkar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagarmainkar"/>
    <language>en</language>
    <item>
      <title>Forecasting in Browser with TensorFlow.js</title>
      <dc:creator>Sagar</dc:creator>
      <pubDate>Sun, 03 May 2020 13:40:16 +0000</pubDate>
      <link>https://dev.to/sagarmainkar/forecasting-in-browser-with-tensorflow-js-2a63</link>
      <guid>https://dev.to/sagarmainkar/forecasting-in-browser-with-tensorflow-js-2a63</guid>
      <description>&lt;p&gt;If you are a javascript developer and interested in doing machine learning or artificial intelligence then the first question that may have crossed your mind is do I need to learn Python or R to get started?&lt;br&gt;
Well most often than not the obvious answer is yes python is best suited for AI and you can build on online jupyter notebooks on google colab etc. But wait you can do the same in javascript yes not a alternative for very large datasets and if your want to run models on gigabytes of data. Still, you can tickle your curiosity. In this article I am going to demonstrate use of &lt;a href="https://www.tensorflow.org/js"&gt;tensorflow.js&lt;/a&gt; a google library for doing AI in browser using javascript.&lt;br&gt;
This example is meant to explain how we can do AI on a simple time series data and not a comprehensive tutorial of getting a best model for time series. Also, I have used ReactJS for building this article but I am not a React expert.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is a time series
&lt;/h1&gt;

&lt;p&gt;First what is a time series data? Any data with a time dimension, example data collected at particular intervals of time forms an &lt;strong&gt;time series&lt;/strong&gt;.&lt;br&gt;
Well, the data can be collected at a non regular interval and later be binned into regular interval in a data pre process step but that is beyond the scope of this article. We assume that data is binned and the data is available at a regular time cadence.&lt;/p&gt;
&lt;h1&gt;
  
  
  Data Processing
&lt;/h1&gt;

&lt;p&gt;We are going to consider a univariate time series i.e. no other variable in the model but only the dependent variable (the one we want to forecast). So a naive question is how can our model predict without independent (input) variables/features? We are going to do a small feature engineering with our data.&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e0ep5jnK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e18iv1gx3kob6acgc86x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e0ep5jnK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e18iv1gx3kob6acgc86x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
But before that we need to first build an application where user can select our time variable and the variable they need to forecast (dependent variable). A simple UI wizard will drive this process. &lt;br&gt;
For purpose of preparing the data and having it in matrix form similar to pandas in python I am using a npm dependency &lt;a href="https://www.npmjs.com/package/dataframe-js"&gt;dataframe.js&lt;/a&gt;. This allows to manipulate data in columns and rows, query etc and even load data easily.&lt;/p&gt;
&lt;h1&gt;
  
  
  Feature Engineering
&lt;/h1&gt;

&lt;p&gt;Once time and dependent features are selected we need to do a feature engineering by generating independent variables. The question is how? Well, we are going to use a simple technique called &lt;a href="https://math.stackexchange.com/questions/2548314/what-is-lag-in-a-time-series"&gt;lag&lt;/a&gt;. The concept of lag is that we assume that the current point is correlated to a previous time point and this relationship is called &lt;a href="https://otexts.com/fpp2/autocorrelation.html"&gt;autocorrelation&lt;/a&gt;. What we are saying is that today's stock price is correlated to previous 6 days stock price. The value 6 here is called 6 lags. We obviously do not know this value and hence it is one hyper parameter for our model, meaning by varying this value we can see how our model performs. Once we get this value from user we split the time series into sequences of no of lags. Example assuming 3 lags we virtually generate a table of 4 columns please check the image below. The LHS shows the actual data and RHS shows split sequences. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1MaWTfHE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5wzia3iyfci353icam5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1MaWTfHE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5wzia3iyfci353icam5t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Hyper Parameters
&lt;/h1&gt;

&lt;p&gt;We are just going to consider simple hyper parameters "Number of  lags" and "Epochs" , one epoch is when the model is trained once over the training data.&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zskYfag3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/onl78169ahxp8deopli9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zskYfag3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/onl78169ahxp8deopli9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Model Training
&lt;/h1&gt;

&lt;p&gt;We are going to train our model which a two layered model. First layer is &lt;a href="https://en.wikipedia.org/wiki/Long_short-term_memory"&gt;LSTM (Long Short Term Memory)&lt;/a&gt; model with 50 units. The number of units can also be a hyper parameter but to keep things simple it is hardcoded. If you do not know what is LSTM do not worry much. It is a form of complex &lt;a href="https://en.wikipedia.org/wiki/Recurrent_neural_network"&gt;RNN (Recurrent Neural Network)&lt;/a&gt; model used to model sequential data like time series of language data.&lt;/p&gt;

&lt;p&gt;The RNN structure looks like &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6P4t2yFx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xlpimes2b55vcbchokja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6P4t2yFx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xlpimes2b55vcbchokja.png" alt="RNN"&gt;&lt;/a&gt;&lt;br&gt;
Image Courtesy: &lt;a href="//commons.wikimedia.org/wiki/User:Ixnay" title="User:Ixnay"&gt;fdeloche&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;While an LSTM looks like &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oRD8xyrm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hjsb7ydoiyc7xnj2191w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oRD8xyrm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hjsb7ydoiyc7xnj2191w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Image Courtesy: &lt;a href="//commons.wikimedia.org/w/index.php?title=User:GChe&amp;amp;action=edit&amp;amp;redlink=1" class="new" title="User:GChe (page does not exist)"&gt;Guillaume Chevalier&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't worry much about the model simply understand it is two layer model with first layer being LSTM with 50 units with activation as &lt;strong&gt;"Relu"&lt;/strong&gt; &lt;br&gt;
The second layer is simple dense layer with one unit and since our model will output number it is a regression model with &lt;a href="https://en.wikipedia.org/wiki/Loss_function"&gt;loss function&lt;/a&gt; as &lt;a href="https://en.wikipedia.org/wiki/Mean_squared_error"&gt;Mean Square Error&lt;/a&gt;&lt;br&gt;
Here is what the JS code for model building looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--51Ske5iA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uswj012vmzjjnypozn0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--51Ske5iA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uswj012vmzjjnypozn0i.png" alt="Model Building and Training"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once a model is built we can check the loss over epochs and see that we are really minimising loss.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AsnijTs3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zvn9o3edl3dkxygehrn3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AsnijTs3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zvn9o3edl3dkxygehrn3.png" alt="Training Losses"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Predicting
&lt;/h1&gt;

&lt;p&gt;Final step is predicting with the model and comparing it with actual series to see how the model predicts.&lt;br&gt;
The prediction code is very simple, we just take original series and run it through model with predict function.&lt;br&gt;
Off course for everything we need to convert the values to tensors.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--urfltbgT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9rwhrt94vusainbqttsy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--urfltbgT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9rwhrt94vusainbqttsy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then compare the predictions with actuals&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3iwzILYZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9le9yndtyaumrn7tvhme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3iwzILYZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9le9yndtyaumrn7tvhme.png" alt="Actual vs Prediction"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If this article generates some curiosity in you then feel free to check out the entire code &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/timeseries-ai-material-xx98t"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Feel free to fork it on GitHub and try digging deeper in the code.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vWogaON8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-28d89282e0daa1e2496205e2f218a44c755b0dd6536bbadf5ed5a44a7ca54716.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/sagarmainkar"&gt;
        sagarmainkar
      &lt;/a&gt; / &lt;a href="https://github.com/sagarmainkar/timeseries-ai"&gt;
        timeseries-ai
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Created with CodeSandbox
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>react</category>
      <category>javascript</category>
      <category>machinelearning</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
