<?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 Eng Chong Yock on Medium]]></title>
        <description><![CDATA[Stories by Eng Chong Yock on Medium]]></description>
        <link>https://medium.com/@engchongyock?source=rss-14e4c1acd289------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vYvnzON3-vroTMHjq8WXIg.png</url>
            <title>Stories by Eng Chong Yock on Medium</title>
            <link>https://medium.com/@engchongyock?source=rss-14e4c1acd289------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:58:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@engchongyock/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[Making Sense of Modern Hopfield Networks and Attention]]></title>
            <link>https://medium.com/@engchongyock/making-sense-of-modern-hopfield-networks-and-attention-64a87223ac1c?source=rss-14e4c1acd289------2</link>
            <guid isPermaLink="false">https://medium.com/p/64a87223ac1c</guid>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[hopfield-network]]></category>
            <dc:creator><![CDATA[Eng Chong Yock]]></dc:creator>
            <pubDate>Tue, 30 Jun 2026 15:54:04 GMT</pubDate>
            <atom:updated>2026-06-30T15:54:04.804Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Reference.</strong> This derivation is based on <em>Hopfield Networks is All You Need</em> (Ramsauer <em>et al.</em>, 2020)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iIj1sl7_VeAuQRL6IYBxUg.png" /></figure><p>Transformer attention can be viewed as a form of associative memory. Given a query vector, attention retrieves the most relevant information from a set of stored patterns by computing similarity scores and taking a weighted average. Modern Hopfield Networks describe this same process from an optimization perspective. Instead of defining retrieval as an attention operation, they define an energy function whose minima correspond to stored memories. Memory retrieval then becomes the problem of minimizing this energy. In this article, we’ll derive the Modern Hopfield retrieval rule step by step and show that a single retrieval update is mathematically equivalent to the transformer attention update.</p><h3>The Gradient of LogSumExp</h3><p>Before deriving the Hopfield update rule, we’ll need one important result. The gradient of the LogSumExp function is the softmax function. This identity is the key step in the derivation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kENaddRHlrW-FbDwppvk2Q.png" /></figure><h3>The Modern Hopfield Energy Function</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*24ymNsmoea-Hde0-_9yBDA.png" /></figure><p><strong>Note.</strong> The original paper includes two additional constant terms,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kWoc7blI1gar-0eZqE44eQ.png" /></figure><p>where N is the number of stored patterns and M bounds their norm. These terms are constant with respect to the state z, so they have zero gradient. Since they do not affect the minimizer or the retrieval update, <strong>we omit them throughout this derivation for simplicity.</strong></p><p>The first term measures how well the current state z matches the stored memories. Each inner product is a similarity score between the current state and the i-th memory. The LSE function smothly emphasises the memories with the highest similarity.</p><p>The second function is a quadratic regularizer. It prevents the energy from decreasing without bound and ensures the optimization problem has a well-defined minimum.</p><p>The first term attracts the state toward stored memories, while the quadratic term keeps the energy well-behaved. We’ll see why in the simple case of a single stored memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mGTeJ-GjhrHlrueS6KDb_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7oG5eD3cAO1bypvCbcX1UA.png" /></figure><p>The quadratic term prevents this by making the energy grow for large z, producing a well-defined minimum at the stored pattern.</p><h3>CCCP: Turning Energy Minimization into an Update Rule</h3><p>We now derive an iterative update rule for minimizing the energy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BsZKVlJRVYm0Wef0T4eITg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5HfYb9DRqDTHr3z7T2PkLQ.png" /></figure><p>Note: Key property of concave functions is that a function f(x) is concave if its graph always lies below any of its tangent lines. This means the tangent line is a <strong>global upper bound</strong> on the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XSbL7iR0bMr9ecCi7FstHQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nw8hl1ILAiUpE3bkdtmhVA.png" /></figure><p>This shows that the next state is obtained by following the negative gradient of the concave part of the energy.</p><h3>Gradient of the Concave Term</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VsyZNAruhovITjxTfMFMpw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6eadZRjKURS2buecQhIgyg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*09Pi3C5bwVnH1X8ZJybApg.png" /></figure><h3>From CCCP to Attention</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l6cNMfsvD_O4mU2HdYwlEA.png" /></figure><p>This is exactly the attention mechanism.</p><p>P.S. The equations have been re-generated using GPT to ensure consistent formatting, since rendering mathematical expressions in Medium can be difficult.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64a87223ac1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making Sense of Variational Autoencoders]]></title>
            <link>https://medium.com/@engchongyock/making-sense-of-variational-auto-encoders-c35ab92ddbe7?source=rss-14e4c1acd289------2</link>
            <guid isPermaLink="false">https://medium.com/p/c35ab92ddbe7</guid>
            <dc:creator><![CDATA[Eng Chong Yock]]></dc:creator>
            <pubDate>Tue, 23 Jun 2026 12:57:35 GMT</pubDate>
            <atom:updated>2026-06-23T13:05:47.351Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kpusjUiGZZ_gKUKdbpIHBA.png" /></figure><p>This article is based heavily on <a href="https://www.youtube.com/watch?v=iwEzwTTalbg">Umar Jamil’s tutorials and resources</a>. All credit goes to his amazing work in making these complex ideas accessible and practical. The illustrations used throughout this article are generated directly with GPT. Although incorporating mathematical equations into these visuals is still quite challenging, the purpose of this article is to present VAEs through an intuitive flow and pipeline, making the concepts easier to follow — for myself and, hopefully, for others as well.</p><h3>Important Formulas</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_VEiTiZtWvHSx0UY5WkbnA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kViF-t6KXAfptN39VENQWA.png" /></figure><h3>Step 1 — The goal</h3><p>We want to maximize the probability of the observed data: p(x). Assume data is generated from a hidden latent variable z.</p><h3>Step 2 — Marginalizing Over the Latent Variable</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SZgRnf6QdwqObC5B0PMV1w.png" /></figure><p>Note: A prior probability is your best guess about how likely something is before seeing any new information.</p><h3>Step 3 — The Posterior Is Intractable</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HIJlM7nACodf5M4MwOY5kA.png" /></figure><p>Note: Posterior probability is the updated probability of an event or hypothesis occurring after new evidence or data is taken into account.</p><h3>Step 4 — Deriving the ELBO</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Kj9CSbGWzvTV7k6sQ9t2w.png" /></figure><h3>Step 5 — Interpreting the ELBO</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2v_gfgaZttb91yr_fCi8UQ.png" /></figure><h3>Step 6 — Gradient Problem</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLFTAzt66htiA1eS1JvqAQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3daDiULC65CZmoZ0bSbKkA.png" /></figure><p>Notice how the issue is that we have <strong>two</strong> terms after differentiating.</p><h3>Step 7 — Selecting an Estimator</h3><p>Biased vs Unbiased Estimator</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_ZrX33XzWYbFpiE4hwllg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XxH21BajXGGsBgAWNe9crg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B5Y_LSIk_wkUislpH1lcmA.png" /></figure><p>Hence, when choosing an estimator, we first need to check whether it is <strong>unbiased</strong>, meaning that its expectation equals the true gradient. We also prefer estimators with <strong>low variance</strong>, since variance affects the stability and speed of convergence. A high-variance estimator produces noisier updates and typically converges more slowly. However, as long as it remains unbiased, its updates still point toward the correct solution on average.</p><h3>Option 1— Score Function Estimator (REINFORCE)</h3><p>First option is that we can use log derivative trick, so we can estimate the expectation using samples from q_ϕ​(z)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V6y9r_-9X15Hew2lXuZNEg.png" /></figure><p>Proof that score function estimator is <strong>unbiased</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*od_xpo1Hkl3qYk1Zxj81Ng.png" /></figure><p>High variance comes from estimating gradients using scattered samples, and from the score term getting large in the tails due to the log.</p><h3>Option 2— Reparameterized Estimator (SGVB)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PBWrWBv3LoNh6RH2_UGaFg.png" /></figure><p>Proof that reparameterized estimator is <strong>unbiased</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5UMJxbZ2Zo2Pj2tK3HKpPw.png" /></figure><h3>Step 8— Why One Sample Is Enough</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YKbfOnUISbwyfz7516xAOA.png" /></figure><p>Proof that one-sample gradient estimator is unbiased</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8_3XUHyproviPnVHNeetgg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k6LXRfQj8CEzeUP2cZhSHw.png" /></figure><p>Apply this to the gradient</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBDb9uUuWPwpIs6qVZEaLg.png" /></figure><h3>Step 9— Closed-Form VAE Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*duO7cRCfr8AcKsZhN_dUeQ.png" /></figure><h3>Final VAE Objective</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*azL1vcj8IVThxa6uBn2Wog.png" /></figure><h3>Conclusion</h3><p>We started with one blunt goal — make the real data likely — and ended with a loss function.</p><ol><li>We couldn’t model p(x) directly, so we marginalized over a hidden z. That made the posterior intractable, so we approximated it with an encoder.</li><li>We couldn&#39;t maximize the log-likelihood, so we maximized its lower bound.</li><li>We couldn&#39;t push gradients through a random sample, so we reparameterized it.</li><li>We couldn&#39;t average over infinitely many samples, so we used one ( the unbiasedness argument told us was allowed ).</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c35ab92ddbe7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making Sense of Induction Heads]]></title>
            <link>https://medium.com/@engchongyock/making-sense-of-induction-heads-3f2178604341?source=rss-14e4c1acd289------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f2178604341</guid>
            <dc:creator><![CDATA[Eng Chong Yock]]></dc:creator>
            <pubDate>Wed, 09 Jul 2025 14:51:17 GMT</pubDate>
            <atom:updated>2025-07-09T14:51:17.026Z</atom:updated>
            <content:encoded><![CDATA[<p>Proof of K-composite induction heads in toy 2L attention-only model</p><p><strong>This article is based heavily on </strong><a href="https://arena-ch1-transformers.streamlit.app/[1.2]_Intro_to_Mech_Interp"><strong>Callum McDougall’s tutorials and resources</strong></a><strong> from the TransformerLens library, especially on induction heads and interpretability. All credit goes to his amazing work in making these complex ideas accessible and practical.</strong></p><p>From experiments, we could observe 4 types of heads.</p><ul><li>Heads attending to current token: L0H9</li><li>Heads attending to the previous token: L0H7</li><li>Heads attending to the first token: L0H3, L1H4, L1H10</li><li>Induction heads: L1H4, L1H10</li></ul><p>Hypothesis is that there are 2 major components</p><ol><li>Previous Token Head (ie. L0H7) — QK circuit makes sure each token attends to its immediate predecessor and the OV circuit writes a copy of the previous token into a different subspace.</li><li>Induction Head (ie. L1H4 or L1H10) — QK circuit of L1H4 attends to the source token, defined as: the token that comes after a previous occurrence of the current token’s previous token and the OV circuit simply copies the value of the source token.</li></ol><p>What do we need to prove and how ?</p><ol><li>QK circuit of L0H7 attends to the previous token</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/0*1G8ZeQ3wRwYLj_zn" /></figure><p>The attention map of this should reflect each current token attending to its previous token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*X6d5TN-Xo7gb_rKM21__CQ.png" /></figure><p>2. OV circuit of L1H4 is copies the value of the source token</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*bP-y2Eb8LDhs7pCbVi9irg.png" /></figure><p>The OV circuit (e.g., of L1H4) performs a near-identity transformation on the value vectors, meaning the token it attends to gets copied into the output. This results in logit attribution heatmaps that are diagonally dominant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*shJuM6x9WC0KI71qV0vxHA.png" /></figure><p>3. OV circuit of L0H7 determining the key vector in L1H4’s QK circuit</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*uRFSmGwovtpZsajCQ52GGw.png" /></figure><p>To put it simply, we want to split the activations up to see how individual components contribute to the final attention score for L1H4. We can do this by splitting up the residual stream into its individual component (embedding, positional embedding and each L0 head).</p><p>With the decomposed attention score, we are most interested in the attention score contributions when query is token embedding and key is L0H7.</p><p>We compute the standard deviation of each (query_component, key_component) pair’s contribution across all token positions. A high std indicates that component meaningfully varies across the sequence and thus contributes significantly to the attention pattern. For L1H4, we should see biggest contribution from L0H7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*SxXO0w39RWUfq_sUaqqz3g.png" /></figure><p>We explored how induction heads work in a 2-layer attention-only model. We found that head L0H7 copies information from the previous token, and heads L1H4 and L1H10 use this to find and copy useful patterns. By breaking down how queries and keys are formed, we saw that these heads work together to help the model continue repeated sequences. This shows how different attention heads can combine to perform meaningful tasks in a clear and understandable way.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f2178604341" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making Sense of the Multi-head Attention Mechanism]]></title>
            <link>https://medium.com/@engchongyock/making-sense-of-the-multi-head-attention-mechanism-a62c58be082e?source=rss-14e4c1acd289------2</link>
            <guid isPermaLink="false">https://medium.com/p/a62c58be082e</guid>
            <category><![CDATA[multihead-attention]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Eng Chong Yock]]></dc:creator>
            <pubDate>Mon, 05 May 2025 15:38:21 GMT</pubDate>
            <atom:updated>2025-05-05T15:38:21.778Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Visual summary of the multi-headed attention mechanism, adapted from Umar Jamil’s explanation in his video “Attention is All You Need (Transformer)” on YouTube" src="https://cdn-images-1.medium.com/max/1024/1*7VM1a7OC6B4f1Cp0ViHEwA.png" /></figure><p>You’ve probably come across tons of publicly available implementations of Multi-Head Attention (MHA) by now, so I’ll skip the boilerplate code. Instead, let’s talk about <em>why</em> each component exists — especially the trainable matrices W_Q, W_K, W_V​ — and build some intuition for queries, keys, and values.</p><p>Here’s an analogy I find helpful.</p><p>Imagine a classroom with 4 students. Each of them wants to improve their personal notes by discussing with the rest of the group.</p><p>Each student brings three things to the discussion:</p><ol><li>A <strong>question</strong> they want answered (this is their <strong>Query</strong>).</li><li>A <strong>summary</strong> of their existing notes (this is their <strong>Key</strong>).</li><li>The <strong>information</strong> they are willing to contribute (this is their <strong>Value</strong>).</li></ol><p>Some students may keep certain things to themselves (so the Value might not contain everything), but what they contribute is enough to enrich others.</p><p>Let’s start with Student 1. Using their <strong>question</strong>, they compare it with each student’s <strong>summary</strong> — including their own — to see whose notes seem most relevant. This comparison gives a <strong>similarity score</strong>, like this:</p><p>[0.8, 0.2, 0.4, 0.6]</p><p>These scores represent how relevant each student’s notes are to the current question. Student 1 then uses a <strong>softmax</strong> function on these scores to get weights — how much attention to pay to each student’s contribution (including his own):</p><p>[0.3, 0.2, 0.2, 0.3]</p><p>Now, to create a new and improved note, Student 1 blends everyone’s contributions according to these weights:</p><blockquote><em>New Note = 0.3 × Value_1 + 0.2 × Value_2 + 0.2 × Value_3 + 0.3 × Value_4</em></blockquote><p>The other students go through the same process using their own questions.</p><h4>Queries, Keys, and Values in Practice</h4><p>So in neural terms:</p><ul><li><strong>Query (Q)</strong> = what you’re looking for</li><li><strong>Key (K)</strong> = what you have</li><li><strong>Value (V)</strong> = what you’re willing to share</li></ul><p>The attention score is computed via <strong>dot product of Q and K</strong>, which tells us how well each key matches the query. Then we apply softmax to get attention weights. Finally, we use these weights to combine the values — this is what gives us the final representation.</p><p>All of this is done efficiently through matrix multiplication.</p><h4>Why Do We Need W_Q, W_K, W_V?</h4><p>Well, imagine if the students always asked the same default question, summarized their notes the same way, and shared the same information. The discussion wouldn’t be very productive. Everyone would end up with the same notes.</p><p>By introducing these <strong>trainable matrices</strong>, we allow the model to:</p><ul><li>Form better, context-specific <strong>questions</strong> (via <strong>W_Q</strong>​)</li><li>Craft more informative <strong>summaries</strong> (via <strong>W_K</strong>​)</li><li>Decide what <strong>information</strong> is most valuable to share (via <strong>W_V</strong>​)</li></ul><h3>Wrapping Up:</h3><p>At its core, Multi-Head Attention is just a smart way for tokens (or students!) to talk to each other and decide what matters most. By using queries, keys, and values — plus those all-important trainable matrices — the model learns how to “study” better with each layer.</p><h3>References:</h3><ul><li>Sebastian Raschka, <em>Build a Large Language Model (From Scratch)</em></li><li><a href="https://youtu.be/bCz4OMemCcA?si=FTZbKNRAr6ck9H4B">Jamil, Umar. <em>Attention is All You Need (Transformer) — Model Explanation (Including Math), Inference and Training</em>. YouTube, uploaded by Umar Jamil.</a></li></ul><h3>Authors Note:</h3><p>This is my first post on Medium, and it’s been both a learning experience and a labor of love. If you found this explanation helpful or have any feedback, I’d genuinely appreciate hearing from you. Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a62c58be082e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>