<?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 Prasadmangala on Medium]]></title>
        <description><![CDATA[Stories by Prasadmangala on Medium]]></description>
        <link>https://medium.com/@prasadmangala02?source=rss-5013e6f03fdf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Gph5kQ5MZaB5Hmh5</url>
            <title>Stories by Prasadmangala on Medium</title>
            <link>https://medium.com/@prasadmangala02?source=rss-5013e6f03fdf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 20:19:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prasadmangala02/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[Catalysts of Deep Learning: The Xavier and He Initialization Revolution (Part - I)]]></title>
            <link>https://medium.com/@prasadmangala02/catalysts-of-deep-learning-the-xavier-and-he-initialization-revolution-part-i-6d06e2c0994f?source=rss-5013e6f03fdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d06e2c0994f</guid>
            <dc:creator><![CDATA[Prasadmangala]]></dc:creator>
            <pubDate>Wed, 13 Sep 2023 08:05:15 GMT</pubDate>
            <atom:updated>2023-09-13T08:05:15.067Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yz7H31wZ0uhB4QRJ.jpg" /></figure><p>Welcome to my latest blog, where we embark on an exciting journey into the worlds of deep learning and uncover the hidden gems that make it all possible. In this series of blogs I will try to uncover the mysteries of <strong>Xavier </strong>and <strong>He </strong>initialization, two indispensable techniques that hold the keys to unlocking the true potential of neural networks.</p><p>If you’ve ever wondered how those <strong>cutting-edge AI</strong> models achieve remarkable accuracy and performance, you’re in the right place. Xavier and He Initialization are the <strong>unsung heroes</strong> behind the scenes, ensuring that your neural networks not only learn effectively but also converge faster and with more stability.</p><p>In this part of the series we will get introduced to the weight initialization schemes and later we will discuss in detail about the initialization and its mathematical proofs.</p><h3><strong>Outline:</strong></h3><blockquote>1. Introduction.</blockquote><blockquote>2. The Importance of Weight Initialization.</blockquote><blockquote>3. Motivation behind Xavier Initialization.</blockquote><blockquote>4. Xavier Initialization.</blockquote><blockquote>5. Backward Propagation with Xavier initialization.</blockquote><blockquote>6. Conclusion.</blockquote><blockquote>7. References.</blockquote><h3>Introduction:</h3><p><strong>I</strong>n deep neural networks, weights connect neurons in one layer to neurons in the next layer. These weights are initialized with some initial values before training begins. The choice of weight initialization can have a profound impact on the network’s performance. Poor initialization can lead to slow convergence, vanishing or exploding gradients, and even prevent the network from learning effectively. To address these issues, researchers have developed specialized weight initialization methods, such as Xavier and He initialization.</p><h3><strong>Importance of Weight Initialization:</strong></h3><p><strong>W</strong>eight initialization is a critical aspect of designing neural networks. In these networks, neurons rely on parameters known as weights to compute the weighted sum of inputs. When training neural networks, we use an optimization method called <strong>stochastic gradient descent</strong> to tweak these weights and minimize a loss function, ultimately leading to accurate predictions.</p><p>However, this optimization process needs a starting point. Weight initialization is the method we use to set the <strong>initial values</strong> of these weights. It involves assigning small random values to the weights, giving us a starting point for the network’s learning journey.</p><blockquote>Now, you may ask a question: Why don’t we initialize the weights to zero or some constant?</blockquote><blockquote><strong>Ans:</strong> If all weights are initialized to the same value (e.g., zero), all neurons in a layer will compute the same output during forward propagation. This symmetry in weights leads to symmetry in gradients during back-propagation, causing all neurons to receive the same gradient updates. As a result, all neurons will continue to be symmetric, and the network won’t learn diverse features, severely limiting its capacity.</blockquote><blockquote>Mathematically,</blockquote><blockquote>Let: x be the input to this neuron, w_i be the weight connecting the input x to the neuron and f be the activation function(e.g., sigmoid or ReLU).</blockquote><blockquote>The out put of this neuron is calculated as:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/164/0*Zr_I7BcNeH3he3Xv" /></figure><blockquote>Now, let’s assume that all the weights w_i in this layer are initialized to the same value, say</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/0*zAJmvTSSg8oRu2LC" /></figure><blockquote>This symmetry in the weights leads to symmetry in the activations. During back-propagation, when gradients are computed, all neurons in the layer will receive the same gradient updates because their outputs were the same. Mathematically, this can be expressed as:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/0*gQ4QyYMKdDympJPe" /></figure><blockquote>Since all dOutput_i / dw_i terms are same, the gradients for all weights w_i be identical:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/0*nz1WYSN_3o3Y-f2p" /></figure><blockquote>As a result, all neurons will continue to be symmetric throughout training, severely limiting the network’s capacity to learn diverse features and complex patterns.</blockquote><h3>Motivation behind Xavier Initialization</h3><p>The motivation behind Xavier initialization, also known as Glorot initialization (named after its co-author Xavier Glorot), stems from addressing the challenges associated with training deep neural networks. When deep networks have many layers, if weights are too small, the signal diminishes layer by layer, leading to <strong>vanishing gradients</strong>. Conversely, if weights are too large, the signal amplifies, leading to <strong>exploding gradients.</strong></p><p>At the time when Xavier Glorot proposed this method, sigmoid and hyperbolic tangent (tanh) activation functions were commonly used. These functions have a certain saturation property, meaning they become nearly flat for very large or very small inputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*84cCwN2bRw4DbjiypTvnUA.png" /><figcaption><a href="http://proceedings.mlr.press/v9/glorot10a.html">Understanding the difficulty of training deep feed-forward neural networks</a></figcaption></figure><p>To prevent gradients from becoming too small during back-propagation, the initialization method needed to ensure that the <strong>variance of activations remained roughly constant </strong>across layers.</p><h3>Xavier Initialization</h3><p>Given our lack of prior knowledge about the data a reliable approach is to initialize the weights from a Gaussian distribution. This distribution inherently possesses a zero mean and finite variance.</p><p>Now, Suppose we have a neural network layer denoted as layer ‘L’ with ‘n_in’ input neurons and ‘n_out’ output neurons.</p><p>The goal is to initialize the weights for this layer in a way that helps stabilizing training and ensure that the variances of the activations are approximately equal</p><p>Let X = an input with ‘n’ components and a linear neuron with random weights W that spits out an output Y.</p><p>The variance of Y can be written as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/0*eb4z0mCpHK5FdeWS" /></figure><p>From Statistics we have,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/0*bDfu_xDT0Ei4LWOY" /></figure><p>For any ‘i’,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/0*Vnv8v4xzhWVCC4RT" /></figure><p>Here, we assume that X_i and W_i are all <strong>identically</strong>(i.e. Sampled from the same distribution) and<strong> independently </strong>distributed with a Gaussian distribution having a mean zero.</p><p>Now,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/0*3i5dwHPgBmIdiAsn" /></figure><p>As we are assuming W_i and X_i are centered around ‘0’, the first two terms in equation (2) becomes zero so from equation (2), we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/0*zEBXSIUnsvNkSZ_o" /></figure><p>So, equation (3) becomes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/274/0*lCYg2eNvrkb96HhO" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/0*5fm6UxV2UjKdMpXN" /></figure><p>Where ‘n_in’ is the number of input neurons .</p><p>The equation (4) shows that the variance of Y in layer ‘L’ is proportional to the product of n_in, the number of input neurons and the product of variances of the weights(W_i) and inputs (X_i).</p><p>To ensure that the variance if Y in layer ‘L’ is roughly equal to the variance of the inputs(x) then the term <strong>n_in.Var(W_i)</strong> should be equal to 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/184/0*5K6ExEa2BSeJDRJY" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/168/0*TfSaTxAe1-NgeTGd" /></figure><p>This is the Xavier initialization formula for the layer from a Gaussian distribution with the zero mean and variance of 1/n_in.</p><h3>Backward-Propagation:</h3><p>For Back-propagation, during training, we not only need well-initialized weights for the forward pass but also consider the stability of gradients during the backward pass.</p><p>Xavier initialization ensures that gradients <strong>don’t vanish or explode</strong> as they are propagated backward through the network.</p><p>Let <strong>n_out</strong>: <strong>The number of output neurons, from layer ‘L’</strong></p><p>and <strong>n_in: The number of inputs to layer ‘L’</strong></p><p>Using the same Xavier initialization principle above to get</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/141/0*3Dh_lp5FObYZAGxh" /></figure><p>To maintain the equality of variance between the input and output gradients, both constraints can only be met when</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/88/0*u8VnS8ki9IopDn9S" /></figure><p>However, in practical scenarios, the equality of <strong>n_in</strong> and <strong>n_out </strong>needn’t hold. So Glorot and Bengio suggested that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/0*lQpZ0trzLYEfyv_-" /></figure><p>Therefore,<strong>Xavier initialization</strong> suggests that initialize weights from Gaussian distribution with <strong>mean = 0.0</strong> and <strong>variance = 2/(n_in+n_out)</strong>.</p><h3>Conclusion:</h3><p>Weight initialization is a crucial step in training deep neural networks. Xavier Glorot and He initialization are two widely adopted techniques that help mitigate issues like vanishing gradients and slow convergence. In this part-1 of the blog series, I have tried to read the original paper by Xavier Glorot (<a href="http://proceedings.mlr.press/v9/glorot10a.html">Understanding the difficulty of training deep feed-forward neural networks</a>) multiple times and tried to understand it. In the next part I will try to deep dive and put my thoughts on the initialization techniques. Here, I have mentioned about He initialization but has not explained about it. I will be trying to explain about He initialization in part-2.</p><p>I suggest the readers to read this part of the blog thoroughly as we will be using some of the techniques in the next part.</p><h3>References:</h3><ol><li><a href="https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf">Understanding the difficulty of training deep feedforward neural networks</a></li><li><a href="https://machinelearningmastery.com/weight-initialization-for-deep-learning-neural-networks/">https://machinelearningmastery.com/weight-initialization-for-deep-learning-neural-networks/</a></li><li><a href="https://arxiv.org/pdf/1704.08863.pdf">https://arxiv.org/pdf/1704.08863.pdf</a></li><li><a href="https://arxiv.org/abs/1502.01852">https://arxiv.org/abs/1502.01852</a>(Delving deep into rectifiers: Surpassing Human-level Performance on ImageNet Classification).</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d06e2c0994f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Revolutionizing NLP— Unleashing the power of “Attention is all you need”]]></title>
            <link>https://medium.com/@prasadmangala02/revolutionizing-nlp-unleashing-the-power-of-attention-is-all-you-need-9a61b995c157?source=rss-5013e6f03fdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a61b995c157</guid>
            <category><![CDATA[natural-language-process]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Prasadmangala]]></dc:creator>
            <pubDate>Tue, 25 Jul 2023 07:37:39 GMT</pubDate>
            <atom:updated>2023-07-25T07:37:39.135Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/232/0*yfW5VkqDr0i2PUaR" /></figure><p>Welcome to my blog, where we delve into the innovatory research that has redefined the landscape of Natural Language processing(NLP). In this small article I will try to explore the paper “Attention is all you need” by Vaswani et al. Although the paper was published in 2017, it is now in limelight because of the success of some of the models like GPTs and BERT. This ground-breaking paper introduced the revolutionary Transformer architecture, shaking the foundation of traditional NLP approaches and venturing into a new era of deep learning.</p><p>Imagine a world where machines could comprehend and communicate with humans in a manner so natural, it would be indistinguishable from human-to-human interactions. Such a realm was considered the holy grail of NLP researchers for decades, and with the advent of deep learning, we witnessed remarkable advancements in this domain.</p><blockquote>If you are fan of Sci-Fi movies or superhero movies then you must be familiar with Iron man’s <strong>Jarvis</strong> that understands what <strong>Tony Stark </strong>is<strong> </strong>saying ? There are some other movies like <strong>Bladerunner </strong>, <strong>Forbidden Planet</strong> and our all time favorite <strong>Star Wars </strong>with speaking robots, those are the capabilities of NLP.</blockquote><p>However, traditional recurrent neural networks (RNNs) and convolutional neural networks (CNNs), while effective in many applications, struggled to capture the intricacies of human language due to inherent limitations. They were slow to train. They also suffered from exploding or vanishing gradient problems.</p><p>Let’s take a moment to visualize a practical example of the challenges faced by conventional NLP models:</p><p>Consider a neural machine translation system tasked with converting an English sentence into French: “Your cat is a cute cat”. With RNNs, the model reads the input sentence sequentially, generating the translation one word at a time. As the sentence length grows, so does the computational burden, leading to slower translation speeds and difficulties in capturing long-range dependencies.</p><p>Similarly, CNNs process input sentences through convolutional filters, enabling parallel computation. However, they might overlook crucial context information, resulting in sub optimal translations.</p><p>These shortcomings in traditional NLP models ignited the need for a revolutionary breakthrough that could transcend the constraints of sequential and convolutional architectures.</p><p>The paper proposed the Transformer architecture, which harnessed the power of self-attention mechanisms to revolutionize the way machines process natural language. By employing a stack of encoder and decoder layers with multi-head self-attention, the Transformer fundamentally altered the approach to sequence modeling.</p><p>Instead of relying on sequential processing, the Transformer attends to all words in a sentence simultaneously, enabling it to weigh the importance of each word relative to others. This parallelism leads to faster training and inference times, making the Transformer architecture an ideal candidate for real-world NLP applications. In this article I will be focusing more on the architecture of a transformer. In the next article I will try to delve into more details like inference, training of the model and experimental results.</p><h3><strong>Outline :</strong></h3><ol><li>Overall Architecture</li><li>Attention.</li><li>Input embedding &amp; Positional Encoding.</li><li>Self-attention.</li><li>Multi-head self attention.</li><li>Conclusion.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/0*0TvqFbWBe3u7l4k-.png" /><figcaption>Transformer Architecture(<a href="https://arxiv.org/abs/1706.03762">https://arxiv.org/abs/1706.03762</a>)</figcaption></figure><h4><strong>1. Overall Architecture :</strong></h4><p>The Transformer consists of an encoder-decoder architecture with a stack of identical layers. Each layer contains two sub-layers: a multi-head self-attention mechanism and a feed-forward neural network. Skip connections and layer normalization facilitate smooth flow of information through the model.</p><ol><li>The <strong>encoder</strong> in the Transformer architecture consists of a total of N = 6 identical layers. Each layer is composed of two sub-layers: a multi-head self-attention mechanism and a position-wise fully connected feed-forward network. The design of each layer includes a residual connection and layer normalization to facilitate learning and alleviate the vanishing gradient problem.</li><li>The<strong> decoder</strong> in the Transformer architecture consists of N = 6 identical layers, similar to the encoder. However, it introduces an additional sub-layer to handle multi-head attention over the output of the encoder stack. The structure of each decoder layer includes three sub-layers: self-attention, encoder-decoder attention, and position-wise fully connected feed-forward network.</li></ol><p>Too many technical terms…. isn’t it?</p><p>Don’t worry we will see in detail what each of the layer is doing.</p><h4>2. Attention :</h4><p>Attention ! Attention ! Attention !</p><p>What do we mean by attention?</p><p>Imagine a scenario where your teacher asks you to count the number of students present in a class. In response, you naturally take a head count and provide the number to your teacher. During this task, you focus solely on what is necessary for the job at hand, disregarding other details like individual faces or specific features of the students. Similarly, when reading a blog, you pay attention to one word at a time, and your brain effortlessly filters out the rest of the words. This process exemplifies attention in action — honing in on relevant information while disregarding the irrelevant — and offers a simple analogy to grasp the concept.</p><h4>3. Input Embedding and Positional Encoding:</h4><p>Consider an example where I have a sentence:</p><p>“You Cat is a Cute Cat.”</p><p>Here each word is considered as a token. In the input embedding phase of the encoder, each of the tokens is converted into vectors of dimension d_model.</p><p>Since the Transformer lacks any inherent notion of word order, positional encodings are introduced to provide the model with sequence information. The positional encodings are added to the input embeddings, allowing the model to consider the positional relationships between tokens.</p><p>Positional Encoding equation:</p><p>PE(pos, 2i) = sin(pos / 10000^(2i/d_model))</p><p>PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))</p><p>where pos is the position, i is the dimension index, and d_model is the dimensionality of the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*EOWCUaAUN99KHwrJ.png" /><figcaption>image source:<a href="https://machinelearningmastery.com/wp-content/uploads/2022/01/PE6.png">https://machinelearningmastery.com/wp-content/uploads/2022/01/PE6.png</a></figcaption></figure><h4>4. Self-attention mechanisms</h4><p>The core innovation of the Transformer is the self-attention mechanism.</p><p>But why self-attention ?</p><p>The authors have compared self-attention, recurrent, and convolutional layers in terms of computational complexity, parallelization, and path length for learning long-range dependencies. Self-attention is faster than recurrent layers for shorter sequences but can be restricted for longer ones. Convolutional layers are generally more expensive than recurrent layers. Self-attention offers interpretability benefits, with attention heads learning distinct tasks related to sentence structure.</p><p>Self-attention allows the model to weigh the importance of different positions within the input sequence when generating output representations. The self-attention mechanism calculates three key components: Query (Q), Key (K), and Value (V) vectors for each word/token in the input sequence. By measuring the similarity between the Query and Key, the model determines how much attention each Key should receive. The attention weights are then applied to the corresponding Values to generate the output representation..</p><p>Self-attention mechanism equations:</p><p>- Compute Query (Q), Key (K), and Value (V) matrices:</p><p>Q = XW_q, K = XW_k, V = XW_v</p><p>where X is the input sequence, and W_q, W_k, and W_v are learnable weight matrices.</p><p>- Calculate the attention score between Query and Key for each token:</p><p>Attention(Q, K) = softmax(QK^T / sqrt(d_k))</p><p>where d_k is the dimension of the Query and Key vectors.</p><p>- Compute the weighted sum of Values based on the attention scores:</p><p>SelfAttention(Q, K, V) = Attention(Q, K)V</p><h4>5. Multi-head self attention :</h4><p>In the Transformer model, rather than using a single attention function with d_model -dimensional keys, values, and queries, the authors found it advantageous to introduce the concept of “multi-head attention.” This approach involves linearly projecting the queries, keys, and values h times using different learned linear projections, resulting in d_k , d_k , and d_v dimensions, respectively. The projected versions of queries, keys, and values are then processed in parallel through the attention function, producing d_v -dimensional output values. These outputs are concatenated and projected once more to obtain the final values,</p><p>The benefit of employing multi-head attention lies in its ability to jointly attend to information from different representation subspaces at different positions. With a single attention head, averaging would inhibit this capability. Instead, multi-head attention allows the model to focus on various aspects of the input simultaneously, enhancing its capacity to capture complex relationships within the sequence.</p><p>The mathematical representation of multi-head attention can be summarized as:</p><p>MultiHead(Q, K, V) = Concat(head_1, …, head_h)W0</p><p>where head_i = Attention(QW_i^Q, KW_i^K, VW_i^V)</p><p>The parameters W_i^Q, W_i^K, and W_i^V are the projection matrices for each attention head, and W0 is the final projection matrix. In this work, the authors use h = 8 parallel attention heads, with d_k = d_v = d_model / h = 64. This dimension reduction in each head helps maintain computational efficiency similar to single-head attention with full dimensionality.</p><h4>6. Conclusion:</h4><p>In this blog, we explored the key components of the Transformer, including self-attention mechanisms and positional encodings. We will also delve into its training procedure, inference strategies, and the impressive results it achieved on benchmark NLP tasks, such as machine translation and language modeling in my next blog.</p><p>The impact of “Attention Is All You Need” has been nothing short of transformative, inspiring an explosion of research in self-attention mechanisms and pushing the boundaries of NLP and other domains.</p><p>If you are interested to read the paper.</p><p>here’s the link : <a href="https://arxiv.org/abs/1706.03762">https://arxiv.org/abs/1706.03762</a></p><p>Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a61b995c157" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>