<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DeVenture Academy</title>
    <description>The latest articles on DEV Community by DeVenture Academy (@deventureacademy).</description>
    <link>https://dev.to/deventureacademy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4258218%2F021c5a3a-a19d-43ca-8c75-84c36867c4e9.png</url>
      <title>DEV Community: DeVenture Academy</title>
      <link>https://dev.to/deventureacademy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deventureacademy"/>
    <language>en</language>
    <item>
      <title>Three attention bugs that still run fine (and a way to catch them)</title>
      <dc:creator>DeVenture Academy</dc:creator>
      <pubDate>Sun, 29 Nov 2026 08:44:30 +0000</pubDate>
      <link>https://dev.to/deventureacademy/three-attention-bugs-that-still-run-fine-and-a-way-to-catch-them-2pnp</link>
      <guid>https://dev.to/deventureacademy/three-attention-bugs-that-still-run-fine-and-a-way-to-catch-them-2pnp</guid>
      <description>&lt;p&gt;This is for people who already write Python and are learning how LLMs work underneath. If you're new to programming, this one will be frustrating, so bookmark it for later.&lt;/p&gt;

&lt;p&gt;Most attention tutorials stop at the formula. The hard part comes when you write it yourself, because a broken attention layer doesn't crash. It returns a matrix of the right shape, full of plausible numbers, and you find out it was wrong much later, if ever.&lt;/p&gt;

&lt;p&gt;Here's a correct version in NumPy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;softmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keepdims&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keepdims&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;scaled_dot_product_attention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;K&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;dk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Q&lt;/span&gt; @ &lt;span class="no"&gt;K&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;weights&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;softmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;weights&lt;/span&gt; @ &lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weights&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And three bugs that all still run:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Forgetting &lt;code&gt;/ np.sqrt(dk)&lt;/code&gt;.&lt;/strong&gt; The scores grow with the dimension, softmax saturates towards one-hot, and gradients die. On small test inputs it can look almost right.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Softmax over the wrong axis.&lt;/strong&gt; Each &lt;em&gt;query's&lt;/em&gt; weights should sum to 1, so softmax goes over the last axis. Use &lt;code&gt;axis=0&lt;/code&gt; and every column sums to 1 instead. Same shape, wrong answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Not subtracting the max before &lt;code&gt;np.exp&lt;/code&gt;.&lt;/strong&gt; Fine on toy inputs, &lt;code&gt;inf&lt;/code&gt; and &lt;code&gt;nan&lt;/code&gt; on real ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking your own version
&lt;/h2&gt;

&lt;p&gt;I built a small lab for exactly this. You write the function, and a checker runs it against a reference implementation on inputs you haven't seen, then shows what yours got vs what it expected for every check that fails. No signup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx deventure start 02-self-attention-from-scratch-lab
npx deventure verify
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  I'm looking for testers
&lt;/h2&gt;

&lt;p&gt;The lab is part of a site I'm building, &lt;a href="https://deventureacademy.com/?utm_source=devto&amp;amp;utm_campaign=testers-attention" rel="noopener noreferrer"&gt;DeVenture Academy&lt;/a&gt;: short written lessons on AI engineering (LLMs, RAG, agents, and the models underneath), each ending in code you write and a check that tells you whether it's right.&lt;/p&gt;

&lt;p&gt;People sign up and read, but I haven't yet watched someone I don't know get all the way through a lab, and the labs are the whole point. So I'm looking for about 10 people who write Python to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;do the lab above and a couple of lessons, about 30 minutes, before Sunday 6 December&lt;/li&gt;
&lt;li&gt;tell me the first moment you got confused or wanted to close the tab&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In return you get full access free. Leave a comment or message me, sign up with any email, and I'll upgrade your account.&lt;/p&gt;

</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>ai</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
