<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>terps</title>
<link>https://terpsfi.xyz/</link>
<atom:link href="https://terpsfi.xyz/index.xml" rel="self" type="application/rss+xml"/>
<description>Notes on machine learning and learning in public.</description>
<generator>quarto-1.5.57</generator>
<lastBuildDate>Fri, 01 May 2026 05:00:00 GMT</lastBuildDate>
<item>
  <title>back at it</title>
  <dc:creator>terps </dc:creator>
  <link>https://terpsfi.xyz/posts/back-at-it/</link>
  <description><![CDATA[ 





<p>Last post here was December 2023. Long time.</p>
<p>Honest version: writing felt like extra work on top of the actual learning. Turns out you have to write to learn the thing properly, so I’m back.</p>
<p>What I’m spending time on right now:</p>
<ul>
<li>Model distillation.</li>
<li>Agent RL workflows: GRPO and other training methods for RL environments.</li>
<li>Math Academy in the background, daily.</li>
</ul>
<p>Not going to promise a posting cadence. The bar is “write when I have something honest to say.” If you want updates, the <a href="../../index.xml">RSS feed</a> works.</p>


<!-- -->


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>meta</category>
  <guid>https://terpsfi.xyz/posts/back-at-it/</guid>
  <pubDate>Fri, 01 May 2026 05:00:00 GMT</pubDate>
  <media:content url="https://terpsfi.xyz/posts/back-at-it/image.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Machine Learner 📚</title>
  <dc:creator>Terps </dc:creator>
  <link>https://terpsfi.xyz/posts/resources/resources.html</link>
  <description><![CDATA[ 





<blockquote class="blockquote">
<p>A collection of resources I found helpful in my programming/ML journey. Will try to continue to update and add better descriptions as time goes on.</p>
</blockquote>
<section id="online-courses" class="level2">
<h2 class="anchored" data-anchor-id="online-courses">Online Courses</h2>
<ul>
<li><strong><a href="https://course.fast.ai/">Practical Deep Learning</a></strong>
<ul>
<li>Starting off strong, Jeremy Howard is an incredible educator and I can’t recommend this course enough. Not only the is the course material great, but the overall approach to learning/meta-learning is fantastic.</li>
</ul></li>
<li><strong><a href="https://www.coursera.org/professional-certificates/google-advanced-data-analytics">Google Advanced Data Analytics Professional Certificate</a></strong>
<ul>
<li>Google doesn’t miss on quality, this course takes you zero to one in data science. From foundations to more advanced ML applications, this course will really help you drive home key concepts in an approachable way.</li>
</ul></li>
<li><strong><a href="https://huggingface.co/learn">Hugging Face Courses</a></strong>
<ul>
<li>Three courses covering NLP, Deep RL, and Audio. Great way to break the cold start problem and get hacking on deep learning.</li>
</ul></li>
<li><strong><a href="https://missing.csail.mit.edu/">Missing Semester</a></strong>
<ul>
<li>MIT course covering all the tools a program can expect to use and master in the field from the shell, editors, data wrangling, cli, git, debugging, profiling, metaprogramming, security, and cryptography.</li>
</ul></li>
<li><strong><a href="https://mathacademy.com/learn">Math Academy</a></strong>
<ul>
<li>Research based pedagogical method to learn math at an accelerated pace. The lessons connect well to one another and the xp / ranking system add some fun gamification.</li>
</ul></li>
<li><strong><a href="https://www.youtube.com/c/AndrejKarpathy">Neural Networks: Zero to Hero</a></strong>
<ul>
<li>Andrej Karpathy can cook. Keep an eye out for his new school he is launching.</li>
</ul></li>
</ul>
</section>
<section id="websites-and-blogs" class="level2">
<h2 class="anchored" data-anchor-id="websites-and-blogs">Websites and Blogs</h2>
<ul>
<li><strong><a href="https://www.kaggle.com/learn">Kaggle</a></strong>
<ul>
<li>Kaggle is not only the premier platform for data science competition, but also has a good library of exercises to brush up on skills.</li>
</ul></li>
<li><strong><a href="https://bbycroft.net/llm">LLM Visualization</a></strong>
<ul>
<li>Awesome visualization and walkthrough of a GPT LLM.</li>
</ul></li>
<li><strong><a href="https://rentry.org/llm-training">The Novice’s LLM Training Guide</a></strong>
<ul>
<li>Get up to speed on fine-tuning LLMs.</li>
</ul></li>
</ul>
</section>
<section id="tools-and-frameworks" class="level2">
<h2 class="anchored" data-anchor-id="tools-and-frameworks">Tools and Frameworks</h2>
<ul>
<li><strong><a href="https://chat.openai.com/">ChatGPT or any LLM assistant</a></strong>
<ul>
<li>The amount of value you can get out of modern LLMs is staggering, I don’t even know where to begin, it gives the novice so much leverage. I recommend using it to augment your learning and work not replacing it, really grok its outputs. Using voice chat to have one on one conversations about topics I’m learning really helps reinforce concepts I’m trying to learn.</li>
</ul></li>
<li><strong><a href="https://www.zotero.org/">Zotero</a></strong>
<ul>
<li>I use Zotero to keep track of research papers that I’m reading or plan to read. Its super handy and allows me to read PDFs on my ipad.</li>
</ul></li>
</ul>
<hr>
<p><em>Thank you for reading my blog! Much more to come soon. Feel free to connect with me on <a href="twitter.com/terpsfi">Twitter</a>.</em></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>jupyter</category>
  <guid>https://terpsfi.xyz/posts/resources/resources.html</guid>
  <pubDate>Sun, 10 Dec 2023 06:00:00 GMT</pubDate>
  <media:content url="https://terpsfi.xyz/posts/resources/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Micrograd 🔧</title>
  <dc:creator>Terps </dc:creator>
  <link>https://terpsfi.xyz/posts/micrograd/micrograd.html</link>
  <description><![CDATA[ 





<blockquote class="blockquote">
<p>Spelled-out intro to neural networks and backpropagation</p>
</blockquote>
<section id="a-blog-implementation-of-andrej-karpathys-micrograd-video" class="level3">
<h3 class="anchored" data-anchor-id="a-blog-implementation-of-andrej-karpathys-micrograd-video">A blog implementation of Andrej Karpathy’s <a href="https://youtu.be/VMj-3S1tku0">micrograd video</a></h3>
</section>
<section id="contents" class="level1">
<h1>Contents:</h1>
<section id="derivative-of-a-function" class="level3">
<h3 class="anchored" data-anchor-id="derivative-of-a-function">1. Derivative of a function</h3>
</section>
<section id="core-value-object" class="level3">
<h3 class="anchored" data-anchor-id="core-value-object">2. Core Value Object</h3>
</section>
<section id="manual-backpropagation" class="level3">
<h3 class="anchored" data-anchor-id="manual-backpropagation">3. Manual Backpropagation</h3>
</section>
<section id="backward-function" class="level3">
<h3 class="anchored" data-anchor-id="backward-function">4. Backward function</h3>
</section>
<section id="more-operations" class="level3">
<h3 class="anchored" data-anchor-id="more-operations">5. More operations</h3>
</section>
<section id="pytorch" class="level3">
<h3 class="anchored" data-anchor-id="pytorch">6. Pytorch</h3>
<p><a id="1"></a></p>
</section>
</section>
<section id="lets-get-an-intuitive-understanding-of-what-a-derivative-is" class="level1">
<h1>Lets get an intuitive understanding of what a derivative is</h1>
<div id="2fb8fca8" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#basic imports</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> math</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>matplotlib inline</span></code></pre></div>
</div>
<p>Lets use a simple quadratic function as an example</p>
<section id="fx-3x2---4x-5" class="level2">
<h2 class="anchored" data-anchor-id="fx-3x2---4x-5"><img src="https://latex.codecogs.com/png.latex?f(x)%20=%203x%5E2%20-%204x%20+%205"></h2>
<div id="1fcda76a" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> f(x):</span>
<span id="cb2-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span></code></pre></div>
</div>
<div id="d4dfc1f2" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">f(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<pre><code>20.0</code></pre>
</div>
</div>
<p>Take a look at the shape of the function, we can expect a parabola since we know its a quadratic function</p>
<div id="50b9c156" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating a range of x values from -5 to 5 incrementing by 0.25 to pass into our function to generate our y values</span></span>
<span id="cb5-2">xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb5-3">ys <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(xs)</span>
<span id="cb5-4">plt.plot(xs, ys)</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>We know want to think through what is the derivative of this function at different points x, let refresh with the definition of a derivative</p>
</section>
<section id="fx-lim_h-to-0-fracfxh-fxh" class="level2">
<h2 class="anchored" data-anchor-id="fx-lim_h-to-0-fracfxh-fxh"><img src="https://latex.codecogs.com/png.latex?f'(x)%20=%20lim_%7Bh%20%5Cto%200%7D%20%5Cfrac%7Bf(x+h)-f(x)%7D%7Bh%7D"></h2>
<p>You are basically trying to see the level of sensitivty the function responds with by bumping any x value at any point slightly by this small number h <br> Intuitively how would you expect this function to respond if we nudged x = 3.0 by this small postitive number h? The amount the x value responds tells you the strength of the slope</p>
<div id="3d176850" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span></span>
<span id="cb6-2">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span></span>
<span id="cb6-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'slope of function at x = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, slope = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>(f(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f(x)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> h<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>slope of function at x = 3.0, slope = 14.000300000063248</code></pre>
</div>
</div>
<p>Lets do a hacky implementation with more variables <br> Look at the function a*b + c in relation to the variables we assigned, imagine if you nudged each variables by a tiny amount would that result in our output being increased or decreased? <br> If we were to slightly nudge each of our input varibles by the tiny amount h(amount approaching 0) we can approximate the instataneous rate of change by looking at the difference before and after over the amount we nudged by, this will give us the slope.</p>
<div id="5e9d3b69" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lets get more complex</span></span>
<span id="cb8-2">h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#inputs</span></span>
<span id="cb8-4">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span></span>
<span id="cb8-5">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span></span>
<span id="cb8-6">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span></span>
<span id="cb8-7"></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#We wanna find the derivative of d with respect to a,b,c</span></span>
<span id="cb8-9">d1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb8-10">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>  h</span>
<span id="cb8-11">d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb8-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d1'</span>, d1)</span>
<span id="cb8-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d2'</span>, d2)</span>
<span id="cb8-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'slope'</span>, (d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>h)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>d1 4.0
d2 3.999699999999999
slope -3.000000000010772</code></pre>
</div>
</div>
<p>Lets do it with b now</p>
<div id="4e836fc8" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">d1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb10-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>  h</span>
<span id="cb10-3">d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb10-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d1'</span>, d1)</span>
<span id="cb10-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d2'</span>, d2)</span>
<span id="cb10-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'slope'</span>, (d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>h)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>d1 3.999699999999999
d2 3.99990001
slope 2.0001000000124947</code></pre>
</div>
</div>
<p>And c…</p>
<div id="b1a93da8" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">d1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb12-2">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>  h</span>
<span id="cb12-3">d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb12-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d1'</span>, d1)</span>
<span id="cb12-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d2'</span>, d2)</span>
<span id="cb12-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'slope'</span>, (d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>h)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>d1 3.99990001
d2 4.00000001
slope 0.9999999999976694</code></pre>
</div>
</div>
<p>Hopefully this has helped build an inuitive sense of what this derivative is telling you about the function, but now we want to move to neural networks, which will be massive mathmatical expressions, so we need some structures to maintain these expressions, so we will build out a value object that can keep track of state and allow us to do expressions</p>
<p><a id="2"></a></p>
</section>
</section>
<section id="core-value-object-1" class="level1">
<h1>Core Value Object</h1>
<div id="5454df0b" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Value:</span>
<span id="cb14-2">    </span>
<span id="cb14-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data):</span>
<span id="cb14-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data</span>
<span id="cb14-5">        </span>
<span id="cb14-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__repr__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb14-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Value(data=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span></span>
<span id="cb14-8">    </span>
<span id="cb14-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__add__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb14-10">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> other.data)</span>
<span id="cb14-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb14-12">    </span>
<span id="cb14-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__mul__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb14-14">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other.data)</span>
<span id="cb14-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb14-16">    </span>
<span id="cb14-17">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>)</span>
<span id="cb14-18">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>)</span>
<span id="cb14-19">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b</span>
<span id="cb14-20"></span>
<span id="cb14-21">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> b</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<pre><code>Value(data=-6.0)</code></pre>
</div>
</div>
<p>We use double underscore or dunder methods so python knows what to interally when we use operators such as print, +, -, * <br> So when we call <code>a + b</code> above what is happening interally is <code>a.__add__(b)</code> with self as a and b as other. Similarly when we do <code>a * b</code>, python is calling <code>a.__mul__(b)</code></p>
<p>Cool so now we can do basic arthmetic now we need to add pointers to what see values produce other values and by what operations</p>
<div id="ab40c8ec" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Value:</span>
<span id="cb16-2">    </span>
<span id="cb16-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data, _children<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(), _op<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>):</span>
<span id="cb16-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data</span>
<span id="cb16-5">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>(_children)</span>
<span id="cb16-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._op <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _op</span>
<span id="cb16-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> label</span>
<span id="cb16-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb16-9">        </span>
<span id="cb16-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__repr__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb16-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Value(data=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span></span>
<span id="cb16-12">    </span>
<span id="cb16-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__add__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb16-14">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'+'</span>)</span>
<span id="cb16-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out </span>
<span id="cb16-16">    </span>
<span id="cb16-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__mul__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb16-18">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'*'</span>)</span>
<span id="cb16-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb16-20">        </span>
<span id="cb16-21">    </span>
<span id="cb16-22">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb16-23">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb16-24">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb16-25">e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb16-26">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb16-27">f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb16-28">L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb16-29">L</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="10">
<pre><code>Value(data=-8.0)</code></pre>
</div>
</div>
<p>Now that we have the a way to store the parents, labels and operations we can visualize them with an expression graph, code below allows us to do that using a library called graphviz</p>
<div id="e136a3f1" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> graphviz <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Digraph</span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trace(root):</span>
<span id="cb18-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># builds a set of all nodes and edges in a graph</span></span>
<span id="cb18-5">  nodes, edges <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>(), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb18-6">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> build(v):</span>
<span id="cb18-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> nodes:</span>
<span id="cb18-8">      nodes.add(v)</span>
<span id="cb18-9">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> child <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> v._prev:</span>
<span id="cb18-10">        edges.add((child, v))</span>
<span id="cb18-11">        build(child)</span>
<span id="cb18-12">  build(root)</span>
<span id="cb18-13">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> nodes, edges</span>
<span id="cb18-14"></span>
<span id="cb18-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> draw_dot(root):</span>
<span id="cb18-16">  dot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Digraph(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'svg'</span>, graph_attr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rankdir'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'LR'</span>}) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LR = left to right</span></span>
<span id="cb18-17">  </span>
<span id="cb18-18">  nodes, edges <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> trace(root)</span>
<span id="cb18-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> nodes:</span>
<span id="cb18-20">    uid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(n))</span>
<span id="cb18-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for any value in the graph, create a rectangular ('record') node for it</span></span>
<span id="cb18-22">    dot.node(name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> uid, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{ </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> | data </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.4f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> | grad </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.4f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">}"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> (n.label, n.data, n.grad), shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'record'</span>)</span>
<span id="cb18-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n._op:</span>
<span id="cb18-24">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if this value is a result of some operation, create an op node for it</span></span>
<span id="cb18-25">      dot.node(name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> uid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n._op, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n._op)</span>
<span id="cb18-26">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and connect this node to it</span></span>
<span id="cb18-27">      dot.edge(uid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n._op, uid)</span>
<span id="cb18-28"></span>
<span id="cb18-29">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n1, n2 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> edges:</span>
<span id="cb18-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># connect n1 to the op node of n2</span></span>
<span id="cb18-31">    dot.edge(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(n1)), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(n2)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n2._op)</span>
<span id="cb18-32"></span>
<span id="cb18-33">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> dot</span></code></pre></div>
</div>
<div id="886deeea" class="cell" data-scrolled="true" data-execution_count="12">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1">draw_dot(L)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="12">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-13-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Lets recap: - we are able to build out mathematical expressions using + and * - Added and tracked grad so we can calculate and update this state later when we do backpropagtion - Forward pass that produces output L and visualized - Now we want to do backpropagation</p>
<p><a id="3"></a></p>
</section>
<section id="manual-backpropagation-1" class="level1">
<h1>Manual Backpropagation</h1>
<p>Lets manually nudge the variable a and manually calculate the derivative of L with respect to a, lets create a gating function lol so we don’t pollute the global scope. We can do this for each variable to calculate their derivative with respect to L</p>
<p><img src="https://i.imgur.com/c53qrkN.png" class="img-fluid"></p>
<section id="l-d-f" class="level3">
<h3 class="anchored" data-anchor-id="l-d-f"><img src="https://latex.codecogs.com/png.latex?L%20=%20d%20*%20f"></h3>
</section>
<section id="fracdldd-f" class="level3">
<h3 class="anchored" data-anchor-id="fracdldd-f"><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BdL%7D%7Bdd%7D%20=?%20f"></h3>
</section>
<section id="lim_h-to-0-fracdhf---dfh" class="level3">
<h3 class="anchored" data-anchor-id="lim_h-to-0-fracdhf---dfh"><img src="https://latex.codecogs.com/png.latex?lim_%7Bh%20%5Cto%200%7D%20%5Cfrac%7B(d+h)*f%20-%20d*f%7D%7Bh%7D"></h3>
</section>
<section id="lim_h-to-0-fracdf-hf---dfh" class="level3">
<h3 class="anchored" data-anchor-id="lim_h-to-0-fracdf-hf---dfh"><img src="https://latex.codecogs.com/png.latex?lim_%7Bh%20%5Cto%200%7D%20%5Cfrac%7Bd*f%20+%20h*f%20-%20d*f%7D%7Bh%7D"></h3>
</section>
<section id="lim_h-to-0-frachfh" class="level3">
<h3 class="anchored" data-anchor-id="lim_h-to-0-frachfh"><img src="https://latex.codecogs.com/png.latex?lim_%7Bh%20%5Cto%200%7D%20%5Cfrac%7Bh*f%7D%7Bh%7D"></h3>
</section>
<section id="f" class="level3">
<h3 class="anchored" data-anchor-id="f"><img src="https://latex.codecogs.com/png.latex?f"></h3>
<p>So we can see that d.grad is just the value of f which is -2.0 and by the property of symmetry f.grad is just the value of d which is 4.0, lets go ahead and manually set these</p>
<div id="ab78ed23" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">f.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span></span>
<span id="cb20-2">d.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span></span></code></pre></div>
</div>
<div id="9a305caf" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">L.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div>
</div>
<div id="585825a0" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lol():</span>
<span id="cb22-2">    </span>
<span id="cb22-3">    h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span></span>
<span id="cb22-4">    </span>
<span id="cb22-5">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb22-6">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb22-7">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb22-8">    e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb22-9">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb22-10">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb22-11">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb22-12">    L1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L.data</span>
<span id="cb22-13">    </span>
<span id="cb22-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is the variable we are nudging by h</span></span>
<span id="cb22-15">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> , label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb22-16">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb22-17">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb22-18">    e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb22-19">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb22-20">    d.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> h</span>
<span id="cb22-21">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb22-22">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb22-23">    L2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L.data</span>
<span id="cb22-24">    </span>
<span id="cb22-25">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>((L2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>L1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>h)</span>
<span id="cb22-26">lol()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>-1.9999999999953388</code></pre>
</div>
</div>
<p>So we have just derived the derivates of f and d with respect to L in a step by step way, now next will uncover the core of backpropagation, we want derive the derivative of L with respect to c and e. <br> We now know how L is sensitive to d and we know how e and c are sensitive to d, we can know put that together to figure out how L is sensitive to e and c.</p>
</section>
<section id="if-a-variable-z-depends-on-the-variable-y-which-itself-depends-on-the-variable-x-that-is-y-and-z-are-dependent-variables-then-z-depends-on-x-as-well-via-the-intermediate-variable-y.-in-this-case-the-chain-rule-is-expressed-as" class="level3">
<h3 class="anchored" data-anchor-id="if-a-variable-z-depends-on-the-variable-y-which-itself-depends-on-the-variable-x-that-is-y-and-z-are-dependent-variables-then-z-depends-on-x-as-well-via-the-intermediate-variable-y.-in-this-case-the-chain-rule-is-expressed-as">If a variable z depends on the variable y, which itself depends on the variable x (that is, y and z are dependent variables), then z depends on x as well, via the intermediate variable y. In this case, the chain rule is expressed as</h3>
</section>
</section>
<section id="fracdzdx-fracdzdy-fracdydx" class="level1">
<h1><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bdz%7D%7Bdx%7D%20=%20%5Cfrac%7Bdz%7D%7Bdy%7D%20*%20%5Cfrac%7Bdy%7D%7Bdx%7D"></h1>
<p>The chain rule is fundamentally telling you how we chain these derivatives together correctly so to differentiate through a function composition we have to apply a multiplication of those derivatives <br> <code>The inuitive explanation here is that knowing the instantaneous rate of change of z with respect to y and y relative to x allows one to calculate the instantaneous rate of change of z</code></p>
<section id="if-a-car-travels-twice-as-fast-as-a-bicyle-and-the-cycle-is-four-times-as-fast-as-a-walking-man-then-the-car-is-2-4-8-times-faster-than-the-man" class="level3">
<h3 class="anchored" data-anchor-id="if-a-car-travels-twice-as-fast-as-a-bicyle-and-the-cycle-is-four-times-as-fast-as-a-walking-man-then-the-car-is-2-4-8-times-faster-than-the-man">If a car travels twice as fast as a bicyle and the cycle is four times as fast as a walking man then the car is 2 * 4 = 8 times faster than the man <br></h3>
<p>We know the derivative of <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BdL%7D%7Bdd%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bdd%7D%7Bdc%7D"> and want to find <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BdL%7D%7Bdc%7D"> the chain rule tells us that <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BdL%7D%7Bdc%7D%20=%20%5Cfrac%7BdL%7D%7Bdd%7D%20*%20%5Cfrac%7Bdd%7D%7Bdc%7D"><br>
<img src="https://latex.codecogs.com/png.latex?1.0%20*%20-2.0%20=%20-2.0"></p>
<p>The chain rule is telling us for plus nodes “+” we are just routing the gradient because the local derivative is just 1.0</p>
<div id="bc470aac" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">e.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span></span>
<span id="cb24-2">c.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span></span></code></pre></div>
</div>
<div id="9e7a40fd" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">draw_dot(L)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="17">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-18-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="lets-manually-check-our-work-by-nudging-c" class="level3">
<h3 class="anchored" data-anchor-id="lets-manually-check-our-work-by-nudging-c">Lets manually check our work by nudging c</h3>
<div id="f66334b5" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lol():</span>
<span id="cb26-2">    </span>
<span id="cb26-3">    h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span></span>
<span id="cb26-4">    </span>
<span id="cb26-5">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb26-6">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb26-7">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb26-8">    e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb26-9">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb26-10">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb26-11">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb26-12">    L1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L.data</span>
<span id="cb26-13">    </span>
<span id="cb26-14">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> , label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb26-15">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb26-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is the variable we are nudging by h</span></span>
<span id="cb26-17">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb26-18">    c.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> h</span>
<span id="cb26-19">    e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb26-20">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb26-21">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb26-22">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb26-23">    L2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L.data</span>
<span id="cb26-24">    </span>
<span id="cb26-25">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>((L2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>L1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>h)</span>
<span id="cb26-26">lol()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>-1.9999999999953388</code></pre>
</div>
</div>
<p>As we expected c.grad equals -2.0*</p>
</section>
<section id="now-we-will-recurse-our-way-backwards-again-and-going-to-do-our-second-application-of-the-chain-rule" class="level2">
<h2 class="anchored" data-anchor-id="now-we-will-recurse-our-way-backwards-again-and-going-to-do-our-second-application-of-the-chain-rule">Now we will recurse our way backwards again and going to do our second application of the chain rule</h2>
<section id="fracdlde--2.0" class="level3">
<h3 class="anchored" data-anchor-id="fracdlde--2.0"><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BdL%7D%7Bde%7D%20=%20-2.0"> <br></h3>
</section>
<section id="fracdeda-b" class="level3">
<h3 class="anchored" data-anchor-id="fracdeda-b"><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bde%7D%7Bda%7D%20=%20b"> <br></h3>
</section>
<section id="fracdlda-fracdlde-fracdeda" class="level3">
<h3 class="anchored" data-anchor-id="fracdlda-fracdlde-fracdeda"><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BdL%7D%7Bda%7D%20=%20%5Cfrac%7BdL%7D%7Bde%7D%20*%20%5Cfrac%7Bde%7D%7Bda%7D"></h3>
<p>We are multiplying the derivative of e with respect to L with the local gradients</p>
<div id="6a631835" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">a.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span></span>
<span id="cb28-2">b.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span></span></code></pre></div>
</div>
<div id="a0017392" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">draw_dot(L)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="20">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-21-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Lets verify</p>
<div id="d971c804" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lol():</span>
<span id="cb30-2">    </span>
<span id="cb30-3">    h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span></span>
<span id="cb30-4">    </span>
<span id="cb30-5">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb30-6">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb30-7">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb30-8">    e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb30-9">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb30-10">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb30-11">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb30-12">    L1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L.data</span>
<span id="cb30-13">    </span>
<span id="cb30-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is the variable we are nudging by h</span></span>
<span id="cb30-15">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> , label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>)</span>
<span id="cb30-16">    a.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> h</span>
<span id="cb30-17">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb30-18">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)</span>
<span id="cb30-19">    e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> e.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span></span>
<span id="cb30-20">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> d.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span></span>
<span id="cb30-21">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f'</span>)</span>
<span id="cb30-22">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> L.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span></span>
<span id="cb30-23">    L2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L.data</span>
<span id="cb30-24">    </span>
<span id="cb30-25">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>((L2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>L1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>h)</span>
<span id="cb30-26">lol()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>6.000000000021544</code></pre>
</div>
</div>
<p>Checks out</p>
</section>
</section>
<section id="we-know-know-what-back-propagation-is-a-recursive-application-of-the-chain-rule-backwards-through-the-computational-graph" class="level2">
<h2 class="anchored" data-anchor-id="we-know-know-what-back-propagation-is-a-recursive-application-of-the-chain-rule-backwards-through-the-computational-graph">We know know what back propagation is; a recursive application of the chain rule backwards through the computational graph</h2>
</section>
<section id="neuron-example" class="level2">
<h2 class="anchored" data-anchor-id="neuron-example">Neuron Example</h2>
<p>One step optimization</p>
<div id="6cd009d4" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1">a.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> a.grad</span>
<span id="cb32-2">b.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> b.grad</span>
<span id="cb32-3">c.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c.grad</span>
<span id="cb32-4">f.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f.grad</span>
<span id="cb32-5"></span>
<span id="cb32-6">e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> b</span>
<span id="cb32-7">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c</span>
<span id="cb32-8">L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f</span>
<span id="cb32-9"></span>
<span id="cb32-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(L.data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>-7.286496</code></pre>
</div>
</div>
<p><img src="https://blog.liang2.tw/2015Talk-DeepLearn-CNN/pics/external/cs231n_note_neuron.png" class="img-fluid"></p>
<p>For our model of neurons we have input axis and these synapses that have weights on them so the w’s are the weights and then the synapse interacts with the input multiplicatively so what flows to the cell body of this neuron is w times x but there’s multiple inputs so there’s many w times x’s flowing into the cell body, the cell body also has some bias which is a sort of trigger happiness of this neuron, making it more or less prone to firing. Then we take it through an activation function which is generally some kinda of squashing function like a sigmoid or tanh. Lets go over an example of a tanh activation function</p>
<div id="a30d04ca" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1">plt.plot(np.arange(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>), np.tanh(np.arange(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> plt.grid()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-24-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>You can see that the inputs that come in get squashed here on the y axis, the function gets capped at 1.00 and -1.00</p>
<div id="4d3832c0" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inputs x1,x2</span></span>
<span id="cb35-2">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>)</span>
<span id="cb35-3">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>)</span>
<span id="cb35-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weights w1,w2</span></span>
<span id="cb35-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>)</span>
<span id="cb35-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>)</span>
<span id="cb35-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bias of the neuron</span></span>
<span id="cb35-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#6.8813735870195432</span></span>
<span id="cb35-9">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb35-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1*w1 + x2*w2 + b</span></span>
<span id="cb35-11">x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1'</span></span>
<span id="cb35-12">x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2*w2'</span></span>
<span id="cb35-13">x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1 + x2*w2'</span></span>
<span id="cb35-14">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span></span>
<span id="cb35-15">draw_dot(n)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="24">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-25-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>We need to add more operations to our Value class to be able to calculate our activation function tanh, lets just do a cheeky implementation of tanh on our value class for now</p>
<div id="6058e2ba" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Value:</span>
<span id="cb36-2">    </span>
<span id="cb36-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data, _children<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(), _op<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>):</span>
<span id="cb36-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data</span>
<span id="cb36-5">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>(_children)</span>
<span id="cb36-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._op <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _op</span>
<span id="cb36-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> label</span>
<span id="cb36-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb36-9">        </span>
<span id="cb36-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__repr__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb36-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Value(data=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span></span>
<span id="cb36-12">    </span>
<span id="cb36-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__add__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb36-14">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'+'</span>)</span>
<span id="cb36-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out </span>
<span id="cb36-16">    </span>
<span id="cb36-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__mul__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb36-18">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'*'</span>)</span>
<span id="cb36-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb36-20">    </span>
<span id="cb36-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tanh(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb36-22">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data</span>
<span id="cb36-23">        t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (math.exp(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(math.exp(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb36-24">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(t, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, ), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tanh'</span>)</span>
<span id="cb36-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span></code></pre></div>
</div>
<div id="d39f297b" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inputs x1,x2</span></span>
<span id="cb37-2">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>)</span>
<span id="cb37-3">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>)</span>
<span id="cb37-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weights w1,w2</span></span>
<span id="cb37-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>)</span>
<span id="cb37-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>)</span>
<span id="cb37-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bias of the neuron</span></span>
<span id="cb37-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#6.8813735870195432</span></span>
<span id="cb37-9">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb37-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1*w1 + x2*w2 + b</span></span>
<span id="cb37-11">x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1'</span></span>
<span id="cb37-12">x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2*w2'</span></span>
<span id="cb37-13">x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1 + x2*w2'</span></span>
<span id="cb37-14">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span></span>
<span id="cb37-15">draw_dot(n)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="26">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-27-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="c3dace14" class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">o <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n.tanh()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> o.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span></span></code></pre></div>
</div>
<div id="b30c206d" class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="28">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-29-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Awesome n goes through tanh to produce the last output, our activation function is working great, now all we need to know is the derivative of tanh and we can use backpropagation.</p>
<div id="88293af4" class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb40-1">o.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span></code></pre></div>
</div>
<p>Lets calculte the gradient of n</p>
<div id="e609b7d5" class="cell" data-execution_count="30">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> o.data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="30">
<pre><code>0.4999999999999999</code></pre>
</div>
</div>
<div id="4668692a" class="cell" data-execution_count="31">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1">n.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span></code></pre></div>
</div>
<p>Now we can easily get the gradients for x1w1x2w1, b, x1w1, x2w2 since we used addition as an operation the local derivatives are just 1 so we just take the value 0.5</p>
<div id="ac549b99" class="cell" data-execution_count="32">
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb44-1">x1w1x2w2.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb44-2">b.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb44-3">x1w1.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb44-4">x2w2.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span></code></pre></div>
</div>
<p>We can know calculate the gradients for x2, w2, x1, and w1, but unlike the last gradients we used multiplication as our operation, so our local derivative is just the other term used in the operation so lets calculate the gradients</p>
<div id="d9ed74b7" class="cell" data-execution_count="33">
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1">x2.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> w2.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2w2.grad</span>
<span id="cb45-2">w2.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2w2.grad</span>
<span id="cb45-3">x1.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> w1.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x1w1.grad</span>
<span id="cb45-4">w1.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x1w1.grad</span></code></pre></div>
</div>
<div id="20ef8073" class="cell" data-execution_count="34">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb46-1">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="34">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-35-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Nice!, we have manually used backpropagation to calculate our gradients, now lets implement a backward function for each operation</p>
<p><a id="4"></a></p>
</section>
</section>
<section id="backward-function-1" class="level1">
<h1>Backward Function</h1>
<p>Lets add a backward methods to our Value object for each operation that we can call to calculate our gradients for us using backpropagation</p>
<div id="31e5155d" class="cell" data-execution_count="35">
<div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb47-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Value:</span>
<span id="cb47-2">    </span>
<span id="cb47-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data, _children<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(), _op<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>):</span>
<span id="cb47-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data</span>
<span id="cb47-5">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>(_children)</span>
<span id="cb47-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._op <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _op</span>
<span id="cb47-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> label</span>
<span id="cb47-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb47-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb47-10">        </span>
<span id="cb47-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__repr__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb47-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Value(data=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span></span>
<span id="cb47-13">    </span>
<span id="cb47-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__add__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb47-15">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'+'</span>)</span>
<span id="cb47-16">        </span>
<span id="cb47-17">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb47-18">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb47-19">            other.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb47-20">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb47-21">        </span>
<span id="cb47-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out </span>
<span id="cb47-23">    </span>
<span id="cb47-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__mul__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb47-25">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'*'</span>)</span>
<span id="cb47-26">                </span>
<span id="cb47-27">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb47-28">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> other.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb47-29">            other.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb47-30">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb47-31">        </span>
<span id="cb47-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb47-33">    </span>
<span id="cb47-34">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tanh(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb47-35">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data</span>
<span id="cb47-36">        t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (math.exp(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(math.exp(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb47-37">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(t, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, ), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tanh'</span>)</span>
<span id="cb47-38">        </span>
<span id="cb47-39">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb47-40">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb47-41">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb47-42">        </span>
<span id="cb47-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span></code></pre></div>
</div>
<div id="c041c6e1" class="cell" data-scrolled="true" data-execution_count="36">
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb48-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inputs x1,x2</span></span>
<span id="cb48-2">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>)</span>
<span id="cb48-3">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>)</span>
<span id="cb48-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weights w1,w2</span></span>
<span id="cb48-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>)</span>
<span id="cb48-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>)</span>
<span id="cb48-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bias of the neuron</span></span>
<span id="cb48-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#6.8813735870195432</span></span>
<span id="cb48-9">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb48-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1*w1 + x2*w2 + b</span></span>
<span id="cb48-11">x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1'</span></span>
<span id="cb48-12">x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2*w2'</span></span>
<span id="cb48-13">x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1 + x2*w2'</span></span>
<span id="cb48-14">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span></span>
<span id="cb48-15">o <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n.tanh()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> o.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span></span>
<span id="cb48-16">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="36">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-37-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>We initialize the gradient of o to 1.0, then call _backward to recursively calculate the gradients</p>
<div id="91d4650b" class="cell" data-execution_count="37">
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb49-1">o.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span></code></pre></div>
</div>
<div id="2ed2d7d2" class="cell" data-execution_count="38">
<div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb50-1">o._backward()</span></code></pre></div>
</div>
<div id="0864ec91" class="cell" data-execution_count="39">
<div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb51-1">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="39">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-40-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="8e304672" class="cell" data-execution_count="40">
<div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb52-1">n._backward()</span>
<span id="cb52-2">b._backward()</span>
<span id="cb52-3">x1w1x2w2._backward()</span>
<span id="cb52-4">x2w2._backward()</span>
<span id="cb52-5">x1w1._backward()</span></code></pre></div>
</div>
<div id="6b5ffdb2" class="cell" data-execution_count="41">
<div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb53-1">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="41">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-42-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Awesome, this works great, we never want to call backward() on a node before we have calculated the gradients for everthing after it since it depends on their gradients. We will use topological sort which lays the graph such that all the edges go only from left to right.</p>
<p><img src="https://miro.medium.com/max/1400/1*uMg_ojFXts2WZSjcZe4oRQ.png" class="img-fluid"></p>
<div id="2dac6a5f" class="cell" data-execution_count="42">
<div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb54-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inputs x1,x2</span></span>
<span id="cb54-2">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>)</span>
<span id="cb54-3">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>)</span>
<span id="cb54-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weights w1,w2</span></span>
<span id="cb54-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>)</span>
<span id="cb54-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>)</span>
<span id="cb54-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bias of the neuron</span></span>
<span id="cb54-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#6.8813735870195432</span></span>
<span id="cb54-9">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb54-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1*w1 + x2*w2 + b</span></span>
<span id="cb54-11">x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1'</span></span>
<span id="cb54-12">x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2*w2'</span></span>
<span id="cb54-13">x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1 + x2*w2'</span></span>
<span id="cb54-14">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span></span>
<span id="cb54-15">o <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n.tanh()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> o.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span></span>
<span id="cb54-16">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="42">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-43-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="2cb1d12e" class="cell" data-execution_count="43">
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb55-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># topological sort</span></span>
<span id="cb55-2">topo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb55-3">visited <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb55-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> build_topo(v):</span>
<span id="cb55-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> visited:</span>
<span id="cb55-6">        visited.add(v)</span>
<span id="cb55-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> child <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> v._prev:</span>
<span id="cb55-8">            build_topo(child)</span>
<span id="cb55-9">        topo.append(v)</span>
<span id="cb55-10">build_topo(o)</span>
<span id="cb55-11">topo</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="43">
<pre><code>[Value(data=1.0),
 Value(data=0.0),
 Value(data=0.0),
 Value(data=2.0),
 Value(data=-3.0),
 Value(data=-6.0),
 Value(data=-6.0),
 Value(data=6.881373587019543),
 Value(data=0.8813735870195432),
 Value(data=0.7071067811865476)]</code></pre>
</div>
</div>
<p>Our Value objects are now ordered properly</p>
<div id="83146c83" class="cell" data-execution_count="44">
<div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb57-1">o.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span></code></pre></div>
</div>
<div id="e43342df" class="cell" data-execution_count="45">
<div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb58-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> node <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">reversed</span>(topo):</span>
<span id="cb58-2">    node._backward()</span></code></pre></div>
</div>
<div id="f2daa860" class="cell" data-execution_count="46">
<div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb59-1">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="46">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-47-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p><a id="5"></a> # Lets implement this as a method in the Value object and add more operations</p>
<div id="b8e6e2da" class="cell" data-execution_count="47">
<div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb60-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Value:</span>
<span id="cb60-2">    </span>
<span id="cb60-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data, _children<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(), _op<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>):</span>
<span id="cb60-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data</span>
<span id="cb60-5">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>(_children)</span>
<span id="cb60-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._op <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _op</span>
<span id="cb60-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> label</span>
<span id="cb60-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb60-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb60-10">        </span>
<span id="cb60-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__repr__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb60-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Value(data=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span></span>
<span id="cb60-13">    </span>
<span id="cb60-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__add__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb60-15">        other <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> other <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(other, Value) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> Value(other)</span>
<span id="cb60-16">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'+'</span>)</span>
<span id="cb60-17">        </span>
<span id="cb60-18">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb60-19">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb60-20">            other.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb60-21">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb60-22">        </span>
<span id="cb60-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out </span>
<span id="cb60-24">    </span>
<span id="cb60-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__mul__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb60-26">        other <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> other <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(other, Value) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> Value(other)</span>
<span id="cb60-27">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other.data, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'*'</span>)</span>
<span id="cb60-28">                </span>
<span id="cb60-29">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb60-30">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> other.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb60-31">            other.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb60-32">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb60-33">        </span>
<span id="cb60-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb60-35">    </span>
<span id="cb60-36">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__rmul__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb60-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other</span>
<span id="cb60-38">    </span>
<span id="cb60-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__truediv__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb60-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> other<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb60-41">    </span>
<span id="cb60-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__neg__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>): <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -self</span></span>
<span id="cb60-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb60-44">    </span>
<span id="cb60-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__sub__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other): <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># self - other</span></span>
<span id="cb60-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>other)</span>
<span id="cb60-47">    </span>
<span id="cb60-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__radd__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other): <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># other + self</span></span>
<span id="cb60-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> other</span>
<span id="cb60-50">    </span>
<span id="cb60-51">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__pow__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, other):</span>
<span id="cb60-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(other, (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"only supporting int/float powers for now"</span></span>
<span id="cb60-53">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>other, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>,), <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'**</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>other<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb60-54"></span>
<span id="cb60-55">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb60-56">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> other <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> (other <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb60-57">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb60-58"></span>
<span id="cb60-59">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb60-60">    </span>
<span id="cb60-61">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tanh(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb60-62">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data</span>
<span id="cb60-63">        t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (math.exp(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(math.exp(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb60-64">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(t, (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, ), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tanh'</span>)</span>
<span id="cb60-65">        </span>
<span id="cb60-66">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb60-67">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad</span>
<span id="cb60-68">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb60-69">        </span>
<span id="cb60-70">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb60-71">    </span>
<span id="cb60-72">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> exp(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb60-73">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.data</span>
<span id="cb60-74">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(math.exp(x), (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, ), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'exp'</span>)</span>
<span id="cb60-75"></span>
<span id="cb60-76">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _backward():</span>
<span id="cb60-77">          <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> out.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> out.grad </span>
<span id="cb60-78">        out._backward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _backward</span>
<span id="cb60-79"></span>
<span id="cb60-80">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb60-81">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now we can call backward on our Value object</span></span>
<span id="cb60-82">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> backward(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb60-83">    </span>
<span id="cb60-84">        topo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb60-85">        visited <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb60-86">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> build_topo(v):</span>
<span id="cb60-87">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> visited:</span>
<span id="cb60-88">            visited.add(v)</span>
<span id="cb60-89">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> child <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> v._prev:</span>
<span id="cb60-90">              build_topo(child)</span>
<span id="cb60-91">            topo.append(v)</span>
<span id="cb60-92">        build_topo(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb60-93"></span>
<span id="cb60-94">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb60-95">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> node <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">reversed</span>(topo):</span>
<span id="cb60-96">          node._backward()</span></code></pre></div>
</div>
<div id="1cb8a673" class="cell" data-execution_count="48">
<div class="sourceCode cell-code" id="cb61" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb61-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>)</span>
<span id="cb61-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb61-3">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> b</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="48">
<pre><code>Value(data=-2.0)</code></pre>
</div>
</div>
<div id="58bc1b33" class="cell" data-execution_count="49">
<div class="sourceCode cell-code" id="cb63" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb63-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>)</span>
<span id="cb63-2">a.exp()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="49">
<pre><code>Value(data=7.38905609893065)</code></pre>
</div>
</div>
<div id="066e642f" class="cell" data-execution_count="50">
<div class="sourceCode cell-code" id="cb65" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb65-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inputs x1,x2</span></span>
<span id="cb65-2">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>)</span>
<span id="cb65-3">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>)</span>
<span id="cb65-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weights w1,w2</span></span>
<span id="cb65-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>)</span>
<span id="cb65-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>)</span>
<span id="cb65-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bias of the neuron</span></span>
<span id="cb65-8">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb65-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1*w1 + x2*w2 + b</span></span>
<span id="cb65-10">x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1'</span></span>
<span id="cb65-11">x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2*w2'</span></span>
<span id="cb65-12">x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1 + x2*w2'</span></span>
<span id="cb65-13">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span></span>
<span id="cb65-14">o <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n.tanh()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> o.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span></span>
<span id="cb65-15">o.backward()</span>
<span id="cb65-16">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="50">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-51-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="da857f16" class="cell" data-execution_count="51">
<div class="sourceCode cell-code" id="cb66" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb66-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inputs x1,x2</span></span>
<span id="cb66-2">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>)</span>
<span id="cb66-3">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>)</span>
<span id="cb66-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weights w1,w2</span></span>
<span id="cb66-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>)</span>
<span id="cb66-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>)</span>
<span id="cb66-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bias of the neuron</span></span>
<span id="cb66-8">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb66-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1*w1 + x2*w2 + b</span></span>
<span id="cb66-10">x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1'</span></span>
<span id="cb66-11">x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2*w2'</span></span>
<span id="cb66-12">x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2w2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1w1x2w2.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1*w1 + x2*w2'</span></span>
<span id="cb66-13">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1w1x2w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> n.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span></span>
<span id="cb66-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ----</span></span>
<span id="cb66-15">e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>n).exp()</span>
<span id="cb66-16">o <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb66-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ----</span></span>
<span id="cb66-18">o.label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span></span>
<span id="cb66-19">o.backward()</span>
<span id="cb66-20">draw_dot(o)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="51">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-52-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p><a id="6"></a></p>
</section>
<section id="pytorch-comparison" class="level1">
<h1>Pytorch comparison</h1>
<div id="a75e7c1f" class="cell" data-execution_count="52">
<div class="sourceCode cell-code" id="cb67" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb67-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb67-2"></span>
<span id="cb67-3">x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.Tensor([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>]).double()                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x1.requires_grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-4">x2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.Tensor([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>]).double()                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> x2.requires_grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-5">w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.Tensor([<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>]).double()               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> w1.requires_grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-6">w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.Tensor([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>]).double()                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> w2.requires_grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-7">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.Tensor([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.8813735870195432</span>]).double()  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> b.requires_grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-8">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b</span>
<span id="cb67-9">o <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.tanh(n)</span>
<span id="cb67-10"></span>
<span id="cb67-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(o.data.item())</span>
<span id="cb67-12">o.backward()</span>
<span id="cb67-13"></span>
<span id="cb67-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'---'</span>)</span>
<span id="cb67-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>, x2.grad.item())</span>
<span id="cb67-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w2'</span>, w2.grad.item())</span>
<span id="cb67-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>, x1.grad.item())</span>
<span id="cb67-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w1'</span>, w1.grad.item())</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>0.7071066904050358
---
x2 0.5000001283844369
w2 0.0
x1 -1.5000003851533106
w1 1.0000002567688737</code></pre>
</div>
</div>
<div id="f1fd4d2f" class="cell" data-execution_count="53">
<div class="sourceCode cell-code" id="cb69" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb69-1">o</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="53">
<pre><code>tensor([0.7071], dtype=torch.float64, grad_fn=&lt;TanhBackward0&gt;)</code></pre>
</div>
</div>
<div id="2eccce5d" class="cell" data-execution_count="54">
<div class="sourceCode cell-code" id="cb71" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb71-1">o.item()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="54">
<pre><code>0.7071066904050358</code></pre>
</div>
</div>
<p><img src="https://cs231n.github.io/assets/nn1/neural_net2.jpeg" class="img-fluid"></p>
<div id="2690ef1b" class="cell" data-scrolled="true" data-execution_count="131">
<div class="sourceCode cell-code" id="cb73" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb73-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb73-2"></span>
<span id="cb73-3"></span>
<span id="cb73-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Neuron:</span>
<span id="cb73-5">  </span>
<span id="cb73-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, nin):</span>
<span id="cb73-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [Value(random.uniform(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(nin)]</span>
<span id="cb73-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Value(random.uniform(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb73-9">  </span>
<span id="cb73-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__call__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x):</span>
<span id="cb73-11">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># w * x + b</span></span>
<span id="cb73-12">        act <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>((wi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>xi <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> wi, xi <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.w, x)), <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.b)</span>
<span id="cb73-13">        out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> act.tanh()</span>
<span id="cb73-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb73-15">    </span>
<span id="cb73-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> parameters(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb73-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.b]</span>
<span id="cb73-18"></span>
<span id="cb73-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Layer:</span>
<span id="cb73-20">    </span>
<span id="cb73-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, nin, nout):</span>
<span id="cb73-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.neurons <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [Neuron(nin) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(nout)]</span>
<span id="cb73-23">        </span>
<span id="cb73-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__call__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x):</span>
<span id="cb73-25">        outs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [n(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.neurons]</span>
<span id="cb73-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> outs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(outs) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> outs</span>
<span id="cb73-27">    </span>
<span id="cb73-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> parameters(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb73-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [p <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> neuron <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.neurons <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> neuron.parameters()]</span>
<span id="cb73-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#         params = []</span></span>
<span id="cb73-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#         for neuron in self.neurons:</span></span>
<span id="cb73-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#             ps = neuron.parameters()</span></span>
<span id="cb73-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#             params.extend(ps)</span></span>
<span id="cb73-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#         return params</span></span>
<span id="cb73-35"></span>
<span id="cb73-36"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> MLP:</span>
<span id="cb73-37">    </span>
<span id="cb73-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, nin, nouts):</span>
<span id="cb73-39">        sz <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [nin] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> nouts</span>
<span id="cb73-40">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.layers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [Layer(sz[i], sz[i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(nouts))]</span>
<span id="cb73-41">        </span>
<span id="cb73-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__call__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x):</span>
<span id="cb73-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> layer <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.layers:</span>
<span id="cb73-44">            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> layer(x)</span>
<span id="cb73-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x</span>
<span id="cb73-46">    </span>
<span id="cb73-47">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> parameters(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb73-48">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [p <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> layer <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.layers <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> layer.parameters()]</span>
<span id="cb73-49">    </span></code></pre></div>
</div>
<div id="24aae167" class="cell" data-execution_count="132">
<div class="sourceCode cell-code" id="cb74" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb74-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>]</span>
<span id="cb74-2">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MLP(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb74-3">n(x)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="132">
<pre><code>Value(data=-0.7028959990425087)</code></pre>
</div>
</div>
<div id="fc0b0256" class="cell" data-execution_count="133">
<div class="sourceCode cell-code" id="cb76" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb76-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(n.parameters())</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="133">
<pre><code>41</code></pre>
</div>
</div>
<div id="e160f6a1" class="cell" data-execution_count="134">
<div class="sourceCode cell-code" id="cb78" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb78-1">xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb78-2">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>],</span>
<span id="cb78-3">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>],</span>
<span id="cb78-4">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>],</span>
<span id="cb78-5">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>]</span>
<span id="cb78-6">]</span>
<span id="cb78-7">ys <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>]</span>
<span id="cb78-8">ypred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [n(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> xs]</span>
<span id="cb78-9">ypred</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="134">
<pre><code>[Value(data=-0.7028959990425087),
 Value(data=0.1757758058598642),
 Value(data=0.39624177470697325),
 Value(data=0.16264413106842704)]</code></pre>
</div>
</div>
<p>So how do we tune the weights to better predict the desired targets? We calculate a single number that measures the total performance of the neural net, this is called the loss.</p>
<div id="37297aca" class="cell" data-execution_count="135">
<div class="sourceCode cell-code" id="cb80" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb80-1">loss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>((yout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ygt)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ygt, yout <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(ys, ypred))</span>
<span id="cb80-2">loss</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="135">
<pre><code>Value(data=6.932959473871423)</code></pre>
</div>
</div>
<div id="b06ca8f3" class="cell" data-execution_count="136">
<div class="sourceCode cell-code" id="cb82" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb82-1">loss.backward()</span></code></pre></div>
</div>
<p>Now that we have called backward on the loss, we can take a look at the gradient of a single neuron in one of our layers to get a look on how it impacts our loss, this will be useful soon when we try to update our weights to decrease the loss</p>
<div id="e82db7f0" class="cell" data-execution_count="137">
<div class="sourceCode cell-code" id="cb83" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb83-1">n.layers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].neurons[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].w[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="137">
<pre><code>0.28438888706081467</code></pre>
</div>
</div>
<p>The gradient for this neuron is positive, so the weights is increasing our loss</p>
<div id="cf1c01c9" class="cell" data-execution_count="138">
<div class="sourceCode cell-code" id="cb85" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb85-1">n.layers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].neurons[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].w[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].grad</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="138">
<pre><code>1.711566891777295</code></pre>
</div>
</div>
<div id="1217d196" class="cell" data-execution_count="139">
<div class="sourceCode cell-code" id="cb87" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb87-1">n.layers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].neurons[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].b.grad</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="139">
<pre><code>1.0849775541180198</code></pre>
</div>
</div>
<p>We can now also call draw_dot on our loss, we can see the DAG has increased tremendously in compelexity</p>
<div id="27ecacff" class="cell" data-scrolled="true" data-execution_count="140">
<div class="sourceCode cell-code" id="cb89" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb89-1">draw_dot(loss)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="140">
<div>
<figure class="figure">
<p><img src="https://terpsfi.xyz/posts/micrograd/micrograd_files/figure-html/cell-65-output-1.svg" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>We can update our parameters by multiplying by substracting them by their gradients multiplied by a learning rate (the desired impact of the gradient has in updating our parameters, here we use 0.01 arbitrarily, but there are many techniques to find an optimal learning rate and to decay the rate as training continues, 0.1 is also a good rule of thumb, too big and you can overstep too small and its costly to train</p>
<div id="5239cb18" class="cell" data-execution_count="141">
<div class="sourceCode cell-code" id="cb90" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb90-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> n.parameters():</span>
<span id="cb90-2">    p.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p.grad    </span></code></pre></div>
</div>
<div id="234df905" class="cell" data-execution_count="142">
<div class="sourceCode cell-code" id="cb91" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb91-1">n.layers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].neurons[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].w[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="142">
<pre><code>0.2672732181430417</code></pre>
</div>
</div>
<p>After updating our parameters we can confirm our loss decreased</p>
<div id="d8b21d16" class="cell" data-execution_count="143">
<div class="sourceCode cell-code" id="cb93" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb93-1">ypred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [n(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> xs]</span>
<span id="cb93-2">loss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>((yout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ygt)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ygt, yout <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(ys, ypred))</span>
<span id="cb93-3">loss</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="143">
<pre><code>Value(data=6.317575661771169)</code></pre>
</div>
</div>
<p>What we have done is gradient decent. Forward pass -&gt; backward pass -&gt; update the parameters. Now we just have to iterate this process, lets turn this manual process into a training loop</p>
<div id="0e971ee4" class="cell" data-execution_count="150">
<div class="sourceCode cell-code" id="cb95" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb95-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>):</span>
<span id="cb95-2">    </span>
<span id="cb95-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># forward pass</span></span>
<span id="cb95-4">    ypred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [n(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> xs]</span>
<span id="cb95-5">    loss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>((yout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ygt)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ygt, yout <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(ys, ypred))</span>
<span id="cb95-6">    </span>
<span id="cb95-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#backward pass</span></span>
<span id="cb95-8">    loss.backward()</span>
<span id="cb95-9">    </span>
<span id="cb95-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#update</span></span>
<span id="cb95-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> n.parameters():</span>
<span id="cb95-12">        p.data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p.grad</span>
<span id="cb95-13">        p.grad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb95-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(k, loss.data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>0 0.10345239945960188
1 0.0941558588278075
2 0.0862952014023131
3 0.07956846088984432
4 0.07375215882362336
5 0.06867739459504289
6 0.06421419054843136
7 0.06026093580621215
8 0.056737086525177276
9 0.05357800634860667</code></pre>
</div>
</div>
<div id="12ba50b6" class="cell" data-execution_count="151">
<div class="sourceCode cell-code" id="cb97" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb97-1">ypred</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="151">
<pre><code>[Value(data=0.8741761793258574),
 Value(data=-0.8786120732704685),
 Value(data=-0.8952118169896238),
 Value(data=0.8903150856076475)]</code></pre>
</div>
</div>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>jupyter</category>
  <guid>https://terpsfi.xyz/posts/micrograd/micrograd.html</guid>
  <pubDate>Thu, 15 Sep 2022 05:00:00 GMT</pubDate>
  <media:content url="https://terpsfi.xyz/posts/micrograd/image.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
