<?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: Akshay Mahajan</title>
    <description>The latest articles on DEV Community by Akshay Mahajan (@_akshaym).</description>
    <link>https://dev.to/_akshaym</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%2F178508%2F7adb148d-8e1b-489b-8c3d-2ef6e1fbea74.jpg</url>
      <title>DEV Community: Akshay Mahajan</title>
      <link>https://dev.to/_akshaym</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_akshaym"/>
    <language>en</language>
    <item>
      <title>Neural Network Basics: Gradient Descent</title>
      <dc:creator>Akshay Mahajan</dc:creator>
      <pubDate>Tue, 07 Apr 2020 12:44:30 +0000</pubDate>
      <link>https://dev.to/_akshaym/neural-network-basics-gradient-descent-4cej</link>
      <guid>https://dev.to/_akshaym/neural-network-basics-gradient-descent-4cej</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/_akshaym/neural-network-basics-training-a-neural-network-47gl"&gt;previous post&lt;/a&gt;, we discussed what a loss function is for a neural network and how it helps us to train the network in order to produce better, more accurate results. In this post, we will see how we can use gradient descent to optimize the loss function of a neural network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gradient Descent
&lt;/h2&gt;

&lt;p&gt;Gradient Descent is an iterative algorithm to find the minimum of a differentiable function. It uses the slope of a function to find the direction of descent and then takes a small step towards the descent direction in each iteration. This process continues until it reaches the minimum value of the function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1mkQNEFo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jmzj24jwurnsg5uuy873.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1mkQNEFo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jmzj24jwurnsg5uuy873.png" alt="Gradient Descent"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say we want to optimize a function J(W) with respect to the parameter W. We can summarize the working of the Gradient Descent algorithm as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start at any random point on the function.&lt;/li&gt;
&lt;li&gt;Calculate the slope of the function at that point.&lt;/li&gt;
&lt;li&gt;Take a small step in the direction opposite to the slope of the function.&lt;/li&gt;
&lt;li&gt;Repeat until it reaches the minimum value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The algorithm works in the same way for any N-dimensional differentiable function. The example above shows a 2D plot because it is easier for us to visualize.&lt;/p&gt;

&lt;p&gt;For our neural network, we need to optimize the value of our loss function J(W), with respect to the weights(W) in the used in the network. We can write the algorithm as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---6wxn6Ar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/np62rev0ganz1s1t6jba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---6wxn6Ar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/np62rev0ganz1s1t6jba.png" alt="Summary"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Rate
&lt;/h2&gt;

&lt;p&gt;The parameter &lt;span&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ASXhYC5C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://math.now.sh%3Ffrom%3D%255Calpha" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ASXhYC5C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://math.now.sh%3Ffrom%3D%255Calpha"&gt;&lt;/a&gt;&lt;/span&gt; is known as the learning rate. It is the rate at which we descend the slope of the function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A small learning rate means that the algorithm will take small steps in each iteration and will take a long time to converge.&lt;/li&gt;
&lt;li&gt;A very large learning rate can cause the algorithm to overshoot the point of minimum value and then overshoot again in the opposite direction, which can eventually cause it to diverge.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To find an optimum learning rate, it is a good idea to start with something small, and slowly increase the learning rate if it takes a long time to converge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To summarize the basics of a neural network:&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Perceptron is the basic building block of a neural network. It multiplies an input with a weight and applies a non-linearity to the product.&lt;/li&gt;
&lt;li&gt;Perceptrons connect together to form a layer of a neural network. There are multiple set of weights between different layers.&lt;/li&gt;
&lt;li&gt;To train the network we choose a loss function and then optimize the loss functions with respect to the weights W using gradient descent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This concludes this series on the basics of neural networks. I would love to hear your views and feedback. Feel free to hit me up on &lt;a href="https://twitter.com/_akshaym"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>learninpublic</category>
    </item>
    <item>
      <title>Neural Network Basics: Training a Neural Network</title>
      <dc:creator>Akshay Mahajan</dc:creator>
      <pubDate>Wed, 25 Mar 2020 13:51:54 +0000</pubDate>
      <link>https://dev.to/_akshaym/neural-network-basics-training-a-neural-network-47gl</link>
      <guid>https://dev.to/_akshaym/neural-network-basics-training-a-neural-network-47gl</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/_akshaym/neural-network-basics-building-a-neural-network-56e4"&gt;previous post&lt;/a&gt;, we discussed how the perceptrons connect to form a layer of a neural network and how these layers connect to form what is called a deep neural network. In this post, we will start with an example and learn how a neural network is trained. Further, we will discuss, how the network computes loss.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Let us consider an example problem where we want to predict whether a student will pass or fail a class given the number of lectures attended(x1) and the number of hours spent(x2) on the final project by the student.&lt;/p&gt;

&lt;p&gt;We can represent this problem with the following model:&lt;/p&gt;

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

&lt;p&gt;To make a prediction, we need to initialize the weights(W). We can start by initializing weights randomly. Now let us say, for a student who attended 4 lectures and spent 5 hours on the final project, the model predicted output of 0.1 or 10%. But in reality, the student passed the class. We need a way to tell our model that the predicted output was wrong so that the model can adjust its weights to predict an output closer to the actual value. This can be done by defining a loss function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8oHbL3YZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wacrcsk57buvil5eolhh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8oHbL3YZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wacrcsk57buvil5eolhh.png" alt="Loss Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can average up the loss for each training set sample to calculate the final average loss. This is also called the &lt;strong&gt;cost function&lt;/strong&gt; or &lt;strong&gt;empirical loss.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LGggsji1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5enbwzqoc2h90hbox99a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LGggsji1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5enbwzqoc2h90hbox99a.png" alt="Loss Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Loss
&lt;/h2&gt;

&lt;p&gt;We can use different loss functions for different models depending on the output produced by the model. Some of the common functions include &lt;strong&gt;mean squared error loss&lt;/strong&gt; &amp;amp; &lt;strong&gt;binary cross-entropy loss&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mean Squared Error Loss&lt;/strong&gt; can be used with models that produce continuous numbers as output. An example of this would be predicting the score of a student or the price of a house.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kxUNZc4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/szss7oo74v3lj8dwtff8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kxUNZc4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/szss7oo74v3lj8dwtff8.png" alt="Mean Squared Error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binary Cross-Entropy Loss&lt;/strong&gt; can be used with models that produce a probability between 0 &amp;amp; 1. An example of this would be predicting whether a student will pass or fail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---HLE-xqJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ws8acoy29d8yhp2t5hiu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---HLE-xqJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ws8acoy29d8yhp2t5hiu.png" alt="Binary Cross Entropy Loss"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final piece left in training the model is to optimize the loss. By optimizing we mean that we want to minimize the loss of the model over all the inputs in the training data. This can be achieved by using an optimization algorithm called &lt;strong&gt;Gradient Descent&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we learned how loss is calculated for a neural network. In the next post and final post, we will learn about the Gradient Descent Algorithm and how it can be used to minimize the loss so that our model can predict outputs with better accuracy.&lt;/p&gt;

&lt;p&gt;I would love to hear your views and feedback. Feel free to hit me up on &lt;a href="https://twitter.com/_akshaym"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Neural Network Basics: Building a Neural Network</title>
      <dc:creator>Akshay Mahajan</dc:creator>
      <pubDate>Mon, 16 Mar 2020 12:33:54 +0000</pubDate>
      <link>https://dev.to/_akshaym/neural-network-basics-building-a-neural-network-56e4</link>
      <guid>https://dev.to/_akshaym/neural-network-basics-building-a-neural-network-56e4</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/_akshaym/neural-network-basics-the-perceptron-3c4"&gt;previous post&lt;/a&gt;, we discussed the structural building block of a neural network, also called the perceptron. In this post, we will learn how these perceptrons connect together to form a layer of a neural network and how these layers connect to form what is called a deep neural network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multi-Output Perceptron
&lt;/h2&gt;

&lt;p&gt;Let us recall the structure of a perceptron, that we discussed in the last post.&lt;/p&gt;

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

&lt;p&gt;Now, let us consider that we have 2 perceptrons, instead of 1, each connected to all the inputs. This can be visually represented as the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fSkjTwz---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/68eh10p86clwmm2zm16v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fSkjTwz---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/68eh10p86clwmm2zm16v.png" alt="Multi Output Perceptron"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is called a multi-output perceptron.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This allows us to create as many outputs as we want, by stacking together multiple perceptrons.&lt;/li&gt;
&lt;li&gt;For the sake of simplicity, the bias term has been omitted here.&lt;/li&gt;
&lt;li&gt;Each perceptron is connected to all the inputs from the previous layer. This is known as a &lt;strong&gt;dense layer&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Single Layer Neural Network
&lt;/h2&gt;

&lt;p&gt;Let's add more perceptrons to create a single layer neural network.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uFGXduZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jln1fzscqji3dbix5ttf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uFGXduZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jln1fzscqji3dbix5ttf.png" alt="Single Layer Neural Network"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can see 3 types of layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first layer contains the inputs to the network and is called Input Layer&lt;/li&gt;
&lt;li&gt;The last layer provides the final output of the network and is called the Output Layer&lt;/li&gt;
&lt;li&gt;The layers between the input and output layer are called the Hidden Layers. &lt;strong&gt;The number of hidden layers represent the depth of the network&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As the number of connections increase, so do the number of weights. For example, there are two sets of weights, w1,1&lt;sup&gt;(1)&lt;/sup&gt; connecting x1 to z1 &amp;amp; w1,1&lt;sup&gt;(2)&lt;/sup&gt; connecting z1 to y&lt;sup&gt;^&lt;/sup&gt;1. The equations for the perceptrons in the hidden and output layer can be represented as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xQdZX6rP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z7216i0cfkilzukxs3es.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xQdZX6rP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z7216i0cfkilzukxs3es.png" alt="Layer Equations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make these easier to manage, and also to speed up computation, the inputs, weights, and outputs are usually stored in the form of vectors. This also allows us to take advantage of multi-core CPUs to speed up training by better parallelizing computations.&lt;/p&gt;

&lt;p&gt;To vectorize the equation, we can represent input, output, and weights as vectors and simply replace the summation of products with a matrix dot product:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5lMy4H1x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g2pz56uak33hw5n2j9dz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5lMy4H1x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g2pz56uak33hw5n2j9dz.png" alt="Vectorized Equations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we learned how a neural network is constructed by connected individual perceptrons. In the next part, we will see how the network computes the output and eventually gets better and more accurate.&lt;/p&gt;

&lt;p&gt;I would love to hear your views and feedback in the comments. Feel free to hit me up on &lt;a href="https://twitter.com/_akshaym"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>learninpublic</category>
    </item>
    <item>
      <title>Neural Network Basics: The Perceptron</title>
      <dc:creator>Akshay Mahajan</dc:creator>
      <pubDate>Sun, 08 Mar 2020 13:09:31 +0000</pubDate>
      <link>https://dev.to/_akshaym/neural-network-basics-the-perceptron-3c4</link>
      <guid>https://dev.to/_akshaym/neural-network-basics-the-perceptron-3c4</guid>
      <description>&lt;p&gt;A Neural Network is a machine learning model inspired by the human brain. A neural network learns to perform a task by looking at examples without being explicitly programmed to perform the task. These tasks can vary from predicting sales based on historic data, detecting objects in images, language translation, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Perceptron
&lt;/h3&gt;

&lt;p&gt;The Perceptron is the structural building block of a neural network. It is modeled after the neurons inside the brain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6btRDbYy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w69vigc9fk48uigte5hf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6btRDbYy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w69vigc9fk48uigte5hf.png" alt="Perceptron"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mathematically, this can be represented by the following equation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OP3RD9XL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rg05f4z4xuj96h60ub7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OP3RD9XL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rg05f4z4xuj96h60ub7s.png" alt="Perceptron Equation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can explain the working of a single perceptron as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each perceptron receives a set of inputs x1 to xm
&lt;/li&gt;
&lt;li&gt;Each input has a weight associated with it ie. w1 to wm for x1 to xm respectively.&lt;/li&gt;
&lt;li&gt;Each input is multiplied with its respective weight, added, and passed as input to an activation function.&lt;/li&gt;
&lt;li&gt;A bias term w0 is also added, which allows us to shift the activation function left or right, irrespective of the inputs.&lt;/li&gt;
&lt;li&gt;An activation function, also called a non-linearity, is a non-linear function that produces the final output of the perceptron.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Activation Functions
&lt;/h3&gt;

&lt;p&gt;An activation function is a non-linear function. A few common activation functions are the sigmoid function, the hyperbolic tangent (tanh) function, and the rectified linear unit (ReLU) function. The choice of the activation function used depends on the type of output expected from the perceptron. For example, the output of the sigmoid function ranges between 0 and 1. Therefore, it is useful when output is supposed to be a probability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I3fCcPSt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5b9zyiptnqvy5l4yckjf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I3fCcPSt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5b9zyiptnqvy5l4yckjf.png" alt="Activation Functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we need an Activation Function
&lt;/h3&gt;

&lt;p&gt;Activation functions introduce non-linearity into the network. In real life, almost all of the data is non-linear. Without an activation function, the output will always be linear. Non-linearities, on the other hand, allows us to approximate arbitrarily complex functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VmoHXKOH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/57ep9dv940b1ndrm5ngx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VmoHXKOH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/57ep9dv940b1ndrm5ngx.png" alt="Decision Boundaries for Linear vs Non-Linear"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this post, we learned how a single perceptron works. In the next part, we will see how these perceptrons are connected together to form a neural network and how the network learns to do a task that it was not explicitly programmed to do.&lt;/p&gt;

&lt;p&gt;I would love to hear your views and feedback. Feel free to hit me up on &lt;a href="https://twitter.com/_akshaym"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Getting Started with Javascript Testing</title>
      <dc:creator>Akshay Mahajan</dc:creator>
      <pubDate>Tue, 25 Feb 2020 15:41:07 +0000</pubDate>
      <link>https://dev.to/_akshaym/getting-started-with-testing-javascript-3h7d</link>
      <guid>https://dev.to/_akshaym/getting-started-with-testing-javascript-3h7d</guid>
      <description>&lt;p&gt;As software engineers, it is our job to write code that solves problems and to make sure that it solves the problem correctly. Testing helps us to ensure that the software we write behaves the way it was intended to. Unit Testing is the most basic type of testing that can be performed on a piece of code that verifies its correctness for a given set of values.&lt;/p&gt;

&lt;h2&gt;
  
  
  General Structure of a Unit Test
&lt;/h2&gt;

&lt;p&gt;A Unit Test generally consists of 3 things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A unit (a block of code or a function) that needs to be tested&lt;/li&gt;
&lt;li&gt;The inputs to the unit for which it needs to be tested&lt;/li&gt;
&lt;li&gt;The expected output for the given inputs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d5ZJKHm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eiilan2y0oe69d02hkyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d5ZJKHm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eiilan2y0oe69d02hkyh.png" alt="Structure of a Unit Test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's build a Mini Testing Library
&lt;/h2&gt;

&lt;p&gt;Let us build a small function that can convert temperatures from Fahrenheit to Celcius. Before we start building the function, we can think of a few possible test cases for the function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The input of 0 should return an output of -17.77777777777778&lt;/li&gt;
&lt;li&gt;The input of 5 should return an output of -15&lt;/li&gt;
&lt;li&gt;The input of -4 should return an output of -20&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This process of building and writing tests before the actual implementation of the functionality is known as &lt;strong&gt;Test-Driven Development (TDD).&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ftoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&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;f&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;9&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;Looking at the Structure of a Unit Test, let us write some utility functions that can help us abstract the working of the test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&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;result&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Expected &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, but got &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`✅ &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`❌ &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Convert 0F to Celcius&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ftoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;17.77777777777778&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Convert Positive Temparaterue to Celcius&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ftoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Convert Negative Temparature to Celcius&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ftoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;20&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;On executing the above code, the following output is produced.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nJtKl8ni--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7kjf5e12xc9d85eu79ez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nJtKl8ni--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7kjf5e12xc9d85eu79ez.png" alt="Test Fail Output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This shows that our conversion function works for just one of the cases and fails for the other two. To fix the function update the function to include a set of parentheses to fix the conversion function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ftoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;9&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;Re-running the tests gets the following output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eEGtp-SP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/51zmz9ivpbkxnu65a9qx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eEGtp-SP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/51zmz9ivpbkxnu65a9qx.png" alt="Test Pass Output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Well, that was easy, right? We built a simple testing library in just a few lines of code that can be used to test any code we write. Although it is far from something that can be used in production, the main idea and structure remain the same. There are a lot of popular and feature-rich testing frameworks like Jest, Mocha, etc. that provide advanced features like a detailed description of failing tests along with the exact line number and stack trace, that is more suitable for use in production environments.&lt;/p&gt;

&lt;p&gt;I would love to hear your views and feedback in the comments. You can also hit me up on &lt;a href="https://twitter.com/_akshaym"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
      <category>learninpublic</category>
    </item>
  </channel>
</rss>
