<?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 Midhun G Raj on Medium]]></title>
        <description><![CDATA[Stories by Midhun G Raj on Medium]]></description>
        <link>https://medium.com/@midhungraj?source=rss-a0f37c3dd0e9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Loq4zuEjca3Ayj2K4TOWvQ.png</url>
            <title>Stories by Midhun G Raj on Medium</title>
            <link>https://medium.com/@midhungraj?source=rss-a0f37c3dd0e9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 09:29:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@midhungraj/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[Multi-Head Attention, Explained Step by Step]]></title>
            <link>https://midhungraj.medium.com/multi-head-attention-explained-step-by-step-1b5b1302104c?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b5b1302104c</guid>
            <category><![CDATA[attention-is-all-you-need]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[attension]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Thu, 22 Oct 2026 07:24:37 GMT</pubDate>
            <atom:updated>2026-10-22T07:25:19.156Z</atom:updated>
            <content:encoded><![CDATA[<p>What Q, K, V, heads, split() and concat() actually do, with tensor shapes you can follow</p><p>Multi-Head Attention (MHA) is the core of every Transformer, and it’s also where many people get lost. The formula looks short, but the tensor reshaping in the code hides what’s going on.</p><p>This post goes through it in order: what attention is, what Q, K and V are, how a head works, and what happens to the shapes at every step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*OLrvSAFOuMdEUUFD_blGuA.png" /></figure><h3>1. What is attention?</h3><p>Attention lets a token gather relevant information from the other tokens in a sequence.</p><p>Take this sentence:</p><blockquote><em>The animal didn’t cross the street because it was tired.</em></blockquote><p>To work out what <em>it</em> refers to, the model has to pull in information from other tokens. Attention calculates how much each token should use information from every other token.</p><h3>2. Q, K and V</h3><p>Every token’s representation gets turned into three different vectors:</p><ul><li><strong>Query (Q):</strong> used to decide which tokens to pay attention to.</li><li><strong>Key (K):</strong> compared against the queries to produce attention scores.</li><li><strong>Value (V):</strong> the information that actually gets combined using those scores.</li></ul><p>They come from learned linear projections of the input X:</p><pre>Q = W_q(X)<br>K = W_k(X)<br>V = W_v(X)</pre><p>Each projection is separate because each has a different job.</p><blockquote><em>Q, K and V are mathematical representations. They are not literal English questions, labels or values.</em></blockquote><h3>3. What are “features”?</h3><p>Features are the numbers that represent a token. A vector like this has 8 features:</p><pre>A = [1, 2, 4, 7, 8, 9, 0, 1]</pre><p>If d_model = 8, every token is represented by 8 features. The model learns these numbers during training, so don’t assume any single feature has a human-readable meaning.</p><p>A typical input tensor looks like this:</p><pre>x.shape = (2, 3, 8)</pre><ul><li>2 is the batch size (two sequences)</li><li>3 is the sequence length (three tokens each)</li><li>8 is the number of features per token</li></ul><h3>4. Scaled dot-product attention</h3><p>This is the core calculation. Formula (written in plain text, since Medium doesn’t render LaTeX):</p><pre>Attention(Q, K, V) = softmax( Q · Kᵀ / √d_k ) · V</pre><p>It has four steps.</p><p><strong>Step 1: similarity scores.</strong> Compare every query with every key:</p><pre>scores = Q @ K.transpose(-2, -1)</pre><p><strong>Step 2: scale.</strong> Divide by √d_k to keep the scores in a reasonable range before softmax, especially when the dimension is large:</p><pre>scores = scores / math.sqrt(d_k)</pre><p><strong>Step 3: attention weights.</strong> Softmax turns scores into weights that sum to 1 across the key positions:</p><pre>weights = torch.softmax(scores, dim=-1)</pre><p><strong>Step 4: combine the values.</strong> Each output is a weighted sum of Value vectors:</p><pre>output = weights @ V</pre><p>To sum up: Q and K decide <em>where to look</em>, and V supplies <em>what gets collected</em>. Attention alone doesn’t predict the next token. A full language model adds more layers and an output projection to get vocabulary logits.</p><h3>5. What is a “head”?</h3><p>One head is one scaled dot-product attention calculation. Multi-Head Attention runs several of them side by side, each with its own learned projections.</p><p>A head is <strong>not</strong> a separate Transformer layer. It’s one attention mechanism inside the MHA module.</p><p>Because each head has its own projections, it can learn a different attention pattern. One might link a pronoun to an earlier noun while another picks up something else. These roles are learned, not guaranteed.</p><h3>6. Why split Q, K and V?</h3><p>Say d_model = 8 and n_head = 2. Each head works on:</p><pre>d_tensor = d_model // n_head   # 8 // 2 = 4</pre><p>So each head sees 4 features per token.</p><p><strong>Why not just use all 8 features in one head?</strong> You can. That’s single-head attention. The point of multiple heads isn’t that the maths forces a split. It’s that the model gets several independent attention patterns, which it then combines.</p><p>Splitting on its own adds no information. The benefit comes from the separate learned projections and the separate attention calculations.</p><p>A small note on //: it’s integer division (8 // 2 gives 4, while 8 / 2 gives 4.0). Tensor dimensions must be integers, and d_model must be divisible by n_head.</p><h3>7. What split() does to the shapes</h3><p>Start with these settings:</p><pre>batch_size = 2<br>length = 3<br>d_model = 8<br>n_head = 2</pre><p>After the learned projections, q, k and v each have shape (2, 3, 8), which means (batch, tokens, features).</p><p><strong>Step 1: reshape with </strong><strong>view().</strong></p><pre>tensor = tensor.view(batch_size, length, n_head, d_tensor)<br># (2, 3, 8) → (2, 3, 2, 4)</pre><p>The dimensions are now (batch, tokens, heads, features_per_head).</p><p><strong>Step 2: swap with </strong><strong>transpose().</strong></p><pre>tensor = tensor.transpose(1, 2)<br># (2, 3, 2, 4) → (2, 2, 3, 4)</pre><p>The dimensions are now (batch, heads, tokens, features_per_head).</p><p>This layout lets attention run separately for each head.</p><p>One thing to be clear about: view() and transpose() only rearrange the tensor. They don’t compute attention and they don’t learn anything. The learning happens in the projections <em>before</em> this point.</p><h3>8. How the heads run in parallel</h3><p>With q, k and v shaped (2, 2, 3, 4), a single batched operation covers every head:</p><pre>scores = q @ k.transpose(-2, -1)<br># scores.shape = (2, 2, 3, 3)</pre><p>Each head gets its own 3×3 score matrix for each sequence. The heads live together in one tensor, so the framework processes them with optimized batched operations instead of separate Python calls. Exactly how that runs on hardware depends on the implementation.</p><h3>9. Why concatenate the heads?</h3><p>Each head produces its own context-aware output:</p><pre>head_1 = [0.2, 0.4, 0.1, 0.7]<br>head_2 = [0.8, 0.3, 0.9, 0.1]</pre><p>Concatenating them restores the full feature size:</p><pre>combined = [0.2, 0.4, 0.1, 0.7, 0.8, 0.3, 0.9, 0.1]   # 8 features again</pre><p>We concatenate so that information from every head is kept. A learned output projection (w_concat) then mixes it together:</p><pre>out = self.concat(out)<br>out = self.w_concat(out)</pre><p>Concatenation doesn’t undo anything. Each head has already combined information using its own attention weights.</p><h3>10. The full forward pass</h3><p>Here’s the whole thing in code:</p><pre>def forward(self, q, k, v, mask=None):<br>    # 1. Learn separate Q, K and V representations<br>    q, k, v = self.w_q(q), self.w_k(k), self.w_v(v)<br>    # 2. Organize the projected features into heads<br>    q, k, v = self.split(q), self.split(k), self.split(v)<br>    # 3. Calculate attention for all heads<br>    out, attention = self.attention(q, k, v, mask=mask)<br>    # 4. Join the head outputs<br>    out = self.concat(out)<br>    # 5. Mix the combined information<br>    out = self.w_concat(out)<br>    return out</pre><p>The flow in one line:</p><p><strong>Input → Q/K/V projections → split into heads → attention → concatenate → output projection</strong></p><h3>11. The formulas</h3><p>Features per head, where h is the number of heads:</p><pre>d_head = d_model / h</pre><p>Multi-Head Attention:</p><pre>head_i = Attention(Q·W_i^Q, K·W_i^K, V·W_i^V)<br><br>MultiHead(Q, K, V) = Concat(head_1, ..., head_h) · W^O</pre><h3>Key takeaways</h3><ul><li><strong>Features</strong> are the numbers that represent a token.</li><li><strong>Q/K/V</strong> are learned representations used to compute attention and retrieve information.</li><li><strong>One head</strong> is one attention calculation, and n_head is how many you run.</li><li><strong>d_model // n_head</strong> is the number of features per head here.</li><li><strong>split()</strong> reorganizes the projected features into heads.</li><li><strong>concat()</strong> joins the head outputs, and <strong>w_concat</strong> learns how to mix them.</li></ul><p>The idea comes from <em>Attention Is All You Need</em> (2017): multiple heads let the model attend to information from different representation subspaces at different positions.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b5b1302104c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Word2Vec From Scratch: From Context Words to Backpropagation]]></title>
            <link>https://midhungraj.medium.com/understanding-word2vec-from-scratch-from-context-words-to-backpropagation-4ff55b9d7a94?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ff55b9d7a94</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[word2vec]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Fri, 28 Aug 2026 16:50:08 GMT</pubDate>
            <atom:updated>2026-08-28T16:52:49.312Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*UIRDEHSwC3XMMBdEBjb1xA.png" /><figcaption>Image credit: Aditi Sikarwar</figcaption></figure><p>When I first heard about Word2Vec, I understood the basic idea: computers cannot directly understand words, so we convert words into numbers called vectors.</p><p>But that immediately raised more questions:</p><ul><li>How does a word become a vector?</li><li>Why are similar words represented by similar vectors?</li><li>What does <em>king − man + woman ≈ queen</em> actually mean?</li><li>How does Word2Vec learn these relationships?</li><li>What happens during forward propagation and backpropagation?</li></ul><p>In this article, I’ll walk through what I learned by building a small version of Word2Vec from scratch using Python and NumPy.</p><h3>What Is Word2Vec?</h3><p>Word2Vec is a technique for learning <strong>word embeddings</strong> — numerical vectors that represent words.</p><p>For example:</p><pre>king  → [0.21, -0.83, 0.45, ..., 0.12]<br>queen → [0.18, -0.79, 0.51, ..., 0.10]<br>car   → [-0.62, 0.34, -0.91, ..., 0.45]</pre><p>Initially, these numbers are random and meaningless. The important part is that Word2Vec <em>learns</em> these vectors from text.</p><blockquote><em>Words that appear in similar contexts tend to receive similar vector representations.</em></blockquote><p>For example:</p><pre>The cat sleeps on the bed.<br>The dog sleeps on the bed.</pre><p>cat and dog appear in similar contexts, so the model receives similar learning signals for both, which eventually pulls their embeddings closer together:</p><pre>Similar contexts → similar prediction tasks → similar gradient updates → similar embeddings</pre><h3>Two Main Word2Vec Architectures</h3><p>Word2Vec has two popular training approaches:</p><p><strong>1. CBOW (Continuous Bag of Words)</strong> — uses surrounding words to predict a target word.</p><pre>The cat ___ on the mat<br>context: The, cat, on, the, mat  →  target: sits</pre><p><strong>2. Skip-gram</strong> — does the opposite: takes a word and predicts the words around it.</p><pre>The king is a man<br>input: king  →  predictions: the, is, a</pre><p>This article focuses on <strong>Skip-gram</strong>.</p><h3>Creating Training Pairs</h3><p>Suppose our text is:</p><pre>the king is a man the queen is a woman</pre><p>First, split it into tokens:</p><pre>tokens = text.lower().split()</pre><pre>[&#39;the&#39;, &#39;king&#39;, &#39;is&#39;, &#39;a&#39;, &#39;man&#39;,<br> &#39;the&#39;, &#39;queen&#39;, &#39;is&#39;, &#39;a&#39;, &#39;woman&#39;]</pre><p>Now choose a context window — how many nearby words to pair with each target:</p><pre>window_size = 2</pre><p>For every target word, we create pairs with nearby words:</p><pre>king → the<br>king → is<br>king → a</pre><pre>pairs = []</pre><pre>for i, target_word in enumerate(tokens):<br>    start = max(0, i - window_size)<br>    end = min(len(tokens), i + window_size + 1)</pre><pre>    for j in range(start, end):<br>        if i != j:<br>            pairs.append((target_word, tokens[j]))</pre><p>if i != j just prevents a word from being paired with itself.</p><h3>Creating a Vocabulary</h3><p>Neural networks work with numbers, so each unique word needs an ID:</p><pre>a → 0    is → 1    king → 2    man → 3<br>queen → 4    the → 5    woman → 6</pre><pre>vocab = sorted(set(tokens))<br>word_to_id = {word: i for i, word in enumerate(vocab)}<br>id_to_word = {i: word for word, i in word_to_id.items()}</pre><p>Now king can be represented internally as 2.</p><h3>One-Hot Encoding</h3><p>We represent a word ID as a one-hot vector — a vector the length of the vocabulary, with a single 1 at the word&#39;s position and 0 everywhere else.</p><p>If the vocabulary has 7 words and king is ID 2:</p><pre>king → [0, 0, 1, 0, 0, 0, 0]</pre><p>In general, if the vocabulary size is <strong>V</strong>, the one-hot vector <strong>x</strong> has V entries — all zero except one.</p><h3>The Word2Vec Model</h3><p>Our small Skip-gram model has two weight matrices. Let:</p><ul><li><strong>V</strong> = vocabulary size</li><li><strong>D</strong> = embedding dimension</li><li>W1 → shape (V × D) — input-to-hidden weights W2 → shape (D × V) — hidden-to-output weights</li></ul><p>Example: vocab size 7, embedding size 10 → W1 is 7×10, W2 is 10×7.</p><p>The architecture, end to end:</p><pre>Input word → One-hot vector → W1 → Embedding → W2 → Scores → Softmax → Probability for every word</pre><h3>Forward Propagation</h3><p>Let’s trace one training pair: <strong>king → is</strong>. The goal is to give a high probability to <em>is</em> given <em>king</em>.</p><p><strong>Step 1 — Get the embedding</strong></p><p>Because the input is a one-hot vector, multiplying it by W1 just selects one row of W1 — the row for king:</p><pre>h = one-hot(king) × W1  =  W1[king&#39;s row]</pre><pre>embedding = W1[input_id]</pre><p>This vector <strong>h</strong> is the learned embedding.</p><p><strong>Step 2 — Calculate scores</strong></p><pre>z = h × W2</pre><p>This produces one raw score (“logit”) per vocabulary word:</p><pre>a → 0.1    is → 0.4    king → -0.2<br>man → 0.3    queen → -0.1    the → 0.5    woman → 0.2</pre><p><strong>Step 3 — Softmax</strong></p><p>Softmax turns raw scores into probabilities that sum to 1:</p><pre>p(i) = e^z(i) / Σ e^z(j)   — summed over every word j in the vocabulary</pre><pre>a → 0.10   is → 0.15   king → 0.08   man → 0.12<br>queen → 0.09   the → 0.28   woman → 0.18</pre><p>Right now P(is | king) = 0.15. Since the correct target is <em>is</em>, training should push that number toward 1.</p><h3>Calculating the Loss</h3><p>The target word as a one-hot vector: y = [0,1,0,0,0,0,0].</p><p>We use <strong>cross-entropy loss</strong>:</p><pre>L = −Σ y(i) · log(p(i))</pre><p>Since only the correct word has y = 1, this collapses to:</p><pre>L = −log(p_target)</pre><p>With p_target = 0.15: <strong>L ≈ −log(0.15) ≈ 1.897</strong></p><p>If instead the model had predicted P(is|king) = 0.9: <strong>L ≈ −log(0.9) ≈ 0.105</strong> — a much lower loss.</p><pre>Low probability for correct word → high loss → larger correction needed</pre><h3>Backpropagation</h3><p>This is where the model learns. The forward pass, as a chain:</p><pre>x → h → z → p → L</pre><p>Specifically:</p><pre>h = xᵀ × W1<br>z = h × W2<br>p = softmax(z)<br>L = −Σ y·log(p)</pre><p>Backprop walks this chain backward, computing how L changes with respect to every parameter — ultimately ∂L/∂W1 and ∂L/∂W2.</p><p><strong>Softmax + cross-entropy gradient</strong></p><p>One of the cleanest results in deep learning:</p><pre>∂L/∂z = p − y</pre><pre>error = probabilities - target</pre><p>With p = [0.10, 0.15, 0.08, 0.12, 0.09, 0.28, 0.18] and y = [0,1,0,0,0,0,0]:</p><pre>p − y = [0.10, -0.85, 0.08, 0.12, 0.09, 0.28, 0.18]</pre><p>For the correct target (<em>is</em>): 0.15 − 1 = <strong>−0.85</strong> — a negative error that pushes the model to <em>increase</em> the score for <em>is</em>. For every incorrect word, the error is just p(i) itself, pushing that score <em>down</em>.</p><p><strong>Gradient for W2</strong></p><p>Since z = h × W2:</p><pre>∂L/∂W2 = hᵀ × (p − y)</pre><pre>dW2 = np.outer(embedding, error)</pre><p>Interpretation: <strong>weight gradient = input activation × error signal</strong>.</p><p><strong>Gradient for the embedding</strong></p><p>The error also has to flow back into the embedding itself:</p><pre>∂L/∂h = W2 × (p − y)</pre><pre>d_embedding = W2 @ error</pre><p>This tells us how the embedding for king should change to reduce the loss.</p><p><strong>Gradient for W1</strong></p><p>Since h = xᵀ × W1, and x is one-hot:</p><pre>∂L/∂W1 = x × (∂L/∂h)ᵀ</pre><p>Only the row of W1 for the input word gets updated:</p><pre>a → no update     is → no update     king → UPDATE<br>man → no update   queen → no update  the → no update   woman → no update</pre><h3>Gradient Descent</h3><p>Once we have the gradients, we update the weights:</p><pre>W1 ← W1 − η · ∂L/∂W1<br>W2 ← W2 − η · ∂L/∂W2</pre><p>where η (eta) is the learning rate.</p><pre>W1 -= learning_rate * dW1<br>W2 -= learning_rate * dW2</pre><p>This nudges the model to be slightly better at predicting <em>king → is</em>. Repeat across every training pair, for many epochs.</p><h3>The Complete Learning Process</h3><p>For one example, king → is:</p><pre>FORWARD PASS<br>king → one-hot x → embedding h = xᵀW1 → scores z = hW2<br>→ softmax p = softmax(z) → loss L = −log(p_target)</pre><pre>BACKWARD PASS<br>∂L/∂z = p − y<br>∂L/∂W2 = hᵀ(p − y)<br>∂L/∂h = W2(p − y)<br>∂L/∂W1 = x(∂L/∂h)ᵀ<br>→ gradient descent → update W1 and W2</pre><p>Then it repeats for the next pairs: king → the, king → a, queen → is, queen → the... After enough examples:</p><pre>Text → context patterns → prediction errors → backpropagation → weight updates → better embeddings</pre><h3>What Does King − Man + Woman ≈ Queen Mean?</h3><pre>embedding(king) − embedding(man) + embedding(woman) ≈ embedding(queen)</pre><p>This doesn’t mean Word2Vec understands arithmetic with words. It means the model has learned certain relationships as <em>directions</em> in embedding space.</p><p>The vector woman − man represents an approximate direction: male → female. If a similar relationship holds between king and queen:</p><pre>queen − king ≈ woman − man</pre><p>Then:</p><pre>king + (woman − man) ≈ queen</pre><p>which rearranges to:</p><p><strong>king − man + woman ≈ queen</strong></p><p>The resulting vector isn’t magically the word “queen” — the model computes a new vector, then searches the vocabulary for the closest existing embedding:</p><pre>king − man + woman → new vector → compare against vocabulary → closest match → queen</pre><h3>Final Thoughts</h3><p>Building this small version of Word2Vec made one thing click: word embeddings aren’t hand-assigned meanings. The model starts with random numbers, and then, over many training examples, it repeatedly:</p><ol><li>Looks at a word and its context</li><li>Makes a prediction</li><li>Measures how wrong the prediction is</li><li>Backpropagates to compute gradients</li><li>Updates its weights via gradient descent</li><li>Repeats a lot</li></ol><p>Eventually, patterns in language get encoded into the vectors. Nobody tells the model <em>“cat is similar to dog”</em> or <em>“king is related to queen”</em> — those relationships emerge purely from statistical patterns in the training data.</p><pre>Text → word-context relationships → prediction task → loss → backpropagation<br>→ gradient updates → learned embeddings → semantic relationships</pre><p>That’s the core idea behind Word2Vec: learning useful representations of language from the contexts words appear in.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ff55b9d7a94" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Experience Switching to CachyOS on an MSI GF65 (GTX 1650 Ti)]]></title>
            <link>https://midhungraj.medium.com/my-experience-switching-to-cachyos-on-an-msi-gf65-gtx-1650-ti-caf9603e0a17?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/caf9603e0a17</guid>
            <category><![CDATA[msi-laptop]]></category>
            <category><![CDATA[cachyos]]></category>
            <category><![CDATA[perfomance]]></category>
            <category><![CDATA[arch-linux]]></category>
            <category><![CDATA[linux-mint]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Sun, 28 Dec 2025 17:28:10 GMT</pubDate>
            <atom:updated>2025-12-28T17:28:32.220Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BskCWGVmmsIoBjaEqUiPYw.png" /></figure><p>I recently switched to <strong>CachyOS</strong> on my <strong>MSI GF65 laptop with a GTX 1650 Ti</strong>, and this is my honest experience so far. This is also my <strong>first time using an Arch-based Linux distribution</strong>.</p><p>Before CachyOS, I was using <strong>Linux Mint Cinnamon</strong>, and before Linux, I was on Windows. I mainly use my laptop for <strong>coding, multitasking, and general productivity</strong>. I haven’t tried gaming yet, so this is not a gaming-focused review.</p><h3>Why I Tried CachyOS</h3><p>Linux Mint is stable and beginner-friendly, but over time it started to feel limiting. I wanted more control over my system, faster updates, and better support for hardware-specific tools — especially for my MSI laptop.</p><p>CachyOS stood out because it’s Arch-based but still approachable. It promises performance-focused optimizations while avoiding the painful manual setup that pure Arch is known for.</p><h3>Installation and First Impressions</h3><p>Since this was my first Arch-based distro, I expected the installation to be difficult. Surprisingly, it wasn’t.</p><p>The installer was straightforward, and I chose <strong>Btrfs with Snapper enabled</strong>. At the time, it felt like a small decision. Later, it turned out to be one of the best choices I made.</p><p>The system booted cleanly, detected my hardware properly, and felt responsive right from the start.</p><h3>Updates, Breakage, and Recovery</h3><p>Being a rolling-release system, I did run into <strong>package-related issues</strong> during updates. This wasn’t shocking — Arch-based systems are known for this, especially for new users.</p><p>What mattered was how easy it was to recover.</p><p>Thanks to <strong>Btrfs snapshots and Snapper</strong>, I could roll back to a previous working state, fix the issue calmly, and then update again properly. Instead of panicking, I felt in control.</p><p>This single feature made Arch Linux feel far less risky than I expected.</p><h3>Daily Use: Coding and Multitasking</h3><p>In everyday use, CachyOS feels noticeably more responsive than both Windows and Linux Mint Cinnamon. Applications launch faster, the system stays smooth while multitasking, and overall it feels lighter and more efficient. There’s a clear sense that the OS isn’t getting in the way of my work, which makes long coding sessions more comfortable and productive.</p><h3>MSI Laptop Support: A Big Improvement</h3><p>This was a major pain point on Linux Mint.</p><p>On Mint, installing and maintaining <strong>msi-ec</strong> and <strong>MControlCenter</strong> was difficult. It involved manual steps, outdated packages, and frequent issues.</p><p>On <strong>CachyOS (and Arch in general)</strong>, this experience is much better:</p><ul><li>MSI tools are easily available through the <strong>AUR</strong></li><li>Package managers like <strong>paru</strong> or <strong>yay</strong> make installation simple</li><li>Updates are faster and more reliable</li></ul><p>For MSI laptop users, this alone can justify switching.</p><h3>Living With Arch for the First Time</h3><p>CachyOS isn’t a “set it and forget it” system like Linux Mint, but it also doesn’t feel fragile.</p><p>You do need to:</p><ul><li>Pay attention to update messages</li><li>Read warnings when they appear</li><li>Learn a bit about how the system works</li></ul><p>In return, you get flexibility, control, and a system that actually feels like it belongs to you.</p><h3>Final Thoughts</h3><p>Switching to CachyOS has been a positive experience for me.</p><p>On my <strong>MSI GF65 with GTX 1650 Ti</strong>, it offers:</p><ul><li>Better everyday responsiveness</li><li>Easier setup for MSI-specific tools</li><li>A smoother development workflow</li><li>Strong recovery options when things go wrong</li></ul><p>It’s not perfect, and it’s not for everyone. But if you’re comfortable learning, primarily focused on productivity, and want more control than beginner distros provide, <strong>CachyOS is absolutely worth trying</strong>.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=caf9603e0a17" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Efficient AI: The Quiet Shift That Really Matters]]></title>
            <link>https://midhungraj.medium.com/efficient-ai-the-quiet-shift-that-really-matters-3c507ef4ff25?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c507ef4ff25</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[future]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[efficiency]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Sat, 20 Dec 2025 10:29:55 GMT</pubDate>
            <atom:updated>2025-12-20T10:29:55.441Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*qKkivmjBKYlClznUzjo9OQ.png" /></figure><p>Most people think AI progress works like this:</p><p><strong>Bigger models -&gt; More compute -&gt; Better results.</strong></p><p>GPT-3 became GPT-4. GPT-4 will become GPT-5.<br>Scale equals progress.</p><p>That story isn’t wrong.<br>But it’s incomplete.</p><p>While the world watches frontier labs race to build larger models, a quieter and more practical shift is happening. Engineers are asking a different question:</p><p><strong>What if AI didn’t need the cloud to be useful?</strong><br><strong>What if it actually worked everywhere?</strong></p><p>That question defines <em>efficient AI</em>.<br>And it matters more than most people realize.</p><h3>The Problem Nobody Talks About</h3><p>Here’s the uncomfortable truth:</p><p>Most AI systems don’t run where people actually live.</p><p>They assume:</p><ul><li>Fast internet</li><li>Constant connectivity</li><li>Expensive cloud servers</li><li>Ongoing per-request costs</li></ul><p>That’s fine if you’re sitting in Silicon Valley with fiber internet.</p><p>It’s a problem if you’re:</p><ul><li>On a phone with limited data</li><li>In a rural area</li><li>In a hospital with unstable connectivity</li><li>In a moving vehicle</li><li>In a developing country</li></ul><p>This isn’t an accident.<br> It’s a design choice.</p><p>We built AI assuming the cloud would always be there.</p><p>For billions of people, it isn’t.</p><h3>What “Efficient AI” Really Means</h3><p>Efficient AI does <strong>not</strong> mean “slightly smaller models.”</p><p>It means <strong>changing where intelligence lives</strong>.</p><p>Compare this:</p><p><strong>Cloud-only models</strong></p><ul><li>Run exclusively in data centers</li><li>Require massive memory and bandwidth</li><li>Depend on constant internet</li></ul><p><strong>On-device models (≈1B parameters)</strong></p><ul><li>Fit on phones</li><li>Run locally</li><li>Work offline</li><li>Respond instantly</li></ul><p>The smaller model isn’t trying to do everything.</p><p>It’s specific.<br> It’s fast.<br> And for real-world use, it’s often good enough.</p><p>That’s the point.</p><h3>How This Is Even Possible</h3><p>This isn’t magic. It’s engineering.</p><p>The toolbox already exists:</p><ul><li><strong>Quantization</strong><br> Lower precision (INT8, INT4) instead of full precision.<br> Result: 4–10× smaller models with minimal quality loss.</li><li><strong>Pruning</strong><br> Remove unnecessary weights.<br> Most networks are bloated with redundancy.</li><li><strong>Knowledge distillation</strong><br> Small “student” models learn from large “teacher” models.</li><li><strong>Efficient architectures</strong><br> Designed with battery, memory, and heat limits from day one.</li></ul><p>Apple, Google, Meta, and startups are already doing this.</p><p>This isn’t hype.<br> It’s production reality.</p><h3>Why This Changes Everything</h3><p>When AI runs on the device instead of the cloud, everything shifts:</p><p><strong>Privacy</strong><br> Your data stays with you.</p><p><strong>Reliability</strong><br> No internet required. No outages.</p><p><strong>Cost</strong><br> No per-request fees. No surprise cloud bills.</p><p><strong>Access</strong><br> AI becomes usable for people without fast or cheap internet.</p><p>This isn’t a marginal improvement.</p><p>It decides <strong>who gets access at all</strong>.</p><h3>The Real Future: Hybrid AI</h3><p>There’s no cloud vs edge war.</p><p>The future is hybrid.</p><ul><li>Devices handle fast, private tasks</li><li>The cloud handles heavy, complex reasoning</li></ul><p><strong>On-device AI</strong></p><ul><li>Voice commands</li><li>Translation</li><li>Photo editing</li><li>Message summaries</li></ul><p><strong>Cloud AI</strong></p><ul><li>Long-form writing</li><li>Deep analysis</li><li>Large-scale reasoning</li></ul><p>Do as much as you can locally.<br> Use the cloud only when necessary.</p><p>Speed and privacy on the edge.<br> Scale in the cloud.</p><p>Both matter.</p><h3>Why Efficient AI Is Hard (and Valuable)</h3><p>Here’s the part people avoid:</p><p>Efficient AI is <strong>harder</strong> than cloud AI.</p><p>You can’t brute-force it with GPUs.</p><p>You have to:</p><ul><li>Care about milliseconds</li><li>Work within hardware limits</li><li>Deal with device-specific failures</li><li>Trade accuracy for speed without breaking things</li></ul><p>There’s no leaderboard for:</p><blockquote>“Works reliably on a cheap phone in hot weather.”</blockquote><p>That difficulty scares people away.</p><p>That’s exactly why it’s valuable.</p><h3>What’s Coming Next</h3><p>Soon, you’ll see:</p><ul><li>Capable models on watches</li><li>Medical devices that work offline</li><li>Autonomous systems without internet dependence</li><li>Personal AIs that respect privacy</li></ul><p>Big models will keep getting better.</p><p>But the AI that touches the most lives won’t live in data centers.</p><p>It’ll live in pockets.</p><h3>Bottom Line</h3><p>The last decade proved intelligence can be built.</p><p>The next decade will prove it can be <strong>deployed</strong>.</p><p>Scale brought us here.<br>Efficiency takes us everywhere.</p><p>And the engineers who understand that will decide where AI truly lives.</p><p>Not just in the cloud.<br><strong>Everywhere.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c507ef4ff25" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Neovim Shortcuts for Beginners: Your First Steps Into the World of Modal Editing]]></title>
            <link>https://midhungraj.medium.com/neovim-shortcuts-for-beginners-your-first-steps-into-the-world-of-modal-editing-ce8961a0abb0?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce8961a0abb0</guid>
            <category><![CDATA[nvim]]></category>
            <category><![CDATA[writing-tips]]></category>
            <category><![CDATA[coding-tips]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Sat, 06 Sep 2025 06:51:01 GMT</pubDate>
            <atom:updated>2025-09-06T06:53:33.992Z</atom:updated>
            <content:encoded><![CDATA[<p>Don’t panic! Yes, Neovim looks intimidating at first, but I promise it gets easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0MxnQExkgEaUZgynK0aSfQ.png" /></figure><p>If you’ve just opened Neovim for the first time and are wondering why you can’t just start typing like in every other editor, you’re not alone. Welcome to the world of modal editing — it’s different, but once it clicks, you’ll wonder how you ever lived without it.</p><h3>Understanding the Basics: Modes Are Your Friends</h3><p>Before we dive into shortcuts, let’s understand what makes Neovim special. Unlike regular text editors where you’re always in “typing mode,” Neovim has different modes for different tasks. Think of it like having different tools for different jobs.</p><h3>The Three Main Modes You Need to Know</h3><p><strong>Normal Mode</strong> — This is your home base. You navigate, delete, copy, and give commands here. When in doubt, press Esc to get back to normal mode.</p><p><strong>Insert Mode</strong> — This is where you actually type text, just like any other editor.</p><p><strong>Visual Mode</strong> — This is where you select text to perform operations on it.</p><p><em>Pro tip: Always remember that </em><em>Esc brings you back to Normal mode. When you&#39;re lost, press </em><em>Esc and start over.</em></p><h3>Essential Movement Shortcuts (Normal Mode)</h3><p>Let’s start with moving around your file. Forget your mouse for now — your keyboard is about to become much more powerful.</p><h3>Basic Movement</h3><ul><li>h - Move left (think &quot;h&quot; is on the left of hjkl)</li><li>j - Move down (j looks like a down arrow)</li><li>k - Move up (k points up)</li><li>l - Move right (l is on the right of hjkl)</li></ul><p><em>Yes, it feels weird at first. Give it a week, and arrow keys will feel slow.</em></p><h3>Word-by-Word Movement</h3><ul><li>w - Jump to the beginning of the next word</li><li>b - Jump back to the beginning of the previous word</li><li>e - Jump to the end of the current word</li></ul><h3>Line Navigation</h3><ul><li>0 - Jump to the beginning of the line</li><li>$ - Jump to the end of the line</li><li>^ - Jump to the first non-blank character of the line</li></ul><h3>Screen Navigation</h3><ul><li>gg - Go to the first line of the file</li><li>G - Go to the last line of the file</li><li>Ctrl+u - Scroll up half a screen</li><li>Ctrl+d - Scroll down half a screen</li></ul><h3>Getting Into Insert Mode</h3><p>Remember, you need to enter Insert mode to actually type text:</p><ul><li>i - Insert before the cursor</li><li>a - Insert after the cursor (append)</li><li>o - Open a new line below and start inserting</li><li>O - Open a new line above and start inserting</li><li>I - Insert at the beginning of the line</li><li>A - Insert at the end of the line</li></ul><p><em>Personal favorite: </em><em>o is incredibly handy when you want to add a new line and start typing immediately.</em></p><h3>Editing Like a Pro</h3><h3>Deleting Text</h3><ul><li>x - Delete the character under the cursor</li><li>dd - Delete the entire line</li><li>dw - Delete from cursor to the beginning of the next word</li><li>d$ - Delete from cursor to the end of the line</li><li>d0 - Delete from cursor to the beginning of the line</li></ul><h3>Copying (Yanking) and Pasting</h3><ul><li>yy - Copy (yank) the entire line</li><li>yw - Copy from cursor to the beginning of the next word</li><li>y$ - Copy from cursor to the end of the line</li><li>p - Paste after the cursor</li><li>P - Paste before the cursor</li></ul><p><em>In Neovim terminology, copying is called “yanking” — don’t ask me why, but you’ll get used to it.</em></p><h3>Undoing and Redoing</h3><ul><li>u - Undo the last action</li><li>Ctrl+r - Redo the last undone action</li></ul><h3>Working with Visual Mode</h3><p>Visual mode is like highlighting text, but more powerful:</p><ul><li>v - Enter visual mode (character-wise selection)</li><li>V - Enter visual line mode (select entire lines)</li><li>Ctrl+v - Enter visual block mode (select rectangular blocks)</li></ul><p>Once in visual mode, use movement keys to select text, then:</p><ul><li>d - Delete the selected text</li><li>y - Yank (copy) the selected text</li><li>c - Change (delete and enter insert mode)</li></ul><h3>Search and Replace</h3><h3>Searching</h3><ul><li>/searchterm - Search forward for &quot;searchterm&quot;</li><li>?searchterm - Search backward for &quot;searchterm&quot;</li><li>n - Jump to the next search result</li><li>N - Jump to the previous search result</li></ul><h3>Basic Find and Replace</h3><ul><li>:s/old/new/ - Replace the first occurrence of &quot;old&quot; with &quot;new&quot; on the current line</li><li>:s/old/new/g - Replace all occurrences of &quot;old&quot; with &quot;new&quot; on the current line</li><li>:%s/old/new/g - Replace all occurrences of &quot;old&quot; with &quot;new&quot; in the entire file</li></ul><h3>File Operations</h3><ul><li>:w - Save the file (write)</li><li>:q - Quit Neovim</li><li>:wq - Save and quit</li><li>:q! - Quit without saving (force quit)</li><li>:e filename - Open a file</li></ul><p><em>The colon (</em><em>:) puts you in command mode – this is where you give Neovim instructions about files, settings, and more.</em></p><h3>Some Life-Saving Shortcuts</h3><h3>When You’re Stuck</h3><ul><li>Esc - Get back to normal mode (use this liberally)</li><li>u - Undo when you&#39;ve messed something up</li><li>:q! - Escape Neovim without saving when everything&#39;s gone wrong</li></ul><h3>Quick Fixes</h3><ul><li>dd then p - Quick way to move a line down</li><li>dd then P - Quick way to move a line up</li><li>&gt;&gt; - Indent the current line</li><li>&lt;&lt; - Unindent the current line</li></ul><h3>Common Beginner Mistakes (I Made Them Too!)</h3><ol><li><strong>Trying to type immediately</strong> — Remember to enter insert mode first with i, a, or o.</li><li><strong>Getting lost in modes</strong> — When confused, press Esc to get back to normal mode.</li><li><strong>Using the mouse</strong> — Try to resist. The keyboard is faster once you learn it.</li><li><strong>Giving up too quickly</strong> — Neovim has a learning curve, but it’s worth it. Give yourself at least two weeks.</li><li><strong>Not practicing regularly</strong> — 15 minutes daily beats 3 hours once a week for building muscle memory.</li></ol><h3>Why Bother Learning All This?</h3><p>You might be thinking, “This seems like a lot of work just to edit text.” Here’s why it’s worth it:</p><ul><li><strong>Speed</strong>: Once these shortcuts become muscle memory, you’ll edit text faster than you ever thought possible.</li><li><strong>Efficiency</strong>: Your hands never leave the keyboard, making your workflow smoother.</li><li><strong>Precision</strong>: Need to delete the next three words? d3w. Want to copy five lines? 5yy. Neovim speaks your language.</li><li><strong>Universal</strong>: Vim shortcuts work in many other tools and websites too.</li></ul><h3>Final Thoughts</h3><p>Learning Neovim shortcuts is like learning a musical instrument — it feels clunky at first, but with practice, it becomes second nature. You’re not just learning an editor; you’re developing a new way to think about text manipulation.</p><p>Remember: every expert was once a beginner who felt overwhelmed by hjkl. You&#39;ve got this! Start with the basics, practice consistently, and before you know it, you&#39;ll be editing text at the speed of thought.</p><p><em>Happy Vimming! And remember, when in doubt, press </em><em>Esc and try again.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce8961a0abb0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How __init__.py Changed the Way I Build Python Libraries ]]></title>
            <link>https://midhungraj.medium.com/how-init-py-changed-the-way-i-build-python-libraries-d37616321cbb?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/d37616321cbb</guid>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[init-python]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Mon, 07 Jul 2025 07:52:25 GMT</pubDate>
            <atom:updated>2025-07-07T07:52:25.108Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JsyCR3pafP5fAs_KSGOVPQ.png" /></figure><p>When I started learning Python, I kept seeing this strange file in tutorials and GitHub repos:</p><blockquote><em>__init__.py</em></blockquote><p>It was always there, tucked away in folders, looking mysterious and empty. For a long time, I ignored it. After all, my scripts ran fine without worrying about it… right?</p><p>But then I decided to build my own machine learning library — <strong>autoforge</strong> — and everything changed. Suddenly, __init__.py went from being a forgotten relic to one of the most powerful tools in my coding toolkit.</p><p>Let me explain how it unlocked a new level of organization and professionalism in my Python code.</p><h3>🏗️ The Project That Opened My Eyes</h3><p>Recently, I started building <strong>autoforge</strong>, a lightweight machine learning library inspired by scikit-learn. My goal was to implement algorithms like:</p><ul><li>Linear Regression</li><li>Logistic Regression</li><li>K-Nearest Neighbors</li><li>Decision Trees</li><li>Polynomial Features</li></ul><p>…and give developers a clean, user-friendly API.</p><p>But as my code grew, I found myself drowning in imports like this:</p><pre>from linear_model.linear_regression import LinearRegression<br>from linear_model.logistic_regression import LogisticRegression<br>from preprocessing.polynomial_features import PolynomialFeatures<br>from neighbors.knn import KNNClassifier</pre><p>It was messy, repetitive, and felt amateurish. I wanted something elegant, like how scikit-learn lets you simply write:</p><pre>from sklearn.linear_model import LogisticRegression</pre><p>How do they achieve that? You guessed it:</p><blockquote><strong><em>__init__.py</em></strong></blockquote><h3>🔍 What Exactly Does __init__.py Do?</h3><p>Here’s the simple truth I wish I’d known earlier:</p><p>✔ <strong>Marks a Folder as a Package</strong></p><p>When you put an __init__.py file inside a folder, you’re telling Python:</p><blockquote><em>“Hey, this folder isn’t just random files. It’s a package.”</em></blockquote><p>This allows you to:</p><ul><li>Import modules from that folder</li><li>Structure your project into logical units</li><li>Control what symbols your package exposes</li></ul><p>Without it (especially in older Python versions), your imports might fail or behave unpredictably.</p><p>✔ <strong>Runs Code Automatically on Import</strong></p><p>Whenever someone does:</p><pre>import autoforge.linear_model</pre><p>Python <strong>runs the code</strong> inside that folder’s __init__.py. That means you can:</p><ul><li>Define default imports</li><li>Initialize constants or configurations</li><li>Hide private modules from users</li></ul><p>It’s like giving your package a “welcome routine” every time someone uses it.</p><h3>✨ How __init__.py Transformed My Project</h3><p>Here’s how I applied it in <strong>autoforge</strong>:</p><h4>Before __init__.py</h4><p>My users had to write:</p><pre>from autoforge.linear_model.linear_regression import LinearRegression</pre><p>Ugly, right?</p><h4>After Adding __init__.py</h4><p>I edited autoforge/linear_model/__init__.py like this:</p><pre>from .linear_regression import LinearRegression<br>from .logistic_regression import LogisticRegression</pre><pre>__all__ = [&quot;LinearRegression&quot;, &quot;LogisticRegression&quot;]</pre><p>Now my users can write:</p><pre>from autoforge.linear_model import LinearRegression</pre><p>Clean. Professional. Exactly like scikit-learn.</p><h3>🔥 Why I Love It Now</h3><p>Thanks to __init__.py, my library:</p><p>✔ Feels professional and intuitive to use<br> ✔ Hides internal implementation details<br> ✔ Helps users discover what’s available<br> ✔ Keeps my import statements tidy</p><h3>🛠️ How to Use __init__.py Like a Pro</h3><p>Here’s the pattern I follow in <strong>autoforge</strong>:</p><h4>In autoforge/__init__.py</h4><p>Expose only the top-level modules:</p><pre>from . import linear_model<br>from . import preprocessing<br>from . import neighbors<br>from . import tree</pre><h4>In autoforge/linear_model/__init__.py</h4><p>Expose specific classes:</p><pre>from .linear_regression import LinearRegression<br>from .logistic_regression import LogisticRegression</pre><pre>__all__ = [&quot;LinearRegression&quot;, &quot;LogisticRegression&quot;]</pre><p>This keeps the public API clean and avoids forcing users to dig into subfolders.</p><h3>🎯 Final Thoughts</h3><p>Building my own machine learning library forced me to finally <strong>understand </strong><strong>__init__.py.</strong></p><p>It’s not just a random empty file — it’s the key to writing Python code that scales, stays organized, and feels professional.</p><p>If you’re working on any serious Python project — even a small one — learn how to use __init__.py early. You’ll save yourself hours of headaches and make your code a joy for others to use.</p><p>💬 <strong>Have questions about </strong><strong>__init__.py or Python packaging? Drop a comment below — let’s keep learning together!</strong></p><p><strong>Happy coding! 🚀</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d37616321cbb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Polynomial Regression: When a Straight Line Just Won’t Cut It]]></title>
            <link>https://midhungraj.medium.com/polynomial-regression-when-a-straight-line-just-wont-cut-it-5b5c8d5cdcc6?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b5c8d5cdcc6</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[polynomial-regression]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Mon, 30 Jun 2025 17:46:42 GMT</pubDate>
            <atom:updated>2025-06-30T17:46:42.510Z</atom:updated>
            <content:encoded><![CDATA[<p>“All models are wrong, but some are useful.” — George Box</p><p>If you’ve ever tried fitting a straight line through data that obviously curves, you know how “wrong” linear regression can sometimes be. Thankfully, there’s a simple yet powerful tool for capturing those curves: Polynomial Regression.</p><p>Let’s dive into what it is, why you’d use it, and how to actually implement it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*ONeRtetNhrH2ZkRkgekqdw.png" /></figure><h3>Why Linear Regression Falls Short</h3><p>Linear regression models relationships like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/134/1*BeAlyREDYIJS0Tux-s3LhQ.png" /></figure><p>A single straight line. Simple. Elegant. But very limited.</p><p>Here’s the problem: many real-world relationships aren’t straight lines. Think:</p><ul><li>The trajectory of a ball (a parabola)</li><li>Growth curves that accelerate then level off</li><li>Economic trends with rises and falls</li></ul><p>Trying to jam a straight line into curved data is like trying to fit a square peg into a round hole.</p><h3>Enter Polynomial Regression</h3><p><strong>Polynomial regression</strong> extends linear regression by adding powers of your input variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*tEZMQqnim8SQ90Gu6SbL2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*xVjzasCwDWblvFS-srk7cA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*SPZJhlf-hDCEcqftIYSqag.png" /></figure><h3>Beware the Dark Side: Overfitting</h3><p>Polynomial regression can be dangerously flexible.</p><ul><li><strong>Low-degree polynomials → underfitting.</strong> Too rigid to capture the pattern.</li><li><strong>High-degree polynomials → overfitting.</strong> They twist and turn to fit every tiny fluctuation, capturing noise rather than true signal.</li></ul><p>A degree-20 polynomial might pass through every point in your dataset — but predict nonsense on new data.</p><p><strong>Tip:</strong> Use cross-validation to choose the right degree. And consider regularization (like Ridge or Lasso) to tame high-degree models.</p><h3>A Quick Python Example</h3><p>Let’s see it in action with Python and scikit-learn.</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.linear_model import LinearRegression<br>from sklearn.preprocessing import PolynomialFeatures<br><br># Sample data<br>X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)<br>y = np.array([2, 5, 10, 17, 26])<br><br># Transform features to polynomial features of degree 2<br>poly = PolynomialFeatures(degree=2, include_bias=False)<br>X_poly = poly.fit_transform(X)<br><br># Fit the model<br>model = LinearRegression()<br>model.fit(X_poly, y)<br><br># Predict on new values<br>X_test = np.linspace(1, 5, 100).reshape(-1, 1)<br>X_test_poly = poly.transform(X_test)<br>y_pred = model.predict(X_test_poly)<br><br># Plot the results<br>plt.scatter(X, y, color=&#39;red&#39;, label=&#39;Data&#39;)<br>plt.plot(X_test, y_pred, color=&#39;blue&#39;, label=&#39;Polynomial Fit&#39;)<br>plt.legend()<br>plt.show()</pre><h3>When to Use Polynomial Regression</h3><p>✔ <strong>Do use it when:</strong></p><ul><li>Your data clearly follows a curve.</li><li>You’ve tested linear regression and see large residual errors.</li><li>You keep the polynomial degree reasonable.</li></ul><p>🛑 <strong>Don’t use it blindly if:</strong></p><ul><li>You have little data (high-degree polynomials will overfit).</li><li>You don’t validate your model on unseen data.</li></ul><h3>Conclusion</h3><p>Polynomial regression is a versatile tool for modeling curves in data. It’s simple, powerful, and easy to implement. But with great flexibility comes great risk: overfitting.</p><p>If your data laughs at your straight-line attempts, give polynomial regression a try — just keep an eye on that polynomial degree.</p><p><em>Further reading:</em></p><ul><li><a href="https://polynomialregression.drque.net/math.html">Polynomial Regression Explained</a></li><li>scikit-learn PolynomialFeatures Documentation</li></ul><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b5c8d5cdcc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ From Messy to Magic: Explaining the Transformation Pipeline Like You’re 5]]></title>
            <link>https://midhungraj.medium.com/from-messy-to-magic-explaining-the-transformation-pipeline-like-youre-5-25a6089ea670?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/25a6089ea670</guid>
            <category><![CDATA[data-pipeline]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Mon, 19 May 2025 22:10:52 GMT</pubDate>
            <atom:updated>2025-09-06T07:00:27.430Z</atom:updated>
            <content:encoded><![CDATA[<h3>From Messy to Magic: Explaining the Transformation Pipeline Like You’re 5</h3><blockquote>“Imagine teaching a robot how to do something smart — but before we give it the instructions, we need to clean up the mess. That’s where the transformation pipeline comes in!”</blockquote><h3>🌅 Morning Routine of a Super Student… or Data!</h3><p>Ever tried going to school in your pajamas with a toothbrush in your mouth and breakfast on your shirt? 😅 Not a good idea, right?</p><p>Well, guess what? Data feels the same way when it’s messy and gets pushed into a machine learning model. It just doesn’t work well.</p><p>That’s why we use a <strong>transformation pipeline</strong> — a series of steps we follow to get our data fresh, clean, and dressed up, ready to go learn in “model school.”</p><p>Let me explain it using something we all understand: your morning routine before school. 🎒</p><h3>🛌 Step 1: Wake Up the Data</h3><p>Our data might be sleepy, full of missing pieces, weird errors, or mixed-up formats.</p><p><strong>Example:</strong><br> Some entries might say &quot;N/A&quot; instead of a number. Others might be blank. Some rows may even have strange symbols!</p><p><strong>What we do:</strong><br> We clean it. Fill in the blanks. Fix the errors. Remove what’s unnecessary. Like shaking the data awake!</p><h3>🪥 Step 2: Brush the Data’s Teeth</h3><p>Now that the data is awake, it still might not be presentable. Like having morning breath. 😬</p><p><strong>What we do:</strong><br> Make everything consistent. Maybe change all “Yes”/”No” answers into 1 and 0. Maybe make everything lowercase. This helps our robot read things properly.</p><h3>👕 Step 3: Get the Data Dressed</h3><p>You wouldn’t wear your dinosaur pajamas to school, would you? 😄</p><p><strong>What we do:</strong><br> We transform and scale data — so all the numbers and words fit in a way the model can understand. Think of it like resizing clothes to fit perfectly!</p><p><strong>Example:</strong><br> If your dataset has ages between 1 and 100, and some values are huge while others are tiny, the model might get confused. So we scale the numbers — like adjusting the clothes for a better fit.</p><h3>🍳 Step 4: Feed the Data Breakfast</h3><p>Okay, now our data is clean and dressed — but we can go the extra mile and give it <strong>superpowers</strong>.</p><p><strong>What we do:</strong><br> We create new features. These are new columns or information made by mixing or calculating from the old ones.</p><p><strong>Example:</strong><br> If you have someone’s height and weight, you can create a new feature called <strong>BMI</strong>. That gives the model a better idea of their physical health!</p><h3>🏫 Step 5: Off to School (Train the Model!)</h3><p>Now your data is all set — neat, clean, and full of brain fuel. It’s time to send it to class.</p><p><strong>What we do:</strong><br> We feed this prepared data into a machine learning model. That’s the “teacher” that learns from the data and makes smart predictions later!</p><h3>🧠 What’s the Point?</h3><p>Without a transformation pipeline, your model will struggle — like a kid trying to do math with messy handwriting and no breakfast.</p><p>But with it? Your data becomes a super-student — focused, organized, and ready to ace the test!</p><h3>🎉 Final Words</h3><p>The transformation pipeline is your data’s <strong>superhero training ground</strong>. It’s how raw, messy, noisy data becomes clean, smart, and powerful — ready to be the brains of tomorrow’s AI.</p><p>So next time you run a machine learning model, think of it like getting ready for school — and remember: clean data = smart model! 🧠✨</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25a6089ea670" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using PyTorch for Calculus: A Deep Dive into Automatic Differentiation]]></title>
            <link>https://midhungraj.medium.com/using-pytorch-for-calculus-a-deep-dive-into-automatic-differentiation-e1435c52c3e2?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1435c52c3e2</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[autograd]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[calculus]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Mon, 31 Mar 2025 18:09:32 GMT</pubDate>
            <atom:updated>2025-03-31T18:09:32.409Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Calculus is the backbone of many machine learning algorithms, particularly in optimization and deep learning. PyTorch, a powerful deep learning framework, comes with built-in tools to perform calculus operations efficiently. In this blog post, we will explore how PyTorch handles differentiation, integration, and other calculus-related tasks.</p><h3>1. Differentiation Using PyTorch</h3><p>One of the most useful features of PyTorch is its <strong>autograd</strong> module, which allows automatic differentiation. This is crucial for gradient-based optimization in machine learning.</p><h3>Computing Derivatives with autograd</h3><p>PyTorch enables automatic differentiation using tensors with requires_grad=True. Here’s a simple example:</p><pre>import torch<br><br># Define a tensor with requires_grad=True<br>x = torch.tensor(2.0, requires_grad=True)<br><br># Define a function: y = x^2<br>y = x ** 2<br><br># Compute the derivative dy/dx<br>y.backward()<br><br># Print the gradient<br>dx = x.grad<br>print(f&quot;dy/dx at x=2: {dx.item()}&quot;)  # Output: 4.0</pre><p>PyTorch automatically computes the derivative of y with respect to x and stores it in x.grad.</p><h3>Partial Derivatives (Gradients of Multi-variable Functions)</h3><p>If a function has multiple inputs, PyTorch calculates the gradient for each variable:</p><pre>x = torch.tensor(2.0, requires_grad=True)<br>y = torch.tensor(3.0, requires_grad=True)<br><br>z = x**2 + y**3  # Function: z = x^2 + y^3<br>z.backward()<br><br>print(f&quot;dz/dx: {x.grad.item()}&quot;)  # Output: 4.0<br>print(f&quot;dz/dy: {y.grad.item()}&quot;)  # Output: 27.0</pre><h3>2. Higher-Order Derivatives</h3><p>PyTorch also supports higher-order derivatives. To compute second-order derivatives, set create_graph=True in backward():</p><pre>x = torch.tensor(2.0, requires_grad=True)<br>y = x**3  # Function: y = x^3<br><br>y.backward(create_graph=True)<br>dy_dx = x.grad  # First derivative: dy/dx = 3x^2<br><br># Compute second derivative<br>dy_dx.backward()<br>d2y_dx2 = x.grad  # Second derivative: d^2y/dx^2 = 6x<br><br>print(f&quot;First derivative dy/dx: {dy_dx.item()}&quot;)  # Output: 12.0<br>print(f&quot;Second derivative d^2y/dx^2: {d2y_dx2.item()}&quot;)  # Output: 6.0</pre><h3>3. Numerical Integration Using PyTorch</h3><p>While PyTorch does not provide built-in integration functions, we can approximate integrals using numerical methods like the <strong>trapezoidal rule</strong> or <strong>Monte Carlo integration</strong>.</p><h4>Trapezoidal Rule Approximation</h4><pre>def trapezoidal_rule(f, a, b, n):<br>    x = torch.linspace(a, b, n, requires_grad=True)<br>    y = f(x)<br>    dx = (b - a) / (n - 1)<br>    integral = torch.sum((y[:-1] + y[1:]) / 2) * dx<br>    return integral<br><br># Example: Integral of f(x) = x^2 from 0 to 3<br>f = lambda x: x ** 2<br>integral_value = trapezoidal_rule(f, 0, 3, 100)<br>print(f&quot;Approximate integral: {integral_value.item()}&quot;)</pre><h3>4. Jacobians and Hessians in PyTorch</h3><h4>Jacobian Matrix</h4><p>For vector-valued functions, PyTorch provides torch.autograd.functional.jacobian to compute the Jacobian matrix.</p><pre>from torch.autograd.functional import jacobian<br><br>def func(x):<br>    return torch.tensor([x[0]**2 + x[1], 3*x[0] + torch.sin(x[1])])<br><br>x = torch.tensor([2.0, 1.0], requires_grad=True)<br>J = jacobian(func, x)<br>print(J)  # 2x2 Jacobian matrix</pre><h4>Hessian Matrix</h4><p>The Hessian matrix, which contains second-order derivatives, can be computed using torch.autograd.functional.hessian.</p><pre>from torch.autograd.functional import hessian<br><br>def scalar_func(x):<br>    return x[0]**2 + 3*x[0]*x[1] + torch.sin(x[1])<br><br>H = hessian(scalar_func, x)<br>print(H)  # 2x2 Hessian matrix</pre><h3>Conclusion</h3><p>PyTorch provides a powerful and flexible framework for performing calculus operations, particularly differentiation and optimization. Whether you’re working on deep learning or numerical methods, <strong>PyTorch’s autograd system and functional API make calculus computations seamless and efficient</strong>.</p><p>If you’re interested in machine learning, mastering calculus in PyTorch is a crucial step toward understanding how models learn and optimize. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1435c52c3e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Leakage in Machine Learning: The Hidden Threat and How to Prevent It]]></title>
            <link>https://midhungraj.medium.com/data-leakage-in-machine-learning-the-hidden-threat-and-how-to-prevent-it-6b03be4cc53a?source=rss-a0f37c3dd0e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b03be4cc53a</guid>
            <category><![CDATA[data-leakage-prevention]]></category>
            <category><![CDATA[data-leak]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Midhun G Raj]]></dc:creator>
            <pubDate>Sat, 01 Feb 2025 17:41:39 GMT</pubDate>
            <atom:updated>2025-02-01T17:43:30.574Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>Data leakage is one of the most overlooked yet dangerous pitfalls in machine learning. It occurs when information from outside the training dataset improperly influences the model, leading to unrealistically high performance during training but poor generalization to new data. If left undetected, data leakage can render a machine learning model useless in real-world applications.</p><p>In this blog, we’ll explore what data leakage is, how it happens, its consequences, and how you can prevent it to build more robust and reliable models</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*MOlg_Op_Z1VVvuyXVmoAoA.png" /><figcaption>credit: javatpoint</figcaption></figure><h3>What is Data Leakage?</h3><p>Data leakage happens when a model accidentally gains access to information that would not be available in a real-world scenario. This results in an overly optimistic assessment of model performance because it learns from patterns that won’t exist during deployment.</p><p>Think of it as cheating on a test by looking at the answers beforehand. Your results will look great, but when faced with a real challenge, you will fail.</p><h3>Types of Data Leakage</h3><h4>1. Target Leakage</h4><p>Target leakage occurs when a feature in the dataset contains information about the target variable that would not be available at prediction time. This results in an overly optimistic model that appears to perform well but is useless in practice.</p><p><strong>Example:</strong></p><ul><li>Predicting whether a bank customer will default on a loan, but including a feature like “number of missed payments” (which directly indicates defaulting behavior).</li></ul><p><strong>Prevention:</strong></p><ul><li>Carefully examine each feature to ensure it does not provide unintended future knowledge.</li><li>Remove features that wouldn’t be available at the time of making a real prediction.</li></ul><h4>2. Train-Test Contamination</h4><p>This occurs when information from the test set leaks into the training set, causing the model to have knowledge of future data.</p><p><strong>Example:</strong></p><ul><li>Preprocessing (such as scaling or imputation) applied to the entire dataset before splitting into training and testing sets.</li></ul><p><strong>Prevention:</strong></p><ul><li>Always split your dataset into training and testing sets <strong>before</strong> applying any preprocessing.</li></ul><pre>from sklearn.model_selection import train_test_split<br>from sklearn.preprocessing import StandardScaler<br><br># Split data first<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<br><br># Apply transformations only on training data<br>scaler = StandardScaler()<br>X_train_scaled = scaler.fit_transform(X_train)<br>X_test_scaled = scaler.transform(X_test)</pre><h4>3. Feature Leakage</h4><p>This happens when features include indirect information about the target variable.</p><p><strong>Example:</strong></p><ul><li>Using a feature like “discount applied” when predicting whether a customer will make a purchase (because discounts are often applied after the decision to buy has already been made).</li></ul><p><strong>Prevention:</strong></p><ul><li>Perform exploratory data analysis (EDA) to check for suspiciously high correlations between features and the target.</li><li>Ensure that all features used in training would be realistically available at prediction time.</li></ul><h4>4. Temporal Leakage</h4><p>This occurs in time-series data when future information is used to predict past or present outcomes.</p><p><strong>Example:</strong></p><ul><li>Trying to predict stock prices on January 1st using data from January 2nd.</li></ul><p><strong>Prevention:</strong></p><ul><li>When dealing with time-series data, always use past data to predict future outcomes.</li><li>Split training and testing sets chronologically rather than randomly.</li></ul><pre>train = data[data[&#39;date&#39;] &lt; &#39;2025-02-01&#39;]<br>test = data[data[&#39;date&#39;] &gt;= &#39;2025-02-01&#39;]</pre><h3>Consequences of Data Leakage</h3><ul><li><strong>False Confidence:</strong> Models appear to have high accuracy during training but fail in production.</li><li><strong>Poor Generalization:</strong> The model struggles to perform on real-world data.</li><li><strong>Wasted Resources:</strong> Time and computational power are spent on building and tuning a flawed model.</li><li><strong>Bad Business Decisions:</strong> Deploying a misleading model can result in financial and reputational damage.</li></ul><h3>How to Prevent Data Leakage</h3><h4>✅ Split Data Early and Correctly</h4><ul><li>Always separate training, validation, and test sets <strong>before</strong> any data transformation.</li><li>Avoid using test data during feature engineering.</li></ul><h4>✅ Apply Preprocessing Separately</h4><ul><li>Perform transformations like scaling, encoding, and imputation <strong>only on the training set</strong>, then apply the same transformations to the validation and test sets.</li></ul><h4>✅ Review Features Carefully</h4><ul><li>Check whether any feature contains information that wouldn’t be available in a real-world scenario.</li><li>Remove highly correlated features if they seem too good to be true.</li></ul><h4>✅ Use Cross-Validation for Robustness</h4><ul><li>Apply <strong>K-Fold Cross-Validation</strong> to ensure that the model is not benefiting from hidden data leaks.</li></ul><pre>from sklearn.model_selection import cross_val_score<br>scores = cross_val_score(model, X_train, y_train, cv=5)<br>print(&quot;Cross-validation score:&quot;, scores.mean())</pre><h4>✅ Conduct Real-World Testing</h4><ul><li>Test the model on a truly unseen dataset to validate its performance in a realistic scenario.</li></ul><h3>Conclusion</h3><p>Data leakage is one of the most deceptive issues in machine learning. A model that appears to be performing exceptionally well may, in reality, be learning from information it shouldn’t have access to. By understanding how data leakage occurs and applying best practices to prevent it, you can build more reliable and generalizable models.</p><p>By following these preventive measures, you can ensure that your machine learning models provide <strong>accurate, trustworthy, and truly predictive insights.</strong></p><p><strong>Happy Learning &amp; Happy Coding!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b03be4cc53a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>