<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[MLWhiz: Recs|ML|GenAI]]></title><description><![CDATA[Making ML careers accessible and GenAI, Recsys, and MLOps understandable. 🔧 No-fluff guides and real-world insights to help you build, deploy, and advance in the machine learning and Generative AI ecosystem]]></description><link>https://www.mlwhiz.com</link><image><url>https://substackcdn.com/image/fetch/$s_!jdCB!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79ca48ed-d331-477b-aa19-029389751190_500x500.png</url><title>MLWhiz: Recs|ML|GenAI</title><link>https://www.mlwhiz.com</link></image><generator>Substack</generator><lastBuildDate>Tue, 28 Jul 2026 06:05:23 GMT</lastBuildDate><atom:link href="https://www.mlwhiz.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Rahul Agarwal]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[mlwhiz@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[mlwhiz@substack.com]]></itunes:email><itunes:name><![CDATA[Rahul Agarwal]]></itunes:name></itunes:owner><itunes:author><![CDATA[Rahul Agarwal]]></itunes:author><googleplay:owner><![CDATA[mlwhiz@substack.com]]></googleplay:owner><googleplay:email><![CDATA[mlwhiz@substack.com]]></googleplay:email><googleplay:author><![CDATA[Rahul Agarwal]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Post-Training 101: From Base Model to Assistant]]></title><description><![CDATA[genAI Fundamentals Part 5: instruction tuning (SFT), RLHF vs DPO, and the RLVR/GRPO reasoning era &#8212; how a base model learns to follow instructions and which answers people actually prefer.]]></description><link>https://www.mlwhiz.com/p/post-training-101-from-base-model</link><guid isPermaLink="false">https://www.mlwhiz.com/p/post-training-101-from-base-model</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Tue, 07 Jul 2026 22:48:10 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/3fd42fff-5563-4f7d-b7d3-1230dbfbc274_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p><em>This is part of the genAI Fundamentals series. Each post picks one building block of modern LLMs and explains it from first principles, with code.</em></p><div><hr></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Px4x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Px4x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!Px4x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!Px4x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!Px4x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Px4x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1420562,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/205910834?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Px4x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!Px4x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!Px4x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!Px4x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81525184-e7b0-4350-87af-58decbf4c753_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here's a fact that is surprising to almost everyone new to LLMs: the giant model that comes out of pretraining isn't a chatbot at all. As <a href="https://www.mlwhiz.com/p/pretraining-101-data-scale-and-the">we saw at the end of Pretraining 101, you get a base model &#8212; not an assistant</a>. <mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">After trillions of tokens and millions of dollars, what comes out the other end can complete text beautifully and won't, on its own, actually answer you.</mark></p><p>Ask a raw base model "What is the capital of France?" and it might not say "Paris." A very plausible continuation is <em>another question</em> &#8212; "What is the capital of Germany? What is the capital of Italy?" &#8212; because on the open web, that line usually appears in a list of quiz questions, not in a helpful reply. The base model isn't broken. It's doing exactly what it was trained to do: continue the pattern.</p><p>So the knowledge is in there. Paris is in there, along with grammar, code, and a startling amount of the internet. <mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">What's missing is the </mark><em><mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">behavior</mark></em><mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);"> of being a helpful assistant &#8212; answering instead of continuing, following instructions, staying on task, knowing when to refuse.</mark> That behavior gets installed in a second phase, and it's a much shorter and cheaper one. It's called <strong>post-training</strong>, and it's where ChatGPT, Claude, and DeepSeek-R1 actually get made.</p><p>By the end of this post, you'll understand the whole modern playbook: <em>instruction tuning (SFT), the reward-model route (RLHF), the shortcut that took over (DPO), its unpaired cousin (KTO), and the new reasoning layer (RLVR with GRPO) that powers models like DeepSeek-R1</em> &#8212; with the actual math worked through on real numbers, plus a simple guide to which one to reach for and when.</p><p><mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">This is where the whole series culminates. How does everything work all together to create an assistant you can talk with?</mark></p><p>The mental model I'd start with: <mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">a base model is a brilliant, impossibly well-read intern who has read every book in the library but has never once been taught how to answer a question or hold a conversation.</mark> Pretraining filled their head. Post-training teaches them manners.</p><p>Let's dive in.</p><div><hr></div><h2>1. The Two Jobs of Post-Training</h2><p>Post-training has two distinct jobs.</p><p>The first job is <strong><mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">behavior</mark></strong>: teach the model to act like an assistant &#8212; to answer the question instead of extending it, follow the instruction it's given, speak in the chat format, and stop when it's actually done. This is about <em>form</em>.</p><p>The second job is <strong><mark data-color="#d9ead3" style="background-color: rgb(217, 234, 211); color: rgb(0, 0, 0);">preference</mark></strong>: teach the model which answer is <em>better</em>. For almost any real prompt, there are many valid responses, and they're not equally good &#8212; one is more helpful, more honest, less likely to make something up, better-toned. Pretraining never taught the model to rank; it only taught it to predict what comes next. This is about <em>taste</em>.</p><p>Those two jobs map onto the modern post-training stack:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Wvif!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Wvif!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 424w, https://substackcdn.com/image/fetch/$s_!Wvif!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 848w, https://substackcdn.com/image/fetch/$s_!Wvif!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 1272w, https://substackcdn.com/image/fetch/$s_!Wvif!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Wvif!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png" width="1200" height="290.1098901098901" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:352,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:502980,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/205910834?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Wvif!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 424w, https://substackcdn.com/image/fetch/$s_!Wvif!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 848w, https://substackcdn.com/image/fetch/$s_!Wvif!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 1272w, https://substackcdn.com/image/fetch/$s_!Wvif!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59e26b8f-f27e-4f71-846f-532d698848c3_5372x1300.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><ul><li><p><strong>Instruction tuning (SFT)</strong> handles behavior.</p></li><li><p><strong>Preference tuning</strong> (RLHF or DPO) handles taste.</p></li><li><p>An optional <strong>RLVR</strong> layer &#8212; Reinforcement Learning from Verifiable Rewards (GRPO and friends) &#8212; adds genuine reasoning on tasks where you can check the answer.</p></li></ul><p>Two things are worth holding onto before we open each box. First, all of this is cheap compared to pretraining. Where pretraining burned months of compute across thousands of GPUs, post-training runs on human-curated data that's tiny by comparison &#8212; thousands to low millions of examples, not trillions of tokens. The base model did the expensive work of learning the world; post-training just shapes how it behaves.</p><p>Second, there's no single recipe anymore. A few years ago you could say "pretrain, then RLHF" and be roughly right. In 2026 that's about as complete as describing a car as "the thing with an engine." Every major model &#8212; DeepSeek-R1, the Llama and Qwen families, the frontier closed models &#8212; uses a <em>different</em> mix of these layers. Post-training has become a modular stack, and the interesting work is in how you combine the pieces. So let's understand the pieces.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/post-training-101-from-base-model">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[Pretraining 101: Data, Scale, and the Loss Function]]></title><description><![CDATA[GenAI Fundamentals Series Part 4: How a pile of random weights becomes a base model. Next-token cross-entropy, trillion-token data pipelines, and compute budgets]]></description><link>https://www.mlwhiz.com/p/pretraining-101-data-scale-and-the</link><guid isPermaLink="false">https://www.mlwhiz.com/p/pretraining-101-data-scale-and-the</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Fri, 26 Jun 2026 20:55:02 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!t31p!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p><em>This is part of the genAI Fundamentals series. Each post picks one building block of modern LLMs and explains it from first principles, with code.</em></p><div><hr></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t31p!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t31p!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!t31p!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!t31p!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!t31p!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t31p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1424632,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/203747062?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t31p!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!t31p!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!t31p!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!t31p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb7ab20c-0112-4dcb-8108-ae3c04a71d89_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the last post, we ended on a tidy one-liner: <a href="https://www.mlwhiz.com/p/what-is-an-llm-tokens-embeddings">we said an LLM is just a function: token IDs in, next-token probabilities out</a>. It&#8217;s a clean, satisfying way to put it &#8212; but I quietly skipped the single most important word in that sentence.</p><p><em>Trained.</em></p><p>When you first build that function, every weight in it is random &#8212; the embeddings, the transformer layers, the output head, all of it meaningless static. Feed a brand-new, freshly-initialized model the prompt &#8220;The capital of France is&#8221; and it won&#8217;t say &#8220; Paris&#8221; &#8212; it&#8217;ll pick a token essentially at random, maybe &#8220; marmalade,&#8221; maybe &#8220; 7,&#8221; maybe &#8220; the.&#8221; Every token in the vocabulary is roughly equally likely, because the model has never seen a single sentence in its life.</p><p>So here&#8217;s the question this whole post answers: how do we get from a pile of random numbers to a model that completes &#8220;The capital of France is&#8221; with &#8220; Paris&#8221; &#8212; and, along the way, picks up grammar, facts, a bit of arithmetic, and enough code to autocomplete your Python?</p><p>The answer is <strong>pretraining</strong>. And the beautiful thing is that it comes down to one loss function, computed over a few trillion tokens, a few trillion times. There are no human labels and no clever supervision anywhere in it &#8212; the model just guesses the next token, checks the answer, nudges its weights, and does it all over again.</p><p>By the end of this post you&#8217;ll understand four things, which also happen to be the four things that decide whether a pretrained model is any good:</p><ol><li><p><strong>The loss function</strong> &#8212; what cross-entropy actually measures, walked through one training step.</p></li><li><p><strong>The data</strong> &#8212; where 15 trillion tokens come from and why the pipeline matters more than the model code.</p></li><li><p><strong>The compute</strong> &#8212; the simple FLOP math behind a training run, and why these runs cost tens of millions of dollars.</p></li><li><p><strong>The scaling laws</strong> &#8212; why Chinchilla proved that a smaller model trained on more data beats a bigger model trained on less.</p></li></ol><p>And finally, what you actually get when the run finishes &#8212; and why it isn&#8217;t ChatGPT.</p><blockquote><p><strong>Follow along in code.</strong> There&#8217;s a companion notebook that pretrains a tiny GPT from scratch on a real public-domain book (pulled from Project Gutenberg) &#8212; it runs on a laptop CPU in a few minutes, and every concept below maps to a cell. <a href="https://www.kaggle.com/code/mlwhiz/pretraining-from-scratch">Grab it on Kaggle</a>.</p></blockquote><p>Let&#8217;s dive in.</p><div><hr></div><h2>1. From Random Noise to a Base Model</h2><p>Pretraining is the first, longest, and most expensive phase of building an LLM. It has exactly one job: take a model full of random weights and teach it to predict the next token across a giant, generic pile of text &#8212; a single objective, repeated at a scale that&#8217;s genuinely hard to picture.</p><p>The clever part &#8212; the thing that makes the whole modern LLM era possible &#8212; is that this learning is <strong>self-supervised</strong>. Here&#8217;s why that matters.</p><p>In a normal supervised setup, you need labeled examples: a photo <em>and</em> a human-written &#8220;cat,&#8221; a transaction <em>and</em> a human-written &#8220;fraud.&#8221; Labels are expensive: a human has to make each one, which puts a hard ceiling on how much data you can learn from.</p><p>Pretraining sidesteps the ceiling entirely. The trick: <strong>any piece of text is already its own answer key.</strong> Take a sentence:</p><blockquote><p>&#8220;The cat sat on the mat.&#8221;</p></blockquote><p>Hide the last word. Now you have a training example for free: the input is &#8220;The cat sat on the,&#8221; and the correct answer is &#8220;mat.&#8221; No human had to label it; the text supplied the answer itself. And you don&#8217;t just get one example per sentence &#8212; you get one for <em>every position</em>:</p><ul><li><p>Given &#8220;The&#8221; &#8594; predict &#8220;cat&#8221;</p></li><li><p>Given &#8220;The cat&#8221; &#8594; predict &#8220;sat&#8221;</p></li><li><p>Given &#8220;The cat sat&#8221; &#8594; predict &#8220;on&#8221;</p></li><li><p>Given &#8220;The cat sat on&#8221; &#8594; predict &#8220;the&#8221;</p></li><li><p>Given &#8220;The cat sat on the&#8221; &#8594; predict &#8220;mat&#8221;</p></li></ul><p>One short sentence becomes five labeled examples, a single web page becomes thousands, and the entire internet becomes a near-infinite supply of free, self-labeling training data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lfhK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lfhK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 424w, https://substackcdn.com/image/fetch/$s_!lfhK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 848w, https://substackcdn.com/image/fetch/$s_!lfhK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 1272w, https://substackcdn.com/image/fetch/$s_!lfhK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lfhK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png" width="1456" height="1113" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1113,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Self-supervised labeling: one sentence becomes five free training examples&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Self-supervised labeling: one sentence becomes five free training examples" title="Self-supervised labeling: one sentence becomes five free training examples" srcset="https://substackcdn.com/image/fetch/$s_!lfhK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 424w, https://substackcdn.com/image/fetch/$s_!lfhK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 848w, https://substackcdn.com/image/fetch/$s_!lfhK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 1272w, https://substackcdn.com/image/fetch/$s_!lfhK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe2e1d860-a9ee-48d7-b411-72d87fbe1ea3_2616x2000.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>That&#8217;s the whole idea. Everything else in this post is detail on top of it: what loss we use to measure a wrong guess, where the text comes from, how much compute it takes, and how big the model should be.</p><p>One last bit of framing before we open up the machinery. Pretraining is only the first of three stages people constantly mix up &#8212; pretraining, post-training, and fine-tuning. They sound interchangeable, but they&#8217;re not. Let&#8217;s put them side by side first, then spend the rest of the post inside pretraining.</p><div><hr></div><h2>2. Pretraining, Post-Training, and Fine-Tuning: Who Does What</h2><p>If you take one thing from this section: <strong>pretraining, post-training, and fine-tuning are three different jobs, done by different people, at wildly different costs.</strong> They are not synonyms, and mixing them up is the most common confusion I see when people talk about &#8220;training&#8221; a model. Here&#8217;s the whole lineage, start to finish:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wMlD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wMlD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 424w, https://substackcdn.com/image/fetch/$s_!wMlD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 848w, https://substackcdn.com/image/fetch/$s_!wMlD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 1272w, https://substackcdn.com/image/fetch/$s_!wMlD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wMlD!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png" width="1200" height="274.45054945054943" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/af57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:333,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;From random weights to your specialist: pretraining, post-training, and fine-tuning&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="From random weights to your specialist: pretraining, post-training, and fine-tuning" title="From random weights to your specialist: pretraining, post-training, and fine-tuning" srcset="https://substackcdn.com/image/fetch/$s_!wMlD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 424w, https://substackcdn.com/image/fetch/$s_!wMlD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 848w, https://substackcdn.com/image/fetch/$s_!wMlD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 1272w, https://substackcdn.com/image/fetch/$s_!wMlD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf57d916-bfbc-4dac-9036-ddf1b4ca33d7_4776x1092.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><strong>Pretraining</strong> is what this whole post is about. Start from random weights, predict the next token over trillions of tokens of generic text, and end up with a <strong>base model</strong>. It&#8217;s self-supervised, so there are no human labels; it costs millions of dollars and months of compute; and only a handful of labs can afford to do it. This single stage is where roughly 99% of the model&#8217;s raw knowledge comes from.</p><p><strong>Post-training</strong> takes that base model and teaches it <em>behavior</em>. A base model can complete text but won&#8217;t reliably answer a question or follow an instruction (I&#8217;ll show you exactly why at the end of the post). Post-training fixes that in two moves: <strong>instruction tuning</strong> &#8212; also called supervised fine-tuning &#8212; where you show it lots of instruction &#8594; good-response pairs, and <strong>preference tuning</strong> &#8212; RLHF, DPO, or GRPO &#8212; where you teach it which answers people actually prefer. The data is human-curated, there&#8217;s far less of it, and it&#8217;s comparatively cheap. The output is the <strong>instruct (chat) model</strong> you actually talk to, and model creators do this before they ship.</p><p><strong>Fine-tuning</strong> is the part <em>you</em> do. You take an existing model (base or instruct) and adapt it to your own domain or task &#8212; whether that&#8217;s your support tickets, your medical notes, or your house code style. It&#8217;s supervised on your own labeled data, and these days it&#8217;s usually <strong><a href="https://www.mlwhiz.com/p/fine-tuning-llms-your-guide-to-peft">parameter-efficient</a></strong><a href="https://www.mlwhiz.com/p/fine-tuning-llms-your-guide-to-peft"> (LoRA/PEFT)</a>, so it runs in hours on one or two GPUs for a handful of dollars. The output is a <strong>specialist</strong>.</p><p>One honest note on terminology: &#8220;post-training&#8221; and &#8220;fine-tuning&#8221; overlap, because instruction tuning literally <em>is</em> a kind of fine-tuning. The distinction that actually matters in practice is <em>who</em> and <em>why</em>: post-training is the model creator turning a raw base model into a general-purpose assistant; fine-tuning is you adapting a finished model to a narrow job. The rest of this post lives entirely in that first box &#8212; pretraining &#8212; but it helps to know what comes after.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sc22!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sc22!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 424w, https://substackcdn.com/image/fetch/$s_!sc22!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 848w, https://substackcdn.com/image/fetch/$s_!sc22!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!sc22!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sc22!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png" width="1200" height="262.0879120879121" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:318,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Pretraining vs post-training vs fine-tuning: three stages, three different jobs&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="Pretraining vs post-training vs fine-tuning: three stages, three different jobs" title="Pretraining vs post-training vs fine-tuning: three stages, three different jobs" srcset="https://substackcdn.com/image/fetch/$s_!sc22!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 424w, https://substackcdn.com/image/fetch/$s_!sc22!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 848w, https://substackcdn.com/image/fetch/$s_!sc22!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!sc22!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5d78ed-740d-4c03-9dc2-189feaace1c3_4612x1008.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>That first stage does the bulk of the learning and burns almost all of the money; the later two are comparatively cheap. Keep that imbalance in mind &#8212; it explains a lot about why the industry looks the way it does. Now let&#8217;s open up that first box.</p><div><hr></div><h2>3. The Loss Function: Cross-Entropy, One Step at a Time</h2><p>We keep saying &#8220;nudge the weights when the guess is wrong.&#8221; Time to make that precise, because the entire training run is just this one step, repeated billions of times.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/pretraining-101-data-scale-and-the">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly Recsys/ML/GenAI Newsletter # 11 - The week US government pulled a frontier model offline on a letter ]]></title><description><![CDATA[Trump Strikes!!!]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-133</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-133</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Wed, 17 Jun 2026 21:07:04 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ZgYS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5229af9-6074-49d3-9a58-235ddd987db2_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><p>I love keeping track of everything week to week &#8212; here&#8217;s what happened this week. Enjoy this free weekly post! For those who want to dive deeper into any of these topics, that&#8217;s what my paid posts are for.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-133">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[What is an LLM? Tokens, Embeddings, and the Big Picture]]></title><description><![CDATA[GenAI Fundamentals Series Part 3: From raw text to next-token prediction. The complete mental model]]></description><link>https://www.mlwhiz.com/p/what-is-an-llm-tokens-embeddings</link><guid isPermaLink="false">https://www.mlwhiz.com/p/what-is-an-llm-tokens-embeddings</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Sun, 14 Jun 2026 01:16:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!SDLD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><p><em>This is the third part of the genAI Fundamentals series. Each post picks one building block of modern LLMs and explains it from first principles, with code.</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SDLD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SDLD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!SDLD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!SDLD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!SDLD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SDLD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1460464,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/201931289?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SDLD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!SDLD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!SDLD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!SDLD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d86bb8a-e980-46de-a82a-1e0068bb3be8_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Have you ever noticed how many screenshots float around LinkedIn of simple problems LLMs get wrong? &#8220;How many r&#8217;s in strawberry?&#8221; Wrong. &#8220;Reverse the word lollipop.&#8221; Wrong. &#8220;What&#8217;s 9.11 vs 9.9, which is bigger?&#8221; Confidently wrong.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!clZP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!clZP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 424w, https://substackcdn.com/image/fetch/$s_!clZP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 848w, https://substackcdn.com/image/fetch/$s_!clZP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 1272w, https://substackcdn.com/image/fetch/$s_!clZP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!clZP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png" width="1456" height="955" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:955,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:501061,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/201931289?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!clZP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 424w, https://substackcdn.com/image/fetch/$s_!clZP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 848w, https://substackcdn.com/image/fetch/$s_!clZP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 1272w, https://substackcdn.com/image/fetch/$s_!clZP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F511384e9-49d3-4f16-b1c4-06e6b3c1fe10_1698x1114.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>People share these as &#8220;gotcha&#8221; moments. Proof that LLMs are dumb. Proof that we&#8217;re overhyping AI.</p><p>But here&#8217;s what most of those posts miss: these failures aren&#8217;t random. They&#8217;re predictable. <em><strong>And once you understand how an LLM actually processes text, you can tell in advance which tasks will trip it up and why.</strong></em></p><p>The strawberry problem? <em><strong>The model never sees individual letters.</strong></em> It sees <em>tokens</em>, chunks like &#8220;str&#8221;, &#8220;aw&#8221;, &#8220;berry&#8221;. By the time any &#8220;reasoning&#8221; starts, the letters are already gone. </p><p>When I started working with LLMs in production, I had the same confusion everyone does. I couldn&#8217;t answer basic questions: <em><strong>Why doesn&#8217;t the model just process one character at a time?</strong></em> If it did, the strawberry problem would be trivial. What happens between typing a prompt and getting a response? <em><strong>Why does the model sometimes just... stop mid-sentence?</strong></em></p><p>Once I understood the pipeline end-to-end, everything clicked. And those LinkedIn gotcha screenshots? They stopped being surprising. You look at one and think, &#8220;yeah, obviously it fails at that.&#8221;</p><p>This post covers the full picture: how text becomes tokens, how tokens become vectors, what happens inside the model, and how vectors become text again. So, let&#8217;s start.</p><div><hr></div><h2>1. What is an LLM, really?</h2><p>Here&#8217;s the shortest accurate description:</p><div class="pullquote"><p>An LLM is a function that takes a list of integers and outputs a probability distribution over which integer comes next.</p></div><p>That&#8217;s the entire interface. The integers represent tokens (subwords). The output is one probability per possible token in the vocabulary. The model picks one, appends it to the list, and runs the function again. ChatGPT, Claude, Llama, Gemini: they all do exactly this. Everything else, the chat formatting, the system prompts, is built on top of this one operation.</p><p>In PyTorch pseudo-code:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">logits = model(input_ids)           # list of ints &#8594; raw scores
probs = softmax(logits[-1])         # last position &#8594; probabilities over vocab
next_token = sample(probs)          # pick one token
</code></pre></div><p>Three lines. That&#8217;s the core loop. The rest of this post explains what each piece does and where the numbers come from.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6Fq7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6Fq7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 424w, https://substackcdn.com/image/fetch/$s_!6Fq7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 848w, https://substackcdn.com/image/fetch/$s_!6Fq7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 1272w, https://substackcdn.com/image/fetch/$s_!6Fq7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6Fq7!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png" width="1200" height="275.27472527472526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:334,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;The LLM Pipeline: text enters as a prompt, gets tokenized to integers, embedded into vectors, transformed through 32 layers, projected by the LM head to probabilities, and sampled to produce the next token&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="The LLM Pipeline: text enters as a prompt, gets tokenized to integers, embedded into vectors, transformed through 32 layers, projected by the LM head to probabilities, and sampled to produce the next token" title="The LLM Pipeline: text enters as a prompt, gets tokenized to integers, embedded into vectors, transformed through 32 layers, projected by the LM head to probabilities, and sampled to produce the next token" srcset="https://substackcdn.com/image/fetch/$s_!6Fq7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 424w, https://substackcdn.com/image/fetch/$s_!6Fq7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 848w, https://substackcdn.com/image/fetch/$s_!6Fq7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 1272w, https://substackcdn.com/image/fetch/$s_!6Fq7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F835c741b-2fb0-414f-82ee-b453b67f5a03_2813x645.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div><hr></div><h2>2. Tokenization: how text becomes numbers</h2><p>A neural network processes numbers. Text is not numbers. So the first step is converting text into a sequence of integers. This conversion is <strong>tokenization</strong>, and it&#8217;s one of those things that sounds boring until you realize it&#8217;s responsible for half the weird behavior you&#8217;ve seen from LLMs.</p><p>The question is: <em><strong>what should each integer represent?</strong></em></p><p>You have three options.</p><p><strong>A. Characters.</strong> Each character gets its own ID. &#8220;cat&#8221; becomes three IDs. The vocabulary stays small. The problem is length: a 4,000-word document is around 20,000 characters, so that&#8217;s 20,000 positions for the attention mechanism to chew through, and <em><strong>attention cost grows quadratically</strong></em> with sequence length. </p><p><strong>B. Words.</strong> Each whole word gets an ID. The vocabulary balloons to 500,000+ for English alone, and you still can&#8217;t handle typos (&#8221;caat&#8221;), brand-new words (&#8221;ChatGPT&#8221; didn&#8217;t exist in older vocabularies), or variants (&#8221;running&#8221;, &#8220;ran&#8221;, &#8220;runs&#8221; all become unrelated entries). <em><strong>Every unseen word is a dead end.</strong></em></p><p><strong>C. Subwords.</strong> Common words stay whole, rare words split into known pieces. &#8220;unfamiliarize&#8221; becomes [&#8221;un&#8221;, &#8220;familiar&#8221;, &#8220;ize&#8221;]. The vocabulary lands in a comfortable middle (32K to 256K). This is what every modern LLM uses.</p><p>The rest of this section is about how subword tokenizers actually get built. But first, two concepts on which everything depends.</p><h3>Unicode and bytes</h3><p><strong>Unicode</strong> is one giant table that assigns a number to every character in every writing system. &#8220;A&#8221; is 65. &#8220;&#233;&#8221; is 233. The Chinese character &#8220;&#20013;&#8221; is 20,013. The emoji &#8220;&#128512;&#8221; is 128,512. About 150,000 characters are defined today, and the table keeps growing. Each number is called a <em>code point</em>.</p><p><strong>Bytes</strong> are how those numbers actually get stored on a computer. A byte is just a value from 0 to 255 (8 bits, hence &#8220;byte&#8221;). Everything on your machine, including text, is ultimately a stream of these byte values.</p><p>The catch is that Unicode has ~150,000 code points, but a byte only goes up to 255. So you can&#8217;t fit &#8220;&#20013;&#8221; (code point 20,013) into a single byte. You need a scheme for packing big code points into sequences of small bytes. That scheme is <strong>UTF-8</strong>, the encoding that essentially all text uses today.</p><p>UTF-8 is variable-length: common characters get fewer bytes, rare ones get more. </p><ul><li><p>Plain English (the original ASCII set, code points 0-127) takes <strong>1 byte</strong>. &#8220;A&#8221; is just byte 65. </p></li><li><p>Accented Latin, Greek, Cyrillic, Hebrew, Arabic take <strong>2 bytes</strong>. </p></li><li><p>Most Chinese, Japanese, and Korean characters take <strong>3 bytes</strong>. </p></li><li><p>Emoji and rarer symbols take <strong>4 bytes</strong>.</p></li></ul><p>So &#8220;&#20013;&#8221; is not stored as the single number 20,013. UTF-8 packs that code point into three bytes: 228, 184, 173. If you wrote &#8220;&#20013;&#8221; to a file, those are the three values actually on disk. The string &#8220;A&#20013;&#8221; would be four bytes total: [65, 228, 184, 173].</p><div class="callout-block" data-callout="true"><p>You might wonder: if &#8220;A&#20013;&#8221; is just [65, 228, 184, 173], how does the computer know that 65 is one character but 228, 184, 173 are three bytes forming a single character? Why not read it as four separate characters? Because UTF-8 is self-describing. The leading bits of each byte announce its role: a byte starting with <code>0</code> is a standalone character, a byte starting with <code>110</code> or <code>1110</code> is the <em>start</em> of a 2- or 3-byte character, and a byte starting with <code>10</code> is a <em>continuation</em>. Here, 65 is <code>01000001</code> (standalone, &#8220;A&#8221;), and 228 is <code>11100100</code> (start of a 3-byte character, &#8220;read the next two bytes with me&#8221;), while 184 and 173 both start with <code>10</code> (continuations). The decoder can always find character boundaries just by looking at the top bits.</p></div><p>This is the punchline for tokenization: no matter what language or symbol you throw at it, every piece of text is, at the lowest level, a sequence of byte values between 0 and 255. There are only ever 256 possible building blocks. </p><p>Hold onto this distinction (characters versus bytes), because the single most important design choice in a tokenizer is which of these it starts from.</p><div><hr></div><h3>BPE: the core algorithm</h3><p><strong>Byte Pair Encoding</strong> (BPE) started life as a data-compression trick in 1994. Sennrich et al. adapted it for NLP in 2016, and it has been the dominant tokenization algorithm ever since. GPT-2, GPT-4, Llama, Qwen, Mistral, DeepSeek: nearly all of them run BPE.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YyKB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YyKB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 424w, https://substackcdn.com/image/fetch/$s_!YyKB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 848w, https://substackcdn.com/image/fetch/$s_!YyKB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 1272w, https://substackcdn.com/image/fetch/$s_!YyKB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YyKB!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png" width="1200" height="416.2087912087912" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:505,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;BPE algorithm: start with byte-level splits, find the most frequent pair, merge it into a new token, repeat until target vocabulary size&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="BPE algorithm: start with byte-level splits, find the most frequent pair, merge it into a new token, repeat until target vocabulary size" title="BPE algorithm: start with byte-level splits, find the most frequent pair, merge it into a new token, repeat until target vocabulary size" srcset="https://substackcdn.com/image/fetch/$s_!YyKB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 424w, https://substackcdn.com/image/fetch/$s_!YyKB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 848w, https://substackcdn.com/image/fetch/$s_!YyKB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 1272w, https://substackcdn.com/image/fetch/$s_!YyKB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F247fb614-e99a-4e50-b92f-c55ad8b5f618_2633x914.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The algorithm is short. I&#8217;ll walk through it, because once you see it the rest falls into place.</p><p><strong>Step 1:</strong> Start with a base set of atomic units. Say there are 256 of them (I&#8217;ll explain what they are in the next section). Every piece of text is now a sequence of these units.</p><p><strong>Step 2:</strong> Scan the entire training corpus. Find the pair of adjacent units that appears most often. Maybe &#8220;e&#8221; followed by &#8220;r&#8221; shows up 50 million times.</p><p><strong>Step 3:</strong> Merge that pair into one new unit, &#8220;er&#8221;. Add it to the vocabulary (now 257). Replace every &#8220;e&#8221; + &#8220;r&#8221; in the corpus with &#8220;er&#8221;.</p><p><strong>Step 4:</strong> Repeat. The next most frequent pair might be &#8220;t&#8221; + &#8220;h&#8221; &#8594; &#8220;th&#8221;, then &#8220;th&#8221; + &#8220;e&#8221; &#8594; &#8220;the&#8221;. Merge, add, repeat.</p><p>Run 128,000 merges and you end up with ~128,256 tokens (the 256 base units plus 128K learned merges). That&#8217;s right in the range where a lot of modern models have landed.</p><p>Merge order matters. Early merges grab the most common letter combos (&#8221;th&#8221;, &#8220;in&#8221;, &#8220;er&#8221;), which combine into &#8220;the&#8221;, &#8220;ing&#8221;, &#8220;tion&#8221;, and eventually whole common words. Rare words stay split into pieces. The ordered list of merges is saved with the model and replayed at inference time.</p><div><hr></div><h3>Characters or bytes? The choice that actually matters</h3><p>BPE merges pairs, but Step 1 has to start from <em>something</em>. This is the real decision, and it&#8217;s where the two main flavors of BPE split apart.</p><p><strong>Character-level BPE</strong> starts from Unicode characters. The catch: there are ~150,000 of them. You either spend vocabulary slots on thousands of rare characters, or you hit characters you&#8217;ve never seen at inference time. Older tokenizers had a special <code>&lt;UNK&gt;</code> (&#8221;unknown&#8221;) token for exactly this, and it caused real failures on emoji, rare scripts, and even unusual typos.</p><p><strong>Byte-level BPE</strong> starts from the 256-byte values instead. This is GPT-2&#8217;s key trick, and it&#8217;s why the base vocabulary is 256. Since every possible text in every language is just a sequence of bytes, there is no such thing as an unknown token. A brand-new emoji is simply four known byte-tokens stitched together. Chinese, Arabic, source code, corrupted text: all representable. Rare text just splits into more tokens. Nothing ever breaks.</p><p>This is why essentially all modern tokenizers are byte-level. When you saw &#8220;256 base units&#8221; in the algorithm above, those 256 are the byte values.</p><div><hr></div><h3>SentencePiece and tiktoken</h3><p><strong>BPE is the algorithm. SentencePiece and tiktoken are libraries that run it.</strong> Moving from one library to the other is <em>not</em> moving away from BPE. This trips up a lot of people who read that some model &#8220;switched from SentencePiece to tiktoken&#8221; and assume the algorithm changed. It didn&#8217;t. Both run BPE.</p><p><strong>SentencePiece</strong> (Kudo &amp; Richardson, 2018) is Google&#8217;s tokenizer library. Its real contribution is handling raw text without language-specific rules. Most tokenizers first split text on spaces, then run BPE per word, which breaks for Chinese, Japanese, and Thai, which don&#8217;t put spaces between words. SentencePiece skips the space-splitting step. It treats the space itself as a character (the &#8220;&#9601;&#8221; symbol) and runs the merge algorithm on the raw stream. The early Llama models, Mistral, and the Gemma family all used SentencePiece, typically with a 32,000-token vocabulary.</p><p>SentencePiece is still widely used. The Gemma models use it, for example. Here it is, tokenizing a sentence (notice the &#8220;&#9601;&#8221; marking each space):</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python"># SentencePiece example
import sentencepiece as spm

