<?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 Madiha Samo on Medium]]></title>
        <description><![CDATA[Stories by Madiha Samo on Medium]]></description>
        <link>https://medium.com/@madizakir.mz?source=rss-e42292d36189------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*O8bHIH47wru_-mNphwfpWQ.jpeg</url>
            <title>Stories by Madiha Samo on Medium</title>
            <link>https://medium.com/@madizakir.mz?source=rss-e42292d36189------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:30:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@madizakir.mz/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[Vectorizing Shallow Neural Nets]]></title>
            <link>https://medium.com/@madizakir.mz/vectorizing-shallow-neural-nets-60dc4118d460?source=rss-e42292d36189------2</link>
            <guid isPermaLink="false">https://medium.com/p/60dc4118d460</guid>
            <category><![CDATA[vectorization]]></category>
            <category><![CDATA[logistic-regression]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Madiha Samo]]></dc:creator>
            <pubDate>Sun, 18 Jul 2021 19:21:24 GMT</pubDate>
            <atom:updated>2021-07-21T08:08:23.857Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gWIz3O3vN-gWwePA" /></figure><p>Neural networks are significantly just like logistic regression except that in neural networks we have multiple layers, each layer consists of multiple activation units and each activation unit is assigned its own set of weights with respect to each feature. Let’s try to grasp the concept visually -</p><figure><img alt="Deep Learning Specialization — Andrew Ng" src="https://cdn-images-1.medium.com/max/315/1*bDz1-rNfJ3jtRzD1dhRSLQ.png" /><figcaption>Deep Learning Specialization — Andrew Ng</figcaption></figure><p>The figure illustrates a logistic regression algorithm with 3 input features, a linear function “z”, passed through a sigmoid function “a” which eventually predicts the outcome. The linear function- “z” takes some initialized weight parameters for each input feature and adds the features, sigmoid function further transforms our values to an S-shaped curve with values ranging from zero to one. This is exactly what a single activation unit of a single layer of a neural network does until the final activation unit or the neuron of the final layer predicts the outcome.</p><blockquote>Note: Activation function does not necessarily have to be a sigmoid function, instead ReLU is most preferred by the Machine Learning practitioners but you get the idea.</blockquote><p>Now let’s visualize what the two-layer neural network looks like <em>(we don’t count the input layer, just the hidden layer, and output layer as official layers of the neural network)</em>.</p><figure><img alt="Deep Learning Specialization — Andrew Ng" src="https://cdn-images-1.medium.com/max/602/1*Vmia04pKzMCNHV3GpzW-lA.png" /><figcaption>Deep Learning Specialization — Andrew Ng</figcaption></figure><p>That’s a lot of information in one picture so let’s just try to break it down a little to gain some intuitions. This time we have different weights for each feature for every single activation unit also known as a neuron. In other words, we’ll have W[1] to W[j] sets of weight matrices and b[1] to b[j] sets of bias vectors where j corresponds to the number of layers. Let’s walk through the first layer in more detail. Once we’ve initialized the weight parameters we can calculate our activation functions for the very first layer as –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*rhtEmwDxd73KrTbkAe7Edw.png" /></figure><p>Implementing these equations for all the layers using for loop might not look like a lot of hassle for a two-layer network but the deeper our network gets, the more complex and inefficient the model gets. The rule of thumb is to <strong><em>ALWAYS AVOID FOR LOOPS</em></strong>.</p><p>Well, it’s not always possible especially while iterating for gradient descent convergence but try to avoid them as much as you can. Okay, enough talking, now is the time for vectorization to shine, let’s take all the weights of our very first neural net layer as seen above and stack them to a matrix we get something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*YLjJOMihAIg_IFviGwJ6Aw.png" /></figure><p>The dimensions of the weight matrix of each layer will be the number of activation units in the previous layer (which is our input layer in this case i-e: 3 features) by the number of activation units in the current layer. Each activation function for layer one needs transpose of the respective column of the weight matrix to get multiplied with input features. In other words, Z1[1] would require transpose of the first column times the features, similarly, Z2[2] would require the transpose of the second column times the features and so on. We will get a vector Z[1] such that each value in Z[1] would correspond to the output of the linear function of each neuron corresponding to layer one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*9NJf4F9ymXH2yGw6HDDVZg.png" /></figure><p>Similarly, we can calculate Z[2], Z[3] up to Z[n] where n is the number of layers and the dimensions of each weight matrix (W[2], W[3],…, W[n] )corresponding to each layer will be equal to the {number of neurons in that layer by the number of neurons in the previous layer}. Even though the output shows the values for a single training example, you can assume that dimensions of the matrix X are (nx by m) where nx is the number of input features and “m” indicates the training examples. W[1]X would now return m columns where each column represents a single training example. This would also make our Z value expand to have dimensions equal to the number of neurons by the number of training examples as shown —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*Z28kWu-nqOQqZzHGBIh9dw.png" /></figure><blockquote><em>nx represents the number of features, [1] represents the layer, and (1) to (m) represents the training example.</em></blockquote><p>Once we get our Z vectors for all the layers, then we can calculate sigmoid simply by applying the sigmoid function to the vector Z, since A and Z have the same dimensions, we don’t need to worry about dimensions this time and we get our A vector (i-e: our final output values) as A[i] = sigmoid(Z[i]) for each layer.</p><p>These were the forward propagation steps of a 2 Layer neural network which can now be written in four lines of code with this vectorized implementation explained above as follows:</p><blockquote><em>Z[1] = W[1] X + b[1]</em></blockquote><blockquote><em>A[1] = sigmoid(Z[1])</em></blockquote><blockquote><em>Z[2] = W[2] A[1] + b[2]</em></blockquote><blockquote><em>A[2] = sigmoid(Z[2])</em></blockquote><p>We can follow the same method for implementing backpropagation as well since the derivatives of these vectors will have exactly the same dimensions and we just need to make sure we follow the matrix multiplication rules which almost always makes sure that we are vectorizing it right. However, every time while implementing a deep L layer neural network it is always a good idea to sketch out the dimensions of the input matrix, each hidden layer as well as output layer which will make the vectorized implementation much easier and easily allow you to expand the above code to a deep neural network in contrast to a shallow network.</p><p>Note that while implementing a deep layer neural network you will still need a for loop to loop through L layers even in a vectorized implementation, which I don’t think there’s any way to escape that but it still works just fine and as efficiently as required. Otherwise, parallel processing is always there to rescue!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60dc4118d460" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>