<?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>Forem: Pulkit Kashyap</title>
    <description>The latest articles on Forem by Pulkit Kashyap (@kpulkit29).</description>
    <link>https://forem.com/kpulkit29</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%2F210258%2F98394fab-3cb6-40bf-bd90-0b485020f36b.JPG</url>
      <title>Forem: Pulkit Kashyap</title>
      <link>https://forem.com/kpulkit29</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kpulkit29"/>
    <language>en</language>
    <item>
      <title>Tensorflow.js: Building a quick and dirty stock market predictor</title>
      <dc:creator>Pulkit Kashyap</dc:creator>
      <pubDate>Tue, 08 Mar 2022 17:13:39 +0000</pubDate>
      <link>https://forem.com/kpulkit29/tensorflowjs-building-a-quick-and-dirty-stock-market-predictor-2d4f</link>
      <guid>https://forem.com/kpulkit29/tensorflowjs-building-a-quick-and-dirty-stock-market-predictor-2d4f</guid>
      <description>&lt;p&gt;When you think of Machine Learning, the first thing that strikes you is Python. Well, great community support and plenty of available packages make Python a great choice. But, while going through &lt;a href="https://www.coursera.org/instructor/andrewng" rel="noopener noreferrer"&gt;Andrew Ng's&lt;/a&gt; ML course I realized that &lt;strong&gt;Machine Learning&lt;/strong&gt; is about how you define your model and not about the programming language being used. So I thought, why not give &lt;strong&gt;Tensorflow.js&lt;/strong&gt; a try.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building a quick and dirty stock market predictor using Tensorflow.js
&lt;/h3&gt;

&lt;p&gt;I'll be using ICICI bank's dataset to predict the closing price based on the provided opening price. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The data is the price history and trading volumes of the ICICI bank stock. The data spans from 1st January 2000 to 30th April 2021.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Checkout &lt;a href="https://www.kaggle.com/rohanrao/nifty50-stock-market-data?select=ICICIBANK.csv" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt; for various datasets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing a model
&lt;/h3&gt;

&lt;p&gt;Let's have a look at the first 1000 values of the dataset using a scatter plot.&lt;/p&gt;

&lt;p&gt;Plotting the open price against the closing price&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl46owhtvfnp3lcymtg7j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl46owhtvfnp3lcymtg7j.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now by looking at the data, we can see that if we define a line of best fit then we establish a relation between the opening and the closing price.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbleu2lg3cql5xc6i79q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbleu2lg3cql5xc6i79q.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Does this ring any bells? Remember the equation of straight line we studied in high school? &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

y = mx + c

m -&amp;gt; slope of the line
c -&amp;gt; y intercept


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And this is exactly what simple linear regression ML models use. It is a statistical model which is used to define a relationship between two variables. The independent variable &lt;strong&gt;x&lt;/strong&gt; is used to predict the value of the dependent variable &lt;strong&gt;y&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In ML terminology this equation is called the hypothesis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now the ICICI bank stock dataset has two columns named Open &amp;amp; Close and contains more than 1000 rows. So instead of adding/operating on these values one by one, they are generally represented in the form of a matrix&lt;/strong&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  Understanding the cost function
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Cost function (sometimes also called an error function)is a function that maps an event or values of one or more variables onto a real number intuitively representing some "cost" associated with the event. An optimization problem seeks to minimize a loss function. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source Wikipedia&lt;/p&gt;

&lt;p&gt;In other words, it is the difference between the value that the hypothesis function spits out and the actual value. Since we are looking to find the line of best fit, the &lt;strong&gt;aim is to minimize the cost&lt;/strong&gt;. We want our predicted value to be very close to the actual value while the model is being compiled.&lt;/p&gt;

&lt;p&gt;Squared error cost function used for linear regression&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2u8vbjwo1602j8g23rx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2u8vbjwo1602j8g23rx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source &lt;a href="https://medium.com/@lachlanmiller_52885/understanding-and-calculating-the-cost-function-for-linear-regression-39b8a3519fcb" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's have a glance at the hypothesis function &lt;/p&gt;

&lt;p&gt;x -&amp;gt; This will be the opening price (Nx1 matrix)&lt;br&gt;
m,c -&amp;gt; Their value is chosen to minimize the cost function. Let's park the explanation part for now.&lt;/p&gt;

&lt;p&gt;In the world of Tensorflow.js these matrices are called tensors. You can read more about them &lt;a href="https://js.tensorflow.org/api/3.13.0/#sub" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting things ready
&lt;/h3&gt;

&lt;p&gt;Add the below mentioned script tags to your HTML file to ensure that Tensorflow and tfjs-vis (used for visualization) are available on your page.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

 &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Loading the csv file and plotting the values on Scatter plot
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;We are using tfvis here to plot our dataset.&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;points&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;predictedPoints&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;values&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;points&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="nx"&gt;predictedPoints&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;predictedPoints&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[])],&lt;/span&gt;
        &lt;span class="na"&gt;series&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;original&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="nx"&gt;predictedPoints&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prediction&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[])]&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;surface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ICICI Bank stock price prediction&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;tfvis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scatterplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;surface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;xLabel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Open&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;yLabel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Close&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;            
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// All the tensorflow utility functions can be &lt;/span&gt;
&lt;span class="nx"&gt;accessed&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;// File path can be changed&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dataset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:4000/ICICIBANK.csv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;points&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
       &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Close&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pointsArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;points&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointsArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;pointsArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cm"&gt;/**
* Shuffling the data set so that our model does not 
* encounter similar values in each step
* */&lt;/span&gt;
&lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shuffle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointsArr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointsArr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now the price values can be in different ranges, so it becomes really important to bring the values on a common scale. This process is also called normalization. Typically you would want to bring the values in the range 0-1.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="cm"&gt;/**
 * Normalize the tensor
* */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevMin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevMax&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prevMin&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
         &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prevMax&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
         &lt;span class="nx"&gt;normalisedTensor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;div&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;normalisedTensor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
