<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aditya Srinivas Menon on Medium]]></title>
        <description><![CDATA[Stories by Aditya Srinivas Menon on Medium]]></description>
        <link>https://medium.com/@adityasrinivas-menon?source=rss-805d45ba2393------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QB4ZqsQvxtMChbdRtFwMVA.png</url>
            <title>Stories by Aditya Srinivas Menon on Medium</title>
            <link>https://medium.com/@adityasrinivas-menon?source=rss-805d45ba2393------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 11:08:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adityasrinivas-menon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Chat GPT: One step closer to AGI]]></title>
            <link>https://adityasrinivas-menon.medium.com/chatgpt-3752f5df3fa7?source=rss-805d45ba2393------2</link>
            <guid isPermaLink="false">https://medium.com/p/3752f5df3fa7</guid>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[generative-art]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[prompt]]></category>
            <dc:creator><![CDATA[Aditya Srinivas Menon]]></dc:creator>
            <pubDate>Thu, 15 Dec 2022 11:47:07 GMT</pubDate>
            <atom:updated>2022-12-15T11:47:07.226Z</atom:updated>
            <content:encoded><![CDATA[<h3>ChatGPT: One step closer to AGI</h3><p>Chatbot-ception, Making a chatbot within ChatGPT and some other cool use cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GT12OE-rdYOImNnvYaPTtw.jpeg" /><figcaption><strong>Image by </strong><a href="https://labs.openai.com/"><strong>DALL-E 2</strong></a><strong> </strong><em>An expressive image of a man interacting with Artificial Intelligence, depicted as an explosion of a nebula, show the Artificial Intelligence as a computer, neon, high quality, digital art</em></figcaption></figure><p><a href="https://chat.openai.com/chat">ChatGPT</a> is an impressive model that can converse like a human. The model was designed to be used in chatbots so that they can have natural, human-like conversations. It’s a HUGE language model (GPT 3.5) that’s trained on conversational data using human-in-the-loop Reinforcement Learning capable of capturing the nuances of a human conversation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W5cJvGS0mBJyxvDo48bMzQ.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>A famous YouTuber <a href="https://t.co/5osPue5EW9">Sentdex</a> pointed out that you could simulate virtual machines in ChatGPT by prompting them to act like one. You can run files, run servers, interact with API and train models in it.</p><h3>Harrison Kinsley on Twitter: &quot;OpenAI&#39;s ChatGPT AI (a neural network) functioning as a Linux operating system terminal that I am able to navigate, read, write and execute scripts on. pic.twitter.com/S4w10kNMcV / Twitter&quot;</h3><p>OpenAI&#39;s ChatGPT AI (a neural network) functioning as a Linux operating system terminal that I am able to navigate, read, write and execute scripts on. pic.twitter.com/S4w10kNMcV</p><p>I wanted to try this out for myself and see the capabilities of ChatGPT. I ask ChatGPT for a modified version of the prompt I found on Twitter, specifically asking them not to give explanations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nF2a4K9hRgCQhDYVOafMQA.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>We are in the root directory!</p><p>A quick ls to check the files in the current directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DpMpysC1L9Dmkf9aC9jmbw.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>Our policy is not to look into these folders due to respect for their privacy. I will make a new directory called chatbot and move into it. ChatGPT keeps a track of the previous prompts and gives accurate results for commands.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xMeci7wnb7DrlqKnrO2fWw.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>I copy the code from <a href="https://huggingface.co/microsoft/DialoGPT-medium">Dialo hugging face model page</a> and write it into a python file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mYrPNAblRMz7F9m2sUaY5A.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>A quick ls shows the current state of the VM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/995/1*LtHohSbczPxieb5sh8AwLQ.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>ChatGPT lets us choose the GPU too! This is an effective way to get A100 for free without having to go through the pain of installing the CUDA toolkit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QYQ2SXzAfrwJwY72k69E1Q.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>Quickly install the transformers library. We need it for the Dialo chatbot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*O2VbfA8p9xMgHXB14CqM5g.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>Running the python script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ME11tpgpjZa7qenP1TFMIg.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>A chatbot inside a chatbot! I wanted to try the same but replace Dialo, an old and outdated chatbot with ChatGPT itself. I am not very proficient at interacting with the ChatGPT API hence I decided to ask the expert themselves:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gxu7nj1HA5GLZEaWkstFtw.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>Made a new python file called chatgpt.py and put the same code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p9nAFCjfho8YeKpURY2iIg.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>Run the file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fR_Ep4DSA4oqdtac-XNNpA.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>And it is correct! We are now able to chat with ChatGPT using a Linux virtual machine within ChatGPT. I ask it to change the code to add a loop so that it will be able to converse with itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*Yi6FyncA9JSBWVrKg-K0oQ.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>Run the updated file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*i6v_qFI-pZw4fhDrqnpLng.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>It works! We are making ChatGPT chat with itself.</p><p>Earlier while I was learning about automatons and the Game of Life by Conway, I came across this video that shows how to make the Game of Life within Game of Life.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FxP5-iIeKXE8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DxP5-iIeKXE8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FxP5-iIeKXE8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ee9b4a595a75dfe1fe6bbc5df22af1/href">https://medium.com/media/01ee9b4a595a75dfe1fe6bbc5df22af1/href</a></iframe><p>I wanted to try it with ChatGPT where we could make a Linux VM using ChatGPT within a Linux VM inside ChatGPT. I prompted the following to Outermost ChatGPT</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OC_AiOJNOsOTvWvQ35jMsA.png" /><figcaption>Image by author. A screenshot from ChatGPT conversation.</figcaption></figure><p>And yes! We have a ChatGPT making a Linux VM inside of a Linux VM made by ChatGPT. Can we go another level down?</p><p>A version of this conversation can be over at ShareGPT for replication purposes: <a href="https://sharegpt.com/c/iymBqWU">https://sharegpt.com/c/iymBqW</a>. Thanks to <a href="https://medium.com/u/352a5f28e96d">Steven Tey</a> for building this amazing product. You can find them on <a href="https://twitter.com/steventey">Twitter</a>!</p><p>If you are new to deep learning please check out my previous blog on MNIST from scratch:</p><p><a href="https://towardsdatascience.com/mnist-handwritten-digits-classification-from-scratch-using-python-numpy-b08e401c4dab">MNIST Handwritten digits classification from scratch using Python Numpy.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3752f5df3fa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MNIST Handwritten digits classification from scratch using Python Numpy.]]></title>
            <link>https://medium.com/data-science/mnist-handwritten-digits-classification-from-scratch-using-python-numpy-b08e401c4dab?source=rss-805d45ba2393------2</link>
            <guid isPermaLink="false">https://medium.com/p/b08e401c4dab</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[backpropagation]]></category>
            <category><![CDATA[mnist-dataset]]></category>
            <dc:creator><![CDATA[Aditya Srinivas Menon]]></dc:creator>
            <pubDate>Sat, 23 Jan 2021 06:58:50 GMT</pubDate>
            <atom:updated>2021-01-24T17:16:03.370Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OG7hlHJ3HlVr7BT7uIv4wA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@popnzebra">Pop &amp; Zebra</a> on <a href="https://unsplash.com/photos/NZ-9RqsE06I">Unsplash</a></figcaption></figure><p>So I recently made a classifier for the MNIST handwritten digits dataset using PyTorch and later, after celebrating for a while, I thought to myself, “Can I recreate the same model in vanilla python?” Of course, I was going to use NumPy for this. Instead of trying to replicate NumPy’s beautiful matrix multiplication, my purpose here was to gain a better understanding of the model by reinventing the wheel.</p><p>I challenged myself to make a similar classifier in numpy and learn some of the core concepts of Deep Learning along the way. You can find the code in my <a href="https://github.com/karynaur/mnist-from-numpy">GitHub repository</a>.</p><p>Our workflow should look something like this:</p><ol><li>Fetch the data from the <a href="http://yann.lecun.com/exdb/mnist/">MNIST website</a></li><li>Split train-images into training set and validation set</li><li>Initialize the weights</li><li>Define our activation functions and its derivatives</li><li>Define a function for forward pass and backward pass (laborious!)</li><li>Train our model in batches using SGD, update the weights and test our model on the validation set</li><li>Predict on the test data and print the accuracy</li></ol><p>For this model, I decided to go with sigmoid and softmax as activation functions to keep things simple.</p><ol><li><strong>Fetch the data</strong></li></ol><p>Let us now welcome NumPy, the protagonist of our story.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F1%26cellId%3D0&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F1%26cellId%3D0&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fc07175a86dcd4a559830ca0cb910c8f2%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bef5d3594d63d81e92dbeb0683fa211/href">https://medium.com/media/8bef5d3594d63d81e92dbeb0683fa211/href</a></iframe><p>Define a function to take the utf-8 encoded data, decompress it and convert it into a NumPy array. This code was taken from a notebook by <strong>George Hotz </strong>which you can find <a href="https://github.com/geohot/ai-notebooks/blob/master/mnist_from_scratch.ipynb">here</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F3%26cellId%3D1&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F3%26cellId%3D1&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F97ade5f2c6ec4ccd8471cb78c022c2ec%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/00167f7aee05039155021e93d2f29246/href">https://medium.com/media/00167f7aee05039155021e93d2f29246/href</a></iframe><p>2. Since the data only contains a training set and testing set, let us split the training set into training(<em>X_train, Y_train</em>) and validation(<em>X_val, Y_val</em>).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F3%26cellId%3D3&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F3%26cellId%3D3&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F78a5a8d029204918b006fae7dfee9afd%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/026d25e33436b2f5ddfe3e7be7b13f31/href">https://medium.com/media/026d25e33436b2f5ddfe3e7be7b13f31/href</a></iframe><p>3. Let us now define a function that takes in, the size of the matrix as input and returns initialized weights.</p><p>The neural network is going to be a simple network of three layers. The input layer consists of 784 units corresponding to every pixel in the 28 by 28 image from the MNIST dataset. The second layer(<em>hidden layer</em>) drops down to 128 units and lastly the final layer with 10 units corresponding to digits 0–9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*IFFqZCJxX-hJlcL2F3IwRw.png" /><figcaption>For the sake of simplicity, only one connection from each unit has been shown. Image by author.</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F5%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F5%26cellId%3D6&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fa503b66230bc48bca500aab9c3da3a79%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e4d03c8453aa1407e72a083c3d803e5/href">https://medium.com/media/3e4d03c8453aa1407e72a083c3d803e5/href</a></iframe><p>Thus l1 is a matrix of size (784,128) and l2 is a matrix of size(128,10).</p><p>4. Functions can be generalized as processes that takes in an input and spits out an output. Activation functions are those functions that take in weighted sum of neurons as input(<em>varying in magnitude</em>) and turn it into meaningful data that can be easily understood or fed into the next layer. Sigmoid and softmax are two such activation functions that we will be working with.</p><p><strong>Sigmoid function:</strong></p><p>The sigmoid function is defined as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/236/1*0hqEPQ8YsKhLUC-H57cmQA.png" /><figcaption>Image by author.</figcaption></figure><p>It can be visualized by plotting a graph taking f(x) = y as such:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*d7mOeb1fz8dq7RVFCNrADw.png" /><figcaption>Image by author.</figcaption></figure><p>It takes in literally anything and spits out a number between 0 and 1. In python code sigmoid and its derivative would look something like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F5%26cellId%3D4&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F5%26cellId%3D4&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Feb7c398db28c4cdeba428d41bdbe78da%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/39403cef46558657fe1740082773dcfc/href">https://medium.com/media/39403cef46558657fe1740082773dcfc/href</a></iframe><p>In our model, we use the sigmoid function to squish the random outputs given out by layer 1 into numbers between 0 and 1.</p><p><strong>Softmax function:</strong></p><p>A Softmax function takes in a vector as input and spits out a vector of same size having elements that sum up to 1. Every element in the output vector is between 0 and 1, and thus these values can be interpreted as probabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/266/1*hDRxcO2ShY3Ud2WGKMBnaw.png" /><figcaption>Image by author.</figcaption></figure><p>Softmax function in python code will look something like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D1&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D1&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F0fab7b51e9664a2c9dc2cac32e01ffbd%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/8201655ccb2eb669d2b0be7cc9020d87/href">https://medium.com/media/8201655ccb2eb669d2b0be7cc9020d87/href</a></iframe><p>To understand how softmax works, let us declare a simple numpy array and call the softmax function on it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D2&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D2&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F862a7f5cffaf49dfb7018b78d1f749d2%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e00c73c8db6b60ffd9f87735f41b34ec/href">https://medium.com/media/e00c73c8db6b60ffd9f87735f41b34ec/href</a></iframe><p>From the second result it is clear that although the sum of <em>out </em>is not 1, the sum of its softmax is indeed 1. Therefore we can consider the values of the softmax array as the probability of the respective elements in the parent array.</p><p>1 in the distribution of [1,2,3] is least probable as its softmax value is 0.090, on the other hand, 3 in the same distribution is highly probable, having a softmax value of 0.6652.</p><p>In our model, the output layer spits out a vector of shape 10 having different magnitudes. Hence, we use softmax to normalize our result. For example, let us take a vector that looks similar to our model output and feed it to the softmax function:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D3&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D3&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F419b5a5f470142eab6aaf11e252067fe%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aef490bc66c02cb35cbb04366eadefa/href">https://medium.com/media/2aef490bc66c02cb35cbb04366eadefa/href</a></iframe><p>Now if we run the NumPy’s argmax function on the softmax result we get the index of the element having the maximum value:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D4&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F1%26cellId%3D4&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F117f8acdff0e43148b9b8e072d3f29c2%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/095bcb2740d8e9d0f63bb0ba5b521e74/href">https://medium.com/media/095bcb2740d8e9d0f63bb0ba5b521e74/href</a></iframe><p>To prevent overflow, we use a simplified version of the softmax. Let us now define the softmax and its derivative:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F5%26cellId%3D5&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F5%26cellId%3D5&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F6a97c42df551480cbb40774ba94bfc08%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbb8448c63a05f06409755d5b343a50b/href">https://medium.com/media/bbb8448c63a05f06409755d5b343a50b/href</a></iframe><p>5. Let us now define a function to carry out the forward pass and the backward pass during an iteration.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F6%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F6%26cellId%3D6&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fd5034a834bfe489ab34f91c907240a32%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1d821efcb1904db02254839d10c5ae1/href">https://medium.com/media/d1d821efcb1904db02254839d10c5ae1/href</a></iframe><p>Let us go over the code line by line, and understand how it works.</p><p>Y_train is essentially a vector of 50,000 elements, having the <em>correct </em>digit corresponding to the images in X_train.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D6&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F9faa8d3dc6b6429f94c55e47aec6e535%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3a0b2002bf814ed4573ff9fa99aba1a/href">https://medium.com/media/a3a0b2002bf814ed4573ff9fa99aba1a/href</a></iframe><p>On the other hand our model outputs a vector of 10 elements for each training example. Therefore our primary task is to convert the Y_train into a vector having 1 corresponding to the “<em>correct</em>” digit and 0 for the rest.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D7&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D7&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F90db3b2bd8ef4a8a8a5adabc5cda83e7%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/c772e304c6a33d1c90606159cc440c48/href">https://medium.com/media/c772e304c6a33d1c90606159cc440c48/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D8&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D8&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fbdf384424b6e4f41b033849738820ad0%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/941a0668a2a1bdf11cf573e74b396d9f/href">https://medium.com/media/941a0668a2a1bdf11cf573e74b396d9f/href</a></iframe><p>As you can see the element in the fifth position having an index of ‘<em>4</em>’ has a 1 whereas the rest of the elements are 0.</p><p>Now comes the forward pass where the input matrix is multiplied with the weights and passed through the respective activation functions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D8&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D8&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F3d064384bb5249a786630adee9aa9b0f%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2f23bdf7ab7853209d0424b81717f46/href">https://medium.com/media/e2f23bdf7ab7853209d0424b81717f46/href</a></iframe><p>In the first line x is matrix multiplied with the first layer and is normalized by passing through the sigmoid function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*ztTQbtaUU9tsXkB9XfKx2w.png" /><figcaption>Screenshot by author.</figcaption></figure><p>The sigmoid product is later matrix multiplied with the second layer and is passed through the softmax function to get the output vector of size 10, similar to our target vector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*RusEdPKOJf9Nnk8a2lvyfA.png" /><figcaption>Screenshot by author.</figcaption></figure><p>Now it’s time to backpropagate and update our weights. Our aim here is to find matrices similar in shape to that of l1 and l2, that can be subtracted from l1 and l2 to get <em>closer </em>to the ideal answer by minimising error.</p><p>But what exactly is <em>error</em>?</p><p><strong><em>error=2*(out-targets)</em></strong></p><p>This calculates the error in the current epoch along with its direction. <em>By how far are we away from achieving the ideal answer.</em></p><p>Let us have a look at what ‘<em>out’</em> really is:</p><p><strong><em>out=softmax(x_l2)</em></strong></p><p>It is the product of our neural network.</p><p>We now need to find an entity that tells us by how much the <em>out</em> changes if there is a change in <em>x_l2</em>.</p><p>YEP! You guessed it right. This entity is called the derivative! We need to find the derivative of softmax at the point <em>x_l2</em> in space such that when we <em>change</em> x_l2 by (<strong>error * derivative of softmax at x_l2</strong>) our <em>out </em>moves closer to the ideal targets, and that in python code looks like:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D9&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D9&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F0d6d76903655483bb0e1d56e59a8abbf%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/6254d3ebd45dc145551096e81313be9a/href">https://medium.com/media/6254d3ebd45dc145551096e81313be9a/href</a></iframe><p>But what exactly is x_l2?</p><p><strong><em>x_l2=x_sigmoid@l2</em></strong></p><p>We now know by how much we need to change x_l2, but x_l2 is the product of the sigmoid output of the first layer and the l2, and we cannot change it. We only have control over the weights! So to turn the previous error into something that can be subtracted from l2 let us define a <em>update_l2.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D10&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D10&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F49666ce695c84cc9bcc7aa2109643db2%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/35306eeead3e967ce445b34dacaa9db6/href">https://medium.com/media/35306eeead3e967ce445b34dacaa9db6/href</a></iframe><p>Phew! We are done with the second layer, now let us do the same for the first. Instead of taking the derivative of the softmax, we shall be taking the derivative of sigmoid at point <em>x_l1</em> in space.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D11&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D11&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F21e656dbfafe40e1896acfbb84de6322%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/f649502657ec6cf2cd55b1d03f42bdec/href">https://medium.com/media/f649502657ec6cf2cd55b1d03f42bdec/href</a></iframe><p>And define <em>update_l1</em> which can be subtracted from l1.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D12&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F2%26cellId%3D12&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F5b1ce787df2e45b980c30e9c3b55536f%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/ceffdab5dd30949faaa48060b7c869b1/href">https://medium.com/media/ceffdab5dd30949faaa48060b7c869b1/href</a></iframe><p>With this we come to the end of forward and backward pass.</p><p>6. Now comes the training part. We shall perform Stochastic Gradient Descent by sending our training set in batches of 128 with a learning rate of 0.001. We can do this by simply creating a sample set containing 128 elements randomly chosen from 0 to 50000(the size of X_train), and extracting all elements from X_train and Y_train having the respective indices.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D13&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D13&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F375fc9528d874825a1fbd46f937cec77%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb73e4513019b70972cdc0c85f7d1939/href">https://medium.com/media/fb73e4513019b70972cdc0c85f7d1939/href</a></iframe><p>Let us now send x and y through the <em>forward_backward()</em> function and store its outputs in out, update_l1 and update_l2 respectively.</p><p>Pick the category and calculate the accuracy of the batch.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D14&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2Fa7147005ffd9402787b6a56889cc585f%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/86bb51f76cd366d6e9cf365a10ab5fb9/href">https://medium.com/media/86bb51f76cd366d6e9cf365a10ab5fb9/href</a></iframe><p>Calculate the loss using mean squared error loss.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D15&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D15&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F0ca6eeb0d9bb45f2b0c0e21998ffdfab%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/888c3f3e29f3f895c55e1dd4817266a9/href">https://medium.com/media/888c3f3e29f3f895c55e1dd4817266a9/href</a></iframe><p>Update the weights.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D16&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D16&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2Fd50986d8127c499299544497c02ff704%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b5f6a0838772eb1c4565db5687be2a2/href">https://medium.com/media/5b5f6a0838772eb1c4565db5687be2a2/href</a></iframe><p>And finally test the model on a dataset it has not yet seen, i.e. the validation set.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D17&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fsoftmax-example%2Fv%2F3%26cellId%3D17&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2Ffb5bf0d061c1428ab0418963d7eeeca5%2Fpreview%2F1dd237b47568400ab542a53fbe5c8729%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/69fd29006a83a7d9e64ae12adb4a24da/href">https://medium.com/media/69fd29006a83a7d9e64ae12adb4a24da/href</a></iframe><p>Putting it all together inside a for loop:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D10&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D10&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fb135158b7b27409b85d452f68dfb7e9e%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f0c5a3518a74b8dc689d4c987e7e246/href">https://medium.com/media/0f0c5a3518a74b8dc689d4c987e7e246/href</a></iframe><p>And YESSS! It is clear from the output that the model has learnt how to predict.</p><p>Now let us visualize the training and validation accuracy.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D11&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D11&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Ff0f19a795ed747ed9663d537a2011c18%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e104f0829010a4198b87ffadca653eb0/href">https://medium.com/media/e104f0829010a4198b87ffadca653eb0/href</a></iframe><p>The training accuracy fluctuates but the validation accuracy increases initially and plateaus later on, signifying that the model has reached its limit.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D12&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D12&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F98536c714c544bf599f14806c20256dd%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6abc32bd74695c86c07bdd7f5698ccc/href">https://medium.com/media/d6abc32bd74695c86c07bdd7f5698ccc/href</a></iframe><p>Let us now test our model on the testing set provided.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D13&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F8%26cellId%3D13&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fe83d371f0df64653a8dd44137a3c33e2%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8a358bf9e5e634ae05ae42130b27537/href">https://medium.com/media/a8a358bf9e5e634ae05ae42130b27537/href</a></iframe><p>Can it handle an ideal test? Inspired by George Hotz from his <a href="https://github.com/geohot/ai-notebooks/blob/master/mnist_from_scratch.ipynb"><em>notebook</em></a>, let us test our model on something that looks like a 7.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F9%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F9%26cellId%3D14&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2Fdb5c9df799c94ddc848493314e36dd60%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/294cfc0444a2eb810a014a05ba55ee44/href">https://medium.com/media/294cfc0444a2eb810a014a05ba55ee44/href</a></iframe><p>Maybe a 1?</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ai%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F9%26cellId%3D15&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ai%2Fkarynaur%2Fmnist%2Fv%2F9%26cellId%3D15&amp;image=https%3A%2F%2Fjovian.ai%2Fapi%2Fgist%2F931fbec4b4be4103806c99f318f9659b%2Fpreview%2F80bfed11529b4d7ca07a0ba32bdc2983%3Fsize%3Dl&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e83fc748e56d9f5de6e6cda3e204e5c6/href">https://medium.com/media/e83fc748e56d9f5de6e6cda3e204e5c6/href</a></iframe><p>There you go, we have successfully trained and tested a deep learning model, completely made from python NumPy!</p><p>If you are new to deep learning please check out my previous blog on a beginners guide to deep learning:</p><p><a href="https://towardsdatascience.com/classifying-cats-vs-dogs-a-beginners-guide-to-deep-learning-4469ffed086c">Classifying Cats vs Dogs. A Beginner’s guide to Deep Learning</a></p><p>Also, check out some great content on AGI by <a href="https://medium.com/u/eabfa50e7559">Prasanna Devadiga</a>:</p><p><a href="https://devadigaprasanna28.medium.com/my-thoughts-on-xyz-61dc4ad833e">My Thoughts on (xyz)</a></p><p>GitHub repository:</p><p><a href="https://github.com/karynaur/mnist-from-numpy">karynaur/mnist-from-numpy</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b08e401c4dab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/mnist-handwritten-digits-classification-from-scratch-using-python-numpy-b08e401c4dab">MNIST Handwritten digits classification from scratch using Python Numpy.</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Classifying Cats vs Dogs. A Beginner’s guide to Deep Learning]]></title>
            <link>https://medium.com/data-science/classifying-cats-vs-dogs-a-beginners-guide-to-deep-learning-4469ffed086c?source=rss-805d45ba2393------2</link>
            <guid isPermaLink="false">https://medium.com/p/4469ffed086c</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[fastai]]></category>
            <dc:creator><![CDATA[Aditya Srinivas Menon]]></dc:creator>
            <pubDate>Sun, 29 Nov 2020 07:18:06 GMT</pubDate>
            <atom:updated>2020-11-29T20:21:03.766Z</atom:updated>
            <content:encoded><![CDATA[<h4>Leveraging fast.ai’s vision library and Gradient Community Notebooks</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uU9y7qN76XGWqF9rKxmGlw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@tranmautritam">Tran Mau Tri Tam</a> on <a href="https://unsplash.com/photos/7QjU_u2vGDs">Unsplash</a></figcaption></figure><p>Want to build and train a deep learning model to achieve state-of-the-art result in classifying cats and dogs? You are at the right place!</p><p>You DO NOT need any prior math knowledge to follow along with me. Just high school math and a little bit of experience in python programming are more than sufficient! I will be taking you through every step to make and train your first model.</p><h3>1. Setup</h3><p>For the sake of simplicity, we shall be running our model on a cloud GPU (<a href="https://www.intel.com/content/www/us/en/products/docs/processors/what-is-a-gpu.html"><strong>What is a GPU?</strong></a>). You can check out the blog below to understand the importance of GPU’s in Deep Learning.</p><p><a href="https://towardsdatascience.com/what-is-a-gpu-and-do-you-need-one-in-deep-learning-718b9597aa0d">What is a GPU and do you need one in Deep Learning?</a></p><p>There are a lot of services that provide free as well as paid instances of cloud GPU’s. We shall be working with Gradient, which offers a Free Tier of GPU and CPU instances. We shall also be using fast.ai’s vision library to create our model.</p><ol><li>First, click <a href="https://console.paperspace.com/signup?gradient=true"><strong>here</strong></a> to create an account on Gradient</li><li>Login to your account and select ‘<em>Gradient’</em></li><li>Click on ‘<em>Notebook</em>’</li><li>Give your instance a name(Optional)</li><li>Select the <em>Paperspace + Fast.AI</em> base container</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zkrPbhb-Ux6XpygPY65hAA.png" /><figcaption>Screenshot by author</figcaption></figure><p>6. Choose any of the free GPU instances</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DNx8L8eB0KaTcGBm2BMN7A.png" /><figcaption>Screenshot by author</figcaption></figure><p>7. Click on Create Notebook</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/199/1*U067bZf0oOpgdIGe-_zS4g.png" /><figcaption>Screenshot by author</figcaption></figure><p>Your Notebook will go from Pending →Provisioning → Running!</p><p>8. Click on ‘<em>OPEN V1(CLASSIC)’</em></p><p>For this model, we shall be scraping images off Bing and DuckDuckGo, which will serve as our dataset. We shall install the <strong>jmd_imagescraper</strong> library by <a href="https://medium.com/u/5ec231296399">Joe Dockrill</a>, which will get the job done for us.</p><p>9. Click on New →Terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*Mc15AjaBh1V2rD-_rwcTkQ.png" /><figcaption>Screenshot edited by author</figcaption></figure><p>Type and click enter:</p><pre><strong>!</strong>pip install -q jmd_imagescraper</pre><p>For more information on the library you can check out its official page:</p><p><a href="https://joedockrill.github.io/blog/2020/09/18/jmd-imagescraper-library/">jmd_imagescraper library</a></p><p>That brings us to the end of the setup. Close the terminal and open the notebook <strong>once again</strong>. Time to code!</p><h3>Jupyter Notebooks</h3><p><a href="https://jupyter.org/"><strong>Jupyter Notebook</strong></a> is a web application that allows you to create documents that contain live code, equations and text. You can do anything from writing code to publishing books and building a stand-alone web application using Voilà! For more information on working with Jupyter notebooks, you can click <a href="https://medium.com/codingthesmartway-com-blog/getting-started-with-jupyter-notebook-for-python-4e7082bd5d46"><strong>here</strong></a>. We shall be writing and executing our code on Jupyter notebooks provided by Gradient.</p><p>Jupyter notebooks are composed of numerous cells. You can control your workflow by deciding the order in which you execute cells.</p><p>Click on New →Python 3 to open a new notebook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*XJPMPEazC3GuMcS_NI5Uuw.png" /><figcaption>Screenshot edited by author</figcaption></figure><p>You can write text(in Markdown cells) or write python code(in code cells)and execute them like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*2zFVkMN6SJN28JbCPBiBLA.png" /><figcaption>Screenshot by author</figcaption></figure><p>Play around until your comfortable with it. Use the <strong>Run</strong> and <strong>Add cells</strong> option to execute your code and add more cells respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/96/1*cohuGEc5eTgCDOpatH6BYg.png" /><figcaption>Executes code written in the cell (Screenshot by author)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/54/1*Xyo9eXljvf90IZlq2sv2_A.png" /><figcaption>Adds a new cell below the current one (Screenshot by author)</figcaption></figure><h3>2. Code</h3><p>Let us begin the fun stuff, shall we? You can copy each code block onto different cells and run them along with me.</p><p>Let us first import fast.ai’s vision library and jmd_imagescraper.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2363886b9bc20a06da79af6cc0ca2465/href">https://medium.com/media/2363886b9bc20a06da79af6cc0ca2465/href</a></iframe><p>Since we aim to classify images of cats and dogs lets go ahead and make a folder called ‘<em>animals</em>’ to which we can download and save our images.</p><p>The last line sets the <em>path </em>variable to the ‘<em>animals</em>’ folder in the current working directory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ec2f15b71312451a122f1ba69e197de/href">https://medium.com/media/0ec2f15b71312451a122f1ba69e197de/href</a></iframe><p>Call for the <em>duckduckgo_search()</em> function from <strong>jmd_imagescraper</strong> library that takes in the following as input:</p><ol><li>download directory(<em>path </em>variable assigned above)</li><li>folder name(<em>cat</em>)</li><li>keyword to search on <a href="https://duckduckgo.com/">DuckDuckGo</a>(“<em>cats</em>”)</li><li>number of images to download(<em>100</em>)</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96b6a455416314a9d47046cdb68ce5d6/href">https://medium.com/media/96b6a455416314a9d47046cdb68ce5d6/href</a></iframe><p>Wait until it searches for the images, download and saves them directly to a folder named ‘<em>cat</em>’ in ‘<em>animals</em>’. The result should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4xBuerZC1b6Y1BR3RcPBrQ.png" /><figcaption>Screenshot by author</figcaption></figure><p>Let us now do the same for images of dogs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d450382b21767992fb03a5ef0884a4fc/href">https://medium.com/media/d450382b21767992fb03a5ef0884a4fc/href</a></iframe><p>Often there can be unrelated images which would hamper our training. It is better to go through individual files and delete the ones that are not cats or dogs. Jupyter provides an interactive GUI to delete the unrelated images efficiently.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59430b421caf68220bf3fa3063114d65/href">https://medium.com/media/59430b421caf68220bf3fa3063114d65/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iRUCvK5ixERFGblGqAFU3A.png" /><figcaption>Aren’t they cute ^w^ (Screenshot by author)</figcaption></figure><p>Switch between<strong> folders</strong> and delete unrelated images. I found a few images like these that had snuck its way into cats and dogs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*PJtWdJFCvu_qLNP4BMeXwg.png" /><figcaption>DuckDuckGo why? (Screenshot by author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/796379f8a7f7f46901834ab4a4282231/href">https://medium.com/media/796379f8a7f7f46901834ab4a4282231/href</a></iframe><p>Look for failed downloads and unlink them from the folder</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/509cd9c3b3f284996764e2333cd73080/href">https://medium.com/media/509cd9c3b3f284996764e2333cd73080/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99700debf9d60ada81d043d1d20a396f/href">https://medium.com/media/99700debf9d60ada81d043d1d20a396f/href</a></iframe><p>Let us now create a <strong>Datablock</strong> object that sends the images to a <strong>DataLoaders </strong>class. The <strong>DataLoaders </strong>class converts the given data into pieces of information that the <strong>Learner </strong>can interpret.</p><p>Intuitively the <strong>Learner </strong>is the one that looks at all the given images and finds a pattern. Do not worry if you come across complicated keywords. Everything will start making sense once you run the model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e28a423edabda008bd8d03d23c1c1a02/href">https://medium.com/media/e28a423edabda008bd8d03d23c1c1a02/href</a></iframe><p>It is important to note that on line 4, we are reserving 20% of the data to check our model at the end of each <strong>training iteration </strong>called an <strong>epoch</strong>.</p><p>Create a <strong>DataLoaders </strong>object called <strong>dls</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73d3700d07fc7c918817d5890ef90b57/href">https://medium.com/media/73d3700d07fc7c918817d5890ef90b57/href</a></iframe><p>Take a look at a few images from a single <strong>batch </strong>in the <strong>dls </strong>object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b49b7a0401a6df2900383b4ebb8f227f/href">https://medium.com/media/b49b7a0401a6df2900383b4ebb8f227f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SfT7bKGEyfApHdr9OQ95sA.png" /><figcaption>cute ^w^ (Screenshot by author)</figcaption></figure><p>Call for a <strong>Learner </strong>that learns based on the input images in 4 different training iterations or <strong>epochs. </strong>This should take some time depending on your network speed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d8842c0d33d7ac898f1e1830c28421a/href">https://medium.com/media/7d8842c0d33d7ac898f1e1830c28421a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*vgwT4qHfJ7WoqlIrvQsFvw.png" /><figcaption>Screenshot by author</figcaption></figure><p>You will probably not see the same results that are in the image, but the error rate should be well less than 0.05.</p><p>Now, let us plot a confusion matrix for our validation set. Confusion matrix tells us how many of the images in the validation set were correctly or incorrectly classified.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1effe558a248653217f06cae967bb319/href">https://medium.com/media/1effe558a248653217f06cae967bb319/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*b91Tq0BVAPCUZPxyTPYBlQ.png" /><figcaption>Screenshot by author</figcaption></figure><p>The confusion matrix speaks about how good our Classifier is. The numbers indicated on <strong>dark blue diagonal</strong> tells us the number of correctly predicted images. Hence our Classifier is doing a pretty good job!</p><p>We shall get to see the two incorrectly predicted pictures in the next line of code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37c7ffd1a698949b2444dd7a9f9ac961/href">https://medium.com/media/37c7ffd1a698949b2444dd7a9f9ac961/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*12hc0oND2LjmLr_aHE7RTw.png" /><figcaption>Screenshot by author</figcaption></figure><p>Now let us put our model to the real test, shall we? Download a cat or dog image of your choice and save it locally.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a543e26897c1685e6db7bf80d56ff87c/href">https://medium.com/media/a543e26897c1685e6db7bf80d56ff87c/href</a></iframe><p>This would create a widget inside your Jupyter notebook that looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*ax9SQ_gAj0w-Kf_n5Zy8jg.png" /><figcaption>Screenshot by author</figcaption></figure><p>Click on upload and choose your image.</p><p>Now, let us resize the image so that our model can predict. The last line displays the resized image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9578b5a582d1c61f6ce7ee8403cb06b/href">https://medium.com/media/c9578b5a582d1c61f6ce7ee8403cb06b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*aatBHuO-6LHaPP9YH3YSlA.png" /><figcaption>Screenshot by author</figcaption></figure><p>The following code predicts the animal in the image and displays it in a format that we can understand.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a361b46b8d8546f7d845d43717364c9f/href">https://medium.com/media/a361b46b8d8546f7d845d43717364c9f/href</a></iframe><p>……………….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*mrktf4Izesc4od1DocC1ng.png" /><figcaption>Screenshot by author</figcaption></figure><p>YES!!!</p><p>With just ~200 images, we were able to create a model that could classify cats and dogs. Now you can go ahead and make changes to create your classifier! You can also work with three or more categories.</p><p>Hope you find this post helpful. If you get stuck, please feel free to comment or contact me by mail at <a href="/service/mailto:adityasrinivas.menon@gmail.com">adityasrinivas.menon@gmail.com</a>.</p><h4>Notebook on GitHub:</h4><p><a href="https://github.com/karynaur/fast.ai">karynaur/fast.ai</a></p><h3>fast.ai Courses</h3><p>All thanks to fast.ai courses, forum and the entire community and its efforts to make Deep Learning available for everyone.</p><ul><li><a href="https://course.fast.ai/">Practical Deep Learning for Coders - Practical Deep Learning</a></li><li><a href="https://forums.fast.ai/">fast.ai Course Forums</a></li></ul><p>Special thanks to <a href="https://twitter.com/jeremyphoward"><strong>Jeremy Howard</strong></a><strong>.</strong></p><p>Keep clapping!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4469ffed086c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/classifying-cats-vs-dogs-a-beginners-guide-to-deep-learning-4469ffed086c">Classifying Cats vs Dogs. A Beginner’s guide to Deep Learning</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>