<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya Goyal</title>
    <description>The latest articles on DEV Community by Aditya Goyal (@aditya_goyal_dd6c97e809fd).</description>
    <link>https://dev.to/aditya_goyal_dd6c97e809fd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4039601%2F91009650-a221-4e20-a1e7-a4d540e5255a.png</url>
      <title>DEV Community: Aditya Goyal</title>
      <link>https://dev.to/aditya_goyal_dd6c97e809fd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aditya_goyal_dd6c97e809fd"/>
    <language>en</language>
    <item>
      <title>I Built My First Gradient Descent Optimizer with JAX-Day 2</title>
      <dc:creator>Aditya Goyal</dc:creator>
      <pubDate>Tue, 21 Jul 2026 08:43:20 +0000</pubDate>
      <link>https://dev.to/aditya_goyal_dd6c97e809fd/i-built-my-first-gradient-descent-optimizer-with-jax-day-2-hkd</link>
      <guid>https://dev.to/aditya_goyal_dd6c97e809fd/i-built-my-first-gradient-descent-optimizer-with-jax-day-2-hkd</guid>
      <description>&lt;p&gt;On Day 1, I explored what JAX is and why people in machine learning love it. I learned about automatic differentiation ("grad") and Just-In-Time compilation ("jit"), but I hadn't actually used them in a real example.&lt;/p&gt;

&lt;p&gt;Today I finally did.&lt;/p&gt;

&lt;p&gt;The biggest "aha!" moment wasn't writing the code—it was understanding what gradients actually represent.&lt;/p&gt;

&lt;p&gt;Thinking About Derivatives Differently&lt;/p&gt;

&lt;p&gt;Imagine you're driving a car uphill.&lt;/p&gt;

&lt;p&gt;You press the accelerator just a little. The car speeds up.&lt;/p&gt;

&lt;p&gt;The question is:&lt;/p&gt;

&lt;p&gt;How much does the speed change when you press the pedal slightly?&lt;/p&gt;

&lt;p&gt;That's exactly what a derivative measures. It tells us how sensitive an output is to a tiny change in the input.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;f(x) = x ** 3&lt;/p&gt;

&lt;p&gt;At "x = 2":&lt;/p&gt;

&lt;p&gt;f(2) = 8&lt;/p&gt;

&lt;p&gt;Increase "x" a tiny bit:&lt;/p&gt;

&lt;p&gt;f(2.001) ≈ 8.012&lt;/p&gt;

&lt;p&gt;A very small increase in the input produces a measurable increase in the output.&lt;/p&gt;

&lt;p&gt;Mathematically, the derivative of "x³" is:&lt;/p&gt;

&lt;p&gt;3x²&lt;/p&gt;

&lt;p&gt;So at "x = 2":&lt;/p&gt;

&lt;p&gt;3 × 2² = 12&lt;/p&gt;

&lt;p&gt;That means around this point, every tiny increase in "x" changes the output about 12 times as much.&lt;/p&gt;




&lt;p&gt;So What Is a Gradient?&lt;/p&gt;

&lt;p&gt;A derivative works when there's only one input.&lt;/p&gt;

&lt;p&gt;But machine learning models don't have one input—they have thousands or even millions of parameters.&lt;/p&gt;

&lt;p&gt;A gradient is simply the derivative with respect to every parameter.&lt;/p&gt;

&lt;p&gt;One variable  → Derivative&lt;/p&gt;

&lt;p&gt;Many variables → Gradient (vector)&lt;/p&gt;

&lt;p&gt;Instead of getting one number, we get a collection of numbers telling us how each parameter affects the loss.&lt;/p&gt;




&lt;p&gt;Why Neural Networks Depend on Gradients&lt;/p&gt;

&lt;p&gt;Every model starts with random parameters.&lt;/p&gt;

&lt;p&gt;The model makes predictions.&lt;/p&gt;

&lt;p&gt;Those predictions are compared with the correct answers using a loss function.&lt;/p&gt;

&lt;p&gt;The gradient answers:&lt;/p&gt;

&lt;p&gt;«"Which parameters should increase, and which should decrease, to reduce this loss?"»&lt;/p&gt;

&lt;p&gt;Update the parameters.&lt;/p&gt;

&lt;p&gt;Repeat.&lt;/p&gt;

&lt;p&gt;Eventually the model learns.&lt;/p&gt;

&lt;p&gt;That simple loop powers almost every deep learning model.&lt;/p&gt;




