<?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 Raghavan on Medium]]></title>
        <description><![CDATA[Stories by Raghavan on Medium]]></description>
        <link>https://medium.com/@raghavan99o?source=rss-c89e4638a116------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LGv35pYrt8-q7dFz4NEPgg.jpeg</url>
            <title>Stories by Raghavan on Medium</title>
            <link>https://medium.com/@raghavan99o?source=rss-c89e4638a116------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 21:21:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raghavan99o/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[Triplet Loss, Sampling, and the Real Villain: Negative Mining]]></title>
            <link>https://medium.com/@raghavan99o/part-2-three-people-walk-into-a-bar-54b24e7ec521?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/54b24e7ec521</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 04:53:06 GMT</pubDate>
            <atom:updated>2026-03-05T04:54:19.302Z</atom:updated>
            <content:encoded><![CDATA[<p>Part 2 — Three People Walk Into a Bar</p><p>In <a href="https://medium.com/@raghavan99o/siamese-networks-and-contrastive-loss-explained-a-practical-guide-for-engineers-a2a7c79bb556">Part 1</a>, we learned how Siamese networks and classic contrastive loss turn “same-ness” into geometry.</p><p>We also ended with a problem:</p><blockquote><em>Pairs are polite. They don’t compete.</em></blockquote><p>And that matters, because production similarity systems are rarely pairwise.</p><p>They are ranking systems.</p><p>A user gives you a query, and your system must do something much harder than “same vs different”:</p><blockquote><em>Rank the true match above all the impostors.</em></blockquote><p>That’s where <strong>Triplet Loss</strong> enters the story.</p><p>Triplet loss is the point where metric learning grows up.</p><p>It stops asking, “Are these two close enough?”<br> And starts asking, “Is the right thing <em>closer than the wrong thing</em>?”</p><p>This sounds like a small change.</p><p>It is not.</p><p>It changes how you think about:</p><ul><li>the loss</li><li>the batch</li><li>the dataset</li><li>and most importantly…</li></ul><blockquote><strong><em>sampling and negative mining</em></strong><em> (the real villain of triplet training)</em></blockquote><p>This post explains triplet loss with intuition first, then optional math, and then goes deep into the dataset construction playbook that actually makes it work.</p><h3>1) The shift that changes everything: from pairs to comparisons</h3><p>Let’s say you’re training a product retrieval model.</p><p>With pairwise contrastive loss, you tell the model:</p><ul><li>this pair is a match</li><li>this pair is not a match</li></ul><p>That’s useful.</p><p>But at inference time, your system doesn’t see one candidate.</p><p>It sees many candidates.</p><p>It has to rank them.</p><p>And that means the real question is not:</p><blockquote><em>“Is A close to B?”</em></blockquote><p>The real question is:</p><blockquote><em>“Is the true match closer to the query than the confusing alternatives?”</em></blockquote><p>That is a <strong>relative</strong> question.</p><p>Triplet loss is built exactly for that.</p><h3>2) Triplet Loss: the anchor, the friend, and the impostor</h3><p>Triplet loss trains on triples:</p><ul><li><strong>anchor (a)</strong> → the query</li><li><strong>positive (p)</strong> → the true match</li><li><strong>negative (n)</strong> → an impostor (should not match)</li></ul><h3>Intuition first</h3><p>Imagine you’re at a noisy party.</p><p>You’re trying to recognize your friend in a crowd.</p><p>Triplet loss does not insist that your friend must stand <em>right next to you</em>.</p><p>It only insists on this:</p><blockquote><em>Your friend should be closer to you than the impostor is — by a safe margin.</em></blockquote><p>That “by a margin” part matters.</p><p>Without a margin, the model could technically satisfy the rule with tiny differences:</p><ul><li>friend distance = 0.99</li><li>impostor distance = 1.00</li></ul><p>That may pass the ranking rule, but it’s fragile.</p><p>The margin says:</p><blockquote><em>“Don’t just be better. Be </em>comfortably<em> better.”</em></blockquote><p>That makes the geometry more robust.</p><h3>Math Corner (optional): triplet loss</h3><p>Let:</p><ul><li>D(a, p) = distance between anchor and positive</li><li>D(a, n) = distance between anchor and negative</li><li>m = margin</li></ul><p>A common triplet loss form is:</p><pre>L = max(0, D(a, p) - D(a, n) + m)</pre><p>Interpretation:</p><ul><li>If D(a, n) &gt;= D(a, p) + m → loss = 0 (good triplet)</li><li>Otherwise → loss &gt; 0 and the model gets pushed to:</li><li>pull a and p closer</li><li>push a and n farther</li></ul><p>This is a margin-based ranking loss.</p><h3>3) Why triplet loss often feels “smarter” than pairwise contrastive</h3><p>Pairwise contrastive says:</p><ul><li>positives should be close</li><li>negatives should be far (at least margin far)</li></ul><p>Triplet says something more task-aligned:</p><blockquote><em>For this specific query, the positive should outrank the negative.</em></blockquote><p>That is much closer to how retrieval and search systems are evaluated in practice.</p><p>In other words:</p><ul><li>Pairwise loss shapes <strong>absolute geometry</strong></li><li>Triplet loss shapes <strong>relative ordering</strong></li></ul><p>And ranking is usually what your product actually cares about.</p><p>So why doesn’t everyone just use triplet loss all the time?</p><p>Because triplet loss has a hidden tax.</p><p>And the tax is paid in <strong>data construction</strong>.</p><h3>4) The real villain: sampling and negative mining</h3><p>Triplet loss looks elegant on slides.</p><p>In training, it can feel disappointing at first.</p><p>Why?</p><p>Because most triplets are useless.</p><h3>The “zero-loss triplet” problem</h3><p>Suppose your anchor is a photo of a shoe, your positive is another photo of the same shoe, and your negative is… a banana.</p><p>That negative is so easy that the model quickly satisfies the triplet constraint.</p><p>Now the loss becomes zero.</p><p>No learning happens.</p><p>If most of your triplets look like that, your training loop runs, your GPU heats up, and your model barely improves.</p><p>This is the central truth of triplet training:</p><blockquote><em>The loss is simple.</em><strong><em>Sampling is the hard part.</em></strong></blockquote><h3>5) The mining ladder: from easy negatives to truly useful ones</h3><p>Think of negative selection as a ladder.</p><p>You usually climb this ladder as your model gets stronger.</p><h3>Level 1: Random negatives (good for bootstrapping)</h3><p>These are easy to generate:</p><ul><li>different class</li><li>different item</li><li>different identity</li></ul><p>Pros:</p><ul><li>simple</li><li>cheap</li><li>works early in training</li></ul><p>Cons:</p><ul><li>quickly becomes too easy</li><li>lots of zero-loss triplets</li></ul><p>Use this to get started — not to finish.</p><h3>Level 2: Metadata-aware negatives (cheap but smarter)</h3><p>Negatives from the same broad bucket:</p><ul><li>same product category, different SKU</li><li>same language, different meaning</li><li>same speaker gender, different speaker</li><li>same topic, different answer</li></ul><p>Why this helps:</p><ul><li>they are more confusable than random negatives</li><li>still easy to generate using metadata</li></ul><p>This is one of the best “low effort, high return” upgrades in production pipelines.</p><h3>Level 3: Semi-hard negatives (the sweet spot)</h3><p>A semi-hard negative is one that is:</p><ul><li>farther than the positive (good)</li><li>but not far enough to satisfy the margin (still useful)</li></ul><p>Intuitively:</p><ul><li>it is wrong, but <em>plausibly</em> wrong</li><li>it teaches the model without blowing up training</li></ul><p>These often give better training stability than always picking the hardest negatives.</p><h3>Level 4: Hard negatives (powerful, risky)</h3><p>These are negatives that the model currently finds very similar to the anchor.</p><p>Examples:</p><ul><li>lookalike faces</li><li>near-identical product variants</li><li>semantically related but wrong documents</li><li>same entity name, different entity</li></ul><p>These are often where model quality jumps.</p><p>But hard negatives come with risk:</p><ul><li>label noise hurts more</li><li>false negatives hurt more</li><li>over-mining can destabilize training</li></ul><p>Hard negatives are like heavy weights in the gym:<br> great for growth, bad if your form is wrong.</p><h3>Level 5: Model-mined negatives (train on your mistakes)</h3><p>This is where triplet systems become serious.</p><p>Workflow:</p><ol><li>Train a baseline model</li><li>Embed a large corpus</li><li>Retrieve nearest neighbors for each anchor</li><li>Select non-matching near neighbors as negatives</li><li>Retrain</li></ol><p>This teaches the model on its own failure modes.</p><p>It also creates a virtuous cycle:</p><ul><li>better model → better mined negatives → better model</li></ul><h3>6) Dataset prep playbook for triplet training (the part most blogs skip)</h3><p>This is the practical playbook for <strong>how to prepare data for Siamese/triplet systems</strong>.</p><p>We’ll keep this triplet-focused, but most ideas apply to pairwise and multi-negative training too.</p><h3>6.1 Define what “same” means in your product (seriously, do this first)</h3><p>Before constructing triplets, write down your product definition of “same.”</p><p>Examples:</p><ul><li><strong>Face verification</strong>: same person identity</li><li><strong>Speaker verification</strong>: same speaker, despite noise/channel changes</li><li><strong>Product retrieval</strong>: same SKU (not just visually similar)</li><li><strong>Semantic retrieval</strong>: relevant answer/document for a query (not literal paraphrase only)</li></ul><p>Why this matters:</p><ul><li>it determines your positives</li><li>it determines your false negatives</li><li>it determines your evaluation metric later</li></ul><p>A lot of training pain is actually label-definition pain.</p><h3>6.2 Choose your supervision sources</h3><p>You can build triplets from multiple supervision sources.</p><h3>A) Explicit labels (cleanest)</h3><p>Examples:</p><ul><li>person ID</li><li>speaker ID</li><li>product SKU</li><li>class labels</li></ul><p>Construction:</p><ul><li>anchor and positive from same label</li><li>negative from different label</li></ul><p>This is the easiest setup.</p><p>But even here, quality depends on sampling diversity:</p><ul><li>same label but different conditions (lighting, angle, noise, wording)</li><li>not just near-duplicates</li></ul><h3>B) Weak supervision (often the real-world path)</h3><p>Examples:</p><ul><li>clicks</li><li>co-watch / co-buy</li><li>long dwell time</li><li>repeated interaction</li><li>add-to-cart after query</li></ul><p>These are not perfect labels. They are behavior signals.</p><p>Treat them as “likely positives,” not absolute truth.</p><p>Good practice:</p><ul><li>use filters (e.g., long click, conversion, repeat engagement)</li><li>combine multiple signals</li><li>manually audit a sample</li></ul><p>Weak supervision is incredibly valuable — but only if you respect its noise.</p><h3>C) Heuristics and rules (great for bootstrapping)</h3><p>Examples:</p><ul><li>exact title match</li><li>normalized string match</li><li>same canonical ID</li><li>duplicate image hash</li><li>URL canonicalization</li></ul><p>These produce high-precision seed positives and candidate pools.</p><p>But beware of shortcut learning:</p><ul><li>if all positives are exact-string matches, your model may learn surface overlap instead of semantic similarity</li></ul><p>Use rules as scaffolding, not the whole building.</p><h3>D) Self-supervision via augmentations (same item, two views)</h3><p>Create two transformed views of the same example and treat them as a positive source.</p><p>Examples:</p><ul><li>image crop A / crop B</li><li>text with dropout noise</li><li>audio with noise/reverb/time shift</li></ul><p>This scales beautifully.</p><p>But only if your augmentations preserve the right notion of identity/similarity.</p><p>Bad augmentation = accidental label corruption.</p><h3>6.3 How to construct anchors and positives well</h3><p>Most people focus on negatives. That’s fair.</p><p>But weak positives can quietly cap your model too.</p><h3>A good positive set has diversity</h3><p>If every positive is an exact duplicate, the model learns a brittle notion of sameness.</p><p>Better positives include natural variation:</p><ul><li>different lighting / camera angle</li><li>different microphones / channels (for speech)</li><li>paraphrases with different wording</li><li>different product backgrounds</li><li>different crops / compressions</li></ul><p>The model should learn invariance to these factors.</p><p>That invariance is the whole point of embeddings.</p><h3>6.4 Building negative pools (before mining)</h3><p>Before online mining, create structured negative pools.</p><p>Think in three buckets:</p><h3>Easy pool</h3><p>Clearly different items.<br> Useful early in training and for regularization.</p><h3>Medium pool</h3><p>Same coarse category, different identity/item.<br> Useful for learning discriminative detail.</p><h3>Hard pool (candidate pool)</h3><p>Known confusing examples:</p><ul><li>same brand, different SKU</li><li>same query intent cluster, wrong doc</li><li>lookalike identities</li><li>semantically close but incorrect answers</li></ul><p>Even if you don’t have model-based mining yet, curated hard pools can dramatically improve training.</p><h3>6.5 Online vs offline mining (what changes during training)</h3><h3>Offline mining</h3><p>You precompute embeddings and mine negatives periodically.</p><p>Pros:</p><ul><li>simpler training loop</li><li>easy to inspect</li><li>scalable for large corpora</li></ul><p>Cons:</p><ul><li>negatives become stale as the model changes</li></ul><h3>Online mining</h3><p>You mine negatives inside the batch (or on the fly using current embeddings).</p><p>Pros:</p><ul><li>always current</li><li>can be very effective</li></ul><p>Cons:</p><ul><li>more implementation complexity</li><li>sensitive to batch composition</li><li>can over-focus on noisy examples</li></ul><p>A practical approach:</p><ul><li>start with offline mining + curated pools</li><li>add online/semi-hard mining as the model stabilizes</li></ul><h3>6.6 False negatives: the silent killer</h3><p>A false negative is something you label as “different” even though it is actually a valid match (or at least a useful positive) under your product definition.</p><p>Examples:</p><ul><li>two paraphrases labeled negative because they came from different sources</li><li>same product sold by different sellers with different IDs</li><li>duplicate documents across content systems</li><li>same person mislabeled across datasets</li></ul><p>Why false negatives hurt triplet training so much:</p><ul><li>hard mining is <em>designed</em> to find near neighbors</li><li>near neighbors are exactly where false negatives hide</li><li>the model gets punished for doing something semantically correct</li></ul><p>This can severely distort the embedding space.</p><h3>Ways to reduce false negatives</h3><ul><li>deduplicate aggressively before training</li><li>use metadata joins / canonicalization</li><li>use human review on mined hard negatives</li><li>downweight uncertain negatives</li><li>avoid always taking the very hardest negative</li><li>keep a “do-not-use-as-negative” exclusion list</li></ul><p>This one issue explains a surprising number of “training got worse after hard mining” stories.</p><h3>6.7 Batch construction (triplet training’s hidden lever)</h3><p>Even with triplet loss, your batch design matters a lot.</p><p>A strong pattern is:</p><blockquote><em>Sample K identities (or groups), and M examples per identity.</em></blockquote><p>Why this helps:</p><ul><li>many anchor-positive combinations inside the batch</li><li>many same-category/similar negatives across groups</li><li>easier online mining (semi-hard/hard within batch)</li></ul><p>This structure also makes later migration to N-pair / InfoNCE much easier.</p><h3>6.8 Validation splits: don’t leak your success</h3><p>A metric learning model can look amazing if your split is sloppy.</p><h3>Common leakage mistakes</h3><ul><li>same identity appears in both train and val (when evaluating generalization)</li><li>near duplicates across train/val</li><li>same session/query family split across train/val</li><li>time leakage (future behavior used in training, past used in eval)</li></ul><h3>Better validation strategies</h3><ul><li><strong>identity-disjoint split</strong> (for verification)</li><li><strong>time-sliced split</strong> (for production search/retrieval)</li><li><strong>OOD split</strong> (new categories/languages/domains)</li><li><strong>hard validation set</strong> (confusing examples, adversarial negatives)</li></ul><p>If your validation set is too easy, your model’s real-world behavior will surprise you — and not in a good way.</p><h3>7) Engineering Corner: triplet loss tips that save time</h3><h3>1) Normalize embeddings unless you have a reason not to</h3><p>Unit-normalized embeddings make distance/similarity behavior easier to interpret and tune.</p><h3>2) Start with semi-hard negatives, not “hardest always”</h3><p>Always picking the hardest negative sounds smart, but often amplifies noise and false negatives.</p><p>Semi-hard negatives are usually a better default.</p><h3>3) Margin is a behavior knob, not a magical constant</h3><p>Too small → weak separation<br> Too large → many active triplets, unstable training</p><p>Tune margin together with:</p><ul><li>embedding normalization</li><li>sampling strategy</li><li>batch construction</li></ul><h3>4) Track “active triplet rate”</h3><p>A super useful debug metric:</p><ul><li>what fraction of triplets have non-zero loss?</li></ul><p>If it drops too low early, your negatives are too easy.<br> If it stays too high forever, your margin/sampling may be too aggressive (or labels noisy).</p><h3>5) Audit your mined negatives</h3><p>Especially early on.</p><p>A quick manual audit of 100 mined “hard negatives” often reveals:</p><ul><li>false negatives</li><li>metadata bugs</li><li>label drift</li><li>indexing errors</li></ul><p>This is one of the highest-ROI debugging steps in metric learning.</p><h3>8) Mini checklist: what to remember from Part 2</h3><p>If you remember only eight things, remember these:</p><ol><li>Triplet loss optimizes <strong>relative ranking</strong>, not just absolute distance.</li><li>The rule is simple: positive should be closer than negative by a margin.</li><li>Most random triplets become useless quickly (zero-loss triplets).</li><li>Triplet training quality depends heavily on <strong>sampling and negative mining</strong>.</li><li>Semi-hard negatives are often the best starting point.</li><li>False negatives are the silent killer — especially with hard mining.</li><li>Batch construction and validation splits can make or break results.</li><li>In metric learning, <strong>data construction is part of the model</strong>.</li></ol><h3>9) Teaser for Part 3: from arguments to tournaments</h3><p>Triplet loss is a huge step forward.</p><p>But it still has one limitation:</p><blockquote><em>It argues with one impostor at a time.</em></blockquote><p>Modern contrastive learning gets stronger by doing something more efficient:</p><blockquote><em>Put the anchor in a room with many impostors and force it to pick the true match.</em></blockquote><p>That takes us from triplets to <strong>multi-negative learning</strong>:</p><ul><li><strong>N-pair loss</strong></li><li><strong>InfoNCE</strong></li><li><strong>NT-Xent</strong></li></ul><p>And once we’re training ranking-style systems at scale, we need a better question than “loss went down.”</p><p>We need to ask:</p><blockquote><em>How do we actually measure whether a similarity system is good — offline and online?</em></blockquote><p>That’s Part 3.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54b24e7ec521" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Siamese Networks and Contrastive Loss Explained: A Practical Guide for Engineers]]></title>
            <link>https://medium.com/@raghavan99o/siamese-networks-and-contrastive-loss-explained-a-practical-guide-for-engineers-a2a7c79bb556?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2a7c79bb556</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Mon, 02 Feb 2026 10:15:55 GMT</pubDate>
            <atom:updated>2026-02-02T10:15:55.732Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9G9o6u9HvjA-iDqMRrji-g.png" /></figure><p>It’s 2:13 AM.</p><p>You’ve just shipped a feature that sounds trivial on paper:</p><p><strong>“Find things that are the same.”</strong></p><p>Same face. Same speaker. Same product. Same sentence meaning.</p><p>You’re feeling good — until support tickets start rolling in.</p><p>Two photos of the same person, different lighting → “Not same.”<br> Two different people, similar pose → “Same.”<br> A paraphrase → “Unrelated.”<br> A completely unrelated sentence → “Very similar.”</p><p>You do the classic debugging ritual:</p><ul><li>Check the dataset</li><li>Check the labels</li><li>Check the thresholds</li><li>Check the model</li></ul><p>And then you notice the deeper problem.</p><p><strong>You trained a classifier.</strong><br> But what you actually needed was something else:</p><p><strong>You needed a map of your world — a space where “same” is literally “nearby.”</strong></p><p>That’s the origin story of Siamese networks and contrastive loss.</p><p>This post builds the idea piece by piece. Each section starts with intuition. If you enjoy equations, you’ll find optional <strong>“Math Corner”</strong> blocks you can read — or skip — without losing the thread.</p><h3>1. The map: why embeddings beat labels for “same-ness”</h3><p><strong>Classification answers:</strong><br> <em>What is this?</em> → “This is Alice.”</p><p><strong>Similarity answers:</strong><br> <em>Is this like that?</em> → “This is like Alice.”</p><p>They look related, but they behave very differently in production.</p><p>A classifier’s output is tied to a <strong>fixed label set</strong>. It’s great when classes are stable and well-defined.</p><p>But “same-ness” problems rarely behave that nicely:</p><ul><li>New identities appear (new faces, new speakers, new products)</li><li>You don’t always have labels</li><li>You care about <strong>ranking</strong> (top-k matches), not just a label</li></ul><p>So instead of predicting a class directly, we learn an <strong>embedding</strong>:</p><blockquote><em>A function that turns any input into a vector, such that similar inputs end up close in vector space.</em></blockquote><p>Once you have that, “same-ness” becomes <strong>geometry</strong>:</p><ol><li>Compute embeddings</li><li>Measure distance / similarity</li><li>Decide if it’s a match (or retrieve nearest neighbors)</li></ol><p>This approach <strong>generalizes far beyond the training classes</strong> — and that’s why it’s everywhere now.</p><p>But we still need one missing piece:</p><p><strong>How do we train embeddings so that “close means same”?</strong></p><p>That’s where Siamese networks enter.</p><h3>2. Siamese networks: the twin detectives</h3><p>Picture two detectives, trained in the same academy, with the same instincts.</p><ul><li>Detective A sees input x1</li><li>Detective B sees input x2</li><li>Each produces a “case file” (an embedding vector)</li><li>You compare the two case files: <strong>close ⇒ likely same, far ⇒ likely different</strong></li></ul><p><strong>The defining feature: they share weights.</strong><br> Same network, applied twice.</p><p>That matters because it forces the model to learn a <strong>consistent representation</strong>. Both detectives must agree on what features matter.</p><p>So the architecture looks like:</p><pre>z1 = f(x1)<br>z2 = f(x2)<br>compare(z1, z2) → distance D or similarity s</pre><p>This is a <strong>pattern</strong>, not a specific neural architecture. f(·) could be a CNN, a transformer, an audio model—anything.</p><p>Now the big question:</p><p><strong>What loss teaches the model to pull “same” together and push “different” apart?</strong></p><p>Enter: classic contrastive loss.</p><h3>3. Contrastive loss (classic): rubber bands + personal space</h3><p>Let’s stay in “physics” mode for a moment.</p><h3>The intuition</h3><p><strong>Positive pairs: rubber bands</strong></p><p>If two inputs should match (same identity / same meaning), imagine a <strong>rubber band</strong> tying them together.</p><ul><li>If they’re far apart in embedding space, the band stretches and pulls them closer</li><li>If they’re already close, the band relaxes</li></ul><p>So positives create a <strong>pull force</strong>.</p><p><strong>Negative pairs: personal space bubbles</strong></p><p>If two inputs should <em>not</em> match, you don’t need them to be infinitely far apart.</p><p>You just need them to <strong>not be confusingly close</strong>.</p><p>So imagine each point has a “personal space bubble” with radius m (the <strong>margin</strong>).</p><ul><li>If a negative pair is inside that bubble (too close), push them apart</li><li>If they’re outside the bubble (far enough), <strong>stop caring</strong></li></ul><p>So negatives create a <strong>push force — but only until “safe distance.”</strong></p><p>That’s contrastive loss in one line:</p><blockquote><strong><em>Pull positives together.</em></strong><em><br> </em><strong><em>Push negatives apart, but only until a margin.</em></strong></blockquote><p>This one design choice — <strong>“stop pushing after margin”</strong> — is the most important behavioral difference between classic contrastive and many classification losses.</p><h3>Math Corner (optional): the classic formula</h3><p>Let:</p><ul><li>z1 = f(x1), z2 = f(x2)</li><li>D = ||z1 − z2|| (distance)</li><li>y = 1 if pair is positive (same), y = 0 if negative (different)</li><li>m = margin</li></ul><p>A common form:</p><pre>L = y * D²  +  (1 - y) * max(0, m - D)²</pre><p><strong>Interpretation:</strong></p><ul><li>If y = 1 (positive): minimize D² → pull together</li><li>If y = 0 (negative): minimize max(0, m - D)²</li><li>if D &gt;= m → loss is 0 → done</li><li>if D &lt; m → push apart</li></ul><h3>4. “This looks like cross-entropy” — and you’re not wrong</h3><p>At this point, many engineers have the same reaction:</p><blockquote><em>“We have </em><em>y in {0,1}. We have a prediction-ish thing (distance).<br> Isn&#39;t this just another form of binary cross-entropy?&quot;</em></blockquote><p><strong>Conceptually: yes. Mechanically: not quite.</strong></p><p>Let’s unpack the similarity and the difference.</p><h3>Similarity: both are training a binary decision on pairs</h3><p>Both losses are trying to <strong>separate “same” from “different.”</strong></p><p><strong>Binary cross-entropy says:</strong></p><ul><li>Produce a score s (a logit)</li><li>Turn it into probability with a sigmoid</li><li>Punish wrong probabilities</li></ul><p><strong>Contrastive says:</strong></p><ul><li>Produce embeddings</li><li>Turn them into distance D</li><li>Punish wrong distances</li></ul><p>In both cases, you’re shaping a <strong>boundary</strong> between positives and negatives.</p><p>A simple way to connect them:</p><blockquote><em>If “match score” increases when distance decreases, then cross-entropy and contrastive are solving the same binary problem, just with different choices of how to measure error.</em></blockquote><h3>The key difference: margin “done zone” vs probability “keep nudging”</h3><p><strong>Classic contrastive has a hard idea of success for negatives:</strong></p><blockquote><em>If a negative pair is farther than </em><em>m, we&#39;re done.</em></blockquote><p>That means:</p><ul><li>Negative pairs beyond margin contribute <strong>zero loss</strong></li><li>They also contribute <strong>zero gradient</strong></li><li>Training focuses on <strong>confusing negatives</strong> automatically (those inside the margin)</li></ul><p><strong>Binary cross-entropy doesn’t have that strict “done zone.”</strong></p><p>Even if a negative is already far and the model is confident, cross-entropy still has a (usually tiny) incentive to increase confidence further.</p><p>So <strong>contrastive is fundamentally a margin-based geometric rule</strong>, while <strong>cross-entropy is a probabilistic confidence rule</strong>.</p><h3>Why you should care (practically)</h3><p><strong>Contrastive tends to produce “clean geometry”</strong></p><p>Because it encodes an explicit notion of “far enough.”</p><p>That’s useful when you want <strong>stable thresholds</strong>:<br> <em>“if D &lt; t ⇒ match.”</em></p><p><strong>Cross-entropy tends to produce calibrated probabilities (sometimes)</strong></p><p>If your goal is a <strong>probability of match</strong>, cross-entropy is a natural fit.</p><p>But many similarity systems don’t need calibrated probabilities. They need <strong>good ranking and thresholds</strong>. That’s why margin-based losses stayed popular for so long.</p><h3>5. Engineering Corner: what you choose matters</h3><p>This is where beginners and experts diverge. The intuition is the same, but the “gotchas” live here.</p><h3>1) Distance choice: L2 vs cosine</h3><p><strong>L2 distance</strong> works well when embedding norms are meaningful.</p><p><strong>Cosine similarity</strong> often works better when you normalize embeddings to unit length.</p><p>A very common modern recipe:</p><ul><li>Normalize embeddings (unit vectors)</li><li>Use dot product / cosine similarity</li><li>Tune temperature or margin accordingly</li></ul><h3>2) Margin m: what does it represent?</h3><p><strong>Margin is not magic. It’s literally “how far is far enough.”</strong></p><ul><li><strong>Too small:</strong> negatives don’t get pushed enough → confusions remain</li><li><strong>Too large:</strong> everything gets pushed aggressively → training can become harder</li></ul><p>In practice, margin <strong>interacts strongly</strong> with:</p><ul><li>Embedding dimension</li><li>Normalization</li><li>The distribution of positives/negatives</li></ul><h3>3) The hidden enemy: easy negatives</h3><p>In real datasets, <strong>many negatives are trivially different</strong>.</p><p>If most negatives already satisfy D &gt;= m, they don&#39;t contribute gradients.</p><p>So you can end up training mostly on positives (pulling clusters tight) without learning good separation.</p><p><strong>This is why “sampling” becomes everything later.</strong></p><p>And that is the bridge to Part 2.</p><h3>6. Mini checklist: what you should remember from Part 1</h3><p>If you remember only <strong>seven things</strong>, remember these:</p><ol><li><strong>Similarity problems want geometry, not just labels.</strong></li><li><strong>Siamese networks create embeddings by applying the same encoder twice.</strong></li><li><strong>Contrastive loss is “rubber band for positives + personal space for negatives.”</strong></li><li><strong>Margin means “far enough.” Negatives beyond margin are ignored.</strong></li><li><strong>Cross-entropy and contrastive both separate positives vs negatives, but…</strong></li><li><strong>Contrastive is margin-based (done zone); cross-entropy is probability-based (keep nudging).</strong></li><li><strong>Pairwise training becomes inefficient at scale → you will crave competition.</strong></li></ol><h3>7. Teaser for Part 2: why pairs aren’t enough</h3><p><strong>Pairs are polite. They don’t compete.</strong></p><p>But production similarity is a <strong>ranking problem</strong>:</p><blockquote><em>For a query, can you rank the true match above the impostors?</em></blockquote><p>Once you start thinking like that, you stop training on pairs…<br> <strong>and start training on comparisons.</strong></p><p>In <strong>Part 2</strong>, we introduce <strong>Triplet Loss</strong> — the “anchor, friend, impostor” story — and the real villain behind it:</p><p><strong>Sampling and negative mining.</strong></p><p><em>If this was helpful, follow for Part 2 where we dive into triplet loss, hard negative mining, and why your similarity model might be secretly lazy.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2a7c79bb556" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Questions in Attention — Part 4: Controlling Attention Dynamics and Generalization]]></title>
            <link>https://medium.com/@raghavan99o/deep-questions-in-attention-part-4-controlling-attention-dynamics-and-generalization-818c09983141?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/818c09983141</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Fri, 04 Apr 2025 04:20:09 GMT</pubDate>
            <atom:updated>2025-04-04T04:20:09.193Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deep Questions in Attention — Part 4: Controlling Attention Dynamics and Generalization</h3><h3>Introduction</h3><p>In the earlier parts of this series, we looked at attention from the angles of computation, structure, and representation. Now, we shift focus to <strong>control and generalization</strong>.</p><p>This post tackles two critical questions:</p><ul><li>Can the “sharpness” of attention be <strong>learned</strong> instead of fixed?</li><li>Does attention still work when inputs change dramatically?</li></ul><p>These questions matter if we care about models that are <strong>adaptive</strong>, <strong>robust</strong>, and <strong>more human-like</strong> in how they control and shift their attention.</p><h3>Question 9: What if Attention Sharpness Was Dynamic?</h3><blockquote><em>Can attention adapt how sharp or diffuse it is depending on the query or input context?</em></blockquote><h4>Beginner-Friendly Explanation</h4><p>In transformers, attention weights are computed using softmax over similarity scores:</p><pre>attention_weights = softmax( Q * K_transpose / sqrt(d_k) )</pre><p>The division by sqrt(d_k) acts as a <strong>temperature</strong>. A lower temperature (larger denominator) makes softmax outputs more uniform, while a higher temperature (smaller denominator) makes them more peaky — giving higher weight to the most similar keys.</p><p>This is a <strong>fixed</strong> hyperparameter. But should it be?</p><h4>Why Might We Want This?</h4><ul><li>Some queries require <strong>broad focus</strong> — like when summarizing a paragraph.</li><li>Others demand <strong>sharp, local focus</strong> — like aligning a translation.</li><li>Sequence complexity or uncertainty could guide the model to <strong>adapt</strong> its sharpness.</li></ul><h4>Going Deeper: The Role of Temperature in Attention</h4><p>Temperature in softmax essentially controls the <strong>entropy</strong> of the attention distribution:</p><ul><li>Low temperature: low entropy, sharp peaks (confidence).</li><li>High temperature: high entropy, spread out (uncertainty).</li></ul><p>When temperature is static, we force <strong>one-size-fits-all</strong> behavior. When it’s dynamic, attention becomes <strong>context-sensitive</strong>, choosing when to zoom in or zoom out.</p><h4>Implementing Dynamic Temperature</h4><p>Dynamic temperature could be a function of:</p><ul><li>Query norm (e.g., ||q||)</li><li>Token-level metadata (e.g., part-of-speech, uncertainty estimates)</li><li>A small neural network (e.g., an MLP conditioned on the input)</li></ul><p>This leads to:</p><pre>alpha = softmax( (Q * K_transpose) / temp(Q, X) )</pre><p>Where temp() is a differentiable function learned alongside the model.</p><h4>Real-World Analogies</h4><ul><li>When you’re confident, you fixate your attention (sharp focus).</li><li>When unsure, you consider multiple sources (broad focus).</li></ul><p>We want attention to emulate this dynamic behavior.</p><h4>Benefits</h4><ul><li>Improved performance on <strong>heterogeneous tasks</strong>.</li><li>Potentially more <strong>interpretable attention maps</strong>.</li><li>Allows attention to <strong>self-calibrate</strong>.</li></ul><h4>Existing Literature &amp; Inspiration</h4><ul><li><strong>Learnable temperature</strong> in contrastive learning (e.g., SimCLR, MoCo)</li><li><strong>Adaptive softmax</strong> in classification (Grave et al., 2017)</li><li><strong>T5 pre-normalization</strong> helps indirectly regulate sharpness</li></ul><h4>Open Questions</h4><ul><li>Can we regularize temperature to avoid collapse to extremes?</li><li>Would task-specific or token-specific temperature yield better generalization?</li><li>How can we best interpret or debug a dynamically scaled attention layer?</li></ul><h3>Question 10: How Does Attention Generalize Under Distribution Shift?</h3><blockquote><em>Does attention still highlight the right tokens when the input domain changes?</em></blockquote><h4>Beginner-Friendly View</h4><p>We often visualize attention maps assuming they reflect where the model is “looking”. But that’s only true if the query and key embeddings preserve meaningful geometry.</p><pre>similarity = Q * K_transpose</pre><p>When Q and K are derived from training distributions, all is well. But under distribution shift (e.g., noisy, novel, or adversarial inputs), these embeddings can drift. That leads to <strong>misaligned attention</strong>.</p><h4>Real Scenarios Where This Breaks</h4><ol><li><strong>Noisy text</strong> — ASR errors, misspellings, informal language.</li><li><strong>Domain shift</strong> — Trained on medical data, tested on legal documents.</li><li><strong>Multilingual generalization</strong> — Rare dialects, code-switching.</li><li><strong>Adversarial perturbations</strong> — Intentionally crafted to deceive.</li></ol><h4>What Happens Internally?</h4><ul><li>Q and K vectors become <strong>less semantically reliable</strong>.</li><li>Dot products no longer correlate with meaningful similarity.</li><li>Softmax turns into noise or hyper-confident spikes.</li></ul><p>This can cause attention to:</p><ul><li>Focus on irrelevant tokens.</li><li>Ignore crucial inputs.</li><li>Become diffuse or uninformative.</li></ul><h4>How to Detect This</h4><ul><li>Drop in attention entropy (suggesting over-confidence).</li><li>Sudden increase in uniform or collapsed attention heads.</li><li>Visualization showing attention to padding or junk tokens.</li></ul><h4>Strategies to Improve Robustness</h4><ol><li><strong>Input-level augmentation<br>- </strong>Train with noise, misspellings, or alternative domain data.</li><li><strong>Embedding robustness<br>- </strong>Pretrained encoders fine-tuned on diverse domains.</li><li><strong>Attention regularization<br>- </strong>Entropy-based penalties, dropout on attention logits.</li><li><strong>Structure-aware attention<br>- </strong>Inject syntactic or positional priors that are less input-sensitive.</li></ol><h4>Theoretical View: Attention as Geometry</h4><ul><li>Attention relies on the <strong>geometric space</strong> of queries and keys.</li><li>Distribution shift alters that space.</li><li>Methods like whitening, calibration, or contrastive alignment may help restore meaningful similarity.</li></ul><h4>Can Attention Be Made OOD-Aware?</h4><ul><li>Could we add a confidence estimator to the attention map?</li><li>Could we use external memory to validate top-scoring attention tokens?</li><li>Could we use an ensemble of attention mechanisms to check for consistency?</li></ul><h4>Final Thoughts</h4><p>Attention mechanisms are not inherently robust. Their generalization is only as strong as the <strong>stability of their representations</strong> and the <strong>resilience of their scoring function</strong>.</p><p>To use attention in the real world — across domains, under noise, or interactively — we need to treat it as a dynamic, vulnerable process that needs <strong>regularization, control, and diagnostics</strong>.</p><h3>Wrap-up</h3><p>We explored two critical questions:</p><ol><li>Can attention learn when to zoom in or zoom out? (Dynamic sharpness)</li><li>Can it maintain meaningful focus even under unfamiliar conditions? (Robustness)</li></ol><p>These areas are ripe for experimentation. They also reveal that the simplicity of softmax-based attention hides a surprisingly rich space of design decisions.</p><p>In the next post, we’ll look at how <strong>regularization techniques</strong> can further stabilize and guide attention — both for better performance and for interpretability.</p><p>Stay tuned for <strong>Part 5 of Deep Questions in Attention</strong>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=818c09983141" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Questions in Attention — Part 3: Multi-Modal and Disentangled Attention]]></title>
            <link>https://medium.com/@raghavan99o/deep-questions-in-attention-part-3-multi-modal-and-disentangled-attention-dcae817454df?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcae817454df</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Fri, 04 Apr 2025 04:12:28 GMT</pubDate>
            <atom:updated>2025-04-04T04:12:28.520Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deep Questions in Attention — Part 3: Multi-Modal and Disentangled Attention</h3><h3>Introduction</h3><p>So far, we’ve looked at how attention mechanisms could be computed differently (Part 2) and how structure and task-specific bias can guide attention (Part 3).</p><p>Now, we move into two powerful questions that touch the <strong>expressive capacity</strong> of attention itself:</p><ul><li>Can attention capture <strong>multiple modes</strong> instead of one mean value?</li><li>Can we <strong>disentangle semantic and syntactic roles</strong> in attention?</li></ul><p>These questions open the door to making attention more <strong>modular</strong>, <strong>interpretable</strong>, and <strong>specialized</strong>, potentially mimicking the diversity of human reasoning paths.</p><h3>Question 7: Could Attention Be Multi-Modal?</h3><blockquote><em>Instead of computing a single weighted average over values, can a token attend to multiple </em><strong><em>modes</em></strong><em> simultaneously?</em></blockquote><h4>Beginner Explanation: What’s wrong with one average?</h4><p>In the classic transformer attention model, each token looks at the rest of the sequence and assigns a weight (alpha_ij) to every other token’s value vector. Then it takes a <strong>weighted average</strong> of all those value vectors. This results in one output vector per token.</p><p>This is simple and effective. But the real world isn’t always simple:</p><ul><li>Some words or tokens can relate to <strong>multiple meanings</strong> at once.</li><li>Some inputs contain <strong>disjoint but relevant contexts</strong>.</li></ul><p>A single weighted average might <strong>blur distinct signals</strong> into a vague representation.</p><h4>Deeper Dive: The Need for Multi-Modality</h4><p>Multi-modal attention assumes that one attention vector isn’t enough — we might need <strong>multiple focused summaries</strong>, each pointing to a different “theme” or cluster in the input.</p><p>Think of a sentence like:</p><blockquote><em>“The banker watched the stock market and her favorite thriller movie.”</em></blockquote><p>A token like “watched” relates to <strong>two very different concepts</strong>: finance and film. A multi-modal attention mechanism could:</p><ul><li>Assign some attention to the “stock market” context.</li><li>Assign a separate attention to the “thriller movie” context.</li></ul><p>Each attention mode would produce its own output vector.</p><h4>Analogy: Mixture-of-Experts</h4><p>In large models, we use <strong>mixture-of-experts (MoE)</strong> to route input through different subnetworks based on content. Multi-modal attention is a similar idea:</p><ul><li>Each attention mode behaves like an “expert lens”</li><li>The model learns to <strong>mix and weigh</strong> these lenses depending on the token</li></ul><h4>Advanced Ideas: How Might This Be Implemented?</h4><ol><li><strong>Parallel Heads with Specialized Routing</strong>: Instead of generic multi-head attention, each head could be regularized to specialize in different attention clusters.</li><li><strong>Learned Attention Clusters</strong>: Group keys into clusters and compute separate alpha distributions per cluster.</li><li><strong>Gated Aggregation</strong>: The model could learn to gate different mode outputs before combining them.</li></ol><h4>Benefits:</h4><ul><li>Captures <strong>ambiguity and multi-context tokens</strong></li><li>Could help in <strong>dialog systems, summarization, multimodal grounding</strong></li><li>May improve <strong>interpretability</strong>: each mode can be visualized separately</li></ul><h4>Existing Research &amp; Gaps:</h4><ul><li>Routing Transformers (Roy et al. 2021)</li><li>Mixture of Attention Heads (MoA)</li><li>Not yet fully explored in mainstream architectures</li></ul><h4>Open Research Questions:</h4><ul><li>Can multi-modal attention improve reasoning or compositionality?</li><li>How many modes are enough? Can the model decide?</li><li>Can modes emerge without explicit clustering loss?</li></ul><h3>Question 8: Can Attention Be Disentangled into Semantic and Syntactic Paths?</h3><blockquote><em>Can we design attention to </em><strong><em>separately</em></strong><em> handle meaning and structure?</em></blockquote><h4>Beginner Explanation: What are we mixing today?</h4><p>When we compute attention, we ask: “Which tokens are most relevant to this token?” — but <strong>relevance</strong> can mean many things:</p><ul><li>Relevance in terms of meaning (semantic)</li><li>Relevance in terms of grammar (syntactic)</li><li>Relevance in terms of position (structural)</li></ul><p>Today’s attention heads blend all of these factors into a single attention score. This makes the result <strong>powerful but opaque</strong>.</p><h4>Why Disentangle?</h4><p>Natural language often separates meaning and form:</p><ul><li>In “She ran quickly”, “quickly” modifies the verb.</li><li>In “The cat, which was black, sat on the mat”, the clause structure doesn’t change the subject (“the cat”).</li></ul><p>If attention could <strong>split its focus</strong> across <strong>what</strong> is being said (semantics) and <strong>how</strong> it is structured (syntax), it could:</p><ul><li>Learn more structured representations</li><li>Improve performance on grammar-sensitive tasks</li><li>Enhance interpretability and control</li></ul><h4>How Might It Work?</h4><p>Imagine each attention layer has <strong>two parallel modules</strong>:</p><ol><li><strong>Semantic attention</strong>: Computes relevance based on content similarity (as current models do).</li><li><strong>Syntactic attention</strong>: Computes relevance based on dependency graphs, part-of-speech tags, or latent structural biases.</li></ol><p>Each of these produces a set of weights (alpha_sem, alpha_syn), and the outputs could be:</p><ul><li>Combined using a learned gate</li><li>Used separately in dual streams</li><li>Composed hierarchically (e.g., syntax guides the structure of semantic paths)</li></ul><h4>Advanced Connections:</h4><ul><li>Inspired by <strong>dual-stream processing</strong> in the human brain</li><li>Aligns with <strong>multi-task learning</strong> in NLP (e.g., syntax + semantics)</li><li>Could inform <strong>neural-symbolic hybrid models</strong></li></ul><h4>Existing Work:</h4><ul><li>Syntax-Aware Attention in Machine Translation</li><li>Attention Probing: BERT captures syntax without being told</li><li>Tree Attention Networks</li></ul><h4>Open Research Questions:</h4><ul><li>Can we <strong>supervise</strong> attention heads with syntactic structure?</li><li>Could attention <strong>route</strong> based on token type (e.g., noun vs verb)?</li><li>How do we evaluate success: is disentangled attention actually useful downstream?</li></ul><h3>Wrap-up</h3><p>This post examined how attention could move beyond its original form:</p><ul><li><strong>Multi-modal attention</strong> breaks the single-mean limitation.</li><li><strong>Disentangled attention</strong> opens the door for interpretable and task-specific focus.</li></ul><p>In the next post, we dive into <strong>dynamic and robust attention mechanisms</strong>, including:</p><ul><li>Adaptive sharpness via learned softmax temperature</li><li>Robustness under distribution shifts</li></ul><p>Stay tuned for <strong>Part 4 of Deep Questions in Attention</strong>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcae817454df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Questions in Attention — Part 2: Structuring Attention with Biases and Task-Aware Variants]]></title>
            <link>https://medium.com/@raghavan99o/deep-questions-in-attention-part-2-structuring-attention-with-biases-and-task-aware-variants-d4bc0cc24aaa?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4bc0cc24aaa</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Fri, 04 Apr 2025 03:59:38 GMT</pubDate>
            <atom:updated>2025-04-04T03:59:38.411Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deep Questions in Attention — Part 2: Structuring Attention with Biases and Task-Aware Variants</h3><h3>Introduction</h3><p>In the last post, we explored foundational variations in how attention weights are computed and what would happen if we changed the way queries, keys, and values are derived.</p><p>In this post, we move to <strong>structure-aware and task-specific designs</strong> of attention. Attention is often considered a content-driven mechanism, but what happens when <strong>position</strong>, <strong>monotonicity</strong>, or <strong>hierarchical structure</strong> come into play?</p><h3>Question 4: What if Attention Was Constrained to Be Monotonic?</h3><blockquote><em>In tasks like speech recognition or text-to-speech, input and output sequences often align monotonically. Can attention respect this?</em></blockquote><h4>Standard attention is flexible</h4><p>The default attention mechanism allows every output token to attend to <strong>any</strong> input position, regardless of order. While this is powerful for general tasks, it may <strong>overcomplicate</strong> models designed for naturally ordered domains.</p><h4>Monotonic attention: Left-to-right alignment</h4><p>In monotonic attention, the model is constrained to only look <strong>forward</strong> or <strong>progressively</strong> along the input:</p><ul><li>Forces decoding to move left-to-right.</li><li>Simplifies alignment, especially for speech.</li></ul><h4>Mathematical approaches</h4><p>Several formulations exist:</p><ul><li>Hard monotonic attention (uses discrete step functions)</li><li>Soft monotonic attention (learnable filters, like Gaussian kernels)</li></ul><h4>Advantages</h4><ul><li>Helps interpretability: we know where the model is “reading” from.</li><li>Stabilizes training in autoregressive settings.</li><li>Especially useful in sequence-to-sequence tasks with natural temporal ordering.</li></ul><h4>Reference:</h4><ul><li><em>Monotonic Attention Mechanisms for Sequence-to-Sequence Learning</em> — Raffel et al., 2017</li></ul><h3>Question 5: Could We Make Attention Content-Free but Position-Sensitive?</h3><blockquote><em>Can attention be driven only by </em><strong><em>where</em></strong><em> tokens appear and not </em><strong><em>what</em></strong><em> they are?</em></blockquote><h4>Why would we do this?</h4><p>In some domains, <strong>positional structure</strong> matters more than content:</p><ul><li>Music composition</li><li>Protein sequences</li><li>Procedural data (e.g., instructions or scripts)</li></ul><h4>Standard attention blends position and content</h4><p>Most Transformer models combine content and position like this:</p><pre>input_embedding = content_embedding + positional_encoding</pre><p>This fusion makes it hard to separate <strong>structural</strong> attention from <strong>semantic</strong> attention.</p><h4>Position-only attention</h4><p>Imagine designing attention weights that depend <strong>only</strong> on relative or absolute position:</p><ul><li>Content is ignored</li><li>Query attends to key based on how far apart they are</li></ul><h4>How is this useful?</h4><ul><li>Gives strong <strong>inductive bias</strong> where structure is critical</li><li>Enables attention to mimic fixed pattern behaviors (like convolutional filters)</li><li>Reduces model complexity in certain low-data regimes</li></ul><h4>Real-world examples</h4><ul><li><em>ALiBi</em> (Attention with Linear Biases) encodes distance directly in the attention score</li><li><em>Relative Positional Encoding</em> (Shaw et al.) separates content from position</li></ul><h4>Open ideas</h4><ul><li>Could we learn <strong>two separate attentions</strong>: one over position, one over content?</li><li>Could position-only attention serve as a backbone in hybrid models?</li></ul><h3>Question 6: Can Attention Be Hierarchical?</h3><blockquote><em>In long documents or videos, should attention happen in two stages: local and then global?</em></blockquote><h4>The challenge</h4><p>Standard attention is quadratic in sequence length. For long sequences (e.g., 2k tokens), this is memory and compute intensive.</p><h4>Hierarchical attention: Two-level focus</h4><ol><li><strong>First stage</strong>: Attend within small, local windows (e.g., sentence or block)</li><li><strong>Second stage</strong>: Attend over the summaries of each window</li></ol><p>This mimics how humans read:</p><ul><li>We process paragraphs line-by-line</li><li>Then summarize to get the document’s meaning</li></ul><h4>Technical implementations</h4><ul><li>Hierarchical Attention Networks (HANs) in NLP</li><li>Longformer uses dilated attention plus global attention tokens</li><li>Routing transformers propose token clustering followed by global mixing</li></ul><h4>Benefits</h4><ul><li>Scalable to long sequences</li><li>Captures local coherence and global structure</li><li>Enables compression of intermediate representations</li></ul><h4>Where it shines</h4><ul><li>Multi-document summarization</li><li>Dialogue modeling</li><li>Code completion (where nesting matters)</li></ul><h4>Open question</h4><ul><li>Can we learn when to apply local vs global attention <strong>dynamically</strong>?</li><li>Could reinforcement learning help in selecting hierarchical paths?</li></ul><h3>Wrap-up</h3><p>This post explored how <strong>structural biases</strong> — like order, position, and hierarchy — can enhance or constrain attention for better task alignment.</p><p>In the next part, we’ll move into <strong>diversifying attention heads</strong>, exploring questions like:</p><ul><li>Can attention learn to focus on multiple modes?</li><li>Can we split attention into syntactic vs semantic heads?</li></ul><p>Stay tuned for <strong>Part 3 of Deep Questions in Attention</strong>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4bc0cc24aaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Questions in Attention — Part 1: Foundational Variations and Alternatives (Deep Dive)]]></title>
            <link>https://medium.com/@raghavan99o/deep-questions-in-attention-part-1-foundational-variations-and-alternatives-deep-dive-58b549e082c5?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/58b549e082c5</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[attention-mechanism]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Wed, 02 Apr 2025 08:37:19 GMT</pubDate>
            <atom:updated>2025-04-02T08:37:19.761Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deep Questions in Attention — Part 1: Foundational Variations and Alternatives (Deep Dive)</h3><h3>Introduction</h3><p>In the earlier post (See: Attention Basics and Scaling — refer to previous blog), we discussed the essential mechanics of attention: queries (Q), keys (K), values (V), and the softmax-based weighting scheme.</p><p>In this post, we take a <strong>deep dive</strong> into foundational variations and question some of the fundamental design choices. Could attention have been designed differently? What might we gain or lose by making such changes?</p><h3>Question 1: What if Attention Weights were Learned Directly Instead of Being Computed from Q and K?</h3><p><strong>The standard design:</strong> In modern attention, attention weights alpha_ti are computed as a function of the query and key vectors:</p><pre>alpha_ti = softmax( Q * K_transpose / sqrt(d_k) )</pre><p><strong>The alternative:</strong> What if we learned alpha_ti directly as parameters, treating attention weights as a table of learned numbers?</p><h4>Why is this tempting?</h4><ul><li>It would simplify attention to just learning a weight matrix.</li><li>No need to compute similarities at inference time.</li></ul><h4>The problem:</h4><ol><li><strong>Loss of input adaptation:<br>- </strong>Learned alpha_ti would be fixed regardless of the input content.<br>- Every input would be attended to in the exact same way.</li><li><strong>Loss of generalization:<br></strong>- How would this work with sequences of unseen lengths or novel token combinations?<br>- You would need one alpha matrix for every possible sequence length.</li><li><strong>Extreme inefficiency:<br></strong>- This approach cannot scale with dynamic-length inputs.</li></ol><ul><li><strong>Static attention patterns</strong> exist in fixed-graph neural networks or convolutional models.</li><li>Some attention mechanisms, like <strong>sparse attention</strong>, do partially specify attention patterns but still condition on the input.</li></ul><h4>Takeaway:</h4><p>The power of attention comes from being <strong>input-conditional</strong>. It dynamically adapts based on the content of Q and K, enabling generalization to new inputs.</p><h3>Question 2: Why is Attention Additive in Bahdanau but Multiplicative (Dot-Product) in Transformers?</h3><h4>Additive Attention (Bahdanau, 2015):</h4><pre>e_ti = v_a_transpose * tanh( W_a * s_{t-1} + U_a * h_i )</pre><h4>Multiplicative Attention (Transformer, Vaswani et al., 2017):</h4><pre>e_ij = Q * K_transpose / sqrt(d_k)</pre><h4>What’s the difference?</h4><ol><li><strong>Additive attention</strong> computes a score using a learned MLP-like operation.</li><li><strong>Dot-product attention</strong> computes similarity directly via an inner product.</li></ol><h4>Why did Transformers switch?</h4><ul><li>Dot-product is more efficient due to matrix multiplication.</li><li>For high-dimensional Q and K, dot-product attention is numerically similar to additive attention.</li><li>It is friendlier for GPUs and TPUs.</li></ul><h4>Geometric Interpretation:</h4><ul><li>Dot-product attention measures <strong>angular similarity</strong> between Q and K vectors.</li><li>Additive attention can learn arbitrary non-linear similarity functions.</li></ul><h4>Trade-offs:</h4><ul><li><strong>Additive:</strong> More flexible, potentially better in low-dimensional settings.</li><li><strong>Dot-product:</strong> Faster, highly parallelizable, suitable for deep networks.</li></ul><h4>Is additive attention dead?</h4><p>Not really. In speech recognition and alignment tasks, additive attention is still competitive. Some modern architectures also revisit hybrid additive-dot approaches.</p><h3>Question 3: What if Q, K, and V were Computed Nonlinearly Instead of by Linear Projections?</h3><h4>Current setup:</h4><pre>Q = X * W_Q<br>K = X * W_K<br>V = X * W_V</pre><h4>Alternative:</h4><pre>Q = f_theta(X)<br>K = g_phi(X)<br>V = h_psi(X)</pre><p>where f, g, h are small neural networks (non-linear).</p><h4>Potential benefits:</h4><ul><li>Could allow Q, K, V to capture <strong>higher-order features</strong>.</li><li>Could model non-linear interactions between inputs more effectively.</li></ul><h4>Potential risks:</h4><ul><li>Training becomes more unstable due to extra depth.</li><li>Computational cost increases significantly.</li><li>May overfit small datasets.</li></ul><h4>Related works:</h4><ul><li>Some papers have explored <strong>Neural Attention Layers</strong> where Q, K, and V come from MLPs.</li><li>Results are mixed — often, the added complexity is not worth the performance gain.</li></ul><h4>Why Transformers kept it linear:</h4><ul><li>The complexity is deferred to the <strong>Feedforward Network (FFN)</strong> layers.</li><li>Linear Q, K, V allow for faster attention computation via optimized matrix multiplications.</li></ul><h4>Open question:</h4><p>Is it time to revisit non-linear Q, K, V now that we have stronger regularization and optimization techniques? Could this benefit <strong>vision</strong>, <strong>speech</strong>, or <strong>multimodal</strong> transformers more than NLP?</p><p>In the next part, we will explore:</p><ul><li>Monotonic Attention</li><li>Position-Only Attention</li><li>Hierarchical Attention</li></ul><p>as we continue unraveling <em>Deep Questions in Attention</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58b549e082c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Attention Regularization, Stability, and Open Research Questions]]></title>
            <link>https://medium.com/@raghavan99o/attention-regularization-stability-and-open-research-questions-2fe2e0ad2d2d?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/2fe2e0ad2d2d</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Mon, 31 Mar 2025 06:14:51 GMT</pubDate>
            <atom:updated>2025-03-31T06:14:51.585Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><blockquote>Attention works well, but is it always stable? Can we make it better? Are there research questions still open?</blockquote><p>Absolutely. In this blog, we dive into <strong>regularization</strong>, <strong>stability</strong>, and some <strong>open-ended research directions</strong> in attention mechanisms — a must-read for anyone who wants to go beyond textbook understanding.</p><h3>Regularizing Attention</h3><h4>1. Entropy Regularization</h4><blockquote>Why might attention be too sharp or too flat?</blockquote><p>Attention weights sometimes become <strong>too peaky</strong> (overconfident) or <strong>too flat</strong> (uncertain).</p><p>The <strong>entropy</strong> of an attention distribution is defined as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*DlT2cQ5ehhQ9RfaGhq1l8g.png" /></figure><ul><li><strong>High entropy</strong> → attention is spread out (less confident).</li><li><strong>Low entropy</strong> → attention is concentrated (more confident).</li></ul><p>By adding entropy regularization to the loss:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*CH9TMzxLQr-ysWuYIW3c2A.png" /></figure><p>You control the distribution shape:</p><ul><li>Increase λ to encourage <strong>diverse</strong>, smoother attention.</li><li>Decrease λ or use negative λ to promote <strong>peaky</strong> attention.</li></ul><p><strong>In practice:</strong></p><ul><li>Some tasks benefit from sharp attention (e.g., alignment tasks).</li><li>Others (summarization, sentiment analysis) benefit from distributed attention.</li></ul><p>Entropy regularization is a flexible tool to tune attention behaviour depending on your task.</p><h4>2. Sparsity Regularization</h4><blockquote>Should attention always be soft?</blockquote><p>Sometimes, we want attention to be <strong>selective</strong> — assigning zero weight to irrelevant tokens.</p><p>One way to enforce this is using an L_1 penalty:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*MMtXyIUXpUGnIjvJt4jOpQ.png" /></figure><p>Alternatively, you can replace softmax with <strong>Sparsemax</strong> or <strong>Entmax</strong>:</p><ul><li><strong>Sparsemax</strong>:<br>- Projects logits directly onto the simplex.<br>- Some alpha_i become exactly zero.</li><li><strong>Entmax</strong>:<br>- Generalizes both softmax and sparsemax.<br>- Controls sparsity smoothly.</li></ul><p><strong>Benefits:</strong></p><ul><li>Enhances interpretability.</li><li>Reduces noise by ignoring less useful tokens.</li><li>Encourages models to be more decisive.</li></ul><h4>3. Diversity Regularization (Multi-Head Attention)</h4><blockquote>Do multiple heads learn the same thing?</blockquote><p>Sometimes, yes! Without constraints, attention heads may become <strong>redundant</strong>.</p><p>You can penalize head similarity via:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*oxzhD1e27XAo8f-4ABCujg.png" /></figure><p>This encourages each head to focus on <strong>different parts</strong> of the input.</p><p><strong>Benefits:</strong></p><ul><li>Avoids wasted computation.</li><li>Captures complementary information.</li><li>Leads to better downstream performance.</li></ul><p>Some recent papers even explore <strong>orthogonality constraints</strong> to push heads further apart in their learned patterns.</p><h3>Attention and Scale Changes</h3><blockquote>What happens if hidden state scales change during inference?</blockquote><p>In dot-product attention:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/230/1*ofrapIidek1zWqD1yMywaQ.png" /></figure><p>If the norms of q_i or k_j increase (e.g., due to input distribution shifts):</p><ul><li>Attention becomes <strong>too sharp</strong>, collapsing focus onto a few tokens.</li></ul><p>Transformers solve this via:</p><ol><li><strong>Scaling</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*Ly6BDaJiR5FNNe2uuPPuvA.png" /></figure><ol><li><strong>LayerNorm</strong> to stabilize hidden state statistics.</li></ol><p>Both together help attention maintain its behaviour across train/test conditions.</p><h3>Monotonic and Position-Only Attention</h3><blockquote>Could we make attention more structured?</blockquote><p>Yes! For some tasks, it helps to restrict or guide attention.</p><h4>Monotonic Attention:</h4><ul><li>Forces attention to <strong>progress left-to-right</strong>.</li><li>Suitable for speech recognition and alignment-heavy tasks.</li><li>Ensures each decoding step looks ahead only minimally.</li></ul><h4>Position-Only Attention:</h4><ul><li>Attends purely based on <strong>positional encodings</strong>, ignoring content.</li><li>Used in music generation, prosody modelling, and time-series.</li><li>Enforces models to pay attention to structural patterns.</li></ul><p>Combining position-only attention with content-based attention leads to <strong>hybrid attention</strong> mechanisms that perform well in structure-sensitive domains.</p><h3>Practical Tips for Stable Attention Models</h3><ol><li>Apply <strong>LayerNorm</strong> both pre- and post-attention.</li><li>Use <strong>scaled dot-product</strong> attention.</li><li>Monitor and visualize attention heatmaps during training.</li><li>Use <strong>entropy regularization</strong> if attention collapses.</li><li>Use <strong>diversity regularization</strong> to encourage heads to specialize.</li><li>Consider <strong>sparse attention</strong> if interpretability is important.</li></ol><h3>Open Research Questions (for the Curious Mind)</h3><ol><li><strong>Can we learn the softmax temperature dynamically?<br> </strong>Could the model decide on its own how sharp or smooth attention should be?</li><li><strong>2. Can attention be hierarchical and recursive?<br></strong>Could attention operate over chunks, phrases, or paragraphs instead of flat tokens?</li><li><strong>Can we disentangle syntactic and semantic attention heads?<br></strong>Could we design attention to separate grammar and meaning more explicitly?</li><li><strong>What does attention actually learn in massive models?<br></strong>Can we systematically interpret the functions of attention heads?</li><li><strong>Can attention universally replace recurrence and convolution?<br></strong>Are there tasks where convolution or recurrence is still superior?</li></ol><p>These are active research areas explored in recent conferences such as NeurIPS, ICLR, and ACL.</p><p>In the next post, we’ll explore a simple pytorch implementation of attention module.</p><p>Stay tuned to <em>Attention Unlocked</em>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2fe2e0ad2d2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Attention Scaling, QKV, and the Transformer Revolution]]></title>
            <link>https://medium.com/@raghavan99o/attention-scaling-qkv-and-the-transformer-revolution-3b26c27b54d8?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b26c27b54d8</guid>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Sun, 30 Mar 2025 13:33:32 GMT</pubDate>
            <atom:updated>2025-04-02T03:20:54.365Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><blockquote>Bahdanau attention worked well, so why did Transformers change it? What’s up with Q, K, V? Why is scaling needed?</blockquote><p>These are exactly the questions that led to the <strong>Transformer Revolution</strong>. In this post, we’ll walk through how attention evolved from Bahdanau’s formulation to the more scalable, parallelizable, and elegant design that became the backbone of GPT, BERT, and other modern models.</p><h3>The Scaling Issue in Dot-Product Attention</h3><blockquote>So, why change Bahdanau attention?</blockquote><p>Bahdanau attention relied on an MLP with a tanh non-linearity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*57bp3475_BvSY16TtyVcAg.png" /></figure><p>While effective, this:</p><ul><li>Was hard to <strong>parallelize</strong>.</li><li>Relied on <strong>RNNs</strong>, which are sequential by nature.</li><li>Wasn’t efficient for <strong>very long sequences</strong>.</li></ul><p>The natural replacement? A <strong>simple dot-product</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/209/1*VE0izjF_tBS2GUcJc5TI8Q.png" /></figure><p>Where q_i and k_j are vectors.</p><blockquote>Isn’t this too simple?</blockquote><p>Not exactly. It works beautifully but introduces a subtle problem:</p><ul><li>When the dimensionality d_k is large, the magnitude of dot_product(q_i, k_j) grows.</li><li>The softmax function then becomes <strong>overconfident</strong>, producing very sharp distributions.</li></ul><h3>Solution: Scaled Dot-Product Attention</h3><p>The fix is simple but elegant:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*BghwID8-4KRng7_xKEPiBQ.png" /></figure><blockquote>Why does this help?</blockquote><p>Since dot_product(q_i k_j) has variance proportional to d_k, dividing by sqrt(d_k) stabilizes the variance and prevents softmax from saturating.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2GJj1frM7b3SBFQWBWTP6Q.png" /></figure><ul><li>Unscaled softmax assigns most of the attention weight to just one or two tokens.</li><li>Scaled softmax produces a more balanced distribution across tokens.</li></ul><h3>Why Q, K, V?</h3><blockquote>In Bahdanau attention, we just had h_i and s_{t-1}. Where did Q, K, V come from?</blockquote><p>The <strong>Transformer</strong> generalizes attention by explicitly projecting input vectors into three separate spaces:</p><ul><li>Query: q_i = dotproduct(W_Q,x_i)</li><li>Key: k_i = dotproduct(W_K,x_i)</li><li>Value: v_i = dotproduct(W_V,x_i)</li></ul><blockquote>Why bother?</blockquote><p>This makes attention more <strong>flexible</strong> and <strong>decouples</strong> the input’s role:</p><ul><li>A token can serve as both a <strong>query</strong> and a <strong>key/value</strong>, depending on where it’s used.</li><li>This naturally enables <strong>self-attention</strong>, where tokens can attend to other tokens in the same sequence.</li></ul><h3>The Evolution from Bahdanau to Transformer</h3><p>Bahdanau:</p><ul><li>Used RNNs.</li><li>Attention was <strong>decoder-driven</strong>, using the decoder’s hidden state to query the encoder states.</li></ul><p>Transformer:</p><ul><li>Removed RNNs entirely.</li><li>Attention is now <strong>position-wise</strong> and <strong>parallelizable</strong>.</li><li>Every token generates its own Q, K, and V vectors.</li></ul><p>This design led to massive speedups and scalability — making models like BERT and GPT possible.</p><h3>Additive vs Dot-Product Attention</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*JxNb5Dj_n7vAUbTH9DZ-7g.png" /></figure><blockquote>Intuition:<em> Additive attention is like a learned, smooth similarity function, while dot-product attention is a direct geometric similarity (angle + scale).</em></blockquote><h3>Why does self-attention work intuitively? Shouldn’t each token attend mostly to itself?</h3><blockquote>Wouldn’t it make more sense if every token just paid attention to itself? Why bother attending to others?</blockquote><p>At first glance, it seems logical that a token should focus mainly on its own information. However, <strong>self-attention</strong> is powerful exactly because it lets tokens incorporate <strong>context</strong>.</p><p>In practice:</p><ul><li><strong>Tokens often attend significantly to themselves</strong> but also to their neighbors.</li><li>This lets the representation of each token get enriched with information about its <strong>surrounding words</strong>.</li><li>For example, the word “bank” in “The bank is near the river” needs to look at “river” to disambiguate whether it’s a financial institution or a riverbank.</li></ul><blockquote>In fact, attention patterns often show that tokens attend strongly to themselves AND to nearby words.</blockquote><p>This is why self-attention is so effective:</p><ul><li>It creates <strong>contextualized embeddings</strong>.</li><li>Each token’s vector now encodes not just itself, but information gathered from the entire sentence.</li><li>This is what makes Transformers so good at tasks like translation, summarization, and even reasoning.</li></ul><h3>Why Transformers Work So Well</h3><ul><li>Faster: No RNNs, just matrix multiplications.</li><li>Flexible: Q, K, V projections enable various attention types (self, cross, encoder-decoder).</li><li>Scalable: Can be stacked into deep layers.</li></ul><h3>📚 Next Blogs in the Attention Series</h3><p><a href="https://medium.com/@raghavan99o/attention-regularization-stability-and-open-research-questions-2fe2e0ad2d2d">Attention Regularization, Stability, and Open Research Questions </a>— Discusses current challenges and open problems related to attention mechanisms.Stay tuned to <em>Attention Unlocked</em> as we keep scaling up!</p><p>Stay tuned to <em>Attention Unlocked</em>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b26c27b54d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Common Beginner Questions About Attention (Answered Deeply)]]></title>
            <link>https://medium.com/@raghavan99o/common-beginner-questions-about-attention-answered-deeply-e7645cdc4fff?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7645cdc4fff</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Fri, 28 Mar 2025 15:11:59 GMT</pubDate>
            <atom:updated>2025-04-02T03:19:08.223Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oi-kR3bddw7u2XzKdcjvTA.png" /></figure><h3>Introduction</h3><blockquote>I get the big picture of attention, but I have so many “small” questions. Is softmax really necessary? Why sum? Can we just attend to nouns?</blockquote><p>If you’ve had these thoughts, you’re not alone. In fact, these are the <strong>right questions</strong> to ask to go beyond just knowing what attention is, to really understanding <strong>how and why</strong> it works.</p><p>In this post, we’ll address <strong>6 common beginner doubts</strong> about attention — each leading to some deep insights.</p><h3>1. Why softmax? Why not min-max or others?</h3><blockquote>Can’t we just scale alignment scores with min-max or normalize in other ways?</blockquote><p>Great question. You could, but softmax has some very special properties:</p><ul><li>It is <strong>smooth</strong> and <strong>differentiable everywhere</strong>, allowing stable gradient flow.</li><li>It produces a <strong>probability distribution</strong> — all attention weights sum to 1.</li><li>It is <strong>exponentially sensitive</strong> to larger alignment scores.</li></ul><blockquote>Intuition:<em> This exponential behavior is useful because it lets the model give high confidence to certain tokens when needed, while still softly distributing attention when unsure.</em></blockquote><p><strong>Min-max scaling</strong>, for example, only shifts and rescales the values linearly and often results in non-sum-to-one attention weights, losing the nice probabilistic interpretation.</p><p>In fact, softmax behaves like a <strong>soft-argmax</strong>, letting the model blend hard decisions with soft uncertainty.</p><h3>2. Why sum encoder hidden states? Could we max or do something else?</h3><blockquote>Why do we take a weighted sum? Why not pick the “max-attended” token?</blockquote><p>Using a <strong>weighted sum</strong> (soft attention) is key because:</p><ul><li>It allows the model to gather <strong>information from multiple relevant tokens</strong>, not just the most relevant.</li><li>It makes the entire attention process <strong>differentiable</strong> — so gradients can flow smoothly.</li></ul><blockquote>What if we used max?</blockquote><p>Taking a <strong>hard max</strong> (also called hard attention) would:</p><ul><li>Lose information from other potentially useful tokens.</li><li>Be <strong>non-differentiable</strong>, making backpropagation impossible without tricks.</li></ul><p>The sum lets the model behave more like a <strong>soft aggregator</strong> that can decide how much to consider each token, instead of making a hard choice.</p><p>This is also why <strong>Gumbel-softmax</strong> or <strong>reinforcement learning</strong> methods were explored to approximate hard attention, but soft attention turned out to be much more stable.</p><h3>3. Could we attend only to nouns/verbs?</h3><blockquote>In languages, aren’t nouns and verbs the most important? Why not attend only to them?</blockquote><p>While it’s tempting, attention learns <strong>what matters</strong> automatically.</p><p>Hard-coding attention to only focus on nouns/verbs would:</p><ul><li>Prevent the model from attending to important function words (negations like <em>not</em>, auxiliaries, or particles).</li><li>Inject <strong>task-specific bias</strong> which might hurt generalization.</li></ul><blockquote>Intuition:<em> The power of attention is that it discovers patterns </em><strong><em>data-driven</em></strong><em> — if nouns/verbs matter, the model will naturally give them higher attention.</em></blockquote><p>Interestingly, in domains like <strong>document summarization</strong> or <strong>dialogue systems</strong>, function words and discourse markers often play critical roles, even if they aren’t content words.</p><h3>4. Can attention learn to ignore irrelevant tokens?</h3><blockquote>What if there’s garbage tokens in the input?</blockquote><p>Actually, yes! Attention is designed to <strong>down-weight</strong> irrelevant tokens.</p><p>During training, if a token never helps reduce the loss, its attention weight tends to stay close to <strong>zero</strong>.</p><blockquote>Example:<em> Padding tokens almost always get negligible attention in practice.</em></blockquote><p>This is one of attention’s <strong>biggest strengths</strong> — it learns not just what to focus on, but also what to ignore.</p><h3>5. Why do we need to combine the context vector and decoder hidden state? Couldn’t we just use the context?</h3><blockquote>Since the context vector c_t already summarizes the input, why involve s_t at all?</blockquote><p>Because c_t only captures <strong>relevant input information</strong>, but s_t carries information about <strong>what has been generated so far</strong>.</p><blockquote>Intuition:<em> Imagine you’re translating a sentence, and you look back at the source. Just the information you find is not enough — you also need to remember what you’ve already written.</em></blockquote><p>Combining c_t and s_t gives the model both “What do I need from the input?” and “What have I said so far?” — crucial for generating <strong>coherent</strong> and <strong>grammatical</strong> outputs.</p><h3>6. Can attention weights be too sharp or too flat? Does it matter?</h3><blockquote>What if attention always puts everything on one token or spreads too much?</blockquote><p>Great observation! Yes, attention weights sometimes become <strong>too peaky</strong> (overconfident) or <strong>too flat</strong> (unsure).</p><ul><li><strong>Peaky attention</strong> risks ignoring useful secondary information.</li><li><strong>Flat attention</strong> could dilute focus and weaken signal strength.</li></ul><p>Some researchers use <strong>entropy regularization</strong> to prevent overconfidence, or even adopt alternatives like <strong>Sparsemax</strong> or <strong>Entmax</strong> to control sparsity.</p><p>But, most modern models tend to learn a good balance naturally if trained properly.</p><h3>Takeaway</h3><p>The most beautiful thing about attention is not just that it helps models focus, but that it does so <strong>flexibly</strong>, <strong>smoothly</strong>, and <strong>automatically</strong>, without us having to dictate which words matter.</p><h3>📚 Next Blogs in the Attention Series</h3><p>If you’re new here here are the next blogs in this series on Attention Mechanisms and their evolution:</p><ol><li><a href="https://medium.com/@raghavan99o/attention-scaling-qkv-and-the-transformer-revolution-3b26c27b54d8">Attention Scaling, QKV, and the Transformer Revolution</a> — A deep dive into how attention is scaled using Query, Key, Value (QKV) concepts in transformers.</li><li><a href="https://medium.com/@raghavan99o/attention-regularization-stability-and-open-research-questions-2fe2e0ad2d2d">Attention Regularization, Stability, and Open Research Questions </a>— Discusses current challenges and open problems related to attention mechanisms.Stay tuned to <em>Attention Unlocked</em> as we keep scaling up!</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7645cdc4fff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scaling Up Attention — From Bahdanau to Transformers]]></title>
            <link>https://medium.com/@raghavan99o/scaling-up-attention-from-bahdanau-to-transformers-325a953afae3?source=rss-c89e4638a116------2</link>
            <guid isPermaLink="false">https://medium.com/p/325a953afae3</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Raghavan]]></dc:creator>
            <pubDate>Fri, 28 Mar 2025 14:30:19 GMT</pubDate>
            <atom:updated>2025-04-02T03:18:08.600Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scaling Up Attention — From Bahdanau to Transformers</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ArEND8lOPh5DLdVeEU6P9w.png" /></figure><h3>Introduction</h3><blockquote>So we’ve seen Bahdanau attention, but how did it evolve into what we now call the “Transformer”?</blockquote><p>Excellent question! In this post, we’ll see <strong>why</strong> and <strong>how</strong> attention was scaled and restructured to form the core of Transformers, which power today’s state-of-the-art models like GPT, BERT, and T5.</p><h3>The Problem with Bahdanau Attention</h3><blockquote>Why not just keep using Bahdanau Attention?</blockquote><p>Bahdanau attention works well but has <strong>two major limitations</strong>:</p><ol><li>It’s tied to the <strong>RNN decoder</strong> structure.</li><li>It requires an <strong>MLP</strong> (via the tanh) to compute alignment scores.</li></ol><p>This makes it:</p><ul><li>Hard to parallelize.</li><li>Less suitable for very long sequences.</li></ul><h3>The Shift: Dot-Product Attention</h3><blockquote>So what’s the alternative?</blockquote><p>The Transformer introduced a simpler attention mechanism called <strong>dot-product attention</strong>. Instead of using an MLP, it uses the raw dot-product between vectors.</p><p>For a given query q_i and key k_j:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*MHOdd5Xw_TZo9gfqyeLLLQ.png" /></figure><blockquote>Intuition:<em> This is just computing the </em><strong><em>similarity</em></strong><em> between the query and key vectors.</em></blockquote><h3>Queries, Keys, and Values (QKV)</h3><blockquote>Where do q_i and k_j come from?</blockquote><p>The input tokens are first passed through <strong>learnable linear layers</strong> to get:</p><ul><li>Query: q_i = W_Q x_i</li><li>Key: k_i = W_K x_i</li><li>Value: v_i = W_V x_i</li></ul><p>So each token now has a query, key, and value vector.</p><blockquote>Why?</blockquote><p>This <strong>decouples</strong> the attention mechanism from being tied to just encoder-decoder settings. Now, any token can query any other token — essential for <strong>self-attention</strong>.</p><h3>Step 1: Compute Scaled Dot-Product</h3><blockquote>Why “scaled”?</blockquote><p>In high dimensions, the dot-product magnitude can grow large. To avoid softmax saturation, we scale by sqrt(d_k):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*eBOh3RsfFfYVo366d72d0A.png" /></figure><h3>Step 2: Softmax Over Scores</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*n-wxgR1oeWpfQ6q4uM5TWA.png" /></figure><blockquote>Intuition:<em> This gives us attention weights — where each token i decides </em><strong><em>how much</em></strong><em> to attend to token j.</em></blockquote><h3>Step 3: Compute Context Vector</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*FvceEIxjpmyt1cTSc20qOQ.png" /></figure><blockquote>Intuition:<em> Each token gathers information from others via a </em><strong><em>weighted sum</em></strong><em>.</em></blockquote><h3>Why Transformers Dropped RNNs</h3><blockquote>Why no more RNNs?</blockquote><p>Because with this setup:</p><ul><li>Every token attends to every other token directly.</li><li>There is <strong>no sequential dependency</strong> during attention computation.</li><li>Computation can be fully <strong>parallelised</strong>.</li></ul><p>This allowed Transformers to scale up to <strong>very long sequences</strong> efficiently.</p><h3>Why This Works So Well</h3><ul><li>Attention is now just matrix multiplications — fast and parallelizable.</li><li>No need for recurrent connections.</li><li>Direct access to the full sequence at every layer.</li></ul><p>This design is now the backbone of almost every modern NLP model.</p><h3>📚 Next Blogs in the Attention Series</h3><p>If you’re new here here are the next blogs in this series on Attention Mechanisms and their evolution:</p><ol><li><a href="https://medium.com/@raghavan99o/common-beginner-questions-about-attention-answered-deeply-e7645cdc4fff">Common Beginner Questions About Attention (Answered!)</a> — Answers to the most frequently asked questions about attention.</li><li><a href="https://medium.com/@raghavan99o/attention-scaling-qkv-and-the-transformer-revolution-3b26c27b54d8">Attention Scaling, QKV, and the Transformer Revolution</a> — A deep dive into how attention is scaled using Query, Key, Value (QKV) concepts in transformers.</li><li><a href="https://medium.com/@raghavan99o/attention-regularization-stability-and-open-research-questions-2fe2e0ad2d2d">Attention Regularization, Stability, and Open Research Questions </a>— Discusses current challenges and open problems related to attention mechanisms.Stay tuned to <em>Attention Unlocked</em> as we keep scaling up!</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=325a953afae3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>