* Denormalize the tensor
* */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;denormalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Defining the feature and output tensor
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;featureTensor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor2d&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;features&lt;/span&gt;&lt;span class="p"&gt;,[&lt;/span&gt;&lt;span class="nx"&gt;features&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;outputTensor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor2d&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;normalisedFeatures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;featureTensor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;normalisedOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outputTensor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Splitting the datasets into training and testing
&lt;/h3&gt;

&lt;p&gt;Why is splitting required?&lt;br&gt;
Splitting ensures that our model is built using a specific set of data so that when we evaluate the model against the test data it is actually evaluated against something it has never encountered during the creation phase. It also gives you a sense of how it might perform in production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generally 70% of the data is reserved for training&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don't find the reasoning very intuitive, I would highly recommend reading this &lt;a href="https://blog.roboflow.com/train-test-split/" rel="noopener noreferrer"&gt;blog&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;trainFeatures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;testFeatures&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedFeatures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;trainOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;testOuput&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Creating a model
&lt;/h3&gt;

&lt;p&gt;We'll use the Tensorflow layers API to create the model.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sequential&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;layers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dense&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;units&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;inputDim&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;activation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;linear&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;useBias&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;

    &lt;span class="c1"&gt;// sgd -&amp;gt; gradient descend&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;optimizer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;train&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sgd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;loss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meanSquaredError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;optimizer&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createModel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;tf.sequential() - This means that the model will be sequential i.e output of one layer will act as an input to the other.&lt;/li&gt;
&lt;li&gt;units - Our model has one unit.&lt;/li&gt;
&lt;li&gt;inputDim - input dimension is 1 as we have only one feature which is the opening price&lt;/li&gt;
&lt;li&gt;activation - We are using linear regression here, so using linear activation function here.&lt;/li&gt;
&lt;li&gt;useBias - 'c' in our hypothesis function is called the bias term&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, the point that is a little unclear here is &lt;strong&gt;tf.train.sgd&lt;/strong&gt;. Remember that we parked the explanation part for m,c previously. Gradient descend is the algorithm that tries to find the minimum value for these terms to minimize the loss(happens at every iteration). Read more about it &lt;a href="https://machinelearningmastery.com/learning-rate-for-deep-learning-neural-networks/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. It takes in a learning rate to find the step of descent. &lt;strong&gt;A traditional default value for the learning rate is 0.1 or 0.01, and this may represent a good starting point on your problem.&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As mentioned earlier our cost(or loss) function will be a squared error function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Evaluating the model against the test set
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

let testing =  await model.evaluate(testFeatures, testOuput);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Predicting the values and plotting them
&lt;/h3&gt;

&lt;p&gt;using tfvis to create a scatterplot&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;plotPrediction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;normalisedXs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedXs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;normalisedXs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;normalisedXs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor2d&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedXs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;normalisedYs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedXs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;denormalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedXs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;featureTensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;featureTensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;dataSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;denormalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalisedYs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outputTensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;outputTensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;dataSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;predictedPoints&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xs&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointsArr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;predictedPoints&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's see how the scatter plot looks like for our predicted values&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvg6z2h1zf6h80o0s0pd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvg6z2h1zf6h80o0s0pd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, there are a couple of things that I didn't mention like saving the model, loading the model from storage, etc. But you can find the complete code in this &lt;a href="https://github.com/kpulkit29/stock-predict" rel="noopener noreferrer"&gt;Github Repo&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A question for the readers
&lt;/h3&gt;

&lt;p&gt;So, if you run this code locally and plot the original and predicted values on the scatter plot, you will notice that every predicted closing price is less than its corresponding opening price. I am not quite sure as to what is causing this issue. Maybe, I'll try tinkering around with the learning rate.&lt;/p&gt;

&lt;p&gt;Let me know if you catch the issue 🙏.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>machinelearning</category>
      <category>tensorflow</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Browser and React onChange  event: The conundrum</title>
      <dc:creator>Pulkit Kashyap</dc:creator>
      <pubDate>Sun, 01 Aug 2021 17:24:20 +0000</pubDate>
      <link>https://forem.com/kpulkit29/browser-and-react-onchange-event-the-conundrum-4ke7</link>
      <guid>https://forem.com/kpulkit29/browser-and-react-onchange-event-the-conundrum-4ke7</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Sometimes I think we all are so inclined towards Javascript frameworks or libraries that we don't pay attention to how things work natively. Recently when I was debugging an issue about input tag's &lt;strong&gt;onchange&lt;/strong&gt; event, I was startled when the callback function was not being called on changing the input value. Well, React triggers onChange whenever one changes the input value. Coming from React I just assumed that such things would work the same in vanilla Javascript 😔 😔 . &lt;strong&gt;The way browser fires the onchange event is different.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript onchange
&lt;/h3&gt;

&lt;p&gt;Coming from React it's easy to fall into the trap. But let's understand some events the browser fires when one interacts with the input tag&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;onfocus - Fired when the user sets focus on the element&lt;/li&gt;
&lt;li&gt;onblur - Opposite of onfocus. Fired when an element loses focus&lt;/li&gt;
&lt;li&gt;onchange - (the most interesting one 😅). Unlike React, the browser fires onchange event after focus from input element is taken off. So when focus is set on an input element and something is typed, onchange won't be fired until and unless the input element is out of focus.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When an element is out of focus, the browser assumes that the user is done making the change(probably the reason why onchange is fired late).&lt;/p&gt;

&lt;p&gt;Let's see this in action. Check out the JS part here and open your console to see what is logged.&lt;br&gt;
&lt;strong&gt;Note that onchange is fired only when input is out of focus&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/pulkit29/embed/QWvxwrW?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Question for the readers 🧐
&lt;/h3&gt;

&lt;p&gt;I am not too sure as to why in the above example onblur callback is fired after the onchange callback. We know now that onchange is fired when the element is out of focus. Know the reason?? Please comment down below.&lt;/p&gt;

&lt;h3&gt;
  
  
  React onChange
&lt;/h3&gt;

&lt;p&gt;Apart from the camel case difference the way React onChange handler works is also pretty different. It gets triggered whenever one makes a change in the input element value.&lt;br&gt;
I tried to create something like React onChange. Let's have a look (not saying that this is how it works exactly).&lt;br&gt;
&lt;iframe src="https://jsfiddle.net/kpulkit29/pg7ew9kv/31//embedded/js,html,result//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I had attached my custom onChange callback to the element.&lt;/li&gt;
&lt;li&gt;Using setter/getter to get the previously entered value and compare it with the latest one.&lt;/li&gt;
&lt;li&gt;Additionally attached a keyup event listener to get hold of the latest value&lt;/li&gt;
&lt;li&gt;Notice that the custom onChange handler gets triggered everytime a change is made.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bye Bye !! 👋👋 Hope there were takeaways.&lt;/p&gt;

&lt;h4&gt;
  
  
  Let's connect
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/pulkit291/"&gt;Linkedin&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/kpulkit29"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/kpulkit29"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>html</category>
    </item>
    <item>
      <title>Custom Validation in Materialize css</title>
      <dc:creator>Pulkit Kashyap</dc:creator>
      <pubDate>Fri, 19 Jun 2020 18:37:03 +0000</pubDate>
      <link>https://forem.com/kpulkit29/custom-validation-in-materialize-css-1p0e</link>
      <guid>https://forem.com/kpulkit29/custom-validation-in-materialize-css-1p0e</guid>
      <description>&lt;p&gt;Hey! Thanks for dropping by. Well, if you are using Materialize in your project then I believe this trick can be really helpful for you. Almost every website has some kind of form or input fields the user interacts with. And most of the fields have some validation logic attached to them. Think of a case where you want the user to enter a unique username as the input. But when this username is not unique we immediately notify the user by displaying a warning for the same.&lt;/p&gt;

&lt;p&gt;I had a similar use case and I was using &lt;a href="https://materializecss.com/"&gt;Materialize&lt;/a&gt;, a CSS framework in my project. It provides you an easy way to show or customize warning messages if the input value is not in the expected format. But what if you want to perform custom validation for an input. For eg. username must not contain  characters like @, { etc. Unfortunately the documentation does not mention any such scenarios.&lt;/p&gt;

&lt;p&gt;So just like any other developer I googled that immediately. But I could not find any useful resource. After digging a bit on how Materialize manages to show the warning messages, I came out with a solution(a little hackish 😅). Basically Materialize adds valid class to the input to show succes message and vice-versa. Accordingly it displays the helper text. Here is a quick demo -&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/pulkit29/embed/ZEQLjqa?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The above code checks if username contains [@.;:] and shows message accordingly. I attached event listeners for fetching the current value and toggled classes accordingly.&lt;/p&gt;

&lt;p&gt;Hope this helped.&lt;/p&gt;

&lt;p&gt;Find me on my social media handles 🤟.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>css</category>
      <category>materialize</category>
    </item>
    <item>
      <title>My Open Source Experience With Fossasia</title>
      <dc:creator>Pulkit Kashyap</dc:creator>
      <pubDate>Tue, 12 Nov 2019 10:23:42 +0000</pubDate>
      <link>https://forem.com/kpulkit29/my-open-source-experience-with-fossasia-27o</link>
      <guid>https://forem.com/kpulkit29/my-open-source-experience-with-fossasia-27o</guid>
      <description>&lt;p&gt;So finally with this post I have decided to pen down my experience of contributing to one of the biggest open source communities which is &lt;a href="https://fossasia.com/"&gt;Fossasia&lt;/a&gt;. Well, I always wanted to contribute to open source projects but always held back thinking it would be difficult to get used to the huge code base they generally have and to sync up with others contributing. Finally I started out with one of the Fossasia projects and I must say it has been a great learning curve. I would be lying if I said codeheat wasn't a motivation to start looking at Fossasia repositories. For those of you who don't know what &lt;a href="https://codeheat.org/"&gt;codeheat&lt;/a&gt; is I would highly recommend checking it out. &lt;br&gt;
I feel with events like codeheat one capture the true essence of open source contribution  while still being in a healthy competitive environment. Not only you focus on your personal contributions but you learn from others you might be doing it better in certain ways. This year codeheat began in Sept'19 and would go on till Jan'20.&lt;/p&gt;

&lt;p&gt;While Fossasia has many of Github repositories but what caught my attention the most was &lt;a href="https://github.com/fossasia/susi.ai"&gt;susi.ai&lt;/a&gt;. So I decided to contribute to it for the following reasons -:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I like working with React and it uses React for front end 😅.&lt;/li&gt;
&lt;li&gt;Love the idea of a bot wherever anyone can add skills.&lt;/li&gt;
&lt;li&gt;Just a few lines of code and you can add susi.ai bot to your website.
I contribute mainly to susi_webclient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;About susi.ai:&lt;/strong&gt; SUSI.AI is an artificial intelligence system, combining pattern matching, internet data, data flow, and inference engine principles. Through some abilities to reflect, it can remember the user input to produce results. What's cool is that you can create your own skills and bot within certain lines of code it can be added to your webpage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Susi.ai web client tech stack&lt;/strong&gt; 🚀 &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSS &lt;/li&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;ReactJS&lt;/li&gt;
&lt;li&gt;Redux&lt;/li&gt;
&lt;li&gt;Material-UI&lt;/li&gt;
&lt;li&gt;styled-components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever you jump into a new codebase it is always tough to relate to it. So for starters I picked a basic UI issue to get familiar with the coding style and I must say that I had a lot to learn. Better if I list down my learnings  -:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reusability of components.&lt;/li&gt;
&lt;li&gt;Making component wrappers wherever necessary.&lt;/li&gt;
&lt;li&gt;Came to know about styled components (a great way to style your components).&lt;/li&gt;
&lt;li&gt;Best practices to use React along with Redux.&lt;/li&gt;
&lt;li&gt;Creating architecture in a such a way that adding any new feature is less of a pain and more of fun.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first major issue that I picked was of converting promises to async/await. This really helped me to familiarise myself with architecture.&lt;br&gt;
Though it took my pull request a while to be finally merged, but it feels very good when your contributions are accepted. I have been a regular contributor since then.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My thoughts on getting started with Fossasia&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To start off explore all the projects and find out the one which you think resonates with your interests. It is important to communicate with others contributing or wanting to contribute. So make sure you join the Gitter channels. If you are a beginner to open source then there are a lot of beginner-friendly issues, try them out. I feel sometimes looking at some other person's PR might also help. Before picking up an issue knowing exactly what it expects is essential as it would save a lot of time. Last but not the least go through the Readme and Coding guidelines to avoid making unnecessary errors.&lt;/p&gt;

&lt;p&gt;Well, Codeheat'19 is almost halfway through and it has been a great experience so far. Looking forward to some more contributions ahead.&lt;/p&gt;

&lt;p&gt;Keep contributing 😀!!&lt;/p&gt;

</description>
      <category>codeheat</category>
      <category>fossasia</category>
      <category>opensourcecontribution</category>
      <category>susiai</category>
    </item>
    <item>
      <title>Understanding Microtasks and tasks in JS</title>
      <dc:creator>Pulkit Kashyap</dc:creator>
      <pubDate>Sun, 18 Aug 2019 07:56:33 +0000</pubDate>
      <link>https://forem.com/kpulkit29/understanding-microtasks-and-tasks-in-js-ba2</link>
      <guid>https://forem.com/kpulkit29/understanding-microtasks-and-tasks-in-js-ba2</guid>
      <description>&lt;p&gt;Well, as Javascript  developers we all are in a habit of using promises, timers etc. Whenever we think of making a particular piece of code asynchronous the first thing that comes to our mind is using promises or maybe a setTimeout(). Have you ever wondered how these things work under the hood? I have been going through a lot of blogs regarding execution &lt;strong&gt;queues&lt;/strong&gt;, &lt;strong&gt;microtasks&lt;/strong&gt; and &lt;strong&gt;tasks&lt;/strong&gt;, so I decided to jot down my learnings in this post.&lt;/p&gt;

&lt;p&gt;Let's start off with a quick exercise(just to brush up your javascript skills)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tZRqVRvx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/yzcd9yf3qlz0vgfm9gt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tZRqVRvx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/yzcd9yf3qlz0vgfm9gt0.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Now put on your thinking hats and think what output of above code snippet would be.&lt;/h3&gt;

&lt;p&gt;The correct answer goes as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;call the printer&lt;/li&gt;
&lt;li&gt;Inside the Printer&lt;/li&gt;
&lt;li&gt;Promise resolved&lt;/li&gt;
&lt;li&gt;Timeout 1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yes the output is not exactly what a lot of us must be expecting🤷‍♂️ 🤷‍♂️. But  what goes on behind the scenes in Javascript is very engaging.&lt;/p&gt;

&lt;p&gt;Actually  when the &lt;em&gt;printer function&lt;/em&gt; is called the &lt;em&gt;Inside the Printer&lt;/em&gt; is logged. Now the important thing to note here is that even when the timer is set to 0 the log statement inside  &lt;em&gt;setTimeout()&lt;/em&gt; is executed after &lt;em&gt;promise&lt;/em&gt; is resolved. To understand this you need to know how the event loop handles tasks and microtasks. The event loop runs continuously and ensures that all the tasks queued are executed sequentially. So in our case the &lt;em&gt;setTimeout()&lt;/em&gt; is scheduled as a task which is executed in the next event loop. But the question here remains is &lt;strong&gt;How the hell does Promise log the statement first?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Promises&lt;/em&gt; are queued as microtasks. &lt;em&gt;Microtasks&lt;/em&gt; are executed straight after the  currently executing script and thus promises are resolved in the same event loop. Calling &lt;em&gt;.then()&lt;/em&gt; puts the promise in the microtask queue. The microtask queue is processed after callbacks as long as no other JavaScript is mid-execution. That is why &lt;em&gt;Promise resolved&lt;/em&gt; is logged before &lt;em&gt;Timeout 1&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;The crux is -:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tasks have to wait for next round of execution&lt;/li&gt;
&lt;li&gt;Microtasks can execute in the same round after the current script finishes execution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hopefully I could explain the topic in a simple yet effective manner. Bye !!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