&lt;p&gt;Trying Automatic Differentiation&lt;/p&gt;

&lt;p&gt;Instead of computing derivatives by hand, JAX can do it automatically.&lt;/p&gt;

&lt;p&gt;import jax&lt;/p&gt;

&lt;p&gt;def cube(x):&lt;br&gt;
    return x ** 3&lt;/p&gt;

&lt;p&gt;cube_grad = jax.grad(cube)&lt;/p&gt;

&lt;p&gt;print(cube_grad(2.0))&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;12.0&lt;/p&gt;

&lt;p&gt;Exactly what calculus predicts.&lt;/p&gt;




&lt;p&gt;Creating a Tiny Learning Problem&lt;/p&gt;

&lt;p&gt;Let's pretend we're trying to learn a simple equation:&lt;/p&gt;

&lt;p&gt;prediction = weight × 4&lt;/p&gt;

&lt;p&gt;The correct answer should be "20".&lt;/p&gt;

&lt;p&gt;def loss(weight):&lt;br&gt;
    x = 4.0&lt;br&gt;
    target = 20.0&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;prediction = weight * x

return (prediction - target) ** 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The ideal weight is:&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;because&lt;/p&gt;

&lt;p&gt;5 × 4 = 20&lt;/p&gt;




&lt;p&gt;Updating the Weight&lt;/p&gt;

&lt;p&gt;grad_loss = jax.grad(loss)&lt;/p&gt;

&lt;p&gt;weight = 0.5&lt;br&gt;
lr = 0.05&lt;/p&gt;

&lt;p&gt;for step in range(6):&lt;br&gt;
    gradient = grad_loss(weight)&lt;br&gt;
    weight -= lr * gradient&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(
    f"Step {step+1}: "
    f"weight={weight:.3f}, "
    f"loss={loss(weight):.3f}"
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Example output:&lt;/p&gt;

&lt;p&gt;Step 1: weight=4.100 loss=12.960&lt;br&gt;
Step 2: weight=4.820 loss=0.518&lt;br&gt;
Step 3: weight=4.964 loss=0.021&lt;br&gt;
Step 4: weight=4.993 loss=0.001&lt;br&gt;
Step 5: weight=4.999 loss=0.000&lt;br&gt;
Step 6: weight=5.000 loss=0.000&lt;/p&gt;

&lt;p&gt;Notice how the weight naturally moves closer to the correct answer after every iteration.&lt;/p&gt;




&lt;p&gt;Making Computations Faster with "jit"&lt;/p&gt;

&lt;p&gt;One of JAX's biggest strengths is compiling Python functions into highly optimized machine code.&lt;/p&gt;

&lt;p&gt;import jax&lt;br&gt;
import jax.numpy as jnp&lt;/p&gt;

&lt;p&gt;def compute(x):&lt;br&gt;
    return jnp.sum(jnp.exp(-x) * jnp.sin(x))&lt;/p&gt;

&lt;p&gt;compiled_compute = jax.jit(compute)&lt;/p&gt;

&lt;p&gt;data = jnp.linspace(0, 100, 500000)&lt;/p&gt;

&lt;p&gt;compiled_compute(data)&lt;/p&gt;

&lt;p&gt;The first execution takes longer because JAX compiles the function.&lt;/p&gt;

&lt;p&gt;After that, repeated calls become significantly faster.&lt;/p&gt;

&lt;p&gt;This compilation overhead is usually worth it whenever the same computation runs many times during training.&lt;/p&gt;




&lt;p&gt;My Biggest Takeaway&lt;/p&gt;

&lt;p&gt;I used to think learning JAX meant memorizing functions.&lt;/p&gt;

&lt;p&gt;Now I realize that's not the important part.&lt;/p&gt;

&lt;p&gt;The real skill is understanding the ideas behind them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"grad" computes derivatives automatically.&lt;/li&gt;
&lt;li&gt;"jit" speeds up repeated computations.&lt;/li&gt;
&lt;li&gt;Together, they form the foundation of efficient machine learning workflows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'm still early in my learning journey, but writing these examples myself made everything much clearer than simply reading documentation.&lt;/p&gt;

&lt;p&gt;Next up: "vmap"—JAX's tool for automatically vectorizing computations. I'm excited to see how it can eliminate explicit Python loops.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>devjournal</category>
      <category>machinelearning</category>
      <category>python</category>
    </item>
  </channel>
</rss>