sp = spm.SentencePieceProcessor()
sp.Load(&#8221;tokenizer.model&#8221;)

text = &#8220;Hello, how are you?&#8221;
tokens = sp.EncodeAsPieces(text)
# [&#8217;&#9601;Hello&#8217;, &#8216;,&#8217;, &#8216;&#9601;how&#8217;, &#8216;&#9601;are&#8217;, &#8216;&#9601;you&#8217;, &#8216;?&#8217;]
# The &#9601; marks the start of a word (space replaced with &#9601;)
</code></pre></div><p>Notice the &#8220;&#9601;&#8221; prefix. SentencePiece uses this to mark where spaces were in the original text. When you decode, it converts &#8220;&#9601;&#8221; back to a space. This is how the model knows that &#8220;Hello&#8221; starts a new word.</p><div><hr></div><h3>tiktoken: OpenAI&#8217;s fast byte-level BPE</h3><p><strong>tiktoken</strong> is OpenAI&#8217;s tokenizer library. It runs byte-level BPE, it&#8217;s written in Rust (with Python bindings), and it&#8217;s significantly faster than older Python implementations. It&#8217;s the tokenizer behind GPT-3.5, GPT-4, and GPT-4o, and many newer open models (the later Llama releases, Qwen, DeepSeek) adopted tiktoken-style tokenizers too.</p><p>tiktoken ships with several pre-trained encodings: </p><ul><li><p><code>cl100k_base</code>: used by GPT-4 and GPT-3.5 Turbo. 100,277 tokens. </p></li><li><p><code>o200k_base</code>: used by GPT-4o. ~200,019 tokens. </p></li><li><p><code>gpt2</code>: the original GPT-2 encoding. 50,257 tokens.</p></li></ul><p>A lot of newer models moved from SentencePiece to tiktoken-style tokenizers, and it&#8217;s worth being clear that this isn&#8217;t &#8220;BPE stopped working.&#8221; Both run BPE. The migration is mostly about speed: tiktoken&#8217;s Rust encoder is much faster at turning text into tokens, which matters when you&#8217;re tokenizing trillions of training tokens and every inference request.</p><p>Let&#8217;s see tiktoken in action:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import tiktoken

# GPT-4&#8217;s tokenizer
enc = tiktoken.get_encoding(&#8221;cl100k_base&#8221;)

# Simple English
text = &#8220;The quick brown fox jumps over the lazy dog&#8221;
tokens = enc.encode(text)
print(f&#8221;Text: {text}&#8221;)
print(f&#8221;Tokens: {tokens}&#8221;)
print(f&#8221;Count: {len(tokens)} tokens&#8221;)
print(f&#8221;Decoded: {[enc.decode([t]) for t in tokens]}&#8221;)
# Tokens: [791, 4062, 14198, 39935, 35308, 927, 279, 16053, 5679]
# Count: 9 tokens
# Decoded: [&#8217;The&#8217;, &#8216; quick&#8217;, &#8216; brown&#8217;, &#8216; fox&#8217;, &#8216; jumps&#8217;, &#8216; over&#8217;, &#8216; the&#8217;, &#8216; lazy&#8217;, &#8216; dog&#8217;]

# Now try something interesting
code = &#8220;def fibonacci(n):\n    return n if n &lt; 2 else fibonacci(n-1) + fibonacci(n-2)&#8221;
code_tokens = enc.encode(code)
print(f&#8221;\nCode: {code}&#8221;)
print(f&#8221;Count: {len(code_tokens)} tokens&#8221;)
print(f&#8221;Decoded: {[enc.decode([t]) for t in code_tokens]}&#8221;)
# Common programming patterns are single tokens: &#8220;def&#8221;, &#8220;return&#8221;, &#8220;fibonacci&#8221;
# Indentation is a single token too

# The strawberry problem
word = &#8220;strawberry&#8221;
word_tokens = enc.encode(word)
print(f&#8221;\n&#8217;{word}&#8217; = {[enc.decode([t]) for t in word_tokens]}&#8221;)
# &#8216;strawberry&#8217; -&gt; [&#8217;str&#8217;, &#8216;aw&#8217;, &#8216;berry&#8217;] (3 tokens)
# The model literally never sees the individual letters
</code></pre></div><p>9 tokens for 9 English words. That&#8217;s efficient. Code tokenizes well, too, because the BPE merges were trained on a corpus that included a lot of code. But look at &#8220;strawberry&#8221;: it splits as &#8220;str&#8221; + &#8220;aw&#8221; + &#8220;berry&#8221;, not into individual letters. This is why the model can&#8217;t count letters. It never sees them.</p><div><hr></div><h3>Who uses what: tokenizers across the major models</h3><p>Here&#8217;s how tokenization evolved across the major model families. This is worth studying because the tokenizer choice tells you a lot about a model&#8217;s design priorities.</p><p>The &#8220;Algorithm&#8221; column tells you the flavor of BPE and the library used. Every model below runs BPE; what changes is whether it&#8217;s byte-level and which library implements it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Aead!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Aead!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 424w, https://substackcdn.com/image/fetch/$s_!Aead!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 848w, https://substackcdn.com/image/fetch/$s_!Aead!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 1272w, https://substackcdn.com/image/fetch/$s_!Aead!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Aead!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png" width="993" height="864" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cc5cc60a-9679-4779-a866-751e839e1957_993x864.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:864,&quot;width&quot;:993,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Tokenizer comparison across major models: vocab size, algorithm, and year&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Tokenizer comparison across major models: vocab size, algorithm, and year" title="Tokenizer comparison across major models: vocab size, algorithm, and year" srcset="https://substackcdn.com/image/fetch/$s_!Aead!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 424w, https://substackcdn.com/image/fetch/$s_!Aead!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 848w, https://substackcdn.com/image/fetch/$s_!Aead!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 1272w, https://substackcdn.com/image/fetch/$s_!Aead!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc5cc60a-9679-4779-a866-751e839e1957_993x864.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A few patterns jump out:</p><p><strong>2019-2022: Small vocabulary, English-first.</strong> GPT-2 and GPT-3 used 50K tokens. Llama 1/2 and Mistral used 32K. These vocabularies were heavily English-biased. The same content in Hindi could need several times more tokens than in English.</p><p><strong>2023: The 100K jump.</strong> GPT-4 doubled the vocabulary to 100K. This was the first big move toward multilingual efficiency: more tokens dedicated to non-English scripts means fewer tokens per sentence in those languages.</p><p><strong>2024-2025: The 128K-256K era.</strong> Everyone expanded. Llama 3 and 4 went to 128K then 202K. Qwen settled on 152K. Gemma pushed to 256K, then 262K. The reasoning is consistent: bigger vocab = fewer tokens per text = faster inference, lower cost, longer effective context. The tradeoff is a bigger embedding table, but at 8B+ model sizes, that table is a small fraction of total parameters.</p><p><strong>The library choice is mostly about speed, not capability.</strong> SentencePiece (Gemma, older Llama, Mistral) and tiktoken (GPT, newer Llama, Qwen) both run BPE. The migration toward tiktoken in newer models is largely because its Rust implementation encodes and decodes faster. The algorithm underneath is the same.</p><p><strong>Qwen&#8217;s CJK advantage.</strong> Qwen&#8217;s 152K vocabulary was built with heavy Chinese, Japanese, and Korean coverage from the start. That&#8217;s why Qwen models tend to do well on CJK benchmarks relative to their size: the tokenizer compresses CJK text efficiently, giving the model more room per context window.</p><div><hr></div><p><strong>The rest of this post is for paid subscribers.</strong> So far, we&#8217;ve covered what an LLM is at its simplest and the full tokenization story: Unicode, bytes, UTF-8, BPE, byte-level vs character-level, SentencePiece vs tiktoken, and the cross-model tokenizer table.</p><p>Behind the paywall: how token IDs become vectors (the embedding table and what those vectors mean), how the model turns vectors back into words (the LM head, softmax, weight tying with a worked numeric example, and temperature), the full end-to-end pipeline with real numbers, autoregressive generation and the KV cache, and why tokenization quietly decides what a model can and can&#8217;t do.</p><p>Subscribe to keep reading.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p>
      <p>
          <a href="https://www.mlwhiz.com/p/what-is-an-llm-tokens-embeddings">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly Recsys/ML/GenAI Newsletter # 10 - The week AI infrastructure crossed from a technology story to a financial one ]]></title><description><![CDATA[Hey, Rahul here!]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-ed3</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-ed3</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Wed, 10 Jun 2026 23:56:34 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!TCN_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308df799-e18f-46da-99a9-89490d0f5c06_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-ed3">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[The Transformer, Demystified — Let's Actually Build One]]></title><description><![CDATA[GenAI Series Part 2: Implementing a Transformer]]></description><link>https://www.mlwhiz.com/p/the-transformer-demystified-lets</link><guid isPermaLink="false">https://www.mlwhiz.com/p/the-transformer-demystified-lets</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Fri, 05 Jun 2026 22:47:17 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!UQgH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><blockquote><p><em>Over the coming weeks, I&#8217;ll be writing more about GenAI, including topics like pre-training and post-training. This post is the second one of the foundational pieces meant to set up that series.</em></p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UQgH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UQgH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!UQgH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!UQgH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!UQgH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UQgH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1511763,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/199921894?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UQgH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!UQgH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!UQgH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!UQgH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce54a3b-027d-4caf-aba3-2e4c903a8f76_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Transformers run most of modern NLP, but they&#8217;re still surprisingly hard to internalize from a diagram alone. In my <a href="https://www.mlwhiz.com/p/transformers">last post</a>, I walked through how they work &#8212; the encoder, decoder, and the data flow between them. </p><p>This post is where we stop reading and start building: an end-to-end English-to-German translator in PyTorch, written from scratch with a Transformer at its core. Because the fastest way to actually understand something is to implement it.</p><div><hr></div><h2><strong>Task Description</strong></h2><p>We want to create a translator that uses transformers to convert English to German. So, if we look at it as a black box, our network takes as input an English sentence and returns a German sentence.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!f8Vo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!f8Vo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 424w, https://substackcdn.com/image/fetch/$s_!f8Vo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 848w, https://substackcdn.com/image/fetch/$s_!f8Vo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 1272w, https://substackcdn.com/image/fetch/$s_!f8Vo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!f8Vo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png" width="1456" height="236" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:236,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Transformer for Translation&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Transformer for Translation" title="Transformer for Translation" srcset="https://substackcdn.com/image/fetch/$s_!f8Vo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 424w, https://substackcdn.com/image/fetch/$s_!f8Vo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 848w, https://substackcdn.com/image/fetch/$s_!f8Vo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 1272w, https://substackcdn.com/image/fetch/$s_!f8Vo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0836bab3-77d4-4cd4-bf82-ac33c56a6a75_1683x273.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Transformer for Translation</figcaption></figure></div><div><hr></div><h2><strong>Data Preprocessing</strong></h2><p>To train our English-German translation Model, we will need translated sentence pairs between English and German.</p><p>Fortunately, there is a pretty standard way to get these with the OPUS-100 dataset (English-German subset), a curated multilingual translation corpus we can access via HuggingFace datasets. </p><p>Also, before we really get into the whole coding part, let us understand what we need as input and output to the model while training. We will actually need two matrices to be input to our Network:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!K5hx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!K5hx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 424w, https://substackcdn.com/image/fetch/$s_!K5hx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 848w, https://substackcdn.com/image/fetch/$s_!K5hx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 1272w, https://substackcdn.com/image/fetch/$s_!K5hx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!K5hx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png" width="1456" height="470" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:470,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:162548,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/199921894?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!K5hx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 424w, https://substackcdn.com/image/fetch/$s_!K5hx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 848w, https://substackcdn.com/image/fetch/$s_!K5hx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 1272w, https://substackcdn.com/image/fetch/$s_!K5hx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1239a84c-833b-4900-98e5-b5a3602bdba5_2664x860.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div>
      <p>
          <a href="https://www.mlwhiz.com/p/the-transformer-demystified-lets">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly Recsys/ML/GenAI Newsletter # 9 - The week AI started its IPOs]]></title><description><![CDATA[The AI industry is about to stop being a private market story. Quarterly earnings calls ask harder questions than venture capitalists.]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-204</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-204</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Tue, 02 Jun 2026 23:30:44 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!45Gc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b614a9d-2751-43ca-ab2c-db383bbb1abd_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-204">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[Understanding Transformers, the MLE Way]]></title><description><![CDATA[GenAI Series Part 1: What even are transformers?]]></description><link>https://www.mlwhiz.com/p/transformers</link><guid isPermaLink="false">https://www.mlwhiz.com/p/transformers</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Fri, 29 May 2026 23:02:21 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!9E2I!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><blockquote><p><em>Over the coming weeks, I&#8217;ll be writing more about GenAI, including topics like pre-training and post-training. This post is one of the foundational pieces meant to set up that series.</em></p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9E2I!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9E2I!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!9E2I!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!9E2I!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!9E2I!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9E2I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1519758,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/152600775?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9E2I!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!9E2I!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!9E2I!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!9E2I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77aeaf82-24d6-47d5-9be2-fe143bae817e_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Transformers have become the de facto standard for almost everything. Though the architecture was introduced for NLP, it now powers computer vision, recommender systems, and&#8212;most importantly&#8212;the entire wave of modern LLMs. </p><p>Yet for all their ubiquity, transformers remain as hard to understand as ever.</p><p>It has taken me multiple readings through the Google research <strong><a href="https://arxiv.org/pdf/1706.03762.pdf">paper</a></strong> that first introduced transformers, along with just so many blog posts, to really understand how a transformer works.</p><p>So, I thought of putting the whole idea down in as simple words as possible, and with some very basic Math and some puns, as I am a proponent of having some fun while learning. I will try to keep both the jargon and the technicality to a minimum, yet it is such a topic that I could only do so much. And my goal is to make the reader understand even the most gory details of Transformer by the end of this post.</p><p><em><strong>Also, this is officially my longest post, both in terms of time taken to write it as well as the length of the post. Hence, I will advise you to Grab A Coffee.</strong></em> &#9749;&#65039;</p><p>Before we dive in, here&#8217;s the path we&#8217;ll walk together: we&#8217;ll start with the big picture of what a transformer even does, then crack open the <strong>encoder</strong> stack (attention, feed-forward, positional encodings, and those mysterious &#8220;Add &amp; Norm&#8221; boxes). From there, we&#8217;ll move to the <strong>decoder</strong> stack and the masking trick that makes it tick, bolt on an <strong>output head</strong> to actually get our German words, and finish with how the whole thing is <strong>trained</strong> and how it makes <strong>predictions</strong> at test time. Long road, but I promise the view is worth it. Onwards.</p><div><hr></div><p><em><strong>Q: So, why should I even understand Transformer?</strong></em></p><p>In the past, the LSTM and GRU architecture(as explained here in my past <strong><a href="https://www.mlwhiz.com/p/deeplearning_architectures_text_classification">post</a></strong> on NLP), along with the attention mechanism, used to be the State of the Art Approach for Language modeling problems (put very simply, predict the next word) and Translation systems. But the main problem with these architectures is that they are recurrent in nature, and the runtime increases as the sequence length increases. That is, these architectures take a sentence and process each word in a <em><strong>sequential</strong></em> way, and hence, with the increase in sentence length, the whole runtime increases.</p><p>Transformer, a model architecture first explained in the paper Attention is all you need, lets go of this recurrence and instead relies entirely on an attention mechanism to draw global dependencies between input and output. And that makes it FAST.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!l5i7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!l5i7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 424w, https://substackcdn.com/image/fetch/$s_!l5i7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 848w, https://substackcdn.com/image/fetch/$s_!l5i7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 1272w, https://substackcdn.com/image/fetch/$s_!l5i7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!l5i7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png" width="380" height="560" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:560,&quot;width&quot;:380,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;\n\n<a href=\&quot;https://arxiv.org/pdf/1706.03762.pdf\&quot; target=\&quot;_blank\&quot; rel=\&quot;nofollow noopener\&quot;>Source</a>\n&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="

<a href=&quot;https://arxiv.org/pdf/1706.03762.pdf&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;>Source</a>
" title="

<a href=&quot;https://arxiv.org/pdf/1706.03762.pdf&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;>Source</a>
" srcset="https://substackcdn.com/image/fetch/$s_!l5i7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 424w, https://substackcdn.com/image/fetch/$s_!l5i7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 848w, https://substackcdn.com/image/fetch/$s_!l5i7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 1272w, https://substackcdn.com/image/fetch/$s_!l5i7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F57b44dee-2f73-4c7e-8569-6f95c48d436a_380x560.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">From the Paper</figcaption></figure></div><p>This is the picture of the full transformer as taken from the paper. And, it surely is intimidating. So, I will aim to demystify it in this post by going through each piece. So read ahead.</p><div><hr></div><h2><strong>The Big Picture</strong></h2><p><em><strong>Q: That sounds interesting. So, what does a transformer do exactly?</strong></em></p><p>Essentially, a transformer can perform almost any NLP task. It can be used for language modeling, Translation, or Classification as required, and it does it fast by removing the sequential nature of the problem. So, the transformer in a machine translation application would convert one language to another, or for a classification problem will provide the class probability using an appropriate output layer.</p><p>It all will depend on the final output layer for the network; the Transformer basic structure will remain quite the same for any task. For this particular post, I will be continuing with the machine translation example.</p><p>So, from a very high place, this is how the transformer looks for a translation task. It takes as input an English sentence and returns a German sentence.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DoSv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DoSv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 424w, https://substackcdn.com/image/fetch/$s_!DoSv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 848w, https://substackcdn.com/image/fetch/$s_!DoSv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 1272w, https://substackcdn.com/image/fetch/$s_!DoSv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DoSv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png" width="1456" height="236" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:236,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Transformer for Translation&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Transformer for Translation" title="Transformer for Translation" srcset="https://substackcdn.com/image/fetch/$s_!DoSv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 424w, https://substackcdn.com/image/fetch/$s_!DoSv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 848w, https://substackcdn.com/image/fetch/$s_!DoSv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 1272w, https://substackcdn.com/image/fetch/$s_!DoSv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e97e6e-5c72-499d-854d-1b60fee54897_1683x273.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Transformer for Translation</figcaption></figure></div><div><hr></div><h2><strong>The Building Blocks</strong></h2><p><em><strong>Q: That was too basic. &#128526; Can you expand on it?</strong></em></p><p>Okay, just remember in the end, you asked for it. Let&#8217;s go a little deeper and try to understand what a transformer is composed of.</p><p>So, a transformer is essentially composed of a stack of encoder and decoder layers. The role of an encoder layer is to encode the English sentence into a numerical form using the attention mechanism, while the decoder aims to use the encoded information from the encoder layers to give the German translation for the particular English sentence.</p><p>In the figure below, the transformer is given an English sentence as input, which gets encoded using 6 encoder layers. The output from the final encoder layer then goes to each decoder layer to translate English to German.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iywS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iywS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 424w, https://substackcdn.com/image/fetch/$s_!iywS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 848w, https://substackcdn.com/image/fetch/$s_!iywS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 1272w, https://substackcdn.com/image/fetch/$s_!iywS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iywS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png" width="1456" height="1402" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1402,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Data Flow in a Transformer&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Data Flow in a Transformer" title="Data Flow in a Transformer" srcset="https://substackcdn.com/image/fetch/$s_!iywS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 424w, https://substackcdn.com/image/fetch/$s_!iywS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 848w, https://substackcdn.com/image/fetch/$s_!iywS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 1272w, https://substackcdn.com/image/fetch/$s_!iywS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F545d075a-b6b9-4ff9-907a-2f333b68d113_1530x1473.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Data Flow in a Transformer</figcaption></figure></div><div><hr></div><h2><strong>1. Encoder Architecture</strong></h2><p><em><strong>Q: That&#8217;s alright, but how does an encoder stack encode an English sentence exactly?</strong></em></p><p>Patience, I am getting to it. So, as I said, the encoder stack contains six encoder layers on top of each other(As given in the paper, but the future versions of transformers use even more layers). And each encoder in the stack has essentially two main layers:</p><ul><li><p><strong>a multi-head self-attention Layer, and</strong></p></li><li><p><strong>a position-wise fully connected feed-forward network</strong></p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TMPS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TMPS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 424w, https://substackcdn.com/image/fetch/$s_!TMPS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 848w, https://substackcdn.com/image/fetch/$s_!TMPS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 1272w, https://substackcdn.com/image/fetch/$s_!TMPS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TMPS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png" width="400" height="255" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:255,&quot;width&quot;:400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Very basic encoder Layer&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Very basic encoder Layer" title="Very basic encoder Layer" srcset="https://substackcdn.com/image/fetch/$s_!TMPS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 424w, https://substackcdn.com/image/fetch/$s_!TMPS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 848w, https://substackcdn.com/image/fetch/$s_!TMPS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 1272w, https://substackcdn.com/image/fetch/$s_!TMPS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd0d60a9-5325-475d-8427-79ba33253a5a_400x255.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Very basic encoder Layer</figcaption></figure></div><p>They are a mouthful. Right? Don&#8217;t lose me yet as I will explain both of them in the coming sections. Right now, just remember that the encoder layer incorporates attention and a position-wise feed-forward network.</p><p><em><strong>Q: But, how does this layer expect its inputs to be?</strong></em></p><p>This layer expects its inputs to be of the shape <code>SxD</code> (as shown in the figure below) where <code>S</code> is the source sentence(English Sentence) length, and <code>D</code> is the dimension of the embedding whose weights can be trained with the network. In this post, we will be using D as 512 by default throughout. While S will be the maximum length of a sentence in a batch. So it normally changes with batches.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/transformers">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[HSTU From Scratch in PyTorch - A complete Walkthrough]]></title><description><![CDATA[RecSys for MLEs Part 9d: data pipeline, three sub-layers, retrieval + rating loss, and benchmarking against rectools' HSTU on MovieLens-1M]]></description><link>https://www.mlwhiz.com/p/hstu-from-scratch-in-pytorch-a-complete</link><guid isPermaLink="false">https://www.mlwhiz.com/p/hstu-from-scratch-in-pytorch-a-complete</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Thu, 28 May 2026 02:11:53 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!6mrf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6mrf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6mrf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!6mrf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!6mrf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!6mrf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6mrf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1484559,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/199390246?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6mrf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!6mrf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!6mrf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!6mrf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1343315-5cfd-4149-89ff-a93ae8cf0ba4_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>This is Part 9d of the RecSys for MLEs series. <a href="https://www.mlwhiz.com/p/hstu-how-meta-built-a-trillion-parameter">Part 9c explained why HSTU works</a>: the softmax-to-SiLU switch that preserves engagement intensity, relative attention bias that gives the model a sense of time, and M-FALCON for cheap candidate scoring. This post is the hands-on follow-up. We&#8217;re going to train an HSTU from scratch, on the MovieLens-1M dataset, and benchmark it against the rectools library&#8217;s reference HSTU implementation, so you have a real number to compare against.</em></p><div><hr></div><p>Last week, I published the conceptual deep dive on HSTU. Within 24 hours, the most common question in my replies was the same: <em>&#8220;OK, but how do I actually train one?&#8221;</em></p><p>Fair. So today we build it from scratch &#8212; the fused (item, action) input layer, all three HSTU sub-layers, the multi-task retrieval + rating heads, and the M-FALCON inference cache. We&#8217;ll train it on MovieLens-1M, then benchmark head-to-head against rectools&#8217; reference HSTU implementation, so you have a real number to compare against.</p><p>We&#8217;ll use PyTorch 2.x on a single GPU (a free Colab T4 works fine). I&#8217;m intentionally keeping the model small (D=64, 2 layers) so it trains in few hours on free hardware. And to make sure I wasn&#8217;t fooling myself with vanity numbers, I trained the <a href="https://github.com/MobileTeleSystems/RecTools">rectools library&#8217;s HSTU</a> on the same data and split, then ran my from-scratch model with a similar training config. That way, if my numbers come out worse, I know exactly where to look.</p><p>Here&#8217;s what we&#8217;ll cover:</p><ul><li><p><strong>The dataset</strong>: how MovieLens ratings map to the (item, action, time) triples HSTU consumes</p></li><li><p><strong>The fused input layer</strong>: item embedding + action embedding + fusion MLP</p></li><li><p><strong>The HSTU block</strong>: all three sub-layers as a single PyTorch module &#8212; SiLU attention, RAB, gated transformation</p></li><li><p><strong>Multi-task heads</strong>: retrieval (sampled softmax with cosine similarity + temperature) and rating prediction</p></li><li><p><strong>Results</strong>: HR@10 and NDCG@10 against rectools HSTU and SASRec, plus the SiLU vs softmax ablation</p></li><li><p><strong>Example predictions</strong>: what the model actually recommends for specific MovieLens users</p></li><li><p><strong>Retrieval and ranking demos</strong>: brute-force, FAISS, and ranking by retrieval score + predicted rating</p></li><li><p><strong>M-FALCON inference</strong>: the K/V caching trick that makes serving feasible</p></li></ul><div class="callout-block" data-callout="true"><p><strong>Notebooks to read alongside the post:</strong> </p><p><a href="https://www.kaggle.com/code/mlwhiz/hstu-from-scratch">&#128211; hstu-from-scratch-ml1m-v2.ipynb</a> &#8212; the from-scratch HSTU we build in this post </p><p><a href="https://www.kaggle.com/code/mlwhiz/rectools-hstu">&#128211; rectools-ml1m.ipynb</a> &#8212; the rectools HSTU + SASRec baseline notebook for the comparison numbers </p></div><div><hr></div><h2>1. The dataset: MovieLens-1M ratings as (item, action, time) triples</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k2g8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k2g8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 424w, https://substackcdn.com/image/fetch/$s_!k2g8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 848w, https://substackcdn.com/image/fetch/$s_!k2g8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 1272w, https://substackcdn.com/image/fetch/$s_!k2g8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!k2g8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png" width="1456" height="647" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:647,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Data pipeline: raw MovieLens ratings get mapped to POSITIVE/NEUTRAL/NEGATIVE actions, sorted into per-user sequences, then split into train/val/test via leave-one-out&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Data pipeline: raw MovieLens ratings get mapped to POSITIVE/NEUTRAL/NEGATIVE actions, sorted into per-user sequences, then split into train/val/test via leave-one-out" title="Data pipeline: raw MovieLens ratings get mapped to POSITIVE/NEUTRAL/NEGATIVE actions, sorted into per-user sequences, then split into train/val/test via leave-one-out" srcset="https://substackcdn.com/image/fetch/$s_!k2g8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 424w, https://substackcdn.com/image/fetch/$s_!k2g8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 848w, https://substackcdn.com/image/fetch/$s_!k2g8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 1272w, https://substackcdn.com/image/fetch/$s_!k2g8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8cc014b-d6ac-4c94-ab86-573a37546195_2020x897.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>One of the most basic but important questions that we need to answer is how the data is structured. We&#8217;re using <a href="https://grouplens.org/datasets/movielens/1m/">MovieLens-1M</a> from GroupLens which contains ~1M ratings across 6,040 users and 3,706 movies.</p><p>Each row is <code>(user, movie, rating, timestamp)</code> where rating is 1-5 stars. SASRec would treat every rating as one positive interaction. HSTU&#8217;s input can be richer &#8212; it can fuse the <em>action type</em> alongside the item ID &#8212; so I want to give the model the rating sentiment, not just the fact that the user watched the movie. This is how I&#8217;m incorporating an action signal in this model. Honestly, every setup can have a different action vocabulary &#8212; add-to-cart vs. purchase on an e-commerce store, click vs. like vs. share on a feed, watch-25% vs. watch-90% vs. skip on a video platform. The point I want to make here is that HSTU lets you encode whichever signal actually matters for your domain, not just &#8220;the user clicked this item.&#8221;</p><p>For our case, we create three behavioral signals, derived from the rating value:</p><ul><li><p><strong>POSITIVE</strong> (rating &#8805; 4): the user liked the movie</p></li><li><p><strong>NEUTRAL</strong> (rating = 3): the user was indifferent</p></li><li><p><strong>NEGATIVE</strong> (rating &lt; 3): the user disliked the movie</p></li></ul><p>The actual rating value (1-5) is kept separately as the label for the rating-prediction head.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import pandas as pd
import numpy as np

ratings = pd.read_csv(&#8221;ml-1m/ratings.dat&#8221;, sep=&#8221;::&#8221;, header=None,
                      names=[&#8221;user&#8221;, &#8220;item&#8221;, &#8220;rating&#8221;, &#8220;ts&#8221;], engine=&#8221;python&#8221;)

def rating_to_action(r):
    if r &gt;= 4: return &#8220;POSITIVE&#8221;
    if r == 3: return &#8220;NEUTRAL&#8221;
    return &#8220;NEGATIVE&#8221;

events = ratings.copy()
events[&#8221;action&#8221;] = events[&#8221;rating&#8221;].apply(rating_to_action)
events[&#8221;value&#8221;]  = events[&#8221;rating&#8221;].astype(np.float32)

print(events.action.value_counts())
# POSITIVE    575281
# NEUTRAL     261197
# NEGATIVE    163731</code></pre></div><p><strong>Train/test split: leave-one-out.</strong> For each user, the last interaction goes to test, and everything before that goes to training. During training, the <em>last item of the training sequence</em> is held out as the validation target.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">def split_seq(seq, max_len=200):
    n = len(seq[&#8221;items&#8221;])
    if n &lt; 3: return None
    history_slice = slice(max(0, n - 1 - max_len), n - 1)
    history = {k: seq[k][history_slice].tolist() for k in seq}
    test_target = (
        int(seq[&#8221;items&#8221;][n - 1]),
        int(seq[&#8221;actions&#8221;][n - 1]),
        int(seq[&#8221;times&#8221;][n - 1]),
        float(seq[&#8221;values&#8221;][n - 1]),
    )
    return history, test_target

splits = [s for s in (split_seq(seq) for seq in sequences) if s is not None]
# Train/test sequences: 6,040
</code></pre></div><p><code>max_len=200</code> is the cap. With an average length of 165, most users will fit. Long-tail users get truncated to their most recent 200 events.</p><div><hr></div><h2>2. The fused input: item embedding + action embedding + fusion MLP</h2><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aaUT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aaUT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 424w, https://substackcdn.com/image/fetch/$s_!aaUT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 848w, https://substackcdn.com/image/fetch/$s_!aaUT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 1272w, https://substackcdn.com/image/fetch/$s_!aaUT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aaUT!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png" width="1200" height="225" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:273,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Fused input layer: item_emb (D) and action_emb (D) concatenate to (B, T, 2D), pass through Linear &#8594; SiLU &#8594; Linear MLP, output a single fused vector (B, T, D)&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="Fused input layer: item_emb (D) and action_emb (D) concatenate to (B, T, 2D), pass through Linear &#8594; SiLU &#8594; Linear MLP, output a single fused vector (B, T, D)" title="Fused input layer: item_emb (D) and action_emb (D) concatenate to (B, T, 2D), pass through Linear &#8594; SiLU &#8594; Linear MLP, output a single fused vector (B, T, D)" srcset="https://substackcdn.com/image/fetch/$s_!aaUT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 424w, https://substackcdn.com/image/fetch/$s_!aaUT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 848w, https://substackcdn.com/image/fetch/$s_!aaUT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 1272w, https://substackcdn.com/image/fetch/$s_!aaUT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e236ce-d3e9-49d9-9fc6-e271c52b323d_2798x525.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Here&#8217;s the actual code.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import torch
import torch.nn as nn
import torch.nn.functional as F

class FusedInputEmbedding(nn.Module):
    &#8220;&#8221;&#8220;Item embedding + action embedding, fused with an MLP into a single D-dim vector.&#8221;&#8220;&#8221;
    def __init__(self, num_items, num_actions, dim):
        super().__init__()
        self.item_emb   = nn.Embedding(num_items + 1, dim, padding_idx=0)
        self.action_emb = nn.Embedding(num_actions + 1, dim, padding_idx=0)
        self.fuse = nn.Sequential(
            nn.Linear(2 * dim, dim),
            nn.SiLU(),
            nn.Linear(dim, dim),
        )

    def forward(self, item_ids, action_ids):
        i = self.item_emb(item_ids)      # (B, T, D)
        a = self.action_emb(action_ids)  # (B, T, D)
        return self.fuse(torch.cat([i, a], dim=-1))  # (B, T, D)
</code></pre></div><p>What&#8217;s happening here is that each item ID looks up a D-dim vector in <code>item_emb</code> (the &#8220;what is this item&#8221; signal) table, and each action ID looks up another D-dim vector in <code>action_emb</code> (the &#8220;how did the user engage with it&#8221; signal). </p><p>The forward pass then concatenates them into a 2D-dim vector per token, then the <code>fuse</code> MLP projects back to D dims.</p><p>The MLP matters. It&#8217;s what lets the model learn that &#8220;watched Toy Story with rating 5&#8221; represents something different from &#8220;watched Toy Story with rating 1&#8221;. A simple sum or concatenation wouldn&#8217;t give the model the capacity to learn that interaction.</p><div><hr></div><h2>3. The HSTU block in PyTorch</h2><p>Now, it is time to write the actual HSTU block. If you want to refresh on what each piece does, <a href="https://www.mlwhiz.com/p/hstu-how-meta-built-a-trillion-parameter">Part 9c walks through the three sub-layers conceptually</a>. Here I&#8217;ll just translate that directly into code.</p><p>First, the relative attention bias (RAB) module. Two learnable tables: one for relative position offset, one for log-spaced time buckets.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">class RelativeAttentionBias(nn.Module):
    &#8220;&#8221;&#8220;Learnable position + time biases, added to QK^T before SiLU.&#8221;&#8220;&#8221;
    TIME_BUCKETS = [
        (0, 3600),                  # 0-1 hour
        (3600, 86400),              # 1-24 hours
        (86400, 86400 * 7),         # 1-7 days
        (86400 * 7, 86400 * 30),    # 7-30 days
        (86400 * 30, float(&#8221;inf&#8221;)), # 30+ days
    ]

    def __init__(self, max_seq_len):
        super().__init__()
        self.max_seq_len = max_seq_len
        self.pos_bias  = nn.Embedding(2 * max_seq_len - 1, 1)
        self.time_bias = nn.Embedding(len(self.TIME_BUCKETS), 1)
        nn.init.zeros_(self.pos_bias.weight)
        nn.init.zeros_(self.time_bias.weight)

    def _bucket(self, time_deltas):
        out = torch.zeros_like(time_deltas, dtype=torch.long)
        for k, (lo, hi) in enumerate(self.TIME_BUCKETS):
            mask = (time_deltas &gt;= lo) &amp; (time_deltas &lt; hi)
            out = torch.where(mask, torch.full_like(out, k), out)
        return out

    def forward(self, times):
        B, T = times.shape
        idx = torch.arange(T, device=times.device)
        rel_pos = (idx.view(T, 1) - idx.view(1, T)) + (self.max_seq_len - 1)
        pos_b = self.pos_bias(rel_pos).squeeze(-1)
        time_deltas = (times.unsqueeze(2) - times.unsqueeze(1)).abs()
        time_b = self.time_bias(self._bucket(time_deltas)).squeeze(-1)
        return pos_b.unsqueeze(0) + time_b
</code></pre></div><p>Initialize both bias tables to zeros. That way, the first forward pass behaves like a vanilla attention block, and the time/position biases learn from gradient flow.</p><p>Now the HSTU block itself:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">class HSTUBlock(nn.Module):
    def __init__(self, dim, max_seq_len, dropout=0.2):
        super().__init__()
        self.linear_in  = nn.Linear(dim, 4 * dim)
        self.rab        = RelativeAttentionBias(max_seq_len)
        self.norm       = nn.LayerNorm(dim)
        self.linear_out = nn.Linear(dim, dim)
        self.dropout    = nn.Dropout(dropout)

    def forward(self, x, times, attn_mask):
        B, T, D = x.shape

        # --- Sub-layer 1: pointwise projection ---
        proj = F.silu(self.linear_in(x))
        K, Q, V, U = proj.chunk(4, dim=-1)

        # --- Sub-layer 2: spatial aggregation (SiLU attention + RAB + causal/pad mask) ---
        scores = torch.matmul(Q, K.transpose(-2, -1)) + self.rab(times)
        causal = torch.tril(torch.ones(T, T, device=x.device))
        scores = scores * causal
        pad_mask = attn_mask.unsqueeze(1).float()
        scores = scores * pad_mask
        activated = F.silu(scores)                     # NOT softmax &#8212; pointwise SiLU
        attn_out  = torch.matmul(activated, V) / T     # 1/T normalization

        # --- Sub-layer 3: gated transformation + residual ---
        gated  = self.norm(attn_out) * U
        output = self.dropout(self.linear_out(gated))
        return output + x
</code></pre></div><p>Stacking blocks is one line:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">class HSTUEncoder(nn.Module):
    def __init__(self, num_items, num_actions, dim, num_layers, max_seq_len, dropout=0.2):
        super().__init__()
        self.embed  = FusedInputEmbedding(num_items, num_actions, dim)
        self.blocks = nn.ModuleList([
            HSTUBlock(dim, max_seq_len, dropout=dropout) for _ in range(num_layers)
        ])

    def forward(self, item_ids, action_ids, times, attn_mask):
        x = self.embed(item_ids, action_ids)
        for block in self.blocks:
            x = block(x, times, attn_mask)
        return x
</code></pre></div><p>That&#8217;s the encoder.</p><div><hr></div><p><em><strong>The rest of this post covers the multi-task heads (retrieval + rating), the full results table comparing our from-scratch HSTU to rectools HSTU and SASRec, the SiLU vs softmax ablation, the learned time-bias curve, M-FALCON inference benchmark (210&#215; speedup), and a production field guide.</strong></em><strong> </strong></p>
      <p>
          <a href="https://www.mlwhiz.com/p/hstu-from-scratch-in-pytorch-a-complete">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly Recsys/ML/GenAI Newsletter # 8 - The week of Google I/O 2026]]></title><description><![CDATA[Google I/O opened up a lot of eyes for major AI firms]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-3da</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-3da</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Tue, 26 May 2026 21:01:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!SWGV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b4d6389-ba09-4d90-af16-e8cc0ad98dd9_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-3da">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly Recsys/ML/GenAI Newsletter # 7 - The week Karpathy Joined Anthropic]]></title><description><![CDATA[The week Andrej Karpathy picked his side, and everyone else picked theirs.]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-0e1</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-0e1</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Tue, 19 May 2026 22:40:02 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ioEB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4cfcbb5e-4d1d-4f5c-9683-c5b432dfd040_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter-0e1">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[HSTU: How Meta Built a Trillion-Parameter Recommender That Actually Scales]]></title><description><![CDATA[The architecture, the math, the code, and why every RecSys team is suddenly building one]]></description><link>https://www.mlwhiz.com/p/hstu-how-meta-built-a-trillion-parameter</link><guid isPermaLink="false">https://www.mlwhiz.com/p/hstu-how-meta-built-a-trillion-parameter</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Mon, 18 May 2026 22:19:45 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!QEsv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QEsv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QEsv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!QEsv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!QEsv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!QEsv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QEsv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2395885,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/197933865?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QEsv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!QEsv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!QEsv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!QEsv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffae5fc51-b381-4f00-a3d0-44187337b086_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>This is Part 9c of the RecSys for MLEs series. <a href="https://www.mlwhiz.com/p/rnns-to-transformers-sequential-recommenders">In Part 9a, we built GRU4Rec and SASRec from scratch</a> on the Steam Games dataset and got our hands dirty with sequential recommenders. <a href="https://www.mlwhiz.com/p/semantic-ids-rqvae-generative-recommender">In Part 9b, we covered Semantic IDs and TIGER</a>, Google&#8217;s clever approach to generative retrieval, where item IDs become a learned token vocabulary. Now we close the arc with the architecture that&#8217;s actually running at Meta scale: HSTU.</em></p><div><hr></div><p>Late 2022. You&#8217;re an ML engineer on a recommendation team, and you&#8217;ve just shipped a beefed-up SASRec model. The metrics look great. Your director walks over and says: <em>&#8220;What if we just scaled this thing? More layers, bigger embeddings, a hundred billion parameters. Like the LLM folks are doing.&#8221;</em></p><p>So you do. And for a while, the model gets better. Then it stops getting better. Then you throw more compute at it and nothing. No improvement. A bigger, slower, more expensive model that performs about the same.</p><p>Frustrating, because over in NLP-land the scaling laws are <em>clean</em>: double the compute, get a predictably better model. GPT-3 had proven that. LLaMA was about to prove it again. DLRMs (Deep Learning Recommendation Models)? They kept plateauing. Something was fundamentally broken, and nobody could quite articulate what.</p><p>Meta&#8217;s answer was <strong>HSTU</strong> (Hierarchical Sequential Transduction Units). </p><p>Here&#8217;s what we&#8217;ll work through in this post:</p><ul><li><p><strong>The three structural issues</strong> that quietly sabotage standard Transformers when you point them at recommendation data</p></li><li><p><strong>What HSTU actually consumes</strong>: the fused (item, action) input format, plus the actual training data schema (impression table + history table)</p></li><li><p><strong>Inside the HSTU block</strong>: three sub-layers, with the math, the code, the intuition, and the diagrams</p></li><li><p><strong>M-FALCON</strong>: the caching optimization that lets you score 10,000 candidates without recomputing 8,000 history events 10,000 times</p></li></ul><p>By the end, you should understand exactly <em>why</em> HSTU works, <em>how</em> it works, and why every big tech RecSys team is suddenly building one.</p><div><hr></div><h2>1. Three reasons standard Transformers fail for RecSys</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hzpz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hzpz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 424w, https://substackcdn.com/image/fetch/$s_!hzpz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 848w, https://substackcdn.com/image/fetch/$s_!hzpz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 1272w, https://substackcdn.com/image/fetch/$s_!hzpz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hzpz!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png" width="1200" height="567.032967032967" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:688,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Softmax vs SiLU Attention&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="Softmax vs SiLU Attention" title="Softmax vs SiLU Attention" srcset="https://substackcdn.com/image/fetch/$s_!hzpz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 424w, https://substackcdn.com/image/fetch/$s_!hzpz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 848w, https://substackcdn.com/image/fetch/$s_!hzpz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 1272w, https://substackcdn.com/image/fetch/$s_!hzpz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5527b6e5-fb30-4670-89e5-7ec9b60a4e0b_2168x1025.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Three structural mismatches between language data and recommendation data make standard Transformers fail at scale. These are fundamental, not edge cases you can patch with a clever hack.</p><p>If you&#8217;d like a refresher on <a href="https://www.mlwhiz.com/p/transformers">how standard Transformer attention works</a> before going further, that post will get you up to speed. From here on, I&#8217;ll assume you&#8217;re comfortable with Q, K, V, softmax, and self-attention.</p><h3>Problem 01: non-stationary vocabularies</h3><p>In NLP, your vocabulary is fixed. &#8220;The&#8221; is always token 42. &#8220;Recommendation&#8221; is always token 18,973. The model trains with a stable set of possible next tokens, and that set doesn&#8217;t really change between training and serving.</p><p>In recommendations, your &#8220;vocabulary&#8221; is the entire item catalog, and that catalog is constantly changing. New videos go live every second on Reels. A trending creator didn&#8217;t exist yesterday. The model you trained last Tuesday has never seen most of the items it&#8217;s being asked to score on a Friday.</p><p><strong>Softmax was designed for a fixed vocabulary with stable class boundaries.</strong> It&#8217;s <em>defined</em> over a set of mutually exclusive classes that sum to 1. When the set of possible &#8220;next tokens&#8221; is a moving target, when items appear and disappear from the catalog every minute, the softmax assumption starts to silently misbehave. The probability mass keeps getting redistributed among the items the model happens to have seen, which is not the same set as the items it needs to recommend.</p><p>This is a much bigger deal than it sounds. Almost every metric you care about (CTR, watch time, retention) depends on the model surfacing <em>new</em> items that the user will love. If your normalization assumes a stable world, you&#8217;re starting from a broken assumption.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/hstu-how-meta-built-a-trillion-parameter">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly Recsys/ML/GenAI Newsletter # 6]]></title><description><![CDATA[The week the AI found curl vulnerabilities and the developers discussed AI usage while coding]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Fri, 15 May 2026 00:28:47 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!NbJT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6ee152-ceb6-43e0-ad9b-23ee9a35a302_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><p>I love keeping track of everything week to week &#8212; here's what happened this week. Enjoy this free weekly post! For those who want to dive deeper into any of these topics, that's what my paid posts are for.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-recsysmlgenai-newsletter">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[From Random IDs to Semantic IDs: Building a Generative Recommender from Scratch]]></title><description><![CDATA[How RQVAE compresses item embeddings into meaningful tokens, enabling TIGER-style generative recommendation. Full code walkthrough on Steam Games with Qwen embeddings.]]></description><link>https://www.mlwhiz.com/p/semantic-ids-rqvae-generative-recommender</link><guid isPermaLink="false">https://www.mlwhiz.com/p/semantic-ids-rqvae-generative-recommender</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Sat, 09 May 2026 01:29:54 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!_kSC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_kSC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_kSC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!_kSC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!_kSC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!_kSC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_kSC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2440950,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196328148?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_kSC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!_kSC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!_kSC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!_kSC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bf61a2-8a27-41bb-9d24-a45788a72425_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>At the end of <a href="https://www.mlwhiz.com/p/rnns-to-transformers-sequential-recommenders">Part 1</a>, I left you with a question: <em><strong>What if items had semantic identifiers that captured their content?</strong></em></p><p>That question sounds innocent enough, but it&#8217;s actually the hinge point for the entire generative recommender revolution we are going to be talking about in this post. And it is very interesting to say the least.</p><p>So, we built GRU4Rec and SASRec on the Steam Games dataset in our previous post. But both those models treat items as arbitrary integers. The model sees &#8220;Item 4,271 &#8594; Item 8,903 &#8594; Item 2,156&#8221; and learns some sort of statistical patterns between these numbers. </p><p>The thing we need to note is that everything about what these games actually <em>are</em> &#8212; their genre, their developer, their visual style, the <em>reason</em> a player moves from one to the next &#8212; lives entirely outside the model. That is a pretty big opportunity to work on.</p><p>Now imagine if every item carried an ID that <em>meant</em> something. Similar games naturally share ID prefixes. <em><strong>A brand-new title gets a meaningful ID the moment it&#8217;s published &#8212; before anyone plays it.</strong></em> The recommender can reason about games it&#8217;s never seen just from their ID, just like a human would by seeing a game&#8217;s description or title.</p><p>That&#8217;s <strong>Semantic IDs</strong>.</p><p>And these semantic IDs unlock something bigger &#8594;instead of scoring candidates from a retrieved shortlist(which is how SASRec and GRU4Rec work), a model can now <em>generate</em> the next item token by token, the way GPT generates words. </p><p>In this post, we will create a complete pipeline to do exactly that on the same Steam dataset, every game compressed into meaningful tokens using <strong>Residual Quantized VAE</strong>, and a generative recommender(<strong>TIGER</strong>) trained from scratch.</p><p>Let&#8217;s dive in!</p><div><hr></div><h2>1. The Problem with Random Item IDs</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4hDh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4hDh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 424w, https://substackcdn.com/image/fetch/$s_!4hDh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 848w, https://substackcdn.com/image/fetch/$s_!4hDh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 1272w, https://substackcdn.com/image/fetch/$s_!4hDh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4hDh!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png" width="1200" height="440.1098901098901" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:534,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:295213,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196328148?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4hDh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 424w, https://substackcdn.com/image/fetch/$s_!4hDh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 848w, https://substackcdn.com/image/fetch/$s_!4hDh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 1272w, https://substackcdn.com/image/fetch/$s_!4hDh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a79638-15c2-4cf2-9cf5-5578aebe55e8_2956x1084.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Just think about how SASRec works for a second - A user plays [Counter-Strike, Portal, Half-Life 2]. The model looks up the embedding for each game, runs self-attention, and predicts the next game. So far, so good.</p><p>But what does the model actually <em>know</em> about Counter-Strike? Nothing. It&#8217;s just item ID 4,271. The model has no idea it&#8217;s a first-person shooter made by Valve in 2004. </p><p>This creates three problems that compound in production:</p><ol><li><p><strong>No knowledge sharing.</strong> If 10,000 users play Counter-Strike &#8594; Team Fortress 2, the model learns that specific transition. But it learns <em>nothing</em> about why they&#8217;re related. A new Valve FPS arrives tomorrow, and the model has zero signal for it &#8212; even though any human could tell you &#8220;people who like Counter-Strike would probably like this.&#8221;</p></li><li><p><strong>Cold-start is brutal.</strong> New items have brand-new IDs with randomly initialized embeddings. As I covered in my <a href="https://www.mlwhiz.com/p/cold-start-problem-recsys-modern-approaches">post on the cold-start problem</a>, this means they need thousands of interactions before the model can meaningfully recommend them. In fast-moving catalogs &#8212; think news, short videos, new game releases &#8212; items can go stale before the model even learns to recommend them.</p></li><li><p><strong>No generalization.</strong> The model memorizes specific ID&#8594;ID transitions. It can&#8217;t reason about <em>categories</em> of items, <em>properties</em> of items, or <em>relationships</em> between items.</p></li></ol><p>And that&#8217;s where semantic IDs could help us.</p><div><hr></div><h2>2. Semantic IDs: Making Item IDs mean Something</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CCZk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CCZk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 424w, https://substackcdn.com/image/fetch/$s_!CCZk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 848w, https://substackcdn.com/image/fetch/$s_!CCZk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 1272w, https://substackcdn.com/image/fetch/$s_!CCZk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CCZk!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png" width="1200" height="506.86813186813185" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:615,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:174157,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196328148?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CCZk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 424w, https://substackcdn.com/image/fetch/$s_!CCZk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 848w, https://substackcdn.com/image/fetch/$s_!CCZk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 1272w, https://substackcdn.com/image/fetch/$s_!CCZk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2e8800-bf65-4ac1-b64a-079c1b138c10_1610x680.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Ok, so now we understand why Semantic IDs might be beneficial, let&#8217;s see how they are made. The idea behind <strong>Semantic IDs</strong> was introduced in <a href="https://papers.neurips.cc/paper_files/paper/2023/file/20dcab0f14046a5c6b02b61da9f13229-Paper-Conference.pdf">TIGER</a> (Transformer Index for Generative Recommenders) at NeurIPS 2023, and it completely changed how we think about item representation in recommender systems.</p><p>The pipeline works in three steps. Here&#8217;s what happens to a single game:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ah85!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ah85!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 424w, https://substackcdn.com/image/fetch/$s_!ah85!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 848w, https://substackcdn.com/image/fetch/$s_!ah85!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 1272w, https://substackcdn.com/image/fetch/$s_!ah85!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ah85!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png" width="1200" height="412.9120879120879" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:501,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:167701,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196328148?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ah85!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 424w, https://substackcdn.com/image/fetch/$s_!ah85!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 848w, https://substackcdn.com/image/fetch/$s_!ah85!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 1272w, https://substackcdn.com/image/fetch/$s_!ah85!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84374c63-fea3-4005-972f-b6376ddbea4e_1766x608.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In step 1, we start by getting a content-based embedding of an item using a powerful text encoder. </p><p>Once we have that, we run the RQVAE algorithm in step 2 to generate Semantic IDs of items. (<em>Don&#8217;t worry, we will talk about this in this post. For now, just understand that it gives you some sort of discrete token-based vector given your item embeddings</em>)</p><p>In Step 3, we <em>generate</em> the next item&#8217;s Semantic ID token by token, the same way GPT writes one word at a time. It&#8217;s not scoring a fixed list; it&#8217;s constructing an answer.</p><p>Now think about what this structure gives you. Counter-Strike and Half-Life might both get the prefix [42, 187, ...] because they&#8217;re both Valve FPS games. A brand-new Valve FPS that launched this morning might then be assigned a similar prefix based purely on its content &#8212; and the model already knows what to do with items that start with [42, 187, ...], even though it&#8217;s never seen a single player interact with this game.</p><p>Remember how in the cold start <a href="https://www.mlwhiz.com/p/cold-start-problem-recsys-modern-approaches">post I wrote</a> about how new items have nothing but their metadata to work with? With Semantic IDs, the metadata <em>becomes their</em> ID. The model doesn&#8217;t need thousands of interactions to figure out what a new game is &#8212; it already knows, just from reading the ID.</p><p>And notice what else changed: we replaced the entire <a href="https://www.mlwhiz.com/p/the-recommendation-engine-under-the">retrieve&#8594;rank&#8594;rerank pipeline</a> with a single model that generates recommendations directly. At inference time, the decoder uses <strong>beam search</strong>: instead of greedily committing to one token at each level and hoping for the best, it keeps the top B candidates at each step and extends them all in parallel. You end up with a ranked list of complete Semantic IDs, each mapping to a real item. That means you don&#8217;t have to worry about maintaining an ANN index, a candidate retrieval stage, and a separate ranker stage. You can get all of this from a single model. </p><p>Now, the magic of this pipeline lives entirely in Step 2 &#8212; the RQVAE. That is how we create these semantic IDs. And if  the quantization is bad, the Semantic IDs are meaningless, and we&#8217;re back to square one. So let&#8217;s understand exactly how it works.</p><div><hr></div><h2>3. How RQVAE Works &#8212; The Engine Behind Semantic IDs</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bn71!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bn71!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 424w, https://substackcdn.com/image/fetch/$s_!bn71!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 848w, https://substackcdn.com/image/fetch/$s_!bn71!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 1272w, https://substackcdn.com/image/fetch/$s_!bn71!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bn71!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png" width="1200" height="721.1538461538462" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:875,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:238280,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196328148?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bn71!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 424w, https://substackcdn.com/image/fetch/$s_!bn71!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 848w, https://substackcdn.com/image/fetch/$s_!bn71!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 1272w, https://substackcdn.com/image/fetch/$s_!bn71!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c4277d4-3d19-47ae-bfba-569aa203f16b_1620x974.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Residual Quantized VAE</strong> sounds intimidating, but the intuition is straightforward. It&#8217;s progressive compression &#8212; like describing a location with increasing precision.</p><p>&#8220;North America&#8221; tells you the continent. &#8220;California&#8221; narrows it down. &#8220;San Francisco&#8221; pins it to a city. Each level adds detail that the previous level didn&#8217;t capture. RQVAE does exactly this, but for item embeddings.</p><p>Here&#8217;s the algorithm. </p><p><strong>Step 1: Encode.</strong> Take the 1024-dim item embedding and compress it through an encoder network (1024 &#8594; 512 &#8594; 256 &#8594; 128 &#8594; 32). It&#8217;s easier to follow with concrete numbers, so let&#8217;s walk through a simplified example &#8212; we will use 4 dimensions instead of 32, but the math is identical.</p><pre><code>Encoded: [0.8, -0.3, 0.5, 0.1]</code></pre><p><strong>Step 2: Level 1 Quantization.</strong> You have a <em>codebook</em> &#8212; a table of 256 learned vectors. Find the one closest to your latent vector:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!g4Y_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!g4Y_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 424w, https://substackcdn.com/image/fetch/$s_!g4Y_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 848w, https://substackcdn.com/image/fetch/$s_!g4Y_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 1272w, https://substackcdn.com/image/fetch/$s_!g4Y_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!g4Y_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png" width="950" height="422" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c72486f2-5c73-41d6-a890-8a991fd55605_950x422.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:422,&quot;width&quot;:950,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:54806,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196328148?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!g4Y_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 424w, https://substackcdn.com/image/fetch/$s_!g4Y_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 848w, https://substackcdn.com/image/fetch/$s_!g4Y_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 1272w, https://substackcdn.com/image/fetch/$s_!g4Y_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc72486f2-5c73-41d6-a890-8a991fd55605_950x422.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div>
      <p>
          <a href="https://www.mlwhiz.com/p/semantic-ids-rqvae-generative-recommender">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly AI/ML/Recsys Newsletter # 5]]></title><description><![CDATA[The week the AI industry's partnership era ended &#8212; and the consulting era began.]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-aimlrecsys-newsletter</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-aimlrecsys-newsletter</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Wed, 06 May 2026 23:12:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!hkHi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hkHi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hkHi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!hkHi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!hkHi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!hkHi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hkHi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2090857,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/196716889?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hkHi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!hkHi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!hkHi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!hkHi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5422bdcf-9529-48fb-965e-0768215d2bdf_3200x2134.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Story of the Week: The Great Unbundling</h2><p>The Microsoft&#8211;OpenAI <a href="https://www.theverge.com/ai-artificial-intelligence/918981/openai-microsoft-renegotiate-contract">partnership</a> came apart Sunday night. Revenue sharing, the AGI clause, and exclusivity all dissolved in a single restructuring. </p><p>Twenty-four hours later, Sam Altman and AWS CEO Matt Garman were on Stratechery announcing OpenAI models on Amazon Bedrock. The speed tells you the deal was pre-negotiated; only the Microsoft contract was holding it back.</p><p><strong>The practical change for ML teams:</strong> GPT models now run natively on Bedrock alongside Claude, Llama, and Mistral. If you standardized on AWS but routed around for OpenAI access, that workaround is gone. <em><strong>If you picked Azure specifically for OpenAI, you have a real alternative for the first time.</strong></em> Multi-cloud routing for frontier models is table stakes now &#8212; expect inference prices to drop and cross-cloud model-parity to start mattering in vendor evaluations.</p><p>Then both labs made a stranger move. Anthropic and OpenAI launched enterprise consulting arms on the same day, aimed at different mar&#8230;</p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-aimlrecsys-newsletter">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[Claude Code vs. Your ML Career: A 2026 Reality Check]]></title><description><![CDATA[The ML job market didn't die. It split into two &#8212; here's how to land on the right side.]]></description><link>https://www.mlwhiz.com/p/will-claude-code-take-your-ml-job</link><guid isPermaLink="false">https://www.mlwhiz.com/p/will-claude-code-take-your-ml-job</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Tue, 28 Apr 2026 22:18:23 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!mffB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7885cd7-467f-428f-9f29-54c2825d332a_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p>
      <p>
          <a href="https://www.mlwhiz.com/p/will-claude-code-take-your-ml-job">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[The Most Complete Guide to PyTorch for Data Scientists]]></title><description><![CDATA[Pytorch is OG]]></description><link>https://www.mlwhiz.com/p/pytorch_guide</link><guid isPermaLink="false">https://www.mlwhiz.com/p/pytorch_guide</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Mon, 27 Apr 2026 00:00:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJst!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AJst!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AJst!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 424w, https://substackcdn.com/image/fetch/$s_!AJst!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 848w, https://substackcdn.com/image/fetch/$s_!AJst!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 1272w, https://substackcdn.com/image/fetch/$s_!AJst!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AJst!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;The Most Complete Guide to PyTorch for Data Scientists&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="The Most Complete Guide to PyTorch for Data Scientists" title="The Most Complete Guide to PyTorch for Data Scientists" srcset="https://substackcdn.com/image/fetch/$s_!AJst!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 424w, https://substackcdn.com/image/fetch/$s_!AJst!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 848w, https://substackcdn.com/image/fetch/$s_!AJst!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 1272w, https://substackcdn.com/image/fetch/$s_!AJst!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06047a8f-c4f0-4e72-a492-a82df82e196e_1920x1280.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em><strong>PyTorch</strong></em> has sort of became one of the de facto standards for creating Neural Networks now, and I love its interface. Yet, it is somehow a little difficult for beginners to get a hold of.</p><p>I remember picking PyTorch up only after some extensive experimentation a couple of years back. To tell you the truth, it took me a lot of time to pick it up but am I glad that I moved from <strong><a href="https://towardsdatascience.com/moving-from-keras-to-pytorch-f0d4fff4ce79">Keras to PyTorch</a></strong> . With its high customizability and pythonic syntax,PyTorch is just a joy to work with, and I would recommend it to anyone who wants to do some heavy lifting with Deep Learning.</p><p>So, in this PyTorch guide, <em><strong>I will try to ease some of the pain with PyTorch for starters</strong></em> and go through some of the most important classes and modules that you will require while creating any Neural Network with Pytorch.</p><p>But, that is not to say that this is aimed at beginners only as <em><strong>I will also talk about the</strong></em> <em><strong>high customizability PyTorch provides and will talk about custom Layers, Datasets, Dataloaders, and Loss functions</strong></em>.</p><p>So let&#8217;s get some coffee &#9749; &#65039;and start it up.</p><div><hr></div><h2><strong>Tensors</strong></h2><p>Tensors are the basic building blocks in PyTorch and put very simply, they are NumPy arrays but on GPU. In this part, I will list down some of the most used operations we can use while working with Tensors. This is by no means an exhaustive list of operations you can do with Tensors, but it is helpful to understand what tensors are before going towards the more exciting parts.</p><h3><strong>1. Create a Tensor</strong></h3><p>We can create a PyTorch tensor in multiple ways. This includes converting to tensor from a NumPy array. Below is just a small gist with some examples to start with, but you can do a whole lot of <strong><a href="https://pytorch.org/docs/stable/tensors.html">more things</a></strong> with tensors just like you can do with NumPy arrays.</p><pre><code><code># Using torch.Tensor
t = torch.Tensor([[1,2,3],[3,4,5]])
print(f"Created Tensor Using torch.Tensor:\n{t}")

# Using torch.randn
t = torch.randn(3, 5)
print(f"Created Tensor Using torch.randn:\n{t}")

# using torch.[ones|zeros](*size)
t = torch.ones(3, 5)
print(f"Created Tensor Using torch.ones:\n{t}")
t = torch.zeros(3, 5)
print(f"Created Tensor Using torch.zeros:\n{t}")

# using torch.randint - a tensor of size 4,5 with entries between 0 and 10(excluded)
t = torch.randint(low = 0,high = 10,size = (4,5))
print(f"Created Tensor Using torch.randint:\n{t}")

# Using from_numpy to convert from Numpy Array to Tensor
a = np.array([[1,2,3],[3,4,5]])
t = torch.from_numpy(a)
print(f"Convert to Tensor From Numpy Array:\n{t}")

# Using .numpy() to convert from Tensor to Numpy array
t = t.numpy()
print(f"Convert to Numpy Array From Tensor:\n{t}")
</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yFko!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yFko!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 424w, https://substackcdn.com/image/fetch/$s_!yFko!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 848w, https://substackcdn.com/image/fetch/$s_!yFko!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 1272w, https://substackcdn.com/image/fetch/$s_!yFko!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yFko!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png" width="1216" height="561" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:561,&quot;width&quot;:1216,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;MLWhiz: Data Science, Machine Learning, Artificial Intelligence&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" title="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" srcset="https://substackcdn.com/image/fetch/$s_!yFko!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 424w, https://substackcdn.com/image/fetch/$s_!yFko!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 848w, https://substackcdn.com/image/fetch/$s_!yFko!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 1272w, https://substackcdn.com/image/fetch/$s_!yFko!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df02c37-6da2-43cc-b2c1-4e84534dab60_1216x561.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>2. Tensor Operations</strong></h3><p>Again, there are a lot of operations you can do on these tensors. The full list of functions can be found <strong><a href="https://pytorch.org/docs/stable/torch.html?highlight=mm#math-operations">here</a></strong> .</p><pre><code><code>A = torch.randn(3,4)
W = torch.randn(4,2)
# Multiply Matrix A and W
t = A.mm(W)
print(f"Created Tensor t by Multiplying A and W:\n{t}")
# Transpose Tensor t
t = t.t()
print(f"Transpose of Tensor t:\n{t}")
# Square each element of t
t = t**2
print(f"Square each element of Tensor t:\n{t}")
# return the size of a tensor
print(f"Size of Tensor t using .size():\n{t.size()}")
</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!r_xE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!r_xE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 424w, https://substackcdn.com/image/fetch/$s_!r_xE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 848w, https://substackcdn.com/image/fetch/$s_!r_xE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 1272w, https://substackcdn.com/image/fetch/$s_!r_xE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!r_xE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png" width="1216" height="264" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:264,&quot;width&quot;:1216,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;MLWhiz: Data Science, Machine Learning, Artificial Intelligence&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" title="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" srcset="https://substackcdn.com/image/fetch/$s_!r_xE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 424w, https://substackcdn.com/image/fetch/$s_!r_xE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 848w, https://substackcdn.com/image/fetch/$s_!r_xE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 1272w, https://substackcdn.com/image/fetch/$s_!r_xE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c973b3-9646-49f1-860d-059a2ead7930_1216x264.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><strong>Note:</strong> What are PyTorch Variables? In the previous versions of Pytorch, Tensor and Variables used to be different and provided different functionality, but now the Variable API is <strong><a href="https://pytorch.org/docs/stable/autograd.html#variable-deprecated">deprecated</a></strong> , and all methods for variables work with Tensors. So, if you don&#8217;t know about them, it&#8217;s fine as they re not needed, and if you know them, you can forget about them.</p><div><hr></div><h2><strong>The nn.Module</strong></h2><p>Here comes the fun part as we are now going to talk about some of the most used constructs in Pytorch while creating deep learning projects. nn.Module lets you create your Deep Learning models as a class. You can inherit from nn.Moduleto define any model as a class. Every model class necessarily contains an<code> __init__</code> procedure block and a block for the <code>forward</code> pass.</p><ul><li><p>In the <code>__init__</code> part, the user can define all the layers the network is going to have but doesn&#8217;t yet define how those layers would be connected to each other.</p></li><li><p>In the <code>forward</code> pass block, the user defines how data flows from one layer to another inside the network.</p></li></ul><p>So, put simply, any network we define will look like:</p><pre><code><code>class myNeuralNet(nn.Module):
    def __init__(self):
        super().__init__()
        # Define all Layers Here
        self.lin1 = nn.Linear(784, 30)
        self.lin2 = nn.Linear(30, 10)
    def forward(self, x):
        # Connect the layer Outputs here to define the forward pass
        x = self.lin1(x)
        x = self.lin2(x)
        return x
</code></code></pre><p>Here we have defined a very simple Network that takes an input of size 784 and passes it through two linear layers in a sequential manner. But the thing to note is that we can define any sort of calculation while defining the forward pass, and that makes PyTorch highly customizable for research purposes. For example, in our crazy experimentation mode, we might have used the below network where we arbitrarily attach our layers. Here we send back the output from the second linear layer back again to the first one after adding the input to it(skip connection) back again(I honestly don&#8217;t know what that will do).</p><pre><code><code>class myCrazyNeuralNet(nn.Module):
    def __init__(self):
        super().__init__()
        # Define all Layers Here
        self.lin1 = nn.Linear(784, 30)
        self.lin2 = nn.Linear(30, 784)
        self.lin3 = nn.Linear(30, 10)

    def forward(self, x):
        # Connect the layer Outputs here to define the forward pass
        x_lin1 = self.lin1(x)
        x_lin2 = x + self.lin2(x_lin1)
        x_lin2 = self.lin1(x_lin2)
        x = self.lin3(x_lin2)
        return x
</code></code></pre><p>We can also check if the neural network forward pass works. I usually do that by first creating some random input and just passing that through the network I have created.</p><pre><code><code>x = torch.randn((100,784))
model = myCrazyNeuralNet()
model(x).size()
--------------------------
torch.Size([100, 10])
</code></code></pre><div><hr></div><h2><strong>A word about Layers</strong></h2><p>Pytorch is pretty powerful, and you can actually create any new experimental layer by yourself using <code>nn.Module</code>. For example, rather than using the predefined Linear Layer <code>nn.Linear</code> from Pytorch above, we could have created our <strong>custom linear layer</strong>.</p><pre><code><code>class myCustomLinearLayer(nn.Module):
    def __init__(self,in_size,out_size):
        super().__init__()
        self.weights = nn.Parameter(torch.randn(in_size, out_size))
        self.bias = nn.Parameter(torch.zeros(out_size))
    def forward(self, x):
        return x.mm(self.weights) + self.bias
</code></code></pre><p>You can see how we wrap our weights tensor in nn.Parameter. This is done to make the tensor to be considered as a model parameter. From PyTorch <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.parameter.Parameter.html#parameter">docs</a></strong> :</p><blockquote><p><em>Parameters are <strong><a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor">&lt;code&gt;*Tensor*&lt;/code&gt;</a></strong> subclasses, that have a very special property when used with Module - when they&#8217;re assigned as Module attributes they are automatically added to the list of its parameters, and will appear in </em><code>parameters()</code><em> iterator</em></p></blockquote><p>As you will later see, the <code>model.parameters()</code> iterator will be an input to the optimizer. But more on that later.</p><p>Right now, we can now use this custom layer in any PyTorch network, just like any other layer.</p><pre><code><code>class myCustomNeuralNet(nn.Module):
    def __init__(self):
        super().__init__()
        # Define all Layers Here
        self.lin1 = myCustomLinearLayer(784,10)

    def forward(self, x):
        # Connect the layer Outputs here to define the forward pass
        x = self.lin1(x)
        return x
x = torch.randn((100,784))
model = myCustomNeuralNet()
model(x).size()
------------------------------------------
torch.Size([100, 10])
</code></code></pre><p>But then again, Pytorch would not be so widely used if it didn&#8217;t provide a lot of ready to made layers used very frequently in wide varieties of Neural Network architectures. Some examples are: <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html#torch.nn.Linear">nn.Linear</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d">nn.Conv2d</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html#torch.nn.MaxPool2d">nn.MaxPool2d</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.ReLU.html#torch.nn.ReLU">nn.ReLU</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.BatchNorm2d.html#torch.nn.BatchNorm2d">nn.BatchNorm2d</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.Dropout.html#torch.nn.Dropout">nn.Dropout</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.Embedding.html#torch.nn.Embedding">nn.Embedding</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.GRU.html#torch.nn.GRU">nn.GRU</a></strong> / <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html#torch.nn.LSTM">nn.LSTM</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html#torch.nn.Softmax">nn.Softmax</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.LogSoftmax.html#torch.nn.LogSoftmax">nn.LogSoftmax</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html#torch.nn.MultiheadAttention">nn.MultiheadAttention</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html#torch.nn.TransformerEncoder">nn.TransformerEncoder</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.TransformerDecoder.html#torch.nn.TransformerDecoder">nn.TransformerDecoder</a></strong></p><p>I have linked all the layers to their source where you could read all about them, but to show how I usually try to understand a layer and read the docs, I would try to look at a very simple convolutional layer here.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Lie5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Lie5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 424w, https://substackcdn.com/image/fetch/$s_!Lie5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 848w, https://substackcdn.com/image/fetch/$s_!Lie5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 1272w, https://substackcdn.com/image/fetch/$s_!Lie5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Lie5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png" width="1456" height="513" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:513,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;MLWhiz: Data Science, Machine Learning, Artificial Intelligence&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" title="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" srcset="https://substackcdn.com/image/fetch/$s_!Lie5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 424w, https://substackcdn.com/image/fetch/$s_!Lie5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 848w, https://substackcdn.com/image/fetch/$s_!Lie5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 1272w, https://substackcdn.com/image/fetch/$s_!Lie5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd35a99de-56c3-4811-981f-58eb15c6c82f_1462x515.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So, a Conv2d Layer needs as input an Image of height H and width W, with <code>Cin</code> channels. Now, for the first layer in a convnet, the number of <code>in_channels</code> would be 3(RGB), and the number of <code>out_channels</code> can be defined by the user. The <code>kernel_size</code> mostly used is 3x3, and the <code>stride</code> normally used is 1.</p><p>To check a new layer which I don&#8217;t know much about, I usually try to see the input as well as output for the layer like below where I would first initialize the layer:</p><pre><code><code>conv_layer = nn.Conv2d(in_channels = 3, out_channels = 64, kernel_size = (3,3), stride = 1, padding=1)
</code></code></pre><p>And then pass some random input through it. Here 100 is the batch size.</p><pre><code><code>x = torch.randn((100,3,24,24))
conv_layer(x).size()
--------------------------------
torch.Size([100, 64, 24, 24])
</code></code></pre><p>So, we get the output from the convolution operation as required, and I have sufficient information on how to use this layer in any Neural Network I design.</p><div><hr></div><h2><strong>Datasets and DataLoaders</strong></h2><p>How would we pass data to our Neural nets while training or while testing? We can definitely pass tensors as we have done above, but Pytorch also provides us with pre-built Datasets to make it easier for us to pass data to our neural nets. You can check out the complete list of datasets provided at <strong><a href="https://pytorch.org/docs/stable/torchvision/datasets.html">torchvision.datasets</a></strong> and <strong><a href="https://pytorch.org/text/datasets.html">torchtext.datasets</a></strong> . But, to give a concrete example for datasets, let&#8217;s say we had to pass images to an Image Neural net using a folder which has images in this structure:</p><pre><code><code>data
    train
        sailboat
        kayak
        .
        .
</code></code></pre><p>We can use torchvision.datasets.ImageFolder dataset to get an example image like below:</p><pre><code><code>from torchvision import transforms
from torchvision.datasets import ImageFolder
traindir = "data/train/"
t = transforms.Compose([
        transforms.Resize(size=256),
    transforms.CenterCrop(size=224),
        transforms.ToTensor()])
train_dataset = ImageFolder(root=traindir,transform=t)
print("Num Images in Dataset:", len(train_dataset))
print("Example Image and Label:", train_dataset[2])
</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7Po2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7Po2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 424w, https://substackcdn.com/image/fetch/$s_!7Po2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 848w, https://substackcdn.com/image/fetch/$s_!7Po2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 1272w, https://substackcdn.com/image/fetch/$s_!7Po2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7Po2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png" width="989" height="513" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:513,&quot;width&quot;:989,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;MLWhiz: Data Science, Machine Learning, Artificial Intelligence&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" title="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" srcset="https://substackcdn.com/image/fetch/$s_!7Po2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 424w, https://substackcdn.com/image/fetch/$s_!7Po2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 848w, https://substackcdn.com/image/fetch/$s_!7Po2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 1272w, https://substackcdn.com/image/fetch/$s_!7Po2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9b21897-4f40-4571-b0cb-29db6b14ac9d_989x513.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This dataset has 847 images, and we can get an image and its label using an index. Now we can pass images one by one to any image neural network using a for loop:</p><pre><code><code>for i in range(0,len(train_dataset)):
    image ,label = train_dataset[i]
    pred = model(image)
</code></code></pre><p><em><strong>But that is not optimal. We want to do batching.</strong></em> We can actually write some more code to append images and labels in a batch and then pass it to the Neural network. But Pytorch provides us with a utility iterator torch.utils.data.DataLoader to do precisely that. Now we can simply wrap our train_dataset in the Dataloader, and we will get batches instead of individual examples.</p><pre><code><code>train_dataloader = DataLoader(train_dataset,batch_size = 64, shuffle=True, num_workers=10)
</code></code></pre><p>We can simply iterate with batches using:</p><pre><code><code>for image_batch, label_batch in train_dataloader:
    print(image_batch.size(),label_batch.size())
    break
-------------------------------------------------
torch.Size([64, 3, 224, 224]) torch.Size([64])
</code></code></pre><p>So actually, the whole process of using datasets and Dataloaders becomes:</p><pre><code><code>t = transforms.Compose([
        transforms.Resize(size=256),
    transforms.CenterCrop(size=224),
        transforms.ToTensor()])

train_dataset = torchvision.datasets.ImageFolder(root=traindir,transform=t)
train_dataloader = DataLoader(train_dataset,batch_size = 64, shuffle=True, num_workers=10)

for image_batch, label_batch in train_dataloader:
    pred = myImageNeuralNet(image_batch)
</code></code></pre><p>You can look at this particular example in action in my previous blogpost on Image classification using Deep Learning <strong><a href="https://towardsdatascience.com/end-to-end-pipeline-for-setting-up-multiclass-image-classification-for-data-scientists-2e051081d41c">here</a></strong> .</p><p>This is great, and Pytorch does provide a lot of functionality out of the box. But the main power of Pytorch comes with its immense customization. We can also create our own custom datasets if the datasets provided by PyTorch don&#8217;t fit our use case.</p><div><hr></div><h3><strong>Understanding Custom Datasets</strong></h3><p>To write our custom datasets, we can make use of the abstract class <code>torch.utils.data.Dataset</code> provided by Pytorch. We need to inherit this <code>Dataset</code> class and need to define two methods to create a custom Dataset.</p><ul><li><p><code>__len__</code> : a function that returns the size of the dataset. This one is pretty simple to write in most cases.</p></li><li><p><code>__getitem__</code>: a function that takes as input an index i and returns the sample at index <code>i</code>.</p></li></ul><p>For example, we can create a simple custom dataset that returns an image and a label from a folder. See that most of the tasks are happening in <code>__init__</code> part where we use <code>glob.glob</code> to get image names and do some general preprocessing.</p><pre><code><code>from glob import glob
from PIL import Image
from torch.utils.data import Dataset

class customImageFolderDataset(Dataset):
    """Custom Image Loader dataset."""
    def __init__(self, root, transform=None):
        """
        Args:
            root (string): Path to the images organized in a particular folder structure.
            transform: Any Pytorch transform to be applied
        """
        # Get all image paths from a directory
        self.image_paths = glob(f"{root}/*/*")
        # Get the labels from the image paths
        self.labels = [x.split("/")[-2] for x in self.image_paths]
        # Create a dictionary mapping each label to a index from 0 to len(classes).
        self.label_to_idx = {x:i for i,x in enumerate(set(self.labels))}
        self.transform = transform

    def __len__(self):
        # return length of dataset
        return len(self.image_paths)

    def __getitem__(self, idx):
        # open and send one image and label
        img_name = self.image_paths[idx]
        label = self.labels[idx]
        image = Image.open(img_name)
        if self.transform:
            image = self.transform(image)
        return image,self.label_to_idx[label]
</code></code></pre><p>Also, note that we open our images one at a time in the <code>__getitem__</code> method and not while initializing. This is not done in <code>__init__</code> because we don&#8217;t want to load all our images in the memory and just need to load the required ones.</p><p>We can now use this dataset with the utility <code>Dataloader</code> just like before. It works just like the previous dataset provided by PyTorch but without some utility functions.</p><pre><code><code>t = transforms.Compose([
        transforms.Resize(size=256),
    transforms.CenterCrop(size=224),
        transforms.ToTensor()])

train_dataset = customImageFolderDataset(root=traindir,transform=t)
train_dataloader = DataLoader(train_dataset,batch_size = 64, shuffle=True, num_workers=10)

for image_batch, label_batch in train_dataloader:
    pred = myImageNeuralNet(image_batch)
</code></code></pre><div><hr></div><h3><strong>Understanding Custom DataLoaders</strong></h3><p><strong>This particular section is a little advanced and can be skipped going through this post as it will not be needed in a lot of situations.</strong> But I am adding it for completeness here.</p><p>So let&#8217;s say you are looking to provide batches to a network that processes text input, and the network could take sequences with any sequence size as long as the size remains constant in the batch. For example, we can have a BiLSTM network that can process sequences of any length. It&#8217;s alright if you don&#8217;t understand the layers used in it right now; just know that it can process sequences with variable sizes.</p><pre><code><code>class BiLSTM(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden_size = 64
        drp = 0.1
        max_features, embed_size = 10000,300
        self.embedding = nn.Embedding(max_features, embed_size)
        self.lstm = nn.LSTM(embed_size, self.hidden_size, bidirectional=True, batch_first=True)
        self.linear = nn.Linear(self.hidden_size*4 , 64)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(drp)
        self.out = nn.Linear(64, 1)


    def forward(self, x):
        h_embedding = self.embedding(x)
        h_embedding = torch.squeeze(torch.unsqueeze(h_embedding, 0))

        h_lstm, _ = self.lstm(h_embedding)
        avg_pool = torch.mean(h_lstm, 1)
        max_pool, _ = torch.max(h_lstm, 1)
        conc = torch.cat(( avg_pool, max_pool), 1)
        conc = self.relu(self.linear(conc))
        conc = self.dropout(conc)
        out = self.out(conc)
        return out
</code></code></pre><p>This network expects its input to be of shape (<code>batch_size</code>, <code>seq_length</code>) and works with any <code>seq_length</code>. We can check this by passing our model two random batches with different sequence lengths(10 and 25).</p><pre><code><code>model = BiLSTM()
input_batch_1 = torch.randint(low = 0,high = 10000, size = (100,**10**))
input_batch_2 = torch.randint(low = 0,high = 10000, size = (100,**25**))
print(model(input_batch_1).size())
print(model(input_batch_2).size())
------------------------------------------------------------------
torch.Size([100, 1])
torch.Size([100, 1])
</code></code></pre><p>Now, we want to provide tight batches to this model, such that each batch has the same sequence length based on the max sequence length in the batch to minimize padding. This has an added benefit of making the neural net run faster. It was, in fact, one of the methods used in the winning submission of the Quora Insincere challenge in Kaggle, where running time was of utmost importance.</p><p>So, how do we do this? Let&#8217;s write a very simple custom dataset class first.</p><pre><code><code>class CustomTextDataset(Dataset):
    '''
    Simple Dataset initializes with X and y vectors
    We start by sorting our X and y vectors by sequence lengths
    '''
    def __init__(self,X,y=None):
        self.data = list(zip(X,y))
        # Sort by length of first element in tuple
        self.data = sorted(self.data, key=lambda x: len(x[0]))

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        return self.data[idx]
</code></code></pre><p>Also, let&#8217;s generate some random data which we will use with this custom Dataset.</p><pre><code><code>import numpy as np
train_data_size = 1024
sizes = np.random.randint(low=50,high=300,size=(train_data_size,))
X = [np.random.randint(0,10000, (sizes[i])) for i in range(train_data_size)]
y = np.random.rand(train_data_size).round()
#checking one example in dataset
print((X[0],y[0]))
</code></code></pre><p><em>Example of one random sequence and label. Each integer in the sequence corresponds to a word in the sentence.</em></p><p>We can use the custom dataset now using:</p><pre><code><code>train_dataset = CustomTextDataset(X,y)
</code></code></pre><p>If we now try to use the Dataloader on this dataset with <code>batch_size</code>&gt;1, we will get an error. Why is that?</p><pre><code><code>train_dataloader = DataLoader(train_dataset,batch_size = 64, shuffle=False, num_workers=10)
for xb,yb in train_dataloader:
    print(xb.size(),yb.size())
</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!w6zO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!w6zO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 424w, https://substackcdn.com/image/fetch/$s_!w6zO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 848w, https://substackcdn.com/image/fetch/$s_!w6zO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 1272w, https://substackcdn.com/image/fetch/$s_!w6zO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!w6zO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png" width="1069" height="29" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:29,&quot;width&quot;:1069,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;MLWhiz: Data Science, Machine Learning, Artificial Intelligence&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" title="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" srcset="https://substackcdn.com/image/fetch/$s_!w6zO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 424w, https://substackcdn.com/image/fetch/$s_!w6zO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 848w, https://substackcdn.com/image/fetch/$s_!w6zO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 1272w, https://substackcdn.com/image/fetch/$s_!w6zO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f6c4f5-874e-418a-8ae0-cc9170e3fe8c_1069x29.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This happens because the sequences have different lengths, and our data loader expects our sequences of the same length. Remember that in the previous image example, we resized all images to size 224 using the transforms, so we didn&#8217;t face this error.</p><p><em><strong>So, how do we iterate through this dataset so that each batch has sequences with the same length, but different batches may have different sequence lengths?</strong></em></p><p>We can use <code>collate_fn</code> parameter in the DataLoader that lets us define how to stack sequences in a particular batch. To use this, we need to define a function that takes as input a batch and returns (<code>x_batch</code>, <code>y_batch</code> ) with padded sequence lengths based on <code>max_sequence_length</code> in the batch. The functions I have used in the below function are simple NumPy operations. Also, the function is properly commented so you can understand what is happening.</p><pre><code><code>def collate_text(batch):
    # get text sequences in batch
    data = [item[0] for item in batch]
    # get labels in batch
    target = [item[1] for item in batch]
    # get max_seq_length in batch
    max_seq_len = max([len(x) for x in data])
    # pad text sequences based on max_seq_len
    data = [np.pad(p, (0, max_seq_len - len(p)), 'constant') for p in data]
    # convert data and target to tensor
    data = torch.LongTensor(data)
    target = torch.LongTensor(target)
    return [data, target]
</code></code></pre><p>We can now use this <code>collate_fn</code> with our Dataloader as:</p><pre><code><code>train_dataloader = DataLoader(train_dataset,batch_size = 64, shuffle=False, num_workers=10,collate_fn = collate_text)

for xb,yb in train_dataloader:
    print(xb.size(),yb.size())
</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GtRA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GtRA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 424w, https://substackcdn.com/image/fetch/$s_!GtRA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 848w, https://substackcdn.com/image/fetch/$s_!GtRA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 1272w, https://substackcdn.com/image/fetch/$s_!GtRA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GtRA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png" width="1224" height="451" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:451,&quot;width&quot;:1224,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;See that the batches have different sequence lengths now&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="See that the batches have different sequence lengths now" title="See that the batches have different sequence lengths now" srcset="https://substackcdn.com/image/fetch/$s_!GtRA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 424w, https://substackcdn.com/image/fetch/$s_!GtRA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 848w, https://substackcdn.com/image/fetch/$s_!GtRA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 1272w, https://substackcdn.com/image/fetch/$s_!GtRA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45587a34-cb22-4a58-a2be-775fe91d0603_1224x451.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It will work this time as we have provided a custom <code>collate_fn</code>. And see that the batches have different sequence lengths now. Thus we would be able to train our BiLSTM using variable input sizes just like we wanted.</p><div><hr></div><h2><strong>Training a Neural Network</strong></h2><p>We know how to create a neural network using <code>nn.Module</code>. But how to train it? Any neural network that has to be trained will have a training loop that will look something similar to below:</p><pre><code><code>num_epochs = 5
for epoch in range(num_epochs):
    # Set model to train mode
    model.train()
    for x_batch,y_batch in train_dataloader:
        # Clear gradients
        optimizer.zero_grad()
        # Forward pass - Predicted outputs
        pred = model(x_batch)
        # Find Loss and backpropagation of gradients
        loss = loss_criterion(pred, y_batch)
        loss.backward()
        # Update the parameters
        optimizer.step()
    model.eval()
    for x_batch,y_batch in valid_dataloader:
        pred = model(x_batch)
        val_loss = loss_criterion(pred, y_batch)
</code></code></pre><p>In the above code, we are running five epochs and in each epoch:</p><ol><li><p>We iterate through the dataset using a data loader.</p></li><li><p>In each iteration, we do a forward pass using <code>model(x_batch)</code></p></li><li><p>We calculate the Loss using a <code>loss_criterion</code></p></li><li><p>We back-propagate that loss using <code>loss.backward()</code> call. We don&#8217;t have to worry about the calculation of the gradients at all, as this simple call does it all for us.</p></li><li><p>Take an optimizer step to change the weights in the whole network using <code>optimizer.step()</code>. This is where weights of the network get modified using the gradients calculated in <code>loss.backward()</code> call.</p></li><li><p>We go through the validation data loader to check the validation score/metrics. Before doing validation, we set the model to eval mode using <code>model.eval()</code>.Please note we don&#8217;t back-propagate losses in eval mode.</p></li></ol><p>Till now, we have talked about how to use <code>nn.Module</code> to create networks and how to use Custom Datasets and Dataloaders with Pytorch. So let&#8217;s talk about the various options available for Loss Functions and Optimizers.</p><div><hr></div><h2><strong>Loss functions</strong></h2><p>Pytorch provides us with a variety of <strong><a href="https://pytorch.org/docs/stable/nn.html#loss-functions">loss functions</a></strong> for our most common tasks, like Classification and Regression. Some most used examples are <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss">nn.CrossEntropyLoss</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss">nn.NLLLoss</a></strong> , <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.KLDivLoss.html#torch.nn.KLDivLoss">nn.KLDivLoss</a></strong> and <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html#torch.nn.MSELoss">nn.MSELoss</a></strong> . You can read the documentation of each loss function, but to explain how to use these loss functions, I will go through the example of <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss">nn.NLLLoss</a></strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qpgd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qpgd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 424w, https://substackcdn.com/image/fetch/$s_!qpgd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 848w, https://substackcdn.com/image/fetch/$s_!qpgd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 1272w, https://substackcdn.com/image/fetch/$s_!qpgd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qpgd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png" width="1456" height="888" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:888,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;MLWhiz: Data Science, Machine Learning, Artificial Intelligence&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" title="MLWhiz: Data Science, Machine Learning, Artificial Intelligence" srcset="https://substackcdn.com/image/fetch/$s_!qpgd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 424w, https://substackcdn.com/image/fetch/$s_!qpgd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 848w, https://substackcdn.com/image/fetch/$s_!qpgd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 1272w, https://substackcdn.com/image/fetch/$s_!qpgd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30bc9067-c381-4d9b-bcc2-990f344be0cd_1488x908.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The documentation for NLLLoss is pretty succinct. As in, this loss function is used for Multiclass classification, and based on the documentation:</p><ul><li><p>the input expected needs to be of size (<code>batch_size</code> x <code>Num_Classes</code> ) &#8212; These are the predictions from the Neural Network we have created.</p></li><li><p>We need to have the log-probabilities of each class in the input &#8212; To get log-probabilities from a Neural Network, we can add a <code>LogSoftmax</code> Layer as the last layer of our network.</p></li><li><p>The target needs to be a tensor of classes with class numbers in the range(0, C-1) where C is the number of classes.</p></li></ul><p>So, we can try to use this Loss function for a simple classification network. Please note the LogSoftmax layer after the final linear layer. If you don&#8217;t want to use this LogSoftmax layer, you could have just used <strong><a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss">&lt;code&gt;nn.CrossEntropyLoss&lt;/code&gt;</a></strong></p><pre><code><code>class myClassificationNet(nn.Module):
    def __init__(self):
        super().__init__()
        # Define all Layers Here
        self.lin = nn.Linear(784, 10)
        self.logsoftmax = nn.LogSoftmax(dim=1)
    def forward(self, x):
        # Connect the layer Outputs here to define the forward pass
        x = self.lin(x)
        x = self.logsoftmax(x)
        return x
</code></code></pre><p>Let&#8217;s define a random input to pass to our network to test it:</p><pre><code><code># some random input:

X = torch.randn(100,784)
y = torch.randint(low = 0,high = 10,size = (100,))
</code></code></pre><p>And pass it through the model to get predictions:</p><pre><code><code>model = myClassificationNet()
preds = model(X)
</code></code></pre><p>We can now get the loss as:</p><pre><code><code>criterion = nn.NLLLoss()
loss = criterion(preds,y)
loss
------------------------------------------
tensor(2.4852, grad_fn=&lt;NllLossBackward&gt;)
</code></code></pre><div><hr></div><h3><strong>Custom Loss Function</strong></h3>
      <p>
          <a href="https://www.mlwhiz.com/p/pytorch_guide">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[I Use Claude Code Every Day. Here's the Setup That Actually Matters]]></title><description><![CDATA[CLAUDE.md, Skills 2.0, permission modes, channels &#8212; an opinionated guide for beginners and the mildly curious]]></description><link>https://www.mlwhiz.com/p/i-use-claude-code-every-day-heres</link><guid isPermaLink="false">https://www.mlwhiz.com/p/i-use-claude-code-every-day-heres</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Thu, 23 Apr 2026 02:06:45 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!mW1F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mW1F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mW1F!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!mW1F!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!mW1F!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!mW1F!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mW1F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1458866,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/194608696?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!mW1F!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!mW1F!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!mW1F!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!mW1F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa904f490-f3ee-4b38-bf3c-e35333be7690_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let me admit something upfront &#8594; there is always a small, mildly annoying blocker before picking up a new tool. </p><p>That little &lt;<em>do I really want to learn yet another thing?&gt;</em> feeling &#8212; even when half of LinkedIn is screaming that you must. </p><p>I felt it with Claude Code. I had it installed on my machine for two full weeks before I actually sat down to use it. And when I finally did, the first hour was a lot of reading docs, clicking &#8220;yes&#8221; to prompts I did not understand, and wondering if this was genuinely going to pay off or if I had just given another AI tool access to my filesystem.</p><p>Here is the thing about Claude Code, though. The hello-world is easy. </p><p>You <code>npm install</code>, you type <code>claude</code>, you ask it to fix a bug, it fixes the bug. You feel clever. Then you look at the docs and you feel lost. Are you making the most out of it?</p><p>This is so confusing. Which of this actually matters? And which of it can you safely ignore for now?</p><p>I&#8217;ve been using Claude Code daily for months since that slow start, and in the first week I made every setup mistake I could. I clicked &#8220;yes&#8221; to permission prompts a thousand times before I realized <code>acceptEdits</code> and <code>dangerously-skip-permissions</code> existed. Racked up a $200 API bill before I discovered the Max plan can be used to login as well. Ended up starting forty-something terminal claude sessions which got lost as I didn&#8217;t know <code>claude -c</code> was a thing. </p><p>This post is the shortcut I wish someone had handed me in week one &#8212; an opinionated setup that gets you from &#8220;I installed it&#8221; to &#8220;I actually use this daily&#8221; in a weekend. The 20% of the surface area that delivers 80% of the value. Plus honest opinions on which features are worth your time and which ones you can skip. </p><div class="pullquote"><p>The <a href="https://code.claude.com/docs/en/overview">official docs</a> are great if you want the full feature dump; this is the opposite of that.</p></div><p>Let&#8217;s dive in.</p><div><hr></div><h2>1. What Claude Code Actually Is (And What It Isn&#8217;t)</h2><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GLPn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GLPn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 424w, https://substackcdn.com/image/fetch/$s_!GLPn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 848w, https://substackcdn.com/image/fetch/$s_!GLPn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 1272w, https://substackcdn.com/image/fetch/$s_!GLPn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GLPn!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png" width="1200" height="175.54945054945054" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:213,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;The Claude Code agent loop: you prompt the CLI, the CLI talks to Claude with your CLAUDE.md and context, Claude calls tools that read and write your files, observations come back, the loop repeats until the task is done&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="The Claude Code agent loop: you prompt the CLI, the CLI talks to Claude with your CLAUDE.md and context, Claude calls tools that read and write your files, observations come back, the loop repeats until the task is done" title="The Claude Code agent loop: you prompt the CLI, the CLI talks to Claude with your CLAUDE.md and context, Claude calls tools that read and write your files, observations come back, the loop repeats until the task is done" srcset="https://substackcdn.com/image/fetch/$s_!GLPn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 424w, https://substackcdn.com/image/fetch/$s_!GLPn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 848w, https://substackcdn.com/image/fetch/$s_!GLPn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 1272w, https://substackcdn.com/image/fetch/$s_!GLPn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe35ba1-1c58-44c2-9d3e-d16d776998ee_2958x433.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Before we install anything, let&#8217;s get the mental model right. This is the single most common reason people bounce off Claude Code in the first hour.</p><p>Claude Code is <strong>not</strong> a VS Code extension that autocompletes your code. It is <strong>not</strong> a chat sidebar. It is <strong>not</strong> Cursor or Copilot. Yes, it has a VS Code integration, but the integration is a thin window over a CLI tool that runs in your terminal.</p><p>What Claude Code actually is: an <strong>agentic CLI</strong>. You run <code>claude</code> in your terminal, you tell it what you want in plain English, and it then reads files, writes files, runs bash commands, executes your tests, browses the web, calls APIs, and keeps iterating until the task is done. You watch it work, you can interrupt at any point, <em><strong>you can steer.</strong></em></p><p>Think of it less like autocomplete and more like pair-programming with a junior developer who types fast, never gets tired, sometimes goes off the rails, and occasionally needs a hard &#8220;no, do not do that.&#8221;</p><p><em><strong>That loop above &#8212; shown in the diagram at the start of this section &#8212; is the whole product</strong></em>. Read, think, act, observe, repeat. Your job is to give it good context up front and steer when it drifts.</p><p>If this sounds familiar, it should &#8212; I wrote about <a href="https://www.mlwhiz.com/p/genai-series-my-tryst-with-ai-assisted">my first dance with vibe coding</a> using Claude Pro a while back. Claude Code is what happens when that same loop moves out of a chat window and into your actual terminal, with access to your actual files and your actual tests.</p><p>And it is not a small thing. As of February 2026, roughly <strong>4% of all public commits on GitHub</strong> were authored by Claude Code &#8212; about 135,000 commits per day. Anthropic itself says <strong>90% of their internal code is now AI-written</strong>. ServiceNow has 29,000 daily users on it. This is not a Twitter fad. <em><strong>This is real and this is here to stay.</strong></em></p><p>The question is no longer &#8220;is this useful.&#8221; The question is how you go from &#8220;I installed it&#8221; to &#8220;I actually use it well.&#8221; Which is the rest of this post.</p><div class="pullquote"><p><strong>Anyone still calling this &#8220;autocomplete&#8221; has either not used it, or has not been paying attention.</strong></p></div><div><hr></div><h2>2. Install and Get Logged In</h2><p>Installation is genuinely a one-liner now.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;bash&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-bash">npm install -g @anthropic-ai/claude-code</code></pre></div><p>You need Node 18 or newer. That is the whole prerequisite list.</p><p>Open a terminal and Run <code>claude</code> in any directory. The first time, it walks you through login. You get two choices: a <strong>claude.ai account</strong> (Pro or Max plan) or an <strong>API key</strong> from the Anthropic Console. Pick the first one if you are a human writing code daily. Pick the second one only if you have a specific reason &#8212; Bedrock, Vertex, headless CI, Corporate use and that kind of thing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8FrP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8FrP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 424w, https://substackcdn.com/image/fetch/$s_!8FrP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 848w, https://substackcdn.com/image/fetch/$s_!8FrP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 1272w, https://substackcdn.com/image/fetch/$s_!8FrP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8FrP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png" width="1456" height="545" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:545,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:88901,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/194608696?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8FrP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 424w, https://substackcdn.com/image/fetch/$s_!8FrP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 848w, https://substackcdn.com/image/fetch/$s_!8FrP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 1272w, https://substackcdn.com/image/fetch/$s_!8FrP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc16f3e4-b9f9-4a45-b426-c2068d12456f_1684x630.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Pricing reality check:</strong> Max plan is $100 a month for individuals, $200 a month for teams. A typical 30-to-60-minute Claude Code session costs roughly $0.50 to $3.00 on the API. Do that math for yourself. If you are coding even a few hours a day, Max is the cheaper option, and it gives you Opus access without rate-limit anxiety.</p><p>If you want my full breakdown of how the AI subscriptions compare across coding, research, and general use, <a href="https://www.mlwhiz.com/p/which-ai-subscription-is-actually">I broke it down here</a>. </p><p>Once you are in, run <code>/status</code> to see which settings are active and how many tokens you have used up. We will come back to that command when things get weird.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7PAA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7PAA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 424w, https://substackcdn.com/image/fetch/$s_!7PAA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 848w, https://substackcdn.com/image/fetch/$s_!7PAA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!7PAA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7PAA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png" width="1456" height="826" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:826,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:56688,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/194608696?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7PAA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 424w, https://substackcdn.com/image/fetch/$s_!7PAA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 848w, https://substackcdn.com/image/fetch/$s_!7PAA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!7PAA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b62c4f3-a6f1-4839-8883-08999c133ef5_1776x1008.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h2>3. CLAUDE.md &#8212; The One File You Have to Write</h2><p>If you only learn one thing from this entire post, learn this.</p><p><code>CLAUDE.md</code> is a markdown file at the root of your project. Every time you start Claude Code in that directory, the contents of this file get loaded into the conversation automatically. Think of this file as your project&#8217;s onboarding document &#8212; except the onboardee shows up every single session and reads it cover to cover.</p><p>This is where you tell Claude things like &#8594; what the project does, which command runs the tests, what the build tool is, conventions the team follows, paths it should never touch, and the gotchas.</p><p>Here is a real-world <code>CLAUDE.md</code> for a Python data science project, slightly trimmed:</p>
      <p>
          <a href="https://www.mlwhiz.com/p/i-use-claude-code-every-day-heres">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[MLWhiz Weekly AI/ML Newsletter # 4]]></title><description><![CDATA[The week AI buyers became AI owners &#8212; and a lot of people decided they&#8217;re done with agents.]]></description><link>https://www.mlwhiz.com/p/mlwhiz-weekly-aiml-newsletter-4</link><guid isPermaLink="false">https://www.mlwhiz.com/p/mlwhiz-weekly-aiml-newsletter-4</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Tue, 21 Apr 2026 22:02:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!T6Vw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!T6Vw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!T6Vw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!T6Vw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!T6Vw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!T6Vw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!T6Vw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2116345,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/194950920?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!T6Vw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!T6Vw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!T6Vw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!T6Vw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33fc1f82-dfbb-4617-b30c-651823d25746_3200x2134.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>&#127942; Story of the Week: OpenAI Just Bought 10% of Cerebras</h2><p>For two years, I&#8217;ve been hearing the same question in every infrastructure conversation: how does the Nvidia monopoly end?</p><p>This week, we got the answer. And it&#8217;s not what anyone predicted.</p><p>It wasn&#8217;t a DOJ antitrust case. It was a procurement contract &#8212; but a procurement contract structured like nothing we&#8217;ve ever seen in this industry.</p><p><a href="https://techcrunch.com/2026/04/18/ai-chip-startup-cerebras-files-for-ipo/">Cerebras filed its S-1 on Friday</a>. But buried inside was a deal between OpenAI and Cerebras that is pretty hard to believe.</p><p>Here&#8217;s what OpenAI committed to:</p><ul><li><p><strong>$20+ billion</strong> in chip spending through 2028</p></li><li><p><strong>750 MW</strong> of capacity, with an option to expand to <strong>2 GW</strong></p></li><li><p>A <strong>$1 billion loan</strong> to OpenAI from Cerebras at 6% interest</p></li></ul><p>In exchange, OpenAI got about <strong>10% of Cerebras</strong> post-IPO.</p><p>Read that again. The customer got equity in the supplier. The customer also got a billion-dollar loan from the supplier.</p><p><em><strong>OpenAI is now Cerebras&#8217;s biggest customer, biggest creditor, and one of its biggest shareholders</strong></em>. People are calling it &#8220;<em><strong>circular&#8230;</strong></em></p>
      <p>
          <a href="https://www.mlwhiz.com/p/mlwhiz-weekly-aiml-newsletter-4">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[From RNNs to Transformers: Building Sequential Recommenders (Part 1)]]></title><description><![CDATA[RecSys Series Part 9a: Implementing GRU4Rec and SASRec on Steam Games &#8212; with production deployment patterns]]></description><link>https://www.mlwhiz.com/p/rnns-to-transformers-sequential-recommenders</link><guid isPermaLink="false">https://www.mlwhiz.com/p/rnns-to-transformers-sequential-recommenders</guid><dc:creator><![CDATA[Rahul Agarwal]]></dc:creator><pubDate>Sat, 18 Apr 2026 10:56:33 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!jEWK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHq9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png" width="1456" height="250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77210,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yHq9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 424w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 848w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1272w, https://substackcdn.com/image/fetch/$s_!yHq9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21050ae0-d6b0-4e64-9cdb-c017d983bf85_1501x258.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><em>Hey, Rahul here! &#128075; Each week, I publish long-form ML+AI posts covering ML, AI, and System design for MLwhiz. Paid subscribers also get how-to guides with full code walkthroughs. I publish occasional extra articles. If you&#8217;d like to become a paid subscriber, here&#8217;s a button for that:</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.mlwhiz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.mlwhiz.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B1mx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png" width="995" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B1mx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 424w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 848w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1272w, https://substackcdn.com/image/fetch/$s_!B1mx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f624f4d-a6b2-4226-808a-e860524c63c7_995x80.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jEWK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jEWK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!jEWK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!jEWK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!jEWK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jEWK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1524772,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.mlwhiz.com/i/194596918?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jEWK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 424w, https://substackcdn.com/image/fetch/$s_!jEWK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 848w, https://substackcdn.com/image/fetch/$s_!jEWK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 1272w, https://substackcdn.com/image/fetch/$s_!jEWK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcead6384-17c0-425f-8db7-802c31f9e357_3200x2134.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Every tech revolution follows the same pattern. First, we solve the problem one way. Then, we realize we&#8217;ve been solving the <em>wrong</em> problem.</p><p><strong>Natural language processing:</strong> we spent a decade on classification (sentiment, NER, QA as pick-the-right-answer). </p><p><strong>Then GPT said:</strong> generation subsumes classification. Just generate the output.</p><p>Recommendation systems are having their moment right now. We spent years building the pipeline that would retrieve 10K candidates with Two-Tower, score 1K with a ranker, re-rank the top 100. <em><strong>But what if the recommender could just generate the next item directly?</strong></em> That&#8217;s where this series is headed.</p><p>But you can&#8217;t understand the generative revolution without understanding what came before it. </p><p>In this two-part post, we&#8217;ll trace the full evolution: <em><strong>how RNNs first cracked sequential recommendation, how Transformers took over, and ultimately how generative models are rewriting the rules entirely.</strong></em></p><p>This is <strong>Part 1</strong> &#8212; covering </p><ul><li><p>GRU4Rec (2016), </p></li><li><p>SASRec (2018), </p></li><li><p>the BERT4Rec controversy, and </p></li><li><p>production deployment patterns. </p></li></ul><p>Part 2 will cover Semantic IDs, TIGER, HSTU, and who&#8217;s deploying generative recommenders in production today.</p><p>Let&#8217;s dive in!</p><div><hr></div><h2>1. The Sequential Problem: Why Order Matters</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dWff!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dWff!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 424w, https://substackcdn.com/image/fetch/$s_!dWff!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 848w, https://substackcdn.com/image/fetch/$s_!dWff!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 1272w, https://substackcdn.com/image/fetch/$s_!dWff!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dWff!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png" width="1456" height="399" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:399,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Static CF vs Sequential Recommendation&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Static CF vs Sequential Recommendation" title="Static CF vs Sequential Recommendation" srcset="https://substackcdn.com/image/fetch/$s_!dWff!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 424w, https://substackcdn.com/image/fetch/$s_!dWff!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 848w, https://substackcdn.com/image/fetch/$s_!dWff!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 1272w, https://substackcdn.com/image/fetch/$s_!dWff!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F69d139ac-6af5-447f-a16b-e33431f33cac_2076x569.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s say that you just finished watching <em>Inception</em>. Netflix recommends <em>Interstellar</em>. You watch it. Next up: <em>Arrival</em>.</p><p>As you can see this watching order is not random. This is not just &#8220;you like sci-fi&#8221; and so are watching sci-fi movies. </p><p>There&#8217;s a <strong>trajectory here</strong>. The recommender is following your path through Christopher Nolan&#8217;s mind-bending sci-fi catalog &#8212; what you watched <em>second might</em> change what you should see <em>third</em>.</p><p>In <a href="https://www.mlwhiz.com/p/the-recommenders-playbook-algorithms">the first post of this series, we covered collaborative filtering</a>, which treats user history as an unordered matrix &#8212; a bag of items. So, if you watched <em>[Inception, Interstellar, Arrival]</em>, traditional CF treats that the same as <em>[Arrival, Inception, Interstellar]</em>. But the order you watched them in tells you something completely different about what to recommend next.</p><p>Sequential models fixed that. They learned to predict not just &#8220;what you might like&#8221; but &#8220;what comes next.&#8221;</p><h3>Formalising the Problem</h3><p>Given a sequence of items a user has interacted with:</p><p style="text-align: center;"><strong>[i&#8321;, i&#8322;, i&#8323;, ..., i&#8345;]</strong></p><p>Predict the next item: <strong>i&#8345;&#8330;&#8321;</strong></p><p>This formulation applies across domains: </p><ul><li><p>E-commerce: product browsing &#8594; purchase prediction </p></li><li><p>Streaming: watch history &#8594; next video </p></li><li><p>Music: listening sequence &#8594; next song </p></li><li><p>News: reading pattern &#8594; next article</p></li></ul><h3>The Benchmark: Steam Games Dataset</h3><p>For this post, we&#8217;ll use the <strong>Steam Games</strong> dataset &#8212; a rich gaming interaction dataset from UCSD&#8217;s repository for building our models: </p><ul><li><p><strong>67,287 users</strong> (raw) &#8594; <strong>56,808 users</strong> (after 5-core filtering) </p></li><li><p><strong>32,133 games</strong> (raw) &#8594; <strong>6,382 games</strong> (after 5-core filtering) </p></li><li><p><strong>2,235,453 interactions</strong> (playtime &gt; 1 hour) </p></li><li><p>Average sequence length: 39.4 games (median: 26) </p></li></ul><p><strong>5-core filtering</strong> is a technique that removes all users and items with fewer than 5 interactions, applied iteratively until every remaining user and item has at least 5. It&#8217;s a standard preprocessing step in RecSys research to eliminate extreme cold-start cases (users who tried one game, games nobody played) that add noise without enough signal to learn from.</p><p>Now let&#8217;s see how different architectures tackle sequential prediction.</p><div><hr></div><h2>2. GRU4Rec &#8212; When RNNs Met Recommendations (2016)</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hbkG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hbkG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 424w, https://substackcdn.com/image/fetch/$s_!hbkG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 848w, https://substackcdn.com/image/fetch/$s_!hbkG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 1272w, https://substackcdn.com/image/fetch/$s_!hbkG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hbkG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png" width="306" height="931.3727506426735" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2368,&quot;width&quot;:778,&quot;resizeWidth&quot;:306,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;GRU4Rec Architecture&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="GRU4Rec Architecture" title="GRU4Rec Architecture" srcset="https://substackcdn.com/image/fetch/$s_!hbkG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 424w, https://substackcdn.com/image/fetch/$s_!hbkG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 848w, https://substackcdn.com/image/fetch/$s_!hbkG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 1272w, https://substackcdn.com/image/fetch/$s_!hbkG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f6a116-966a-457b-b886-a84994ca6ad0_778x2368.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In 2016, Gravity R&amp;D published &#8220;<a href="https://arxiv.org/abs/1511.06939">Session-based Recommendations with Recurrent Neural Networks</a>&#8221; at ICLR. First major work applying RNNs to sequential recommendation. It dominated the field for nearly two years.</p>
      <p>
          <a href="https://www.mlwhiz.com/p/rnns-to-transformers-sequential-recommenders">
              Read more
          </a>
      </p>
   ]]></content:encoded></item></channel></rss>