<?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 Anirudh S on Medium]]></title>
        <description><![CDATA[Stories by Anirudh S on Medium]]></description>
        <link>https://medium.com/@baakchsu.sprx77?source=rss-1a92cae35860------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vjcgLb5Wkse0Z8Vg2LnkpQ.jpeg</url>
            <title>Stories by Anirudh S on Medium</title>
            <link>https://medium.com/@baakchsu.sprx77?source=rss-1a92cae35860------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 06:56:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@baakchsu.sprx77/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[Understanding 1x1 Convolutions]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science-collective/understanding-1x1-convolutions-492c5ad67d34?source=rss-1a92cae35860------2"><img src="https://cdn-images-1.medium.com/max/2600/1*xtrx3yCF-2P-HkE1t6W6kg.jpeg" width="3168"></a></p><p class="medium-feed-snippet">Deep convolution networks often follow a specific structural pattern: the number of filters increases from the first layer to the last&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/data-science-collective/understanding-1x1-convolutions-492c5ad67d34?source=rss-1a92cae35860------2">Continue reading on Data Science Collective »</a></p></div>]]></description>
            <link>https://medium.com/data-science-collective/understanding-1x1-convolutions-492c5ad67d34?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/492c5ad67d34</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Fri, 27 Mar 2026 15:30:18 GMT</pubDate>
            <atom:updated>2026-04-08T18:10:10.213Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Xception: Meet The Xtreme Inception]]></title>
            <link>https://medium.com/data-science-collective/xception-meet-the-xtreme-inception-db569755f4d6?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/db569755f4d6</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[xception]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Wed, 12 Aug 2020 20:02:15 GMT</pubDate>
            <atom:updated>2020-08-12T20:02:15.147Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxigSAUM_XPHvMb3UEpWyQ.jpeg" /><figcaption>Xception The Xtreme Inception - <em>Image by author</em></figcaption></figure><h3>Intro to Xception</h3><p>Xception - the Extreme Inception! Sounds cool and Xtreme! But “Why such a name?”, one might wonder! One obvious thing is that the author Francois Chollet (creator of Keras) had been inspired by the Inception architecture. He tells about how he sees the Inception architecture in his abstract, which I’ve quoted below.</p><blockquote>We present an interpretation of Inception modules in convolutional neural networks as being an intermediate step in-between regular convolution and the depthwise separable convolution operation</blockquote><blockquote>-Francois Chollet in the Xception paper</blockquote><p>Another new deep learning term for today, “<strong><em>depthwise separable convolution</em></strong>”. Now, I know that most of you would’ve guessed that it’s some kind of layer. But for those who have never heard it, fret not. I’ve got you covered in this post. We’ll go deeper about what’s a <strong><em>depthwise separable convolution </em></strong>and how it’s used to build the Xception model. And also more on how it builds upon the Inception hypothesis. As always, I’ll try to throw in more illustrations to make the details clear. Let’s dive in!</p><h3>The Inception Hypothesis</h3><p>The Inception-style architecture was introduced in the “<strong><em>Going Deeper With Convolution</em></strong>” paper. The authors called the model introduced in the paper as <strong><em>GoogLeNet,</em></strong> which used the Inception blocks. It was a novel and innovative architecture and it still is. Also, it got much attention as many architectures at that time were stacks of more and more layers to increase network capacity. Inception, on the other hand, was more creative and slick!</p><p>Rather than just going deeper by simply adding more layers, it also went wide. We’ll see what I mean by “wide” shortly. The Inception blocks take in an input tensor and perform a combination of convolution and pooling in parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X9LVSn6hGI8TzXvQwgHtiw.png" /><figcaption><em>Image by author</em></figcaption></figure><p>For people who have seen or read the Inception papers, you might find that this is not exactly like an Inception block. Yeah, you’re right! I’ve just illustrated it this way so that everybody gets a rough idea of what it does. You can call it “the naive version of inception” as the authors called it.</p><p>Now, the actual Inception block is a little bit different in terms of the number of convolutions, their size, and how they’re layered. But this naive illustration conveys what I meant by “wide” before. The block performs convolution with different filter sizes in parallel. And the output tensors are concatenated along the channel dimension i.e stacked one behind the other.</p><h4>Going a Bit Deeper</h4><p>Now that you’ve seen the parallel convolution block, I shall go a bit deeper about the block above. The input tensor which is processed by the different convolution would be of shape (BatchSize, Height, Width, Channels). In the Inception block, the input tensor’s channel dimension is reduced using 1x1 convolution before applying 3x3 or 5x5 convolutions. This reduction in channel size is done to reduce the computation when feeding this tensor to the subsequent layers. You can find this concept explained in great detail in the <a href="https://hackerstreak.com/1x1-convolution/">1x1 convolution</a> article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*rz8ZBCgaZevH0tyUQeyNbw.jpeg" /><figcaption><em>Image by author</em></figcaption></figure><p>Again, this is just another depiction to understand and is not drawn to be the same as the original Inception block. You can give or take some layers, maybe even wider, and make your own version. The input tensor is individually processed by the three convolution towers. And the three separate output tensors are concatenated along the channel dimension. The GoogLeNet uses multiple Inception block and a few other tricks and tweaks to achieve its performance. I believe, now you get the idea of what an Inception block does.</p><p>Next, we shall remodel the above block to make it <strong>“Xtreme”!</strong></p><h4>Making it Xtreme</h4><p>We’ll replace the three separate <strong><em>1x1</em></strong> layers in each of the parallel towers, with a single layer. It’ll look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j8ft9DSe8qussMoXf_v4lA.png" /><figcaption>Inception block with a <em>common 1x1 layer - Image by author</em></figcaption></figure><p>Rather than just passing on the output of the 1x1 layer to the following 3x3 layers, we’ll slice it and pass each channel separately. Let me illustrate with an example. Say, the output of the <strong><em>1x1</em></strong> layer is of shape <strong><em>(1x5x5x5). </em></strong>Let’s not consider the batch dimension and just see it as a <strong><em>(5x5x5)</em></strong> tensor. This is <em>sliced </em>along the channel dimension as shown below and fed separately to the following layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mRXnFyYhSvEI5Fneqrq4jg.jpeg" /><figcaption><em>Slicing of the output tensor of 1x1 convolution along the channel dimension - Image by author</em></figcaption></figure><p>Now, each of the slices is passed on to a separate 3x3 layer. This means that each 3x3 block will have to process a tensor of shape <strong><em>(5x5x1). </em></strong>Therefore, there’ll be 5 separate convolution blocks, one for each slice. And each of the convolution blocks will just have a single filter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2q3iGl-EqpZcaBuVgHRrA.jpeg" /><figcaption>Each channel slice is fed to a separate 3x3 layer with just one filter each - <em>Image by author</em></figcaption></figure><p>This same process scales up to bigger input tensors. If the output of the pointwise convolution is <strong><em>(5x5x100), </em></strong>there’d be 100 convolution blocks each with one filter. And all of their output would be concatenated at last. This way of doing convolution is why it’s named <strong><em>EXTREME </em></strong>as each channel of the input tensor is processed separately.</p><p>We have seen enough about the idea of Inception and an <strong><em>Xtreme </em></strong>version of it too. Now, let’s venture further to see what powers the Xception architecture.</p><h3>Depthwise Separable Convolution: That which powers Xception</h3><p>The depthwise separable convolution layer is what powers the Xception. And it heavily uses that in its architecture. This type of convolution is similar to the extreme version of the Inception block that we saw above. But differs slightly in its working. Let’s see how!</p><p>Consider a typical convolution layer with ten 5x5 filters operating on a <strong><em>(1x10x10x100)</em></strong> tensor. Each of the ten filters is of shape <strong><em>(5x5x100)</em></strong> and slides over the input tensor to produce the output. Each of the 5x5 filters covers the whole channel dimension (the entire 100) as it slides over the input. This means a typical convolution operation encompasses both the spatial (height, width) and the channel dimensions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*tGfXDs3Z_1ORpTVU9Z28eg.png" /><figcaption>A 5x5 convolution filter sliding over a (10x10x100) tensor — <em>Image by author</em></figcaption></figure><p>If you’re not familiar with convolutions, I suggest you go through <a href="https://hackerstreak.com/1x1-convolution/#how-convolution-works">How Convolution Works?</a></p><p>A depthwise separable layer has two functional parts that split the job of a conventional convolution layer. The two parts are depthwise convolution and pointwise convolution. We’ll go through them one by one.</p><h4>Depthwise Convolution</h4><p>Let’s take an example of a depthwise convolution layer with <strong><em>3x3</em></strong> filters that operate on an input tensor of shape <strong><em>(1x5x5x5)</em></strong>. Again, let’s lose the batch dimension for simplicity as it doesn’t change anything and consider it as a <strong><em>(5x5x5) </em></strong>tensor. Our depthwise convolution will have five <strong><em>3x3</em></strong> filters one for each channel of the input tensor. And each filter will slide spatially through a single channel and generate the output feature map for that channel.</p><p>As the number of filters is equal to the number of channels of the input, the output tensor will also have the same number of channels. Let’s not have any zero paddings in the convolution operation and keep the <strong><em>stride </em></strong>as <em>1.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*wdpF-BvSVAUZgiG2dZXIHA.jpeg" /><figcaption>Output Height and Width calculation after applying a convolution - <em>Image by author</em></figcaption></figure><p>Going by the formula for the output size after convolution, our <strong><em>(5x5x5)</em></strong> will become a <strong><em>(3x3x5) </em></strong>tensor. The illustration below will make the idea clear!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pOvIrlOYR3C_jRKRuK2VnA.jpeg" /><figcaption><em>Illustration of Depthwise Convolution Operation - Image by author</em></figcaption></figure><p>That’s Depthwise Convolution for you! You can see that it’s almost similar to the way we did the <strong><em>Xtreme </em></strong>convolution in the Inception.</p><p>Next up, we have to feed this output tensor to a pointwise convolution which performs <strong>cross-channel correlation</strong>. It simply means that it operates across all the channels of the tensor.</p><h4>Pointwise Convolution</h4><p>The pointwise convolution is just another name for a <strong><em>1x1</em></strong> convolution. If we ever want to increase or decrease the depth (channel dimension) of a tensor, we can use a pointwise convolution. That’s why it was used in the Inception block to reduce the depth before the <strong><em>3x3</em></strong> or <strong><em>5x5</em></strong> layers. Here, we’re gonna use it to increase the depth. But how?</p><p>The pointwise convolution is just a normal convolution layer with a filter size of <strong>one </strong>(<strong><em>1x1</em></strong> filters). Therefore, it doesn’t change the spatial output size after convolution. In our example, the output tensor of the depthwise convolution has a size of <strong><em>(8x8x5)</em></strong>. If we apply 50 <strong><em>1x1 </em></strong>filters, we’ll get the output as <strong><em>(8x8x50)</em></strong>. And <strong><em>RELU </em></strong>activation is applied in the pointwise convolution layer.</p><p>See <a href="https://hackerstreak.com/1x1-convolution/">Pointwise Convolution</a> for more detailed illustrations and its advantages.</p><p>Combining the depthwise convolution and pointwise convolution, we get the <strong>Depthwise Separable Convolution. </strong>Let’s just call it <strong><em>DSC </em></strong>from here.</p><h3>Differences between DSC and the Xtreme Inception</h3><p>In the Inception block, first comes the pointwise convolution followed by the 3x3 or 5x5 layer. Since we’d be stacking the <strong><em>DSC </em></strong>blocks on above the other, the order doesn’t matter much. The Inception block applies an activation function on both pointwise and the following convolution layers. But in the <strong><em>DSC, </em></strong>it’s just applied once, after the pointwise convolution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*JOf5EqRV-1_OOLrpVbtSjw.png" /><figcaption><em>Image by author</em></figcaption></figure><p>The Xception author discusses the effect of having activation on both the depthwise and pointwise steps in the DSC. And has observed that learning is faster when there’s no intermediate activation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*yYD89YUNNefgoXhEZizhTw.png" /><figcaption><em>Illustration of DSC with and without having an intermediate activation - Image by author</em></figcaption></figure><h3>Xception Architecture</h3><p>The author has split the entire Xception Architecture into 14 modules where each module is just a bunch of <strong><em>DSC</em></strong> and pooling layers. The 14 modules are grouped into three groups viz. the entry flow, the middle flow, and the exit flow. And each of the groups has four, eight, and two modules respectively. The final group, i.e the exit flow, can optionally have fully connected layers at the end.</p><blockquote>Note: All the DSC layers in the architecture use a filter size of 3x3, stride 1, and “same” padding. And all the MaxPooling layers use a 3x3 kernel and a stride of 2.</blockquote><h4>Entry Flow of Xception</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0CmbFm5F65hNaDWJz_lBrw.png" /><figcaption>Entry Flow of Xception - <em>Image by author</em></figcaption></figure><p>The above illustration is a detailed version of the one given in the Xception paper. Might seem intimidating at first but look again, it’s very simple.</p><p>The very first module contains conventional convolution layers and they don’t have any <strong><em>DSC </em></strong>ones. They take input tensors of size (-1, 299, 299, 3). The <strong>-1 </strong>in the first dimension represents the batch size. A negative <strong>-1 </strong>just denotes that the batch size can be anything.</p><p>And every convolution layer, both conventional and DSC, is followed by a Batch Normalization layer. The convolutions that have a stride of 2 reduces it by almost half. And the output’s shape is shown by the side which is calculated using the convolution formula that we saw before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*ZWI303_D2qufnhWYe0vzlg.png" /><figcaption>First Convolution Module in the Entry Flow - <em>Image by author</em></figcaption></figure><p>Excluding the first module, all the others in the entry flow have residual skip connections. The parallel skip connections have a pointwise convolution layer that gets added to the output from the main path.</p><h4>Middle Flow of Xception</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*7O1bA4kBpXNidl-pfLWOkQ.png" /><figcaption><em>Illustration of middle flow - Image by author</em></figcaption></figure><p>In the middle flow, there are <strong><em>eight</em></strong> such modules, one after the other. The above module is repeated eight times to form the middle flow. All the 8 modules in the middle flow use a stride of 1 and don’t have any pooling layers. Therefore, the spatial size of the tensor that’s passed from the entry flow remains the same. The channel depth remains the same too as all the middle flow modules have 728 filters. And that’s the same as the input’s depth.</p><h4>Exit Flow of Xception</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*s2qanUKZP9ku8J6LNHkwHQ.png" /><figcaption>Exit Flow - <em>Image by author</em></figcaption></figure><p>The exit flow has just two convolution modules and the second one doesn’t have any skip connection. The second module uses Global Average Pooling, unlike the earlier modules which used Maxpooling. The output vector of the average pooling layer can be fed to a logistic regression layer directly. But we can optionally use intermediate Fully Connected layers too.</p><h3>To Sum Up</h3><p>The Xception model contains almost the same number of parameters as the Inception V3 but outperforms Inception V3 by a small margin on the ImageNet dataset. But it beats Inception V3 with a better margin on the JFT image classification dataset (Google’s internal dataset). Performing better with almost the same number of parameters can be attributed to its architecture engineering.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db569755f4d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science-collective/xception-meet-the-xtreme-inception-db569755f4d6">Xception: Meet The Xtreme Inception</a> was originally published in <a href="https://medium.com/data-science-collective">Data Science Collective</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[YOLO Made Simple: Interpreting the You Only Look Once Paper]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science-collective/yolo-made-simple-interpreting-the-you-only-look-once-paper-55f72886ab73?source=rss-1a92cae35860------2"><img src="https://cdn-images-1.medium.com/max/2600/1*c0YBYFrm86EWCTJ201xB_g.jpeg" width="3648"></a></p><p class="medium-feed-snippet">Going through the nitty-gritty details in the paper and facts that are often overlooked in a simple and clean way!</p><p class="medium-feed-link"><a href="https://medium.com/data-science-collective/yolo-made-simple-interpreting-the-you-only-look-once-paper-55f72886ab73?source=rss-1a92cae35860------2">Continue reading on Data Science Collective »</a></p></div>]]></description>
            <link>https://medium.com/data-science-collective/yolo-made-simple-interpreting-the-you-only-look-once-paper-55f72886ab73?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/55f72886ab73</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[yolo]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Sun, 02 Feb 2020 13:18:12 GMT</pubDate>
            <atom:updated>2020-02-04T04:16:46.186Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Accelerate Model Training With Batch Normalization]]></title>
            <link>https://medium.com/data-science-collective/accelerate-model-training-with-batch-normalization-a8c9374c2d72?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8c9374c2d72</guid>
            <category><![CDATA[batch-normalization]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[deep-neural-networks]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Sat, 11 Jan 2020 14:51:13 GMT</pubDate>
            <atom:updated>2020-01-11T17:48:08.672Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is batch normalization and how does it work? What enables it to achieve faster training? This post aims to answer the above questions with illustrations that help to develop an intuition.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x7tVUKdpJaYNkq3aLnk0og.jpeg" /><figcaption><a href="https://hackerstreak.com/batch-normalization-how-it-really-works/"><strong>Accelerate Model Training With Batch Normalization</strong></a> Image Credits: Photo by Sebastiaan<a href="https://unsplash.com/@chillarea?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"> stam</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The Batch Normalization paper published back in 2015 by <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Ioffe%2C+S">Sergey Ioffe</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Szegedy%2C+C">Christian Szegedy</a> took the deep learning community by storm. It became one of the most implemented techniques in deep learning after it was released. Notably, its ability to accelerate training of deep learning models and achieve the same accuracy in 14 times fewer training steps was a great catch. Indeed that brought in the attention which it gets today (who doesn’t want to train faster?). And there’s been a lot of similar papers like layer normalization, instance normalization and a few others. These try to overcome some of the disadvantages of batch normalization.</p><p>Let’s dive in!</p><h3>What Is Normalization?</h3><p>Let’s start by understanding what the typical input data normalization does. Though deep learning models don’t need much feature engineering, scaling input features to the same range helps to train the model faster. Min-Max scaling is one such feature scaling methods that brings all the inputs to the 0 to 1 range. It’s given by the following expression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/0*a8XFwqA9KxsR0Zc4" /><figcaption>Min-Max Normalization</figcaption></figure><p>It’s simply subtracting the least value in the data from each data point and dividing it by the range of the values in the data. The range is just the maximum value in the input data distribution minus the least value. You could intuitively try to digest how this improves model training with the following example. Consider a machine learning model that takes the square footage and the number of rooms in a house as input and predicts the price.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dQl42rnsjj8dIu9s" /></figure><p>While the square footage of houses may be in hundreds or thousands, the number of rooms would mostly be under 10. Owing to this vast difference in range between the different input features, the model struggles converge. This is because the model values larger features more than the smaller ones.</p><p>One more thing to notice is that a normalized feature column won’t have large variations in values like the unnormalized version. For example, a mansion may have thousands of square feet of land but a small house may be only a few hundred square feet. While the model should value houses with larger area more, too big a difference will hinder the model’s learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BZT-T9m_ACBrXw0n" /></figure><h3>What’s Batch Normalization Then?</h3><h3>The Short Version</h3><p>We saw how an ML model can benefit from normalized input features in the previous section. Now, let’s consider a fully connected deep neural network as our model. And, what if we added normalization for the hidden layers too? Technically, each hidden layer’s input is the previous layer’s output. It seems about right to apply normalization to that too, right? Yes. And that’s what batch normalization does.</p><h3>The Long Version</h3><p>In naive gradient descent, we feed one input at a time and update the model using backpropagation. In contrast, using mini-batch gradient descent, we pass forward and back a bundle of input examples at once. And this bundle is what we call the “<strong><em>batch</em></strong>“. Therefore, each hidden layer would see batches of activations coming from the layer before. Let’s explore further with an example.</p><h4>Fully Connected Network For MNIST</h4><p>Consider a fully connected neural network with 2 hidden layers and let’s assume we’re training it on the MNIST digits dataset. In case you’re not familiar with MNIST, the dataset contains 28×28 sized images of digits from 0 to 9. The digit’s image is given as input to the model by ravelling it into a long 1D vector of size 784. While training in batches, multiple digit image vectors are stacked and fed. Given that, the input shape becomes (batch_size, 784).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_XBXYCW2b0LesY_I" /><figcaption>Dimensions of input data</figcaption></figure><p>The above illustration’s batch size is 2 and the number of features in each of the inputs is 784 (pixels). For simplicity’s sake, let’s assume that each layer of our model has 50 neurons each. Therefore, the activations of the first hidden layer would be of shape (2,50). Because 2 is our batch size and 50 is the number of neurons. If our batch size is 64, then the shape would be (64,784)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*jQc4HMbERaI_CXfh" /><figcaption>The shape of 1st Hidden Layer’s Activations</figcaption></figure><p>Further, let’s dive in to inspect the activation of a single neuron of the first hidden layer. A single neuron will produce just one activation if only one input was given. However, if the inputs are given in batches, it produces activation of shape (batch_size,1). Such that 1 activation for each input in the batch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*EAXWrGI7dCFJZ-tf" /></figure><h4>Adding a Batch Normalization Layer</h4><p>Normally, each layer in a network would apply a non-linear activation function like ‘<strong><em>Sigmoid</em></strong>‘ after multiplying the input with the weights. If we’re inserting a batch normalization layer, we have to add it before we apply the non-linearity. The batch normalization layer normalizes the activations by applying the standardization method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*UqV2ycvkfSMMReL5" /><figcaption>μ is the mean and σ is the standard deviation</figcaption></figure><p>It subtracts the mean from the activations and divides the difference by the standard deviation. The standard deviation is just the square root of variance. In detail, we calculate the mean and standard deviation across the batch and subtract and divide respectively.</p><p>You see, the layers adjust their parameters during backpropagation with the assumption that their input distribution stays the same. But all the layers are updated with backpropagation thereby changing each of their outputs.</p><p>This changes the inputs of the layers as each layer gets its inputs from the previous layer. This is called <strong><em>internal covariate shift</em></strong>. Hence, the layers experience a constantly changing input distribution. Importantly, this normalization method standardizes the distribution of the output activations.</p><p>Therefore it produces the desired distribution of activations that don’t vary too much when the weights change. In other words, it minimizes the <strong><em>internal covariate shift </em></strong>and aids the model to learn faster.</p><p>Although this is regarded as the main contributor for batch normalization’s success, some papers attribute it to other factors. <a href="https://arxiv.org/pdf/1805.11604.pdf">This </a>paper tells about how the batch normalization layer smoothens the loss curve which helps train faster.</p><h4>Understanding With An Example</h4><p>Again, let’s just consider the activation of a single neuron for clarity and set the batch size as 64. And, our two hidden layers have 50 neurons each. As we saw before, the output of a single neuron would be (64,1). The batch normalization layer computes the mean (μ) and standard deviation (σ) of the 64 values in the batch.</p><p>It is this μ value that is subtracted from the 64 values. And the σ divides the mean subtracted activations from the neuron. Rather, let’s visualize what the batch normalization layer does.</p><p><strong>Note: This explanation is just for a single neuron in a layer for simplicity.</strong></p><p><strong>Batch Normalization’s output considering all the neurons differs from this in shape but all the processes are the same.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DHZ9yf_p4Z6AMCTc" /><figcaption>The flow of activation of a single neuron through a batch normalization layer</figcaption></figure><p>If we consider all the neurons in a layer, the activation shape would be (64,50). The mean and variance would then be calculated for each of the 50 neurons leading to 50 means and 50 variance values. And each column corresponding to a single neuron should be normalized with its respective mean and variance.</p><h3>Effects of Batch Normalization On Activations</h3><p>Well, we’ve successfully normalized a hidden layer’s activations and all that’s left is applying the nonlinearity, right?</p><p><strong><em>No. It does not end here!</em></strong></p><p>Well, there’s a problem with normalizing the activations of a hidden layer. If we’re feeding the normalized activations to a Sigmoid, the expressive power of the model is reduced. To clarify, the normalized activations are brought a 0 mean and unit variance distribution. Because of that, the activations fall in the linear region of the sigmoid non-linearity. We’ll go ahead and try to plot the functions using matplotlib and Python.</p><pre>import numpy as np</pre><pre>import matplotlib.pyplot as plt</pre><pre>def sigmoid(x):</pre><pre>return 1 / (1 + np.exp(-x))</pre><pre>x = np.arange(-10,11,1)        #creates a distribution [-10,-9,-8,.........7,8,9,10]</pre><pre>x_sig = sigmoid(x)</pre><pre>plt.plot(x,x_sig)</pre><pre>plt.scatter(x,x_sig)</pre><p>Consider this data distribution as the activation that we are gonna normalize. Below, the diagram shows the plot of the sigmoid of unnormalized data [-10 to 10].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*0CJqMLXgnZo1VqhS" /></figure><p>Now, we’ll normalize the same data and see where the data points fall on the curve. And, here’s the Python code to do that.</p><pre>import numpy as np</pre><pre>import matplotlib.pyplot as plt</pre><pre>def sigmoid(x):</pre><pre>return 1 / (1 + np.exp(-x))</pre><pre>x = np.arange(-10,11,1)        #creates a distribution [-10,-9,-8,.........7,8,9,10]</pre><pre>x_sig = sigmoid(x)</pre><pre>plt.plot(x,x_sig)      #to plot the blue, unnormalized sigmoid curve</pre><pre>x = (x-np.mean(x))/np.std(x) #standardizing the data</pre><pre>x_sig = sigmoid(x)</pre><pre>plt.scatter(x,x_sig)#plot the standardized values  (red data points)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tPSfbtV7ILH0IN-I" /><figcaption>The normalized points fall on the linear region of the sigmoid curve</figcaption></figure><h3>Parameters of Batch Normalization</h3><p>As we saw, batch normalization doesn’t let the model take advantage of the sigmoid non-linearity. This makes the sigmoid layer almost linear and we don’t want that! So, the authors of this paper gave a way to restore the representation power of the network. They introduced two parameters that shift and scale the normalized activations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/0*-3U7fQO-Hcu3YnTV" /><figcaption>The gamma and beta parameters are learned with backpropagation. x_hat is the normalized activation</figcaption></figure><p>These two parameters are introduced so that they can undo the normalization done by the batch normalization layer. If <em>Gamma </em>is set to σ and <em>Beta </em>to the mean, it’ll fully denormalize the activations back to normal. At first, it may seem counter-intuitive. But this is done to enable the model to adjust the normalization the way it wants if that reduces the loss. And this brings back the expressive power that is lost when we normalized the activations.</p><p>One of the key advantages of using batch normalization is that it is fully differentiable. So, this means that you can backpropagate through it and can train using gradient descent. That said, we can update <em>Gamma </em>and <em>Beta </em>along with the model parameters.</p><h3>Batch Normalization For Convolutions</h3><p>Batch normalization after a convolution layer is a bit different. Normally, in a convolution layer, the input is fed as a 4-D tensor of shape (batch, Height, Width, Channels). But, the batch normalization layer normalizes the tensor across the batch, height and width dimensions. For each channel, mean and variance are computed across the other three dimensions. So, that gives ‘C’ number of mean and variance values which are then used for normalization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/0*6mZhqm1InFHSaNSL" /><figcaption>For each channel in the tensor, the mean and variance are calculated across all the other dimensions</figcaption></figure><p>As a result, for each of the 64 channels in the above example, mean and variance are calculated for the 10x200x200 (400000) values. This gives us 64 means and 64 variances to normalize each channel separately.</p><h3>Advantages of Batch Normalization</h3><p>Batch normalization can prevent a network from getting stuck in the saturation regions of a non-linearity. It also helps the weights in a layer to learn faster as it normalizes the inputs. You see, a large input value (X) to a layer would cause the activations to be large for even small weights. This pushes the activations to the saturation regions of sigmoid or tanh nonlinearities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*eiQiVrvkamS2jqh-" /><figcaption>The derivative is almost zero near the saturation regions of the sigmoid</figcaption></figure><p>Saturated activations have very small derivatives. Hence, they make very small gradient steps and slow down the training. This effect grows more as the network depth increases. But batch normalization puts the activations where it wants it to be on the sigmoid.</p><p>And this prevents the network from getting stuck due to saturation. Also, it removes the necessity to initialize the model parameters carefully. Sometimes random initialization makes some weight values high that pushes the activations into saturation. But batch normalization allows us to be less careful about initialization.</p><p>The authors claim that it also regularizes the model and reduces the need for Dropout. This regularization effect is due to normalization with mini-batch statistics (which introduces some noise) rather than the population statistics. If the batch size is very large, the regularization effect diminishes.</p><p>Since the gradients’ dependency on the scale of the weights is reduced, it allows us to use higher learning rates.</p><h3>Reference</h3><p>Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift <a href="https://arxiv.org/pdf/1502.03167.pdf">https://arxiv.org/pdf/1502.03167.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8c9374c2d72" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science-collective/accelerate-model-training-with-batch-normalization-a8c9374c2d72">Accelerate Model Training With Batch Normalization</a> was originally published in <a href="https://medium.com/data-science-collective">Data Science Collective</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linear Regression with Only Python and Numpy]]></title>
            <link>https://medium.com/data-science/linear-regression-with-python-and-numpy-25d0e1dd220d?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/25d0e1dd220d</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[numpy]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Wed, 08 Jan 2020 16:29:56 GMT</pubDate>
            <atom:updated>2020-01-10T10:28:22.981Z</atom:updated>
            <content:encoded><![CDATA[<h4>Writing a machine learning model just with Numpy and Python</h4><p>In this post, we’ll see how to implement linear regression in Python without using any machine learning libraries. In <a href="https://hackerstreak.com/linear-regression/">another post</a>, we saw how the linear regression algorithm works in theory. With the rise in popularity of machine learning libraries, anyone can implement ML algorithms with a few API calls. But only a few dig deeper to understand how the algorithms work under the hood. We’ll program linear regression in Python and train it!</p><h3>Linear Regression In Python Code</h3><p>We’ll be coding the linear regression algorithm and train it with Python language. If you already have programming experience but new to Python, it would be an absolute breeze. However, if you don’t have any programming experience, I’d suggest you go through Python’s documentation. Just to get a basic understanding of programming and language syntax. Moreover, Python is really intuitive and you’d be up and running in no time. And Google’s Colab is a nice way to run your program as it is hassle-free when installing libraries. But if you have Python IDE already, you’re welcome to skip. Alright then. Without further ado let’s jump right to the code.</p><h4>Importing Python Libraries</h4><pre><strong>import numpy as np<br>import matplotlib.pyplot as plt<br>import pandas as pd<br>import time</strong></pre><p>First, we import Numpy, the most needed matrix operation library. Then, ‘matplotlib’ is for sketching plots and graphs. This is to visualize our model’s fitting and performance. And Pandas helps to easily load datasets (csv, excel files) into pandas data frames. We’ll use these to load the dataset, initialize a linear regression model and train it.</p><pre><strong>df = pd.read_csv(“https://raw.githubusercontent.com/Baakchsu/LinearRegression/master/weight-height.csv”)</strong></pre><p>This line pulls the “predict the height with weight” dataset from my Github repo and reads it into a data frame.</p><p>Up next, we’ll create a class for the linear regression model just like scikit’s model. Its model can be called with “sklearn.linear_model.LinearRegression” method. But, we’ll make it as just <strong>LinearRegression</strong>. Let’s see how it comes up.</p><h4>Creating a Linear Regression Class</h4><pre><strong>class LinearRegression:<br> def fit(self,X,Y):<br> X=np.array(X).reshape(-1,1)<br> Y=np.array(Y).reshape(-1,1)<br> x_shape = X.shape<br> self.parameter_cache = []<br> num_var = x_shape[1]       #the shape corresponds to number of input variable dimensions. There’s only one for this dataset i.e weight of person<br> self.weight_matrix = np.random.normal(-1,1,(num_var,1))<br> self.intercept = np.random.rand(1)<br> for i in range(50):<br> self.dcostdm = np.sum(np.multiply(((np.matmul(X,self.weight_matrix)+self.intercept)-Y),X))*2/x_shape[0] #w.r.t to the weight<br> self.dcostdc = np.sum(((np.matmul(X,self.weight_matrix)+self.intercept)-Y))*2/x_shape[0]          #partial derivative of cost w.r.t the intercept<br> self.weight_matrix -= 0.1*self.dcostdm                                                                  #updating the weights with the calculated gradients<br> self.intercept -= 0.1*self.dcostdc                                                                      #updating the weights with the calculated gradients<br> self.parameter_cache.append(np.array((self.weight_matrix,self.intercept)))                             #the parameters are cached just to track the progress<br> return self.weight_matrix,self.intercept,self.parameter_cache<br> def predict(self,X):<br> product = np.matmul(np.array(X).reshape(-1,1),self.weight_matrix)+self.intercept<br> return product</strong></pre><p>Turned out pretty simple right? Let us go through the code to understand a bit more in detail. Here, we define the <strong>LinearRegression</strong> class with two methods “Fit” and “Predict”. Yes, they do what their names suggest. The fit method takes in the input feature vector (weight of people) and output variable (Height of people ) and trains the Linear model to get a perfect fit.</p><pre><strong>X=np.array(X).reshape(-1,1)<br>Y=np.array(Y).reshape(-1,1)<br>x_shape = X.shape<br>num_var = x_shape[1]</strong></pre><p>We convert the arguments X and Y into numpy arrays and reshape them into the shape (number of data, number of feature variables). Since we’re predicting the height with just weight, the number of variables is just one (num_var). We do the same for the output target variable Y.</p><h4>Parameter Initialization with Numpy</h4><pre><strong>self.weight_matrix = np.random.normal(-1,1,(num_var,1))<br>self.intercept = np.random.rand(1)<br>self.parameter_cache = [ ]</strong></pre><p>After that, we initialize the weight matrix and the intercept variable. The weight matrix will have a shape (num_var, number of output variables to predict). Hence, for our problem, it turns out to be (1,1). Because both the number of input and output variables are just one (weight is just one variable and so is the height). If we were to predict the height of a person with the weight and gender of the input, we’d have two input and one output variables. Hence, the weight matrix will have a shape (2,1).</p><p>An intercept is just a single number that denotes the Y-intercept of our linear regression model. If you’re confused about what is weight and intercept, check out the <a href="https://hackerstreak.com/linear-regression/">previous article</a> that explains in detail. We use parameter_cache to cache our model parameters as we train.</p><h4>Training the Linear Regression Model</h4><pre><strong>for i in range(50):<br> self.dcostdm =  np.sum(np.multiply(((np.matmul(X,self.weight_matrix)+self.intercept)-Y),X))*2/x_shape[0] #partial derivative of cost w.r.t the weights<br> self.dcostdc = np.sum(((np.matmul(X,self.weight_matrix)+self.intercept)-Y))*2/x_shape[0]                #partial derivative of cost w.r.t the intercept<br> self.weight_matrix -= 0.1*self.dcostdm                                                                  #updating the weights with the calculated gradients<br> self.intercept -= 0.1*self.dcostdc                                                                      #updating the weights with the calculated gradients<br> self.parameter_cache.append(np.array((self.weight_matrix,self.intercept)))</strong></pre><pre><strong>return self.weight_matrix,self.intercept,self.parameter_cache</strong></pre><p>Finally, we train our model with backpropagation. Here’s the equation of the partial derivative of the cost with respect (w.r.t) to the model parameters from the previous post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*aYJZC0yeNkHKRCDreOmgQQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*NZyR0zwZzINv2uD4ywF6-Q.jpeg" /><figcaption><strong>The partial derivative of cost w.r.t ‘m’ (weight matrix) and the partial derivative of the cost function with respect to ‘C’</strong></figcaption></figure><p>We implement the above equations in the first two lines (dcostdm and dcostdc).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*wvcgOhor355xxH7VcREYEg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*lC2NgEtwOAtSwlcA0W1q8Q.jpeg" /></figure><p>Next, we update the parameters ‘weight_matrix’ and ‘intercept’ with the calculated gradients using the above equations. Then, we append the model parameters to the cache. After the training loop ends, we return the parameters and the cache from the fit function.</p><h4>The Predict Method</h4><pre><strong>def predict(self,X):<br> product =  np.matmul(np.array(X).reshape(-1,1),self.weight_matrix)+self.intercept<br> return product</strong></pre><p>The predict method takes in the input feature and predicts the output with the trained parameters of the Linear regression class. The ‘self.weight_matrix’ and ‘self.intercept’ denote the model parameters that we saw in the fit method. Likewise, we return the predicted values.</p><h4>How do We Use the Model Class?</h4><pre><strong>reg = LinearRegression()</strong></pre><p>Till now, we just created the model class and the training code. Now, we’ll create an instance of the Linear Regression class with the above line of code.</p><pre><strong>x = (df[‘Weight’]-df[‘Weight’].mean())/df[‘Weight’].std() #standardization of the dataset<br>y = (df[“Height”]-df[‘Height’].mean())/df[“Height”].std() #standardization of the dataset</strong></pre><p>These two lines will standardize the dataset using the mathematical formula X-u/std. In that, X is the variable we want to standardize, ‘u’ is the mean of that variable and ‘std’ is the standard deviation. This helps the model learn faster as all the variables will be in the range ( -1 to 1). It centers the mean to 0 and unit standard deviation (std=1).</p><pre><strong>params = reg.fit(x[:-180],y[:-180])</strong></pre><p>By calling the fit method of the class instance ‘reg’ and passing the X and Y values, we initiate the training. Here, we pass the dataset leaving the last 180 data points for testing.</p><h4>Model Visualization</h4><pre><strong>pred = reg.predict(np.array(x[-180:]))</strong></pre><pre><strong>plt.scatter(x[-180:],y[-180:])</strong></pre><pre><strong>plt.plot(x[-180:],pred)</strong></pre><p>Once we finish training, we can predict using our trained regression model. We do that using the ‘reg.predict’ method and we pass in the test data (last 180 data points). Now, the ‘pred’ variable will have the predicted outputs for the test data. In order to visualize how the model fits, we first create a plot with the test data points. Then, we plot the model fitting line with the ‘plt.plot’ function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*kq9d0nhwWd27CBCaEHnp-A.jpeg" /></figure><p>For a linear regression model made from scratch with Numpy, this gives a good enough fit. Notably, from the plot, we can see that it generalizes well on the dataset. Well, it is just a linear model. But knowing its working helps to apply it better.</p><pre><strong>plt.figure(figsize=(19, 10))<br>plt.scatter(x[-180:],y[-180:])<br>for i in list(np.arange(0,50,5)):<br> <br> value = params[2][i]<br> prediction = np.matmul(np.array(x[-180:]).reshape(-1,1),value[0])+value[1]<br> plt.plot(x[-180:],prediction)</strong></pre><p>From the parameter cache that we returned from the ‘fit’ method, we plot the model’s fit during the course of training. The blue line from the top is the initial fit with random parameters before training. As you can see, the model improves its predictive performance as it trains. You can play around with the code, changing initialization conditions and see how the model fit changes. For that, find the code here on the GitHub <a href="https://github.com/Baakchsu/LinearRegression">repository</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JleVu4ZnLQAgtFw-8k3HDw.png" /><figcaption><strong>History showing how the model improved its fit</strong></figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a139faf8d46d13fe046f9b071e2ca33/href">https://medium.com/media/9a139faf8d46d13fe046f9b071e2ca33/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25d0e1dd220d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/linear-regression-with-python-and-numpy-25d0e1dd220d">Linear Regression with Only Python and 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[GAN Pix2Pix Generative Model]]></title>
            <link>https://medium.com/data-science/gan-pix2pix-generative-model-c9bf5d691bac?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9bf5d691bac</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[generative-art]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Wed, 01 Jan 2020 19:38:38 GMT</pubDate>
            <atom:updated>2020-10-10T16:16:54.466Z</atom:updated>
            <content:encoded><![CDATA[<h4>Image-to-image translation with Pix2Pix model</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CWwcld7Md7zyFsdQT6g0ig.jpeg" /><figcaption>Pix2Pix</figcaption></figure><h3>Pix2Pix GAN: Introduction</h3><p>We hear a lot about language translation with deep learning where the neural network learns a mapping from one language to another. In fact, Google translate uses it to translate to more than 100 languages. But, can we do a similar task with images? Certainly, yes! If it’s possible to capture the intricacies of languages, it’ll surely be possible to translate an image to another. Indeed, this shows the power of deep learning.</p><p>Pix2Pix GAN paper was published back in 2016 by <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Isola%2C+P">Phillip Isola</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Zhu%2C+J">Jun-Yan Zhu</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Zhou%2C+T">Tinghui Zhou</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Efros%2C+A+A">Alexei A. Efros</a>. Find the paper <a href="https://arxiv.org/abs/1611.07004">here</a>. It was later revised in 2018. When it was published, internet users tried something creative. They used the pix2pix GAN system to a variety of different scenarios like a frame-to-frame translation of a video of one person to another, mimicking the former’s moves. Cool, isn’t it? Using pix2pix, we can map any image to any other image like the edges of an object to the image of the object. Further, we’ll explore more about its architecture and its working in detail. Now, let’s dive right in!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ga-0-EktAHDXDv_zs59VxA.jpeg" /><figcaption><strong>Source: GitHub</strong></figcaption></figure><h3>How Pix2Pix GAN works?</h3><h3>cGAN: Overview</h3><p>Heard about GANs (Generative Adversarial Network) that generate realistic synthetic images? Similarly, Pix2pix belongs to one such type called conditional GAN or cGAN. They have some conditional settings and they learn the image-to-to mapping under this condition. Whereas, basic GAN’s generate images from a random distribution vector with no condition applied. Confused? Try to get this.</p><p>Say, we have a GAN trained with the images of the MS-COCO data set. In GANs, the output image that is generated with the generator network is random. That is, it might generate images of any object that was there in the data set. But, with a cGAN, we can generate images that we want. If we want it to generate a person, it’ll generate an image of a person. This is achieved by conditioning the GAN.</p><h3>Pix2Pix GAN: Overview</h3><p>Let’s take another example of an image-to-image translation task, ‘black&amp;white to color image ‘ conversion. In pix2pix cGAN, the B&amp;W image is given as input to the generator model. And, the output of the generated model and the given input (B&amp;W image) pair of images is the generated pair (fake pair). The B&amp;W input image and the target output (i.e the real color version of the input B&amp;W image) forms the real pair.</p><p>The discriminator classifies a given pair of images as the real pair or the generated pair. The one that is used in Pix2Pix is different from normally how we’d expect a classifier’s output to be. It produces an output classification that classifies multiple patches in the input image pairs (patchGAN). I’ll explain more about it in detail. In the below depictions, concatenation is represented as ⊕.</p><h3>Pix2Pix GAN Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*60EbkAOFZDMEPYX6FnmrkQ.jpeg" /></figure><p>Pix2Pix GAN has a generator and a discriminator just like a normal GAN would have. For our black and white image colorization task, the input B&amp;W is processed by the generator model and it produces the color version of the input as output. In Pix2Pix, the generator is a convolutional network with U-net architecture.</p><p>It takes in the input image (B&amp;W, single-channel), passes it through a series of convolution and up-sampling layers. Finally, it produces an output image that is of the same size as the input but has three channels (colorized). But before training, the generator produces just random output.</p><p>After the generator, the synthetic image is concatenated with the input B&amp;W image. Therefore, the number of color channels will be four (height x width x 4). This is concatenated tensor is fed as input to the discriminator network. In Pix2Pix, the authors employ a different type of discriminator network (patchGAN type). The patchGAN network takes the concatenated input images and produces an output of size NxN.</p><h3>Loss Function</h3><h4>Discriminator Loss</h4><p>The discriminator loss function measures how good or bad the discriminator’s predictions are. The lesser the discriminator loss, the more accurate it becomes at identifying synthetic image pairs.</p><p>A normal binary classifier that’s used in GANs produces just a single output neuron to predict real or fake. But, the patchGAN’s NxN output predicts a number of overlapping patches in the input image. For example, in Pix2Pix, the output size is 30x30x1 which predicts for each 70×70 patch of the input. We’ll see more about patchGANs in another post. The 30×30 output is fed to a log loss function that compares it with a 30×30 zero matrix (since it is generated and not real).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*4ispSlAOULKxDkdY5J2yqA.jpeg" /><figcaption><strong>Loss of synthetic image pairs</strong></figcaption></figure><p>This is called generated loss. The real loss is calculated for the pair of B&amp;W and its corresponding color image from the data set. This is a real pair. Hence, ‘real loss’ is the sigmoid cross-entropy of the NxN output and a matrix of ones of the NxN size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*K0Xpha8R84QVTrU7t9PShw.jpeg" /></figure><p>The total discriminator loss is the sum of the above two losses. The gradients of the loss function are computed with respect to the discriminator network and are backpropagated to minimize the loss. While the discriminator loss is back-propagated, weights of the generator network are frozen. Phew! Now we’re almost done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*BaXc1zu6okoMIeKbZJ9rIw.jpeg" /><figcaption><strong>The loss function of the discriminator</strong></figcaption></figure><h4>Generator Loss</h4><p>The generator loss measures how real the synthetic images look. By minimizing this, the generator could produce more realistic images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*elkUCeankeGTf2NQ0B18yw.jpeg" /><figcaption><strong>Generator loss function</strong></figcaption></figure><p>This loss is almost the same as generated loss except that it is the sigmoid cross-entropy of the NxN discriminator output and a matrix of ones. When this loss is back-propagated, the discriminator network’s parameters are frozen. And only the generator’s weights are adjusted.</p><p>To improve the aesthetics of the generated image, the authors of the pix2pix paper added an L1 loss term. It calculates the L1 distance between the target image and the generated image. It is then multiplied with a parameter ‘Lambda’ and gets added to the generator loss.</p><h3>Training the Pix2Pix model</h3><p>To train the model to convert B&amp;W images to color, we have to feed the network with the input and target images. Hence, any data set with color images like the ImageNet can be used. The data set can be constructed by converting the color images to B&amp;W to form the input. And the color image itself forms the target. Thus, the network can be trained by iterating through the data set, feeding the images to the pix2pix model, one by one or by batch.</p><h3>And…Done!</h3><p>That’s pix2pix for you! Hope you got a clear idea about what pix2pix GAN is and how it works.</p><p>See how to train a cGAN model yourself on your custom data set.</p><p><a href="https://hackerstreak.com/colorizing-pokemon-with-deep-learning/">1. How to train an AI cGAN model in 5 simple steps</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9bf5d691bac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/gan-pix2pix-generative-model-c9bf5d691bac">GAN Pix2Pix Generative Model</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[Word Embedding Using BERT In Python]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science-collective/word-embedding-using-bert-in-python-dd5a86c00342?source=rss-1a92cae35860------2"><img src="https://cdn-images-1.medium.com/max/1920/1*aqyD9wau4lFI14Vmtr2RTQ.jpeg" width="1920"></a></p><p class="medium-feed-snippet">Embedding Words Into Vectors Using a Deep Learning NLP Model (BERT)</p><p class="medium-feed-link"><a href="https://medium.com/data-science-collective/word-embedding-using-bert-in-python-dd5a86c00342?source=rss-1a92cae35860------2">Continue reading on Data Science Collective »</a></p></div>]]></description>
            <link>https://medium.com/data-science-collective/word-embedding-using-bert-in-python-dd5a86c00342?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd5a86c00342</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[bert]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Mon, 16 Dec 2019 14:57:29 GMT</pubDate>
            <atom:updated>2020-04-28T10:46:03.868Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Linear Regression Explained]]></title>
            <link>https://medium.com/analytics-vidhya/linear-regression-explained-5a5e9027cd6d?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a5e9027cd6d</guid>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[kaggle-competition]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[beginner]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Tue, 17 Sep 2019 04:45:38 GMT</pubDate>
            <atom:updated>2019-12-31T11:55:57.516Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w64HM-ZiZfBrasI9yv9pdA.jpeg" /></figure><p>Linear regression is the first algorithm to pop up when you start to learn about machine learning. It is one of the simplest learning algorithms and it’s very easy to comprehend. Check it <a href="https://hackerstreak.com/dive-into-machine-learning-with-google-colab/">here</a> to see how to run ML programs right in your browser. We’ll ponder over it’s working under the hood and later implement it in Python. I’ve tried to explain with a lot of pictures and graphical representations to help you to grasp the concept. Let’s see how it works!</p><h3>What’s Regression?</h3><p>Regression is a method to estimate the relationship between variables that have a continuous range of values. In contrast, classification is categorizing variables into discrete classes or groups. Say, we have to model the relationship between the height and weight of people. So, given the weight of a person, we have to predict the height. Here, the height and weight both have a continuous range of values. But, in a classification task, the output can only belong to a set of classes or groups. One such example is predicting whether an image is a ‘cat’ or a ‘dog’.</p><p>The key takeaway is, when the target variable is continuous, it is a regression task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8KQh5n6gjpZWZtFYZ2JDDA.jpeg" /><figcaption><strong>Height in inches and weight in lbs</strong></figcaption></figure><p>In the above dataset, we can use either of the two variables to predict the other. I’m using the weight to predict the height. For the sake of simplicity, we’ll ignore the gender column of the table. Do you see that both the ‘height’ and ‘weight’ have a continuous range of values?</p><h3>Intro to Linear Regression</h3><p>Linear regression is the use of linear predictor models to predict the target variable. Let’s use the ‘predicting the height using weight’ problem to understand it in detail. Here, the weight is taken as the input or the independent variable. And the height is the dependent variable which is predicted using the input weight. The weight to height relation is established with a straight line.</p><figure><img alt="Mathematical equation of a line" src="https://cdn-images-1.medium.com/max/1024/1*lCEhXyg83Bd1VMpSyP8Jnw.jpeg" /><figcaption><strong>The mathematical equation of a line</strong></figcaption></figure><p>We represent a line mathematically as Y = m*X+ C where X is the input feature, Y is the target variable. In the above equation, ‘m’ and ‘C’ are the parameters that determine the slope and intercept of the line.</p><p>Can’t comprehend what’s ‘slope’ and ‘intercept’ are? No problem, I got you covered.</p><h3>Slope and intercept. What are they?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kEiqTEOVnM5-6RZBqGneSA.jpeg" /><figcaption><strong>Equation of a straight line</strong></figcaption></figure><p>The slope ‘m’ of any straight line determines how much the line is inclined towards the horizontal X-axis. In the above figure, the line has an inclination of 45 degrees. Therefore, the slope of the line is tangent(45) or tan(45) which is 1. And the intercept is where the line meets the corresponding axis. In our case, ‘C’ is the Y-intercept and its value is zero. Hence, the line equation is Y=1*X + 0 i.e Y=X.</p><h3>Fitting a line to the dataset</h3><p>Now, let’s go back to our “weight-height” dataset and see how our data looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*8ixsXOlGFXb_SR4voV2zOw.jpeg" /></figure><p>In this scatter plot of 20 samples, the data points are spread in a slanted way. If we want to fit a line to this data, it would be a slant one too. But, how slant do we have to make it and where do we position the line? Remember the slope and the intercept? By tuning their values, we’ll be able to position the line to fit the data. But how do we know what should be the values of the slope and the intercept of our linear regression model? Here comes the training part.</p><p>During training, we let our model see the input and output data (weight and height) and adjust its parameters to make the line a perfect fit. But before we start training, we start with random values for ‘m’ and ‘C’. Due to this, the model predicts random output values for a given set of inputs. Then, the model sees how bad it has predicted by comparing its outputs to the true output with a loss function.</p><h3>Loss Function of The Linear Regression Model</h3><p>To see how bad our model is predicting, we use a metric called Mean Squared Error (MSE). Because only when we see how big the error is, we’ll be able to correct it by minimizing the error. The MSE takes the average of the sum of the square of the difference between the actual height value and the predicted height value of the entire training dataset.</p><figure><img alt="Mean Squared Error Cost Function" src="https://cdn-images-1.medium.com/max/996/1*_ZZ9C629cdAidCWp-IQmDQ.jpeg" /><figcaption><strong>Mean Squared Error Cost Function</strong></figcaption></figure><p>In the above equation, ‘pred’ is what the model predicts and ‘true’ is the ground truth label. But, why MSE? Because it makes big errors bigger and very small errors smaller due to the square term. The summation is done over the entire dataset to calculate the error.</p><h3>Minimizing the Cost</h3><p>The cost that we saw above should be minimized for the optimal fitting of our model. This is done by what they call ‘Gradient Descent’. Basically, the gradient descent tunes the values of the ‘m’ and ‘c’ such that the total loss is minimal. It does this by taking the partial derivative of the cost function (using chain rule) with respect to ‘m’ and ‘C’ and updates their values.</p><h3>Gradient Descent</h3><p>The cost function when partially differentiated with respect to ‘m’ and ‘C’ separately yields the updated value for each of the model’s parameter (m and C) respectively.</p><figure><img alt="Partial derivative of mean squared error w.r.t ‘m’" src="https://cdn-images-1.medium.com/max/700/1*ZiJ5Jby4ANad3I2yMZS73A.jpeg" /><figcaption><strong>The partial derivative of mean squared error w.r.t ‘m’</strong></figcaption></figure><p>Next, using the chain rule of calculus, we differentiate the cost function elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*Malrqwt2aGsH6-wvzlkx3w.jpeg" /><figcaption><strong>Taking the constant 1/n outside the partial derivative</strong></figcaption></figure><p>By the scalar multiple rules, constant multipliers can be moved outside of the derivative.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*nfO_rNT9d7tLsv59kDVhww.jpeg" /><figcaption><strong>By the power rule, the squared term is differentiated</strong></figcaption></figure><p>Now, the constant ‘2’ can also be taken out leaving the “(mX+C-ytrue)” term inside.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*aYJZC0yeNkHKRCDreOmgQQ.jpeg" /><figcaption><strong>The final equation</strong></figcaption></figure><p>The partial derivative of “m*X+C-ytrue” with respect to ‘m’ is X as all the other terms are considered as constants in partial differentiation. Hence, the ‘X’ term is multiplied with the remainder of the expression.</p><p>The procedure is the same for the derivative with respect to ‘C’. It’s resulting equation will look like this.</p><figure><img alt="Partial derivative of cost function with respect to ‘C’" src="https://cdn-images-1.medium.com/max/700/1*NZyR0zwZzINv2uD4ywF6-Q.jpeg" /><figcaption><strong>The partial derivative of the cost function with respect to ‘C’</strong></figcaption></figure><p>These derivatives are used to update the existing values of ‘m’ and ‘C’ using the gradient descent update formula. We use the parameter ‘alpha’ to define how much parameters should be updated. The higher the alpha, the larger will be the update. But, the alpha value should be optimally set so not to overshoot the optimum value for the parameters. The update equation looks like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*wvcgOhor355xxH7VcREYEg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*lC2NgEtwOAtSwlcA0W1q8Q.jpeg" /><figcaption><strong>Update of ‘C ‘and ’m’</strong></figcaption></figure><p>The ‘t+1’ is the updated value and ‘t’ is the present value.</p><h3>How does it look like?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*31Mn2N3kumAl6T3seGLYpA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*kq9d0nhwWd27CBCaEHnp-A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*uSkuQzWdvsvjh6KxgAP71g.jpeg" /><figcaption>1) Random fit 2) Perfect fit 3) Imperfect fit</figcaption></figure><p>As the parameters are updated, the line fits the dataset more perfectly and the final linear regression line is the best fit.</p><h3>Linear Regression Done!</h3><p>I hope that this article gives you a clear understanding of the working of linear regression. And we’ll be back with programming a linear regression model from scratch using just Python in the next post.</p><p>Until then, <a href="https://hackerstreak.com/">happy hacking</a>!</p><p><em>Originally published at </em><a href="https://hackerstreak.com/"><em>https://hackerstreak.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a5e9027cd6d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/linear-regression-explained-5a5e9027cd6d">Linear Regression Explained</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dive into machine learning with Python and Google Colab]]></title>
            <link>https://medium.com/@baakchsu.sprx77/dive-into-machine-learning-with-python-and-google-colab-3eba4dbf6b4d?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/3eba4dbf6b4d</guid>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[google-colab]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Wed, 24 Jul 2019 16:42:43 GMT</pubDate>
            <atom:updated>2019-08-31T04:28:40.374Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HVJH6pWt4Y2Uoqgf2W9yYA.jpeg" /></figure><h3>Getting started</h3><p>Many programmers who want to get into machine learning (ML) start their journey by getting into the math behind that later intimidates them. They try to make it through by first learning math in order to learn deep learning (which isn’t the only way). This scares away many forcing them to give up early.</p><p>I still remember the first day of our college machine learning guest lecture filled with hundreds of people. Most of them attended just because of the catchy title “Artificial Intelligence”. But a week later, the room was almost empty. Many left when they came to know that machine learning has a lot of math. Starting from bottom-up doesn’t have to be your way into machine learning. In this series, we’ll see how a top-down, coding-first approach helps us to overcome the intimidation of mathematics.</p><h3>Where to start?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2KhDOt8Dgcq17b-8rlMsig.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@emilymorter?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Emily Morter</a> on <a href="https://unsplash.com/search/photos/question-mark?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Well, for starters, learning Python would be the easiest way to start playing with ML algorithms. It’s okay if you don’t know Python. One can learn the basic constructs of python language in under a month or even earlier too! It is intuitive, easy to code and it’s widely adopted by the machine learning community. It has got a huge number of libraries that aid in pretty much everything. If you already know C, C++ or Java, you can catch up with Python easily.</p><p>It has excellent documentation and a huge community to help you out in StackOverflow. And once you get familiar with the language, you can check out machine learning and other libraries that would come handy to train your own AI model. Scikit-learn is one of the most popular libraries for ML and it comes with out-of-the-box algorithms and is incredibly intuitive. We’ll glance more at the algorithms to start with ML.</p><h3>Machine learning with Colab</h3><p>Once people jump into ML, the question that usually pops up is “Do I have a laptop/PC that’s powerful enough for ML?”. Running some complex algorithms needs more compute resources but you don’t have to go laptop shopping when you’re just starting out. But, if you have bought your PC recently with decent specs (8 GB RAM, i5 or higher version processor with an optional dedicated VRAM), it should do.</p><p>Even if you don’t, worry not. Google’s <a href="https://colab.research.google.com/">Colab</a> provides virtual instances of Jupyter notebook that can run in your browser with a GPU and 12 GB of RAM! For free! And that’s more than enough to get started. It provides sessions that you can run for 12 hours straight. All you need is to sign up with your Google account.</p><p>After you log into colab, a pop up will ask you to select the version of Python. You’ll get a CPU session of Jupyter Notebook by default. But if you want to use a hardware accelerator like a GPU or TPU (Tensor Processing Unit), click “Run time” tab and select “change run time” and select your desired hardware accelerator.</p><h3>And that’s all for now!</h3><p>There you go! You’ve taken your first step into machine learning. We’ll see more about how to implement our very first machine learning model and get rolling. Until then, explore more over <a href="https://hackerstreak.com/posts/">here</a>. Happy hacking!</p><p><em>Originally published at </em><a href="https://hackerstreak.com/dive-into-machine-learning-with-google-colab/"><em>https://hackerstreak.com</em></a><em> on July 24, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3eba4dbf6b4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Siamese Neural Network for signature verification]]></title>
            <link>https://medium.com/analytics-vidhya/siamese-neural-network-for-signature-verification-efd2f399d586?source=rss-1a92cae35860------2</link>
            <guid isPermaLink="false">https://medium.com/p/efd2f399d586</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[analytics-vidhya]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <dc:creator><![CDATA[Anirudh S]]></dc:creator>
            <pubDate>Mon, 01 Jul 2019 04:44:30 GMT</pubDate>
            <atom:updated>2019-10-20T05:04:05.790Z</atom:updated>
            <content:encoded><![CDATA[<h4>Let’s see how Siamese Architecture works using CNNs to perform a signature verification task!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g0HklSUXPIRLt5bb9kJlVg.jpeg" /><figcaption>Source: <a href="https://pixabay.com/photos/writing-pen-man-ink-paper-pencils-1149962/">https://pixabay.com/photos/writing-pen-man-ink-paper-pencils-1149962/</a></figcaption></figure><p><strong>Introduction</strong></p><p>A person’s signature hardly varies each time they sign and the algorithm that we employ for fraud detection must account for the variations in strokes. But the detection system should also manage to catch forged signatures that might be very similar in case of skilled forgeries. Fortunately, deep learning offers to learn the similarity by itself and all we have to do is plain, simple backpropagation. We’ll build a siamese network and train it to approximate the similarity function that outputs a score between 0 and 1 (1 being perfectly genuine and 0 being completely dissimilar)</p><p><strong>Data Preparation</strong></p><p>Let’s consider this as a supervised learning task and prepare the features and labels. The siamese network is fed with pairs of images and their corresponding labels (similar or dissimilar). The dataset for signature verification is available at SigComp’s website whose link is <a href="http://www.iapr-tc11.org/mediawiki/index.php/ICDAR_2011_Signature_Verification_Competition_(SigComp2011)">here</a>. The data is prepared by looping over the dataset and forming an array of pairs of images and their labels in another array. This ultimately makes it a binary classification problem!</p><p><strong>Network Architecture</strong></p><p>Siamese network has a stack of convolutional and pooling layers and a final fully connected layer with 128 neurons. The sister network takes on the same weights and biases as the original network (essentially means running the same network twice). It takes the input image pair and produces two 128-D vectors as outputs. The network learns to encode similar input images closer and dissimilar ones, farther apart from each other in the vector space. The conventional method to train it is by using a contrastive loss function or a triplet loss that takes three images at once. In this post, we’ll be training our network with ‘cross-entropy’ loss. How you ask? Here’s the layout of our architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*_HcPUerQPoKTeyaLL4Bb3w.jpeg" /><figcaption>Siamese network with a classification</figcaption></figure><p>The two vectors are then subtracted element-wise to form a single 128-D vector (not to be confused with the L1 distance function that yields just a single scalar value as output).</p><p>Well, we’re almost finished. The next part of it is a fully connected network that takes the 128-D distance vector as the input and the output layer has a single output neuron for classification with sigmoid activation. The number of hidden layers is a hyper-parameter and can be experimented with to get the best results. Now, the network is trained with the image pairs and labels that we gathered earlier. The loss function is ‘binary-cross entropy’ and the optimizer that best suits is ‘Adagrad’.</p><p><strong>Conclusion</strong></p><p>After training for a few epochs (5 to 7), we’ll get an accuracy close to 93% in the validation set. Note: For the network to perform well with real-world signatures, the SigComp dataset can be augmented by adding noise and random blur to force the network to generalize well since the dataset was collected using a digital tablet with little to no artefacts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UqaADGWHcYw_mXYUvPaq5w.png" /><figcaption>And, that’s how it’s done.</figcaption></figure><p>Source code: <a href="https://he-s3.s3.amazonaws.com/media/sprint/axis-hackathon/team/456719/97f5efaproject.rar">https://he-s3.s3.amazonaws.com/media/sprint/axis-hackathon/team/456719/97f5efaproject.rar</a></p><p>Github link: <a href="https://github.com/Baakchsu">https://github.com/Baakchsu</a></p><p><a href="https://www.linkedin.com/in/anirudh-s-37307114b/">Anirudh S - AI Researcher - Student Copter Research Organization (SRM-SCRO) | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efd2f399d586" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/siamese-neural-network-for-signature-verification-efd2f399d586">Siamese Neural Network for signature verification</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>