<?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 Praveen on Medium]]></title>
        <description><![CDATA[Stories by Praveen on Medium]]></description>
        <link>https://medium.com/@praveenkumar2909?source=rss-cbc912cbe452------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jLkpBFEroYEVbCjqEnMojg.jpeg</url>
            <title>Stories by Praveen on Medium</title>
            <link>https://medium.com/@praveenkumar2909?source=rss-cbc912cbe452------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 17:08:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@praveenkumar2909/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[Online Softmax in Attention]]></title>
            <link>https://medium.com/codex/online-softmax-in-attention-1230276cafec?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/1230276cafec</guid>
            <category><![CDATA[online-softmax]]></category>
            <category><![CDATA[flash-attention]]></category>
            <category><![CDATA[softmax]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Tue, 07 Jul 2026 10:06:54 GMT</pubDate>
            <atom:updated>2026-08-20T09:57:59.581Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A plain walk through the trick that lets attention compute softmax and the value multiply in a single streaming pass.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p36WFRUvdrUOSMn5bh2y6w.jpeg" /><figcaption>Chocolate Cake — Thanks wifee!</figcaption></figure><p><strong>Disclaimer:</strong> I read this blog post, along with a few other resources, to understand the concept before writing this piece. For the original material, check out: <a href="https://www.sethweidman.com/blog/streaming_softmax.html">Streaming Softmax: A Trick Powering FlashAttention</a></p><p>Attention does not compute its softmax in the naive way anymore. The naive way needs several passes over the data. First you multiply the Query by the transpose of the Key. Then you divide every score by the square root of the feature dimension, the number we call d, which is simply the length of each Query and Key vector. Then you take the softmax. Then you multiply by the Value.</p><p>The modern approach folds two of these steps, the softmax and the multiply by the Value, into one streaming pass. Two ideas make this possible. The first keeps the numbers from blowing up. The second lets us build the softmax while reading only one chunk of data at a time. A third part then stitches both ideas together with the Value vectors.</p><p>Let us take them one at a time.</p><h3>Part one: keeping the numbers stable</h3><p>The softmax of a list of scores turns each score into a weight between zero and one. To do that it raises the constant e to the power of each score.</p><p>The problem is that scores can be large. If a score is 222, then e to the power of 222 is an astronomically large number. It is so large that it no longer fits in the space the computer uses to hold it, and the calculation overflows.</p><p>There is a simple fix. You are allowed to subtract the same constant from every score before taking the exponential, and the softmax does not change. Here is the identity, where M is that constant:</p><pre>softmax(x_i)  =  e^(x_i) / Σ_j e^(x_j)  =  e^(x_i - M) / Σ_j e^(x_j - M)</pre><p>The two sides are equal because subtracting M multiplies the top and the bottom by the very same factor, and that shared factor cancels out.</p><p>The clever choice is to let M be the largest score in the list. Then the largest exponential becomes e to the power of zero, which is exactly one, and every other exponential is smaller. Nothing overflows, and every weight sits safely between zero and one.</p><p>This safe version of softmax is often called the <strong>numerically stable softmax</strong>, or the <strong>safe softmax</strong>.</p><h3>Part two: building the sum as the data streams in</h3><p>The bottom of the softmax is a sum of all the exponentials. Normally you would need to see every score before you can add them up. But we would like to start adding as soon as the first chunk of scores arrives, and keep a total that grows as more chunks come in.</p><p>Keeping a total that you update as you go, rather than waiting for all the data, is called a <strong>running sum</strong>, and processing the data one chunk at a time like this is called working in an <strong>online</strong> fashion.</p><p>Say the first ten scores arrive. Call the largest of these ten M₁. We subtract M₁ from each of the ten, take the exponentials, and add them up:</p><pre>S_1  =  Σ_j e^(x_j - M_1)                         for j from 0 to 9</pre><p>Now the next ten scores arrive, with their own largest value. The true largest value so far is whichever is bigger, the old one or the new one. This gives us two situations.</p><p><strong>The easy situation: the old maximum is still the biggest.</strong></p><p>Nothing about the old total needs to change. We simply add the new block, measured against the same M₁:</p><pre>S_2  =  S_1 + Σ_j e^(x_j - M_1)                   for j from 10 to 19</pre><p><strong>The harder situation: the new block contains a bigger maximum, M₂.</strong></p><p>Now there is a catch. Our old total S₁ was built by subtracting the smaller number M₁. It is measured on the wrong scale. Before we add anything, we have to correct it so that it looks as if we had used M₂ all along. We do that by multiplying the old total by e^(M₁ − M₂):</p><pre>S_2  =  S_1 * e^(M_1 - M_2) + Σ_j e^(x_j - M_2)   for j from 10 to 19</pre><p>This correction step, multiplying the old total by e raised to the difference of the old maximum and the new maximum, is called the <strong>rescaling trick</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jvgE6Z4wU-nvKRkah416LQ.png" /></figure><p><em>The old total was measured against a smaller maximum, so it is too large. The factor is e raised to a negative number, which is always between zero and one, so it shrinks the old total down to the size it should have been all along. Then the new block is added on top.</em></p><p>Here is the intuition for why the factor is always between zero and one. Since M₁ is smaller than M₂, the difference M₁ − M₂ is negative, and e raised to a negative number lands somewhere between zero and one. In other words, we had been a little too generous earlier, measuring against a maximum that was smaller than the real one, so the factor gently shrinks the old total to the correct size.</p><h3>Part three: bringing in the value vectors</h3><p>So far we have only rebuilt the bottom of the softmax. The full attention output is a weighted sum of the Value vectors, where the weights are the softmax weights. We would like to stream that too. This combined step, the softmax followed immediately by the multiply with the Value vectors, is often called the <strong>softmax dot operation</strong>, or a <strong>fused softmax</strong>.</p><p>The key idea is to keep <strong>two</strong> running totals side by side.</p><ul><li>A single number, the <strong>denominator</strong> S. This is exactly the running sum from part two.</li><li>A vector, the <strong>numerator</strong> O. This is the same running sum, except every exponential is multiplied by its own Value vector v_j before being added.</li></ul><p>We keep both of them unnormalised, which simply means we do not divide anything yet. We start them empty:</p><pre>O  =  0                (a vector)<br>S  =  0                (a single number)<br>M  =  minus infinity   (the running maximum)</pre><p>For every new block, we first update the running maximum to the larger of the old maximum and the block’s maximum:</p><pre>M_new  =  maximum( M_old , M_block )</pre><p>Then we update both totals with the very same rescaling factor:</p><pre>O_new  =  O_old * e^(M_old - M_new) + Σ_j e^(x_j - M_new) * v_j<br>S_new  =  S_old * e^(M_old - M_new) + Σ_j e^(x_j - M_new)</pre><p>Notice that this single pair of formulas already covers both situations from part two. When the maximum does not change, the difference is zero, and e raised to zero is one, so the rescaling factor does nothing and we are left with plain addition. When the maximum does change, the factor corrects both totals at once. There is no need to write the two situations separately.</p><p>And now the most important point of the whole method:</p><blockquote><em>You never divide in the middle. The numerator O and the denominator S both keep growing, block after block, still unnormalised. Only once, after the very last block, do you divide.</em></blockquote><pre>Output  =  O / S          (done once, at the very end)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*elWlJwY5LFEcWWBmrPqSTg.png" /></figure><p><em>Every block updates the running maximum, the numerator, and the denominator. The single division happens only after the last block.</em></p><h3>One worry, answered</h3><p>You might ask a fair question. The old numerator already had the Value vectors multiplied into it. So how can one small factor possibly correct it?</p><p>The answer is that the Value vectors do not depend on the maximum at all. The maximum is a property of the scores, not of the Values. When the rescaling factor sweeps through the old numerator, it sits outside the sum, so it lands on every stored term at once, and it only touches the exponential part of each term. The Value vectors simply ride along, untouched. And untouched is exactly what we want, because the Value vectors were never supposed to be scaled by the maximum in the first place.</p><h3>Why this is worth the trouble</h3><p>By keeping a running maximum, a running numerator, and a running denominator, we can compute the exact attention output while reading the scores only one block at a time. We never have to build and store the full grid of scores. That is the idea at the heart of what makes modern attention kernels, such as FlashAttention, both memory friendly and fast. The rescaling trick is the small piece of arithmetic that makes the whole streaming pass possible.</p><p>Feel free to connect: <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1230276cafec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/online-softmax-in-attention-1230276cafec">Online Softmax in Attention</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convolutional kernels]]></title>
            <link>https://medium.com/analytics-vidhya/convolutional-kernels-92ba98e7713f?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/92ba98e7713f</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[convolution-neural-net]]></category>
            <category><![CDATA[kernel]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Wed, 08 Apr 2026 09:40:03 GMT</pubDate>
            <atom:updated>2026-04-08T09:45:49.130Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog, I will go though convolutional kernel operation. I will talk about how kernels look like and how they are multiplied with input. This is a blog to deepen the basic theoretical understanding of convolutional kernels and it does not cover optimizations that Pytorch does during kernel spawning and multiplication. I also don’t cover how training changes the kernel parameters and effects of activation functions in this blog.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uakEpGIyQqgoyygeXAB3vw.jpeg" /><figcaption>Palma, Spain</figcaption></figure><p>Now, let’s get started:</p><pre>Conv2d(in_channels=3, out_channels=10, kernel_size=3)</pre><p>This PyTorch code generates <strong>10 three-dimensional kernels</strong>, each of size 3×3×3. The kernel_size parameter decides the first two dimensions (height and width), while in_channels decides the depth of each kernel.</p><p>I created figures using Claude to visualize these steps. Below you will see the 10 kernels spawned when the above Conv2d command is run at the start of training:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*zy-e3HyqrNr7ESxIOYGlcw.png" /><figcaption>Image 1: Kernels created by Conv2d</figcaption></figure><p>You can see that each of these 10 kernels has three slices: one per input channel. Now we will visualize the multiplication with an RGB input image of size 5×5×3, where the 3 channels correspond to red, green, and blue. These 3 channels are also the reason in_channels is set to 3 in the Conv2d call.<br>To calculate the first output channel, the kernel is applied to 3×3 patches starting from the top-left corner. All three channel slices of the kernel are multiplied element-wise with the corresponding three channels of the input patch, and the results are summed together. A small bias, which is also part of the kernel, is then added to this sum. This bias is a scalar value (1×1).<br>In the next image, we see the output of the first kernel’s multiplication. Since this kernel produces only the first output channel, the image shows just channel index 0. The images are high resolution: if you cannot see the text clearly in the blog, download them or zoom in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vNAkf1Pz759SttWLKt5AvQ.png" /><figcaption>Image 2: Multiplications of first kernel with input image</figcaption></figure><p>The actual output is of size 3×3×10. The following image shows the final output across all channels once every kernel has been convolved across all patches in the input image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IdIBt_gVdig0SMeTJC0nEw.png" /><figcaption>Image 3: Complete output tensor after full kernel operation</figcaption></figure><p>In the above example, I assumed stride to be 1 and padding to be 0, which are the default values in PyTorch. Stride defines how many positions the kernel jumps while sliding across the image. Padding adds additional zeros around the input image.<br>Notice that in the first kernel multiplication in Image 2, the kernel’s centre sits at index (1, 1) of the input image. This is because the entire kernel must overlap with valid input values to perform the multiplication. But this leads to some loss of spatial information, since the kernel never gets to centre on the edge pixels of the input. If we set padding=1, the image below shows how the kernel multiplication changes: the kernel can now centre on position (0, 0) because the zero-padded border provides the missing neighbors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0egKVo9PvE0k8QBNUr4T2w.png" /><figcaption>Image 4: Change because of Padding</figcaption></figure><p>Both padding and stride determine how much spatial dimensionality loss occurs at the output. The formula to calculate the output size directly is:</p><p>output_size=⌊strideinput_size+2×padding−dilation×(kernel_size−1)−1​+1⌋</p><p>Here is what each parameter does:</p><ul><li><strong>padding</strong> — adds a zero-filled border around the input. padding=1 on a 5×5 input makes it 7×7 before convolution.</li><li><strong>stride</strong> — how many pixels the kernel jumps between positions. stride=1 moves one pixel at a time; stride=2 skips every other position, roughly halving the output size.</li><li><strong>dilation</strong> — spaces out the kernel elements with gaps. dilation=2 on a 3×3 kernel makes it cover a 5×5 receptive field by inserting holes between the weights. The effective kernel size becomes dilation × (kernel_size - 1) + 1.</li></ul><p>Hope this gave some clarity on how convolution actually works under the hood. Feel free to drop any questions or let me know if you think I got any calculations wrong.</p><p>Feel free to connect: <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92ba98e7713f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/convolutional-kernels-92ba98e7713f">Convolutional kernels</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[Why Attention Models Take Longer to Train]]></title>
            <link>https://medium.com/analytics-vidhya/why-attention-models-take-longer-to-train-10645709470c?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/10645709470c</guid>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[generative-ai]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Tue, 07 Apr 2026 16:53:06 GMT</pubDate>
            <atom:updated>2026-04-07T16:59:57.704Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zVlc6W-r21gmzYM-pYLmjQ.jpeg" /><figcaption>Caminito Del Rey, Spain</figcaption></figure><p>Training a vision transformer from scratch is a notoriously painful experience. Where a well designed CNN might converge in 30 to 50 epochs on a moderately sized dataset, a pure attention based model doing the same task often needs 300 epochs or more. ViT famously required pretraining on JFT-300M before it became competitive with CNNs on ImageNet. DeiT needed aggressive augmentation, regularization, and distillation to train from scratch on ImageNet alone. BEVFormer and DETR style detectors routinely train for 200+ epochs where their convolutional counterparts are done in a fraction of that.<br>This is not a coincidence, and it is not just about “transformers needing more data.” There is a fundamental mathematical reason why attention based architectures start from a worse place and take longer to escape it. The culprit is the convergence problem baked into how dot product attention interacts with standard weight initialization.</p><h3>The Uniform Attention Trap</h3><p>Let us walk through what happens at the very first forward pass of a cross attention layer.<br>We have queries projected by a matrix U and keys projected by a matrix V. With standard parameter initialization, whether Xavier or Kaiming, the projected queries and keys follow distributions with approximately zero mean and unit variance. The dot product between a query and a key is therefore a sum of products of zero mean random variables. By construction, this dot product lands near zero with small variance.<br>Now we pass these dot products through softmax. The softmax function exponentiates each value and normalizes. When all the dot products are near zero, exp(0) = 1, so every key gets roughly the same weight:</p><p>A(q, k) ≈ 1 / N_k for all k</p><p>This is uniform attention. Every query attends to every key equally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*1LHdiFg3_q_TX4Uld0W1Jw.png" /><figcaption>The light purple bars show what happens at initialization: every key gets exactly 1/16 of the attention (6.25%), completely uniform. The dark purple bars show what trained attention should look like: a few keys dominate while most get almost nothing. The gap between those two distributions is what the network has to close over hundreds of epochs</figcaption></figure><h3>Why Uniform Attention Is Catastrophic</h3><p>Consider what this means in a spatial attention setting. If we are doing cross attention over a feature map of size H x W, every query pixel is computing a weighted average of all H x W value vectors, with all weights approximately equal. The output of the attention layer is essentially a global spatial average.</p><p>This is the equivalent of applying global average pooling at every layer. The spatial structure is destroyed. Every output token looks nearly identical regardless of its position.</p><p>The problem compounds when we think about gradients. The loss function needs to push the network toward attending to specific, relevant locations. But when attention is uniform, the gradient of the loss with respect to each attention weight is spread thinly across all N_k keys. The signal that says “pay more attention to this pixel and less to that one” is diluted by a factor of N_k. For a 200 x 200 feature map, that is a dilution factor of 40,000.</p><p>The network has to make many small, incremental updates across many iterations to gradually break this symmetry. It needs to slowly sharpen the flat attention distribution into sparse, peaked patterns where specific queries attend to specific relevant keys. This is a fundamentally harder optimization problem than what a convolution faces at initialization, where locality is built into the structure from day one.</p><h3>CNNs Do Not Have This Problem</h3><p>A 3x3 convolution kernel only ever looks at 9 neighbors. There is no softmax normalization across the entire spatial extent. The inductive bias of locality means that even at random initialization, a convolutional layer is already doing something spatially reasonable: it is combining a small local neighborhood. The gradients are concentrated on those 9 weights, not spread across thousands.</p><p>This is the core asymmetry. CNNs start with a strong structural prior (locality) and learn to combine local features into global understanding. Attention models start with no structural prior and must learn locality itself from the loss signal, which initially provides almost no information about where to attend.</p><h3>This Explains Several Known Phenomena</h3><p>Once we see the uniform attention trap clearly, a number of well known observations start making sense.<br>ViT needs massive pretraining or heavy regularization. The model has no way to bootstrap meaningful attention patterns without either seeing enormous amounts of data or being heavily constrained during training to prevent the flat attention maps from causing divergence.<br>DETR trains for 300+ epochs. The Hungarian matching loss in DETR already makes optimization hard, but the attention layers on top of that are fighting against the uniform initialization problem simultaneously. The model spends hundreds of epochs just learning where to look before it can learn what to detect.<br>Deformable attention was a breakthrough. Deformable DETR replaced dense global attention with attention over a small set of learned reference points. This is essentially injecting a locality prior back into the attention mechanism. Instead of starting from uniform attention over the entire feature map, the model starts from a small neighborhood around each reference point. The convergence problem is dramatically reduced, which is why Deformable DETR converges in 50 epochs instead of 500.<br>Warmup is critical for transformers. Large learning rates early in training can destabilize the fragile symmetry breaking process. The attention maps are flat and the gradients are diffuse. A large step in a random direction does not help. Slow warmup gives the network time to gently break the uniform pattern before accelerating.<br>Hybrid architectures converge faster. Models that use a CNN backbone with attention layers on top, or that interleave convolutions with attention, consistently train faster than pure attention models. The convolutional layers provide spatially structured features from the start, giving the attention layers a much better starting point than random uniform maps.</p><h3>The Practical Implications</h3><p>If we are designing an attention based model, especially for a domain where massive pretraining datasets are not available, we should be deliberate about mitigating this convergence problem.<br>Initialize attention with locality in mind. If we know the task has spatial structure, we can initialize the attention biases or reference points to favor local neighborhoods. This gives the model a reasonable starting point instead of uniform.<br>Use hybrid architectures. Feeding CNN extracted features into attention layers lets us get the best of both worlds. The CNN provides spatial structure cheaply, and the attention layers learn long range dependencies on top of that.<br>Monitor attention entropy during training. If the attention maps stay near uniform (high entropy) for too many epochs, something is wrong. The model is stuck in the flat attention regime and may need a different learning rate schedule, initialization, or architectural change.<br>Consider deformable or windowed attention. Restricting the attention scope, whether through deformable reference points, sliding windows, or other mechanisms, directly reduces the severity of the uniform initialization problem by shrinking N_k.</p><h3>Conclusion</h3><p>The reason attention models take longer to train is not mysterious. It is a direct consequence of dot product attention combined with standard initialization producing uniform attention maps at the start of training. Every query attends to every key equally, gradients are diffuse, and the network must slowly and painfully learn to concentrate attention where it matters.<br>CNNs sidestep this entirely through their structural bias toward locality. Attention models must learn what CNNs get for free, and that learning process is inherently slow.<br>Understanding this gives us a clear framework for designing attention architectures that converge faster: inject locality priors, use hybrid designs, and monitor whether the attention is actually learning to be selective. The epochs we save are not just a convenience. They are evidence that we have given the optimization problem a better starting point.</p><p>Feel free to reach out here: <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10645709470c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/why-attention-models-take-longer-to-train-10645709470c">Why Attention Models Take Longer to Train</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[Goggle Gemma LLM Deployment on AWS]]></title>
            <link>https://medium.com/analytics-vidhya/fastapi-based-chatbot-deployment-on-aws-0d8c7a0be965?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d8c7a0be965</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[gemma]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[google-gemma]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Thu, 26 Jun 2025 00:39:21 GMT</pubDate>
            <atom:updated>2025-07-06T20:46:23.453Z</atom:updated>
            <content:encoded><![CDATA[<p>A quick, hands-on guide to spinning up your own mini LLM demo for about the price of a coffee.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*SK1aAQkumyIoC5XDDDa5yg.gif" /><figcaption>Silvester, Munich, DE</figcaption></figure><h3>Why bother?</h3><p>Running a small language model yourself is the best way to peek under the hood of modern AI.<br>In this walk-through we’ll launch a GPU-backed EC2 instance, build a Docker image that bundles <strong>FastAPI</strong> with Google’s lightweight <strong>Gemma-2B</strong> model, and serve a basic web chat UI.</p><p>At the end you should be able to access your own chatbot from anywhere in world-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FTS5RHISB2RdqVptmenwg.png" /></figure><p>Total cost if you follow the steps and shut everything down when you’re done: <strong>≈ €3–4</strong>.</p><p>Prerequisites</p><ul><li><strong>AWS account</strong> — sign up if you haven’t already.</li><li><strong>Quota increase</strong> for G5 instances (explained below).</li><li>A <strong>Hugging Face access token</strong> (free — create one from your HF profile).</li><li>Source code: <a href="https://github.com/prav2909/deploy_gemma.git">https://github.com/prav2909/deploy_gemma.git</a></li><li>About 30 minutes of focused clicking and copy-pasting.</li></ul><p>Let’s get started!</p><h3>Request a GPU quota</h3><p>AWS limits how many accelerated instances you can launch by default.</p><ol><li>Open <em>Service Quotas</em> → <strong>Elastic Compute Cloud</strong> → <strong>Running On-Demand G and VT instances</strong>.</li><li>Hit <strong>Request quota increase</strong>, type <strong>8</strong>, and submit.</li><li>Approval usually lands within a couple of hours.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*DKzwlgOg3_Lh7WsM8mxqMg.png" /></figure><h3>Grab my project repo</h3><p>git clone <a href="https://github.com/prav2909/deploy_gemma.git">https://github.com/prav2909/deploy_gemma.git</a><br>cd <a href="https://github.com/prav2909/deploy_gemma.git">deploy_gemma</a></p><h3>Create an AWS security group</h3><ol><li>Go to <strong>EC2</strong> → <strong>Security Groups</strong> → <strong>Create security group</strong>.</li><li>Name it <strong>chatbot</strong>.</li><li>Add inbound rules:</li><li><strong>SSH</strong> (TCP 22) — <em>your IP only</em> if possible, otherwise 0.0.0.0/0</li><li><strong>HTTP</strong> (TCP 8080) — 0.0.0.0/0 so anyone can test the bot.</li><li>Leave outbound rules as-is (all traffic allowed).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FNMYVLhx6HHxHADkTu5l6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JBAb5MU4OKkFYQnWBsBA3g.png" /></figure><h3>Generate an EC2 key pair</h3><ol><li>Go to : <a href="https://us-east-1.console.aws.amazon.com/ec2/home?region=us-east-1#KeyPairs:">Key pairs | EC2 | us-east-1</a> or <strong>EC2 → Key pairs.</strong></li><li><strong>Create key pair</strong>.</li><li>Name: <strong>docker_cli</strong>.</li><li>Type: <strong>RSA</strong>.</li><li>Download the .pem file and secure it:</li></ol><p>chmod 400 /path/to/docker_cli.pem</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bpQV-fQ9lw4AbKY1arY2aQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qQqHufgc-XRkpONyko6lUg.png" /></figure><h3>Launch the GPU instance</h3><ul><li>Go to : <a href="https://us-east-1.console.aws.amazon.com/ec2/home?region=us-east-1#LaunchInstances:">Launch an instance | EC2 | us-east-1</a></li><li>Name: <strong>chatbot ec2 instance</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JmbBNYZzv_Cb8XxIvX1gbw.png" /></figure><ul><li><strong>AMI</strong>: choose <strong>Deep Learning AMI GPU Ubuntu 22.04 v2025–05–15</strong> (pre-installs drivers and nvidia-docker2). Image id: <strong>ami-0d989cf983fcb6fda</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eia4i9og9-ZZDNlFd5qqfQ.png" /></figure><ul><li><strong>Instance type</strong>: <strong>g5.xlarge: </strong>1 × NVIDIA A10G (24 GiB VRAM), 4 vCPU, 16 GiB system RAM</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vPHj3cANXyc_j--OFV6BXg.png" /></figure><ul><li><strong>Key pair</strong>: <strong>Name = docker_cli</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FkSLeY4-BeQlcd0d_VK5gw.png" /></figure><ul><li><strong>Network settings</strong>: pick <strong>chatbot</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7CbhqLFHKzSRq4rsmytfVw.png" /></figure><ul><li><strong>Storage</strong>: <strong>80 GiB</strong> (gp3)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VbOduNJ2VZhCNKe1zacdpQ.png" /></figure><ul><li>Enter <strong>User data:</strong></li></ul><blockquote>#!/bin/bash -xe<br>export DEBIAN_FRONTEND=noninteractive<br>apt-get update<br>apt-get install -y docker.io git python3 python3-pip<br>systemctl enable — now docker<br>usermod -aG docker ubuntu<br>docker pull nvidia/cuda:12.8.0-runtime-ubuntu22.04</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*y-h0nd3QoQ3oynQd44wvZw.png" /></figure><ul><li>Click <strong>Launch instance</strong> and wait until its state is <strong>running</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*j_OkAMdLHa4oj6CfhMekaQ.png" /></figure><ul><li>Copy the <strong>Public IPv4 address. I will refer to this IP address as EC2_IP.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*GVAU4KEvgsdK13Er6mh8kQ.png" /></figure><h3>Upload the code to EC2</h3><p>On your local machine, inside deploy_gemma:</p><blockquote>scp -i /path/to/docker_cli.pem -r . ubuntu@&lt;EC2_IP&gt;:~/deploy_gemma</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QRW58paqz8VVVVb2TxJH4Q.png" /></figure><h3>Then SSH into EC2 instance</h3><blockquote>ssh -i /path/to/docker_cli.pem ubuntu@&lt;EC2_IP&gt;<br>cd ~/deploy_gemma</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*txkI_kA-uDSoxokqPcYUOg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lAiMr3VA67VfPUTPhTZszA.png" /></figure><h3>Build and run the Docker image</h3><blockquote>docker build -t gemma_chatbot:latest .</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KXxoNx2BeOyo1knqZeTmA.png" /></figure><h3>Start the container, map port 8080, and pass your Hugging Face token</h3><blockquote>docker run — gpus all -it \<br> -e HF_TOKEN=&lt;YOUR_HF_TOKEN&gt; \<br> -p 8080:8080 \<br> gemma_chatbot:latest</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AJRYlCnhx0ICexRDm-sX3g.png" /></figure><h3>Talk to your model from anywhere🎉</h3><p>Make sure to use http and not https.</p><blockquote>http://&lt;EC2_IP&gt;:8080</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FTS5RHISB2RdqVptmenwg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W1Gp81PdLo7SpmsSkYP6xw.png" /></figure><h3>Clean up (avoid surprise bills)</h3><ol><li><strong>EC2 → Instances</strong></li><li><strong>Instance state → Terminate → Yes, terminate</strong>.</li></ol><p>With prompt shutdown, you’ll spend about <strong>€1.50 per hour</strong> of actual runtime plus a few cents for storage, well within the promised €3–4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uk2pTa8_FC2dDv-Y2qNGlQ.png" /></figure><h3>This blog ends here, but next possible steps could(rather should) be:</h3><ul><li>Add TLS (ACM certificate + Application Load Balancer).</li><li>Store model weights on <strong>S3</strong> and mount with <strong>EFS</strong> for faster cold starts.</li><li>Swap in <strong>Gemma-7B</strong> or another model that fits your budget.</li><li>Replace the bare-bones HTML with a React or Svelte front end.</li></ul><p>If this guide helped, drop a clap and connect with me on <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">LinkedIn</a>.</p><p>Happy building!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d8c7a0be965" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/fastapi-based-chatbot-deployment-on-aws-0d8c7a0be965">Goggle Gemma LLM Deployment on AWS</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[Understanding the Bias-Variance Tradeoff in Machine Learning]]></title>
            <link>https://medium.com/analytics-vidhya/understanding-the-bias-variance-tradeoff-in-machine-learning-f1cb74169250?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1cb74169250</guid>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Tue, 20 Aug 2024 12:01:49 GMT</pubDate>
            <atom:updated>2024-08-20T19:11:53.444Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*RbkRZlAahSwG8WsJpOdoJA.jpeg" /></figure><blockquote><em>“</em>Kaamyaab hone ke liye nahin, Kaabil hone ke liye padho! (Study not to be successful, but to be capable!)” by Ranchoddas Shamaldas Chanchad, 3 Idiots</blockquote><p>Machine learning models aim to make predictions or uncover patterns from data. However, achieving a perfect model is often an unattainable goal. Instead, we encounter the challenge of balancing two types of errors that our models can make: bias and variance. This balance is known as the Bias-Variance Tradeoff.</p><h3>What is Bias?</h3><p>Bias refers to the error introduced by approximating a real-world problem, which may be complex, by a simplified model. High bias can cause an algorithm to miss the relevant relations between features and target outputs, leading to underfitting. Underfitting occurs when a model is too simple to capture the underlying structure of the data.</p><p>For example:</p><ul><li>If we use a linear model (like linear regression) to capture a non-linear pattern in the data, this would typically result in high bias.</li><li>In this scenario, the model makes strong assumptions about the structure of the data and likely will not capture the complexity of the true relationship.</li></ul><h3>What is Variance?</h3><p>Variance refers to the error introduced because the model is too sensitive to small fluctuations in the training data. High variance means the model learns the noise in the training data, rather than the actual trend, leading to overfitting. Overfitting occurs when a model is too complex and matches the training data too closely.</p><p>For example:</p><ul><li>Using a high-degree polynomial might fit the training data perfectly but perform poorly on unseen data because it captures the noise in the training data as well.</li><li>In this situation, the model has low bias but high variance.</li></ul><h3>The Bias-Variance Tradeoff</h3><p>The tradeoff between bias and variance is a crucial concept in machine learning. Here’s the crux of the tradeoff:</p><ul><li><strong>Low bias &amp; High variance</strong>: Complex models (e.g., high-degree polynomials, deep neural networks) which fit the training data very well but may not generalize to new, unseen data.</li><li><strong>High bias &amp; Low variance</strong>: Simple models (e.g., linear regression) which may not fit the training data well but are more generalizable.</li></ul><p>The goal is to find the “sweet spot” where both bias and variance are minimized to reduce the overall error, which can be decomposed into:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*hgprWQykrnqo6moAUlAA6A.png" /></figure><ul><li><strong>Bias</strong>: Systematic error due to incorrect assumptions in the learning algorithm.</li><li><strong>Variance</strong>: Error due to variability in the model prediction.</li><li><strong>Irreducible Error</strong>: Error that can’t be reduced due to noise in the system.</li></ul><h3>Visualizing the Tradeoff</h3><p>Imagine plotting the training error and validation error as functions of model complexity:</p><ul><li>As model complexity increases, training error generally decreases because the model fits the training data better.</li><li>However, after a certain point, the validation error starts to increase as the model begins to overfit, capturing noise rather than the intended data structure.</li></ul><h3>Strategies for Managing the Tradeoff</h3><ol><li><strong>Regularization</strong>: Techniques like Lasso, Ridge, and ElasticNet penalize model complexity to prevent overfitting.</li><li><strong>Cross-Validation</strong>: Split the data into training and validation sets to ensure that the model generalizes well.</li><li><strong>Pruning</strong>: In decision trees, removing parts of the model that do not provide power to generalize, reducing complexity.</li><li><strong>Ensemble Methods</strong>: Combining the predictions of multiple models to reduce variance (like bagging, boosting).</li></ol><h3>How Data Can Impact Bias During Learning</h3><p>The quality and quantity of data provided to machine learning models significantly influence the bias during the learning process. Here are several ways in which data can impact bias:</p><ol><li><strong>Insufficient Training Data</strong>:</li></ol><ul><li><strong>Issue</strong>: When training data is limited, the model may not have enough information to learn the underlying patterns effectively.</li><li><strong>Impact on Bias</strong>: The model is likely to make broad assumptions about the data, leading to high bias and underfitting. For instance, a model trained on a small dataset may not capture all possible variations and anomalies present in a larger, more comprehensive dataset.</li></ul><p>2. <strong>Irrelevant or Noisy Features</strong>:</p><ul><li><strong>Issue</strong>: Including irrelevant or noisy features can mislead the model, causing it to focus on unimportant details.</li><li><strong>Impact on Bias</strong>: The presence of noisy features can increase bias by creating confusion between the true signal and the noise, leading to model simplification and general patterns being missed.</li></ul><p>3. <strong>High Dimensionality</strong>:</p><ul><li><strong>Issue</strong>: High-dimensional datasets can make it challenging for models to identify relevant patterns without sufficient samples.</li><li><strong>Impact on Bias</strong>: To cope with high-dimensional data, simpler models may revert to oversimplification, increasing bias as they fail to capture complex, high-dimensional relationships.</li></ul><p>4. <strong>Class Imbalance</strong>:</p><ul><li><strong>Issue</strong>: When one class significantly outnumbers another in classification problems, the model might learn to favor the majority class.</li><li><strong>Impact on Bias</strong>: This skewed learning can lead to high bias, particularly against the minority class, resulting in poor predictive performance for those underrepresented classes.</li></ul><p>5. <strong>Non-representative Data</strong>:</p><ul><li><strong>Issue</strong>: If the training data is not representative of the real-world scenario (e.g., it covers specific conditions or time periods inadequately), the model may generalize poorly.</li><li><strong>Impact on Bias</strong>: Such non-representative data can cause the model to develop strong assumptions that do not hold in unseen data, leading to high bias.</li></ul><h3>Strategies to Mitigate Data-Induced Bias</h3><ol><li><strong>Collect More Data</strong>: Increasing the quantity of high-quality training data helps models learn more comprehensive patterns, reducing bias. Diverse and larger datasets capture the complexity of real-world scenarios more effectively.</li><li><strong>Feature Engineering</strong>: Carefully selecting relevant features and removing noisy or irrelevant ones can help lower bias by ensuring the model focuses on pertinent information.</li><li><strong>Data Augmentation</strong>: For image, text, or audio data, techniques like cropping, rotation, or adding noise can artificially expand the dataset, contributing to better generalization and reducing bias.</li><li><strong>Resampling Methods</strong>: Techniques such as undersampling the majority class or oversampling the minority class (e.g., SMOTE) can help address class imbalance, leading to a fairer representation of all classes.</li><li><strong>Cross-Validation</strong>: Using k-fold cross-validation helps ensure that the model tests on various data segments, providing a more reliable estimate of its predictive performance and reducing the likelihood of high bias.</li></ol><h3>Conclusion</h3><p>Understanding and managing the bias-variance tradeoff is essential for developing machine learning models that generalize well to new data. Striking the right balance involves careful model selection, tuning, and cross-validation to ensure that both underfitting and overfitting are minimized.</p><p>Finding the optimal tradeoff can significantly improve the predictive performance of your models, making bias-variance analysis an indispensable tool for any machine learning practitioner.</p><p>Feel free to reach out here: <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1cb74169250" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/understanding-the-bias-variance-tradeoff-in-machine-learning-f1cb74169250">Understanding the Bias-Variance Tradeoff in Machine Learning</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[Types of Loss functions and their use cases and code]]></title>
            <link>https://medium.com/codex/quick-overview-types-of-loss-functions-and-their-use-cases-2868fc649d79?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/2868fc649d79</guid>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Tue, 20 Aug 2024 08:02:48 GMT</pubDate>
            <atom:updated>2024-08-23T10:53:45.266Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*HdzeCk5boNaU9MYnuzRsRQ.gif" /><figcaption>Neuschwanstein Castle, Bavaria.</figcaption></figure><p>In the fields of machine learning and statistics, loss functions (also known as cost functions or objective functions) play a pivotal role in training models. They provide a way to measure how well a model’s predictions align with actual outcomes. The choice of loss function is crucial because it directly affects the model’s performance, including its accuracy, stability, and generalization capabilities. This article delves into various types of loss functions, explaining their mathematical formulations and typical use cases, to help you choose the right one for your application.</p><h3>1. Mean Squared Error (MSE)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*5yi2DkOvFJhgZ35qRFcsiQ.png" /></figure><ul><li><strong>Use Case</strong>: House price prediction, stock price forecasting, or any situation where the goal is to predict a continuous variable.</li><li><strong>Details</strong>: In scenarios where you need to penalize larger errors more significantly, MSE is a good choice. For example, in predicting house prices, an inaccurate prediction with a large error would be more detrimental, and MSE ensures such large errors are minimized over iterations.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lFFQaDb9uZmyfLbKcjFYOw.png" /></figure><h3>2. Mean Absolute Error (MAE)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*SWfxo4MzYdMpFmpg9F5Eag.png" /></figure><ul><li><strong>Use Case</strong>: Median house price prediction, demand forecasting.</li><li><strong>Details</strong>: MAE is useful in situations where all errors are equally weighted, making it robust to outliers. For example, predicting the median sales price of properties in different areas where occasional outliers (extremely high or low prices) should not disproportionately affect the model.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cKJhy08vIFzi3KeTEN67qQ.png" /></figure><h3>3. Binary Cross-Entropy Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*YUcfDWRQloi_xFOHt62JVw.png" /></figure><ul><li><strong>Use Case</strong>: Spam detection, credit card fraud detection.</li><li><strong>Details</strong>: Suitable for binary classification problems where the output is probabilistic. In spam detection, for example, the model predicts the probability of an email being spam. Binary cross-entropy is optimized to distinguish between the two classes effectively.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4QAVnaYOCQQKub91TgVPCQ.png" /></figure><h3>4. Categorical Cross-Entropy Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*_ZYJRFSLLJ5qqczFKlrzCA.png" /></figure><ul><li><strong>Use Case</strong>: Image classification, sentiment analysis.</li><li><strong>Details</strong>: Essential for multi-class classification problems. For instance, in image classification, where an image needs to be classified into one of several categories, categorical cross-entropy helps in training a model that outputs a probability distribution over the possible classes.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*rOFs3xRMjz-2kqvB-0R0pw.png" /></figure><h3>5. Kullback-Leibler Divergence (KL Divergence)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*0h0DLBizTxyLFWOtLLT6tw.png" /></figure><ul><li><strong>Use Case</strong>: Variational autoencoders, information retrieval.</li><li><strong>Details</strong>: Used in situations involving probability distributions. In variational autoencoders, KL divergence helps in regularizing the latent space to resemble a specified distribution. In information retrieval, it measures the difference between the observed frequency distribution and the predicted distribution.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*a2sGhTxXz1y3T5FOitPKPg.png" /></figure><h3>6. Hinge Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*_Aszo8ve2MDILx2RYM_9NA.png" /></figure><ul><li><strong>Use Case</strong>: Text classification, image recognition using SVM.</li><li><strong>Details</strong>: Common in training support vector machines (SVMs) for binary classification tasks. For example, in text classification tasks like sentiment analysis, where the text needs to be classified as positive or negative, hinge loss ensures the model maximizes the margin between the two classes.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NXGdwRxErqlmkb7D75PwZA.png" /></figure><h3>7. Log-Cosh Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*g2_ASNgNZFAwzxpP5ZQc9Q.png" /></figure><ul><li><strong>Use Case</strong>: Financial forecasting, anomaly detection.</li><li><strong>Details</strong>: Log-Cosh is beneficial in financial forecasting where you want a smooth loss function. It combines the properties of MSE and MAE, making it robust against outliers while providing a smooth gradient for optimization. In anomaly detection, it can help identify rare events while minimizing the effect of outliers.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sBNuXwh1Ac2FhJGLJH_UVA.png" /></figure><h3>8. Quantile Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*G56-bucqMkFdXtPhfEz-OA.png" /></figure><ul><li><strong>Use Case</strong>: Predictive quantile regression, risk assessment.</li><li><strong>Details</strong>: Used in scenarios requiring uncertainty estimation and predictions at different quantiles. For instance, in financial risk assessment, predicting different quantiles (e.g., 10th, 50th, and 90th percentiles) of potential losses can provide a comprehensive risk profile.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zELVUd97rvlXfPVAoAzzQQ.png" /></figure><h3>9. Poisson Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*VvhdnqiX8_AM_mx71_i5nw.png" /></figure><ul><li><strong>Use Case</strong>: Medical research, citation prediction.</li><li><strong>Details</strong>: Suitable for count data modeling. For instance, in medical research, predicting the number of occurrences of a particular event (like the number of hospital visits) can be naturally modeled using Poisson loss. Similarly, predicting the number of citations an academic paper will receive.</li><li><strong>Sample Code:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ELfIHxcuS23SIijtnJMWFg.png" /></figure><h3>Choosing the Right Loss Function</h3><p>The choice of loss function significantly influences:</p><ul><li><strong>Model Performance</strong>: Different loss functions can converge differently and may affect the final model’s accuracy, stability, and generalization capabilities.</li><li><strong>Outliers Handling</strong>: Some loss functions like MSE are sensitive to outliers, whereas others like MAE or Log-Cosh Loss handle them more gracefully.</li><li><strong>Application Specificity</strong>: The specific problem domain often dictates the appropriate loss function. For instance, cross-entropy losses are well-suited for classification tasks.</li></ul><p>In summary, loss functions are fundamental in guiding the optimization and ultimate performance of machine learning models. Selecting the right loss function depends on the nature of the problem, the type of data, and the desired properties of the predictive model. These Python code snippets with PyTorch illustrate how to implement each loss function, providing a practical approach to understanding and applying them in real-world scenarios.</p><p>Feel free to connect here: <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2868fc649d79" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/quick-overview-types-of-loss-functions-and-their-use-cases-2868fc649d79">Types of Loss functions and their use cases and code</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decoding the Transformer Model: Architecture, Loss Function, and Inference from the ‘Attention is…]]></title>
            <link>https://medium.com/analytics-vidhya/decoding-the-transformer-model-architecture-loss-function-and-inference-from-the-attention-is-717b98d183b3?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/717b98d183b3</guid>
            <category><![CDATA[attention-is-all-you-need]]></category>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[transformer-basics]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Sun, 18 Aug 2024 22:09:47 GMT</pubDate>
            <atom:updated>2024-08-19T08:26:18.649Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Decoding the Transformer Model: Architecture, Loss Function, and Inference from the ‘Attention is All You Need’ Paper</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*rKmvQZkqpr0lspx1Vcun7g.gif" /><figcaption>Eiffel Tower caught on my iPhone, quality reduced a million times to fit on this page</figcaption></figure><p>The “Attention is All You Need” paper by Vaswani et al. revolutionized the field of natural language processing (NLP) and machine learning with its introduction of the Transformer model. Unlike previous neural network models that relied on recurrence, the Transformer leverages self-attention mechanisms to improve efficiency and performance. In this blog post, we’ll delve into the intricate details of the Transformer’s architecture, the creation of Query, Key, and Value, the loss function, and what happens during inference time.</p><h3>The Transformer Architecture:</h3><p>The Transformer model consists of two main components: the <strong>encoder</strong> and the <strong>decoder</strong>. Both are designed to process sequences of words (or tokens) efficiently using multi-head self-attention mechanisms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*Bq14VhoMF_YtYuEvsZ52FQ.png" /><figcaption>Transformer Architecture from Original Paper</figcaption></figure><h4>1. Encoder:</h4><p>The encoder processes the input sequence and converts it into a context-rich representation. The encoder itself is made up of a stack of identical layers, each containing two main components:</p><p><strong>a) Multi-Head Self-Attention Mechanism:</strong></p><p>The self-attention mechanism calculates attention scores for each word in a sequence concerning all other words in the sequence. This allows the model to weigh the importance of each word relative to the others.</p><p>Creation of Query, Key, and Value:</p><p>Each word (or token) in the input sequence is first embedded into a continuous vector space. These embeddings are then linearly transformed to generate three different vectors: Query ((Q)), Key ((K)), and Value ((V)).</p><p>Mathematically, this can be represented as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*6jNumFyUHM2INCFfXS3gZA.png" /></figure><p>where:</p><ul><li>(X) is the input embedding matrix,</li><li>(W^Q, W^K, W^V) are weight matrices learned during the training process.</li></ul><p>The self-attention mechanism operates as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/951/1*FSUQDC4-Zi7AAlmJyIVKcg.png" /></figure><p>where:</p><ul><li>(Q) (Query) is the matrix of query vectors,</li><li>(K) (Key) is the matrix of key vectors,</li><li>(V) (Value) is the matrix of value vectors,</li><li>(d_k) is the dimension of the key vectors.</li></ul><p><strong>b) Position-Wise Feed-Forward Network:</strong></p><p>This component processes the output of the self-attention mechanism for each word independently through a series of linear transformations and activation functions. This helps to introduce non-linearity and further refine the word representations.</p><h4>2. Decoder:</h4><p>The decoder generates the output sequence by attending (focusing) on the relevant parts of the input sequence (using the encoded representations). Like the encoder, it has a stack of identical layers, each containing:</p><p><strong>a) Masked Multi-Head Self-Attention Mechanism:</strong></p><p>This mechanism is similar to the encoder’s self-attention but is masked to prevent the decoder from “cheating” by looking at the future words in the sequence during training.</p><p><strong>b) Encoder-Decoder Attention:</strong></p><p>This layer allows the decoder to attend to the encoder’s output representations, effectively aligning the input and output sequences.</p><p><strong>c) Position-Wise Feed-Forward Network:</strong></p><p>Similar to the encoder, this network further processes the attention outputs.</p><h3>Loss Function:</h3><p>The “Attention is All You Need” paper utilizes a variant of the cross-entropy loss known as <strong>label smoothing cross-entropy</strong>. Traditional cross-entropy loss can sometimes lead to overconfident predictions, which in turn might make the model less robust. Label smoothing helps mitigate this issue by smoothing the target labels.</p><p><strong>Label Smoothing:</strong> With label smoothing, the hard one-hot encoded vectors representing true labels are replaced by a smoothed version, which assigns a little bit of probability mass to all other classes.</p><p>Mathematically, if the original one-hot encoded target is ( y ) and ( \epsilon ) is the smoothing factor, the new smoothed target ( y’ ) is given by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*c9O7w1x8YS_h5laFiQ9_-A.png" /></figure><p>where ( K ) is the number of classes.</p><p>By employing label smoothing, the loss becomes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*KB8bxcjOhokgfl5L2pIWoA.png" /></figure><p>where ( p_i ) are the predicted probabilities, and ( N ) is the number of classes.</p><h3>Inference Time:</h3><p>At inference time, the Transformer model goes through a slightly modified process as compared to training time, especially in the decoding phase.</p><h4>Inference Process:</h4><p><strong>Step 1: Encoding the Input Sequence</strong></p><ul><li>Just as during training, the encoder processes the entire input sequence. Each word is embedded and transformed into the Query, Key, and Value vectors. The multi-head self-attention mechanism is applied, followed by the feed-forward network, to produce context-rich representations for each word in the input sequence.</li></ul><p><strong>Step 2: Decoding the Output Sequence</strong></p><ul><li>Unlike training, where the entire output sequence is available, inference is typically done one step at a time (autoregressive generation). Initially, the decoder takes a start token as input.</li><li>The decoder processes its input with masked self-attention, ensuring it only attends to previously generated tokens.</li><li>The encoder-decoder attention mechanism is utilized to align the decoder’s input with the encoder’s output representations.</li></ul><p><strong>Step 3: Generating the Next Token</strong></p><ul><li>At each time step, the decoder generates a probability distribution over the vocabulary for the next token using the softmax layer.</li><li>The token with the highest probability (or using techniques like beam search for more sophisticated selection) is added to the output sequence.</li></ul><p><strong>Step 4: Iterative Process</strong></p><ul><li>The newly generated token is fed back into the decoder as input for the next time step.</li><li>The process repeats until a special end-of-sequence token is produced or a specified maximum length is reached.</li></ul><h3>Conclusion:</h3><p>The “Attention is All You Need” paper introduced the Transformer model, setting new benchmarks in NLP tasks, especially in machine translation. With its unique architecture featuring multi-head self-attention, parallelizable stacks of encoders and decoders, and robust label smoothing cross-entropy loss function, the Transformer model delivers unparalleled efficiency and effectiveness. Furthermore, its autoregressive decoding process ensures that it generates coherent and contextually accurate sequences during inference. The Transformer’s success has not only transformed machine translation but has paved the way for advancements in many other NLP applications.</p><p>Feel free to connect here: <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=717b98d183b3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/decoding-the-transformer-model-architecture-loss-function-and-inference-from-the-attention-is-717b98d183b3">Decoding the Transformer Model: Architecture, Loss Function, and Inference from the ‘Attention is…</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[ByteTrack …Because deep learning is not the ‘only’ solution]]></title>
            <link>https://medium.com/analytics-vidhya/bytetrack-because-deep-learning-is-not-the-only-solution-adce87deb282?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/adce87deb282</guid>
            <category><![CDATA[mot]]></category>
            <category><![CDATA[paper]]></category>
            <category><![CDATA[explained]]></category>
            <category><![CDATA[bytetrack]]></category>
            <category><![CDATA[byte]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Sun, 31 Jul 2022 14:08:41 GMT</pubDate>
            <atom:updated>2022-08-02T05:19:45.010Z</atom:updated>
            <content:encoded><![CDATA[<h3>ByteTrack …Because deep learning is not the ‘only’ solution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SXDsicy0_zAUZ80cuuKurQ.jpeg" /><figcaption>Scarborough, England.</figcaption></figure><p>Several deep learning models(<a href="https://arxiv.org/abs/1409.4842">GoogleNet</a>, <a href="https://proceedings.neurips.cc/paper/2012/hash/c399862d3b9d6b76c8436e924a68c45b-Abstract.html">Alexnet</a>) exist that can perform object classification tasks quite efficiently. It is a simple task of predicting which among a set of classes an object belongs to, without giving any information about the localization of the object in an image. The next challenge is to find the location of the object in the image. Several models(<a href="https://arxiv.org/abs/2107.08430">Yolox</a>, <a href="https://arxiv.org/abs/1506.01497">Faster RCNN</a>) can help accomplish this. The next obvious step is to track these detected objects. Many deep learning-based trackers have already been developed and work pretty well. However, ByteTrack is a tracker that uses deep learning-based detectors to find objects and their location in a video sequence and uses <strong>non-deep learning</strong> techniques to track these detections across the frames. It saves computational complexity, but not at the expense of performance. In fact, it achieved a score of 77.8 on MOTA 2020 challenge(<a href="https://paperswithcode.com/sota/multi-object-tracking-on-mot20-1">Link</a>), outperforming every other tracker submitted in the challenge.</p><p>The algorithm has piqued my interest and in this short article, I will walk us through the steps of the algorithm.</p><p>The figure below shows the Algorithm presented in the original paper:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*caWHiytZKGtzGxRYEv58TQ.png" /><figcaption>Figure 1: Byte Algorithm</figcaption></figure><p>ByteTrack is an overall solution that performs both detection and tracking, however, the tracking part of the solution is the novel thing presented in the paper and is called BYTE. The authors of the paper combined BYTE with an existing detection algorithm called YOLOX (the latest version of YoLo) and called it ByteTrack.</p><p>Before we dive into the algorithm, the below keywords are good to keep in mind:</p><ol><li>Detector: An algorithm that performs detection on an image or a video sequence and gives information about the location and classification of several objects of interest</li><li>Confidence score: The confidence or probability, as given by the detector, that the detected object is present at a location</li><li>Tracklet: The dynamic buffer that stores the recent detections along with a unique track ID assigned to them, so that they can be tracked over the future frames of a video sequence</li></ol><p>Now, jumping into the algorithm:</p><p><strong>Step 1:</strong></p><p>The first step in the process is to get the detections from a chosen detector, for the paper, it was YOLOX. For each frame of the video sequence, the detector will give the locations of the objects in a frame and the probability/confidence that an object is present there. The detector is referred to as <em>Det()</em> in Figure 1. These detections are then transferred to two different bins: <strong>D</strong><em>high</em> and <strong>D</strong><em>low. </em>All detections with confidence above a threshold<em> t </em>are moved to <strong>D</strong><em>high </em>and all below the threshold to <strong>D</strong><em>low</em>. The same is shown from Line 3 to Line 13 of the algorithm from figure 1.</p><p><strong>Step 2:</strong></p><p>The next step is tracking. Here the task is to identify if any of these detections are the objects already detected in the previous frame. These tracked detections are stored in Tracklets. During the first frame, the Tracklets are empty, but it gets eventually populated as we progress with the frames.</p><p>At first, the existing Tracklets are used to predict the possible position of the objects(already present in Tracklet) in the current frame. For this purpose, Kalman Filter is used. Then the high confidence detections(<strong>D</strong><em>high</em>) are first compared with the Kalman Filter Predictions(t). If there is enough similarity then they are assumed to be objects of interest and are assigned a track ID and retained. Similarity is calculated using IuU and Re-ID</p><p>The remaining objects of the Tracklet are then compared with the low confidence detections(<strong>D</strong><em>low</em>). The detections with enough similarity are also retained.</p><p>All remaining detections are quashed. The Tracklet is updated with newly retained detections. These steps are represented by line 14 to line 26 of Figure 1.</p><p>In reality, detections are not immediately quashed if they are not found in the immediate next frames, as the objects might be occluded and reappear. However, for sake of simplicity, these details are not discussed in the algorithm from the original paper.</p><p>A key point to note here is that almost all conventional Detectors use a thresholding mechanism, which performs predictions on a frame and discards all detections below a certain threshold. Discarding low score predictions seems too naive as objects might really be present and just be partially occluded. I argue that using Tracklets to recover some detections seems like a much smarter and natural approach. Furthermore, since the algorithm seems to need just the detections from a separate detector and no end-to-end deep training is involved, it will be easy to incorporate the algorithm with other detectors without much hassle.</p><p>I highly recommend reading the original paper, they have given a much more elaborate and easy-to-understand explanation: <a href="https://arxiv.org/pdf/2110.06864.pdf">ByteTrack: Multi-Object Tracking by Associating Every Detection Box</a>.</p><p>Github Code:</p><p><a href="https://github.com/ifzhang/ByteTrack">https://github.com/ifzhang/ByteTrack</a></p><p>Colab Demo: <a href="https://colab.research.google.com/drive/1bDilg4cmXFa8HCKHbsZ_p16p0vrhLyu0?usp=sharing">https://colab.research.google.com/drive/1bDilg4cmXFa8HCKHbsZ_p16p0vrhLyu0?usp=sharing</a></p><p><em>Thanks for reading, hope you enjoyed the article.</em></p><p><em>Feel free to connect.</em></p><p><a href="https://www.linkedin.com/in/praveen-kumar-b2096391">Praveen Kumar - Radar Feature Developer - Veoneer | LinkedIn</a></p><figure><img alt="Bruges, Belgium" src="https://cdn-images-1.medium.com/max/1024/1*TFmT7v6C2VC59X_KjhB00A.jpeg" /><figcaption>Bruges, Belgium.</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=adce87deb282" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/bytetrack-because-deep-learning-is-not-the-only-solution-adce87deb282">ByteTrack …Because deep learning is not the ‘only’ solution</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[Double DQN on TurtleBot 2(ROS)]]></title>
            <link>https://medium.com/analytics-vidhya/double-dqn-on-turtlebot-2-ros-c8a143b61dc8?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8a143b61dc8</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[roses]]></category>
            <category><![CDATA[ddqn]]></category>
            <category><![CDATA[reinforced-learning]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Thu, 17 Feb 2022 14:30:03 GMT</pubDate>
            <atom:updated>2022-02-17T14:30:03.645Z</atom:updated>
            <content:encoded><![CDATA[<h3>Double DQN on TurtleBot 2(ROS)</h3><p>This post is a tutorial on how to set up and run a Double DQN(DDQN) algorithm on ROS based robot TurtleBot 2. By the end, you will have a working environment where you can train the TurtleBot 2 robot using the DDQN algorithm. You will be able to visualize the movements of the robot in a 3D simulation tool, Gazebo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*clmzgI28fJ04TGMO5g-7uQ.png" /><figcaption>Visualization of operation of the robot in the 3D simulation tool, Gazebo</figcaption></figure><p>This post assumes that you are familiar with ROS and ROS packages, and if this is not the case, I highly recommend going through them before starting off(helpful link: <a href="http://wiki.ros.org/catkin/Tutorials/create_a_workspace">ROS-Creating a workspace</a>). However, with basic knowledge of Linux, python and ROS package, you should still be able to follow this tutorial. I will be using my <a href="https://github.com/prav2909/DoubleDQN_turtlebot2">repository </a>for code scripts and I will not be discussing the scripts here as it would increase the length of this tutorial. In the case of doubts, you can get in touch with me over <a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Linkedin</a>.</p><p>Here are some important keywords:</p><ol><li>TurtleBot2: It is the robot that we will simulate and use for training (<a href="https://www.turtlebot.com/turtlebot2/">Link</a>).</li><li>Openai_ros package: ROS uses packages that contain libraries, configuration files, etc to run ROS projects. opeai_ros package is a package for ROS that helps us use OpenAI architecture based environments in our projects. If you do not know what OpenAI is, I strongly suggest you go through it <a href="https://towardsdatascience.com/reinforcement-learning-with-openai-d445c2c687d2">here</a> and <a href="http://wiki.ros.org/openai_ros">here</a></li><li>Gazebo: A 3D simulation tool for ROS robots, <a href="http://gazebosim.org/">Link</a>.</li></ol><p>First, you need Ubuntu 18.04, which can be downloaded from <a href="https://releases.ubuntu.com/18.04/">here</a>. You can do a dual boot with help of this <a href="https://www.youtube.com/watch?v=-iSAyiicyQY&amp;ab_channel=KskRoyal">link</a>.</p><p>The next step is to install Anaconda for Linux(Debian), which will be used to create a Conda environment for installing all the python packages needed for running the simulation. To install Anaconda follow these links: <a href="https://www.anaconda.com/products/individual">File</a>, <a href="https://docs.anaconda.com/anaconda/install/linux/">Installation</a>. Once installed, create a Conda environment, named env1, with python interpreter version 3.6. You will also need to install a bunch of libraries, the list of libraries can be found in the <a href="https://github.com/prav2909/DoubleDQN_turtlebot2/blob/main/README.md"><em>readme.md</em></a> of the <a href="https://github.com/prav2909/DoubleDQN_turtlebot2">repository</a>. You could also skip installing all these libraries for now and install them as the compiler asks(by throwing errors 😅) as you go ahead with setting up the rest of the things.</p><pre>conda create -n env1 python=3.6<br>conda activate env1</pre><p>Next, you need to install the Robot Operating System(ROS). You will need ROS Melodic for this tutorial. To install ROS follow this <a href="http://wiki.ros.org/melodic/Installation/Ubuntu">link</a>.</p><p>Now, you need to create a workspace where all ROS projects will be stored. It is called catkin workspace. Open terminal and type:</p><pre>source /opt/ros/melodic/setup.bash<br>mkdir -p ~/catkin_ws/src<br>cd ~/catkin_ws/<br>catkin_make<br>source devel/setup.bash</pre><p>To make sure your workspace is properly overlayed by the setup script, make sure <em>ROS_PACKAGE_PATH</em> environment variable includes the directory of your catkin_ws. In the same terminal, type :</p><pre>echo $ROS_PACKAGE_PATH</pre><p>You should see something like below, where <em>youruser </em>will be replaced by your username:</p><pre>/home/youruser/catkin_ws/src:/opt/ros/melodic/share</pre><p>Next, you need to set up another workspace, simulation_ws where you will have the <em>openai_ros </em>package and other TurtleBot 2 files. To do so navigate to your home directory and open the terminal and type:</p><pre>mkdir -p ~/simulation_ws/src<br>cd ~/simulation_ws/<br>catkin_make<br>source devel/setup.bash</pre><p>Again, to ensure environment variables have the path of this directory, type:</p><pre>echo $ROS_PACKAGE_PATH</pre><p>You should see something like this:</p><pre>/home/youruser/catkin_ws/src:/home/youruser/simulation_ws/src:/opt/ros/melodic/share</pre><p>Next, download the openai_ros package, by typing:</p><pre>cd ~/simulation_ws/src<br>git clone <a href="https://bitbucket.org/theconstructcore/openai_ros.git">https://bitbucket.org/theconstructcore/openai_ros.git</a><br>cd openai_ros<br>git checkout version2<br>cd ~/simulation_ws<br>catkin_make<br>source devel/setup.bash</pre><p>Next, you need to create a ROS package for the project. To do so, navigate to catkin_ws and create a package by the name fa_turtlebot. Type in terminal:</p><pre>cd ~/catkin_ws/src<br>catkin_create_pkg fa_turtlebot std_msgs rospy roscpp<br>cd ~/catkin_ws<br>catkin_make<br>source ~/catkin_ws/devel/setup.bash</pre><p>After this, you need to download files for training the robot. Inside fa_turtlebot, navigate to the src folder and clone files from <a href="https://github.com/prav2909/DoubleDQN_turtlebot2">this Github repository</a>. Make sure that the src folder is empty, or you will get an error. Also, add permissions to the script.</p><pre>cd ~/catkin_ws/src/fa_turtlebot/src<br>git clone <a href="https://github.com/prav2909/DoubleDQN_turtlebot2.git">https://github.com/prav2909/DoubleDQN_turtlebot2.git</a> .<br>chmod +x start_training.py</pre><p>The robot runs in a simulated world, this world is saved as a <em>.world</em> file. In the cloned repository files, you will see a <em>custom_world_3.world</em> file that you have to move from <em>~/catkin_ws/src/fa_turtlebot/src</em> folder to <em>/home/youruser/simulation_ws/src/turtlebot/turtlebot_gazebo/worlds</em>. You can do this manually or type the below command in the terminal:</p><pre>mv ~/catkin_ws/src/fa_turtlebot/src/custom_world_3.world ~/simulation_ws/src/turtlebot/turtlebot_gazebo/worlds</pre><p>Afterwards, this world file needs to be pointed to in the launch file. You will find a launch file, <em>start_world_maze_loop_brick.launch</em>, among the cloned files. You should move this file to TurtleBot folder <em>/home/youruser/simulation_ws/src/turtlebot/turtlebot_gazebo/launch</em>. You can do this manually, or by typing below commands in the terminal.</p><pre>mv ~/catkin_ws/src/fa_turtlebot/src/start_world_maze_loop_brick.launch ~/simulation_ws/src/turtlebot/turtlebot_gazebo/launch</pre><p>EDIT(28/07/2021):</p><p>You will also need to move <em>start_training.launch </em>file to the launch folder of your projet i.e. <em>/home/youruser/catkin_ws/src/fa_turtlebot/launch</em></p><p>EDIT(01/08/2021):</p><p>You will also need to move <em>fa_turtlebot.yaml </em>file to the launch folder of your projet i.e. <em>/home/youruser/catkin_ws/src/fa_turtlebot/config</em></p><p>Alright, all the setup steps are completed and you are ready to start the training of the robot. To do so, make sure conda environment <em>env1</em> is activated, which you created with python 3.6 and launch the ROS project. If you get any missing library errors, you can install them now. Most of the libraries can be installed via pip, but if a library is not available via pip then a simple google search should give you an idea about how to install them.</p><pre>conda activate env1<br>roslaunch fa_turtlebot start_training.launch</pre><p>You should see the terminal getting filled with warnings and messages as ROS is initiated. In a few seconds, you should see messages as the robot starts to train. It should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*u2-sWz1oTrOsOjoApjasGA.png" /><figcaption>Screenshot of how terminal will look like once training starts</figcaption></figure><p>You will also see a Gazebo window popup parallelly. It will show a robot roaming in the environment. By default, the script should load a pre-trained model, <em>saved_model.pt. </em>However, if that does not happen, then the script will create a neural network with fresh weights to run the robot. Again, I do not want to discuss the scripts here as it would drag this tutorial. In case a fresh model is created, you will see the robot performing badly in the beginning but it will start doing better after about 50–60 episodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MEeUjjZcBwIC_r2YV71jNA.png" /><figcaption>Bird’s eye view of the Simulation</figcaption></figure><p>That’s all, thank you for reading and if you liked the tutorial, give it a clap 👏. If you have any questions or you run into some trouble while setting up things yourself, post in the comments and I will try to help if I can.</p><p><strong>Let’s connect over Linkedin: </strong><a href="https://www.linkedin.com/in/praveen-kumar-b2096391/"><strong>Praveen Kumar</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8a143b61dc8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/double-dqn-on-turtlebot-2-ros-c8a143b61dc8">Double DQN on TurtleBot 2(ROS)</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[Generative Facial Prior GAN(GFP-GAN) Explained]]></title>
            <link>https://medium.com/codex/generative-facial-prior-gan-gfp-gan-explained-5d126bc7f383?source=rss-cbc912cbe452------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d126bc7f383</guid>
            <category><![CDATA[refine-image]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[blur-removal]]></category>
            <category><![CDATA[gans]]></category>
            <category><![CDATA[gfp-gan]]></category>
            <dc:creator><![CDATA[Praveen]]></dc:creator>
            <pubDate>Thu, 23 Dec 2021 14:17:45 GMT</pubDate>
            <atom:updated>2022-01-17T15:56:57.145Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nH1lhdLAddAJ39DULXDK9g.jpeg" /><figcaption>Stuttgart, germany</figcaption></figure><p>GFP-GAN generates high-resolution images by removing noise and degradation in the input image. Here I have explained the key architectural points of the model, along with losses. At the end, you will find some sample input/output results of the model and also a few “<strong>Try yourself</strong>” links that let you upload your own picture and download results via a simple user interface.</p><p><strong>Contents:</strong></p><ol><li><strong>Architecture</strong></li><li><strong>Losses</strong></li><li><strong>Results</strong></li><li><strong>Try Yourself</strong></li><li><strong>References</strong></li></ol><p><strong>1. Architecture</strong></p><p>The overall framework of GFP-GAN is depicted in Figure 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XWV75PnSNTmmWK4sdmyNFw.png" /><figcaption>Figure 1: overall framework of GFP-GAN</figcaption></figure><p>The architecture of GFP-GAN consists of three major parts:</p><p><strong>1.1. Degradation Removal Module</strong></p><p>The <strong>Degradation Removal Module</strong> is<strong> </strong>highlighted with the red box below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rbRM2F_cuZrVcaqXGPmlLA.png" /><figcaption>Figure 2: Degradation Removal Module</figcaption></figure><p>This part of the network removes the noise, blur, and other low-resolution artifacts collectively called as removal of degradation. It provides input to the next section of the overall framework i.e. StyleGAN(section 2). It uses the architecture of <a href="https://paperswithcode.com/method/u-net">U-Net</a> with a latent space in the middle. This latent space is called <strong>Fₗₐₜₑₙₜ </strong>and has the most significant features of the input image in a compressed one-dimensional vector.</p><p>Another output of this section is <strong>Fₛₚₐₜᵢₐₗ</strong>. It is the output of each convolutional layer of the expansive path (right side) of the degradation removal network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*7mGXLEh0BqiDbeJpMXhY3A.png" /><figcaption>Equation 1</figcaption></figure><p><strong>1.2. Generative Facial Prior and Latent Code Mapping</strong></p><p>The next part of the architecture is a pre-trained face GAN, in this case, <a href="https://arxiv.org/abs/1912.04958">StyleGAN</a>, I have also written a blog to give a basic understanding of StyleGAN <a href="https://medium.com/analytics-vidhya/a-short-introduction-of-stylegan-898fe781937">here</a>. The highlighted red boxes show this part in the official architecture representation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qEz3qvRt6Mq46P4hOCKO8g.png" /><figcaption>Figure 3: Pre trained face GAN(StyleGAN)</figcaption></figure><p>A pre-trained GAN (in this case StyleGAN) helps capture the distribution over facial images, and this is what the authors call generative prior.</p><p>First, <strong>Fₗₐₜₑₙₜ</strong> from the degradation removal network is passed through a series of linear layers called Multi-Layer Perception to preserve semantic property and the result is referred to as intermediate latent codes <strong><em>W.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*UCOaNV_2r1WPX945nU7VpQ.png" /><figcaption>Equation 2</figcaption></figure><p>These intermediate latent codes <strong><em>W </em></strong>are later passed through each layer of the pre-trained GAN to get GAN features at different resolution scales, called <strong><em>F-GAN</em></strong>.</p><p><strong>1.3. Channel-Split Spatial Feature Transform</strong></p><p>This part of the architecture helps preserve the spatial information from input and adaptive restoration at different spatial locations of a face. The highlighted red boxes show this part in the official model framework.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GeT6d6qQIgB4oaXyNmuOKw.png" /><figcaption>Figure 4: Channel-Split Spatial Feature Transform</figcaption></figure><p>Spatial Feature Transform(SFT) is the essence of this section. So let’s get familiar with it.</p><blockquote>Say, we have a feed-forward network that gives high-resolution(HR) output image y-hat based on some low-resolution(LR) input image x, with y being the true HR image.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/109/1*hofXImbWvufhK2zFItBPcA.png" /><figcaption>Equation 3</figcaption></figure><blockquote>With the loss function being:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*i5OioUma8Uf7hAg4g3Cy4g.png" /><figcaption>Equation 4</figcaption></figure><blockquote>The semantic segmentation categorical prior Ψ( i.e., knowing which region belongs to the sky, water, or grass) can be conveniently represented by semantic segmentation probability maps P.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*QLmPItOC0SbfpfjNdj7ueQ.png" /><figcaption>Equation 5</figcaption></figure><blockquote>where Pₖ is the probability map of the <strong>kᵗʰ</strong> category and K is the total number of considered categories. This priori can be introduced as:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/143/1*003JqpJNrRJw6nTTSe4I9Q.png" /><figcaption>Equation 6</figcaption></figure><blockquote>where Ψ defines the prior upon which the mapping function can condition.</blockquote><blockquote>An SFT layer learns a mapping function M that outputs a modulation parameter pair (γ, β) based on some prior condition Ψ. The learned parameter pair adaptively influences the outputs by applying an affine transformation spatially to each intermediate feature maps in an SR network. Consequently,</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*RCt37IxuBNBKnmZo6Z-zmA.png" /><figcaption>Equation 7</figcaption></figure><blockquote>After obtaining (γ, β) from conditions, the transformation is carried out by scaling and shifting feature maps of a specific layer:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*rZdrOqtLZ9NB8KSlKzK5Uw.png" /><figcaption>Equation 8</figcaption></figure><blockquote>where F denotes the feature maps.</blockquote><blockquote><strong>If nothing of the above made sense, just remember the above equation 8. This is what we apply to the feature maps of each layer and the parameters learned from training are γ and β.</strong></blockquote><p>Now, coming back to GFP-GAN, the learned parameters for this section are α and β, similar to (γ, β) of SFT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/204/1*pvZL7BhhwuWOVgb8eFH8Xg.png" /><figcaption>Equation 9</figcaption></figure><p>Now, instead of using the classic SFT, authors use a variation of SFT, called Channel-Split SFT(CS-SFT).</p><p>CS-SFT splits the F-GAN into two features and then applies the affine transformation(equation 8) on the part responsible for fidelity and lets the other part, responsible for realness, pass as it is. The two parts are concatenated after the operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qySszzE7mvrAr4-muUzSLQ.png" /><figcaption>Equation 10</figcaption></figure><p>CS-SFT helps acheive a balance between texture faithfulness and fidelity. CS-SFT is applied at each resolution scale to finally generate a restored image y-hat.</p><p>2<strong>. Losses</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*awntWP81_KZKWZ5qCY4RVQ.png" /><figcaption>Figure 5: Overall Losses</figcaption></figure><p>There are 4 types of losses used in the training:</p><p><strong>2.1. Reconstruction Loss</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EvemeaXd0_SEHPmxGeDLsw.png" /><figcaption>Figure 6: Reconstruction Loss</figcaption></figure><p>This is a combination of L1 loss and <a href="https://deepai.org/machine-learning-glossary-and-terms/perceptual-loss-function">perceptual loss</a>. This loss helps keep the generated image(y) similar to ground truth image(y-hat).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*ZzCHRz-x6w3a0Ob_bo-M2A.png" /><figcaption>Equation 11: Reconstruction Loss</figcaption></figure><p>where φ is the pretrained <a href="https://arxiv.org/abs/1409.1556">VGG-19 network</a>. <strong>λₗ₁</strong> and <strong>λₚₑᵣ</strong> denote the loss weights of L1 and perceptual loss, respectively.</p><p><strong>2.2. Adversarial Loss</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*klrHSYXUURnjkVweo9PL3Q.png" /><figcaption>Figure 7: Adversarial Loss</figcaption></figure><p>This is similar to the logistic loss used in StyleGAN2. It helps promote realistic texture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*Bos6f7jm9x-xsv9FbjRisg.png" /><figcaption>Equation 12: Adversarial Loss</figcaption></figure><p>where <strong>D</strong> denotes the discriminator, <strong>λₐ𝒹ᵥ</strong> represents the adversarial loss weight, and E expected value.</p><p><strong>2.3. Facial Component Loss</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uiy36K-jnDirVwNuyFd6Hg.png" /><figcaption>Figure 8: Facial Component Loss</figcaption></figure><p>This loss helps enhance the clarity of significant facial components like eyes and mouth. It uses local discriminators for left eye, right eye and mouth.</p><p>The facial component loss is defined as follows. The first term is the discriminative loss [18] and the second term is the feature style loss.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*dM-TQd6ub35heMGE_v7lxQ.png" /><figcaption>Equation 13: Facial Component Loss</figcaption></figure><p>where<strong> ROI</strong> is region of interest from the component collection {left eye, right eye, mouth}. <strong>Dᵣₒᵢ</strong> is the local discriminator for each region. <strong>ψ</strong> denotes the multi-resolution features from the learned discriminators. <strong>λₗₒ𝒸ₐₗ</strong> and <strong>λ𝒻ₛ</strong> represent the loss weights of local <a href="https://arxiv.org/abs/1406.2661">discriminative loss</a> and feature style loss, respectively.</p><p><strong>2.4. Identity Preserving Loss</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*784hSnUAUQnIjbkzcBLOGw.png" /><figcaption>Figure 9: Identity Preserving Loss</figcaption></figure><p>The identity preserving loss enforces the restored result to have a small distance with the ground truth in the compact deep feature space:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/279/1*Tz5VBeazuQt3hkKICHQH6w.png" /><figcaption>Equation 14: Identity Preserving Loss</figcaption></figure><p>where η represents face feature extractor[<a href="https://arxiv.org/abs/1801.07698">Ref</a>]. λi𝒹 denotes the weight of identity preserving loss.</p><p><strong>2.5. Total Loss</strong></p><p>The overall loss will be a combination of above four losses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*2FTldFuG-1iO-gvuZqjJlw.png" /><figcaption>Equation 15: Total Loss</figcaption></figure><p><strong>3. Results</strong></p><p>Results speak for themselves, below are some sample ouputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h1HujzDXB8rSQdgTWeC04A.png" /><figcaption>Figure 10: Results</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iKORQ5BUIzt8FVqiKC9V4g.png" /><figcaption>Figure 11: Results</figcaption></figure><p><strong>4. Try Yourself</strong></p><p>These links give a simple user interface to upload your own image and see results yourself! However, sometimes they don’t work, perhaps because of overload.</p><ul><li><a href="https://refineryai.com/">Refine!</a></li><li><a href="https://huggingface.co/spaces/akhaliq/GFPGAN">GFPGAN - a Hugging Face Space by akhaliq</a></li><li><a href="https://replicate.com/xinntao/gfpgan">xinntao/gfpgan - Replicate</a></li></ul><p><a href="https://colab.research.google.com/drive/1sVsoBd9AjckIXThgtZhGrHRfFI6UUYOo">https://colab.research.google.com/drive/1sVsoBd9AjckIXThgtZhGrHRfFI6UUYOo</a></p><p><strong>5. References</strong></p><p>Original Paper:</p><p><a href="https://arxiv.org/abs/2101.04061">Towards Real-World Blind Face Restoration with Generative Facial Prior</a></p><p>Github code:</p><p><a href="https://github.com/TencentARC/GFPGAN">https://github.com/TencentARC/GFPGAN</a></p><p><strong><em>Hope you enjoyed the article!</em></strong></p><p><strong><em>Feel free to connect:</em></strong></p><p><a href="https://www.linkedin.com/in/praveen-kumar-b2096391/">Praveen Kumar - Radar Feature Developer - Veoneer | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d126bc7f383" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/generative-facial-prior-gan-gfp-gan-explained-5d126bc7f383">Generative Facial Prior GAN(GFP-GAN) Explained</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>