<?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 Shaina Verma on Medium]]></title>
        <description><![CDATA[Stories by Shaina Verma on Medium]]></description>
        <link>https://medium.com/@shainaverma?source=rss-b8454ba40007------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Shaina Verma on Medium</title>
            <link>https://medium.com/@shainaverma?source=rss-b8454ba40007------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:36:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shainaverma/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[Understanding Attention in Transformers — Classroom Analogy]]></title>
            <link>https://medium.com/@shainaverma/understanding-attention-in-transformers-classroom-analogy-4a910020598f?source=rss-b8454ba40007------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a910020598f</guid>
            <dc:creator><![CDATA[Shaina Verma]]></dc:creator>
            <pubDate>Fri, 18 Jul 2025 06:38:06 GMT</pubDate>
            <atom:updated>2025-07-18T07:09:37.510Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding Attention in Transformers — Classroom Analogy</h3><p><strong>Abstract</strong></p><p>Despite being a core building block of modern language models like BERT, GPT, and T5, the attention mechanism remains a black box for many students, practitioners, and even some researchers entering the field of deep learning. Its mathematical formulation — full of dot products, matrix multiplications, and Softmax operations — can seem abstract and disconnected from intuition.</p><p><a href="https://arxiv.org/abs/1706.03762">Attention</a> is what gives Transformers their unique ability to understand context. It allows models to weigh the importance of each word in a sentence relative to others, making it possible to capture long-range dependencies and nuanced meanings — capabilities that traditional RNNs or CNNs struggled with.</p><p>This article aims to bridge that gap in understanding. By introducing a classroom analogy, we translate the dense technical process into a familiar human scenario — making attention not only easier to understand, but also easier to remember.</p><p><strong>Introduction</strong></p><p>Let’s imagine each word in a sentence is a <strong>student</strong> in a classroom. Each student is trying to understand the full meaning of the sentence — but to do that, they need to <strong>pay attention to what the other students are saying</strong>.</p><p>Here’s how that process works, step by step.</p><h3><strong>Step 1 -Every student gets a role</strong></h3><p>Imagine a classroom where each <strong>student represents a word</strong> in a sentence.</p><p>Each student has:</p><pre>A profile (a vector of numbers describing the word).<br>A question in mind - what they want to understand.<br>An answer key - what they can offer.<br>A note - what information they carry.</pre><p>In Transformer terms, these are:</p><ul><li>Input vector → the student’s <em>profile</em></li><li><strong>Query (Q)</strong> → the student’s <strong><em>question</em></strong></li><li><strong>Key (K)</strong> → the student’s <strong>answer</strong></li><li><strong>Value (V)</strong> → the student’s information <strong><em>note</em></strong></li></ul><p>These Q, K, and V are obtained by multiplying the input vector with trained matrices. Think of them as special lenses 👓 that help a student think differently depending on whether they’re asking, answering, or writing notes.</p><h3><strong>Step 2 - How Student Pays Attention</strong></h3><p>Let’s walk through how one student pays attention to others by taking an example of sentence <em>“The cat ate the fish.” </em>Now word ‘ate’ is the student we are focusing on in this scenario.</p><h4>1. Creating Queries, Keys, and Values</h4><p>Each student (word) has a profile — a numeric representation of its meaning.</p><p>This profile is transformed in <strong>three different ways</strong>, creating:</p><ul><li><strong>Query (Q)</strong> — What question am I asking?</li><li><strong>Key (K)</strong> — What kind of questions can I answer?</li><li><strong>Value (V)</strong> — What knowledge am I carrying?</li></ul><p>These are created using three learned matrices: Q, K, and V — like putting on different glasses to see the world from a specific lens.</p><h4>2. Matching Questions with Answers</h4><blockquote>Now student “ate” wants to understand the context. So it:</blockquote><blockquote>- Takes its <strong>Query</strong>.</blockquote><blockquote>- Compares it to the <strong>Keys</strong> of all students (including itself).</blockquote><blockquote>- Calculates how <em>similar</em> the query is to each key.</blockquote><p>This is done using a <strong>dot product</strong>, which gives us a <strong>score</strong> — a number that says:<br> <em>“How relevant is this word to my understanding?”</em></p><h4>3. Turning Scores into Attention Weights</h4><p>Now “ate” has a list of relevance scores — but they’re just raw numbers. To make them useful, we pass them through a <strong>SoftMax function</strong>, which:</p><ul><li>Turns them into <strong>probabilities</strong></li><li>Makes all the weights <strong>positive</strong></li><li>Ensures they <strong>add up to 1</strong></li></ul><p>These are now the <strong>attention weights</strong></p><p>Now “ate” knows <em>how much attention to pay</em> to each student (word) in the class.</p><h4>4. Gathering the Notes (Values)</h4><p>Each word carries a <strong>Value</strong> — think of it as a note about its meaning.</p><blockquote>Now, “ate” goes to each student, takes their note (Value), multiplies it by the attention weight, and <strong>adds them all up</strong>.</blockquote><p>The result is a single, blended note — a <strong>summary of the whole class</strong>, tailored specifically to what “ate” was looking for.</p><p>This gives “ate” a final <strong>summary of relevant context</strong> from the class.</p><h4>5. Adding Original + New Info</h4><p>This new summary is <strong>added</strong> to “ate’s” original profile — so it keeps its original identity but now understands itself in context.</p><p>We then:</p><ul><li><strong>Normalize</strong> the result (to stabilize learning),</li><li><strong>Pass it through a small neural network</strong> (like extra thinking),</li><li>And <strong>add + normalize again</strong> (residual learning).</li></ul><p>The result? “Ate” now has a smarter, context-aware representation of itself.</p><h4>6. Thinking Deeper: Feed Forward + Residual</h4><p>This final vector is passed through a small neural network (called a <strong>feed-forward network</strong>). We again:</p><ul><li>Add the input of this step to its output (residual connection).</li><li>Normalize once more.</li></ul><p>The result? A smarter, context-aware version of “ate”.</p><h3>This Happens for Every Student (Word)</h3><p>Every word in the sentence plays the same game. Each one:</p><ul><li>Looks at the others.</li><li>Decides who to listen to.</li><li>Builds its meaning accordingly.</li></ul><p>This parallel attention is what makes Transformers so powerful and efficient.</p><p><strong>Why It Matters</strong></p><p>Before Transformers, models like <strong>RNNs and LSTMs</strong> processed text sequentially — word by word. That made it hard for them to remember earlier words when dealing with long sentences.</p><p><strong>Attention changed the game</strong>:</p><p>It allows every word to look at every other word — all at once. It doesn’t matter if two words are far apart. This parallelism makes training faster and understanding deeper and that’s the heart of a Transformer.</p><p><strong>Future Reading</strong></p><ol><li><a href="https://arxiv.org/abs/1706.03762"><em>[1706.03762] Attention Is All You Need</em></a></li><li><a href="https://jalammar.github.io/illustrated-transformer/">The Illustrated Transformer — Jay Alammar — Visualizing machine learning one concept at a time.</a></li><li><a href="https://peterbloem.nl/blog/transformers">Transformers from scratch | peterbloem.nl</a></li></ol><p>In our next post, we’ll explore how Transformers don’t stop at one student’s view — they use <strong>multiple attention heads</strong> to look at language from many angles at once. Stay tuned for:<br> <strong>“Multi-Head Attention — A Group Discussion Analogy.”</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a910020598f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>