<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged gradient</title>
<link>https://coderlegion.com/tag/gradient</link>
<description>Powered by Question2Answer</description>
<item>
<title>Understanding Backpropagation: How Neural Networks Learn from Their Mistakes</title>
<link>https://coderlegion.com/21638/understanding-backpropagation-how-neural-networks-learn-from-their-mistakes</link>
<description>
&lt;h2&gt;
  
  
  From Linear Regression to Gradient Descent
&lt;/h2&gt;

&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt; to help more developers discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In previous parts we discussed about linear regression, gradient descent and how to calculate the optimal slope and intercept using Gradient Descent.&lt;/p&gt;

&lt;p&gt;In this article, we'll build an intuition for backpropagation, understand why it is necessary, and explore how the chain rule and gradient descent work together to enable neural network learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Backpropagation?
&lt;/h2&gt;

&lt;p&gt;Backpropagation is an algorithm used to train neural networks. &lt;/p&gt;

&lt;p&gt;It is a way to adjust the weights and biases of a neural network to reduce the error between the predicted output and the actual output. &lt;/p&gt;

&lt;p&gt;In simple terms, it is a way for the neural network to learn from its mistakes.&lt;/p&gt;

&lt;p&gt;This learning process is powered by an algorithm called &lt;strong&gt;backpropagation&lt;/strong&gt;, one of the most important concepts in machine learning. Backpropagation provides a systematic way for a neural network to determine which internal parameters contributed to an error and how those parameters should be updated to reduce future mistakes.&lt;/p&gt;

&lt;p&gt;Imagine teaching a student to solve math problems. &lt;/p&gt;

&lt;p&gt;After each attempt, you compare the student's answer with the correct one, identify where mistakes occurred, and provide feedback. Over time, the student adjusts their approach and improves. Backpropagation works in a similar way: the network makes a prediction, calculates the error, traces that error backward through the network, and updates its parameters accordingly.&lt;/p&gt;

&lt;p&gt;At its core, backpropagation combines two fundamental mathematical ideas:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Chain Rule&lt;/strong&gt; from calculus, which helps determine how changes in one part of the network affect the final error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradient Descent&lt;/strong&gt;, an optimization technique that uses those calculations to update the network's parameters in the direction that reduces error.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By repeatedly answering these questions across thousands or millions of training examples, neural networks gradually learn patterns hidden within data and become increasingly accurate.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnuphqwcp6bha9ol6pdo3.png&quot; alt=&quot;git-lrc&quot; width=&quot;360&quot; height=&quot;540&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use.&lt;/p&gt;

&lt;p&gt;&amp;#11088; &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt;&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/21638/understanding-backpropagation-how-neural-networks-learn-from-their-mistakes</guid>
<pubDate>Sun, 05 Jul 2026 07:23:45 +0000</pubDate>
</item>
<item>
<title>Understanding Idea behind Full Backpropogation</title>
<link>https://coderlegion.com/21634/understanding-idea-behind-full-backpropogation</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt; to help more developers discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In the previous article, we learned how to calculate the gradient of the last bias in a neural network.&lt;/p&gt;

&lt;p&gt;Now we will explore how gradients flow through the entire network and how to calculate the weights of previous layers.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to calculate weights
&lt;/h2&gt;

&lt;p&gt;Now we will calculate weights of the previous layer&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiofmkjdlpbvn0qcg2pxz.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiofmkjdlpbvn0qcg2pxz.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;486&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The challenge is that the loss function does not directly depend on these earlier weights.&lt;/p&gt;

&lt;p&gt;For example, consider a weight (w1 and w2 are already calculated ) in a hidden layer.&lt;/p&gt;

&lt;p&gt;Changing (w3 and w4):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Changes the hidden neuron output.&lt;/li&gt;
&lt;li&gt;Changes the output neuron input.&lt;/li&gt;
&lt;li&gt;Changes the final prediction.&lt;/li&gt;
&lt;li&gt;Changes the loss.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So there is an indirect relationship between the weight and the loss.&lt;/p&gt;

&lt;p&gt;This is exactly why we need the chain rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Similar to previous calculation we should also calculate for all weights and biases using chain rule and gradient descent.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnuphqwcp6bha9ol6pdo3.png&quot; alt=&quot;git-lrc&quot; width=&quot;360&quot; height=&quot;540&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use.&lt;/p&gt;

&lt;p&gt;&amp;#11088; &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt;&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/21634/understanding-idea-behind-full-backpropogation</guid>
<pubDate>Thu, 02 Jul 2026 18:14:45 +0000</pubDate>
</item>
<item>
<title>How to calculate weights using gradient descent</title>
<link>https://coderlegion.com/21633/how-to-calculate-weights-using-gradient-descent</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt; to help more developers discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In the previous article, I explained the requirements for finding the weights of the last layer. Now let's see how we actually assign and optimize those weights using gradient descent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting Random Weights From a Normal Distribution
&lt;/h2&gt;

&lt;p&gt;First, we assign random numbers (drawn from a normal distribution) to the weights w3 and w4.&lt;/p&gt;

&lt;p&gt;Then we sum up both results along with the bias b3 = 0:&lt;/p&gt;

&lt;p&gt;predicted = (output of top neuron &amp;times; w3) + (output of bottom neuron &amp;times; w4) + b3&lt;/p&gt;

&lt;p&gt;This gives us our initial prediction, and we can plot the final graph:&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fihfkif6jyy7wrwx0g276.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fihfkif6jyy7wrwx0g276.png&quot; alt=&quot;&quot; width=&quot;799&quot; height=&quot;326&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With these initial random weights, the SSR (Sum of Squared Residuals) is calculated again to measure how far off our predictions are.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gradient Descent Algorithm For Optimal Values
&lt;/h2&gt;

&lt;p&gt;Now we need to find the derivative of SSR with respect to b3 so we can update it.&lt;/p&gt;

&lt;p&gt;Recall our loss function:&lt;/p&gt;

&lt;p&gt;SSR = &amp;Sigma; (observed &amp;minus; predicted)&amp;sup2;&lt;/p&gt;

&lt;p&gt;And our predicted value is:&lt;/p&gt;

&lt;p&gt;predicted = (output of top neuron &amp;times; w3) + (output of bottom neuron &amp;times; w4) + b3&lt;/p&gt;

&lt;p&gt;This is the same chain rule approach we used for backpropagation when optimizing only b3.&lt;/p&gt;

&lt;p&gt;The key insight here is that the products of w3 and w4 with their respective neuron outputs are treated as constants for a single gradient calculation with respect to b3. Since only b3 is the variable in this expression, the derivative simplifies cleanly &amp;mdash; just as we saw in the previous articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By assigning random weights from a normal distribution and then applying gradient descent with the chain rule, we can iteratively optimize each weight and bias in the network. The same process that worked for b3 alone now extends to w3 and w4 &amp;mdash; we just need to carefully apply the chain rule at each step to compute the correct gradients.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnuphqwcp6bha9ol6pdo3.png&quot; alt=&quot;git-lrc&quot; width=&quot;360&quot; height=&quot;540&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.&lt;/p&gt;

&lt;p&gt;&amp;#11088; &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt;&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/21633/how-to-calculate-weights-using-gradient-descent</guid>
<pubDate>Thu, 02 Jul 2026 02:57:30 +0000</pubDate>
</item>
<item>
<title>From Linear Regression to Gradient Descent</title>
<link>https://coderlegion.com/21640/from-linear-regression-to-gradient-descent</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt; to help more developers discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In the previous section, we learned that linear regression finds the best-fitting line by determining the optimal slope and intercept.&lt;/p&gt;

&lt;p&gt;In this article, we will discuss how to calculate the optimal slope and intercept using Gradient Descent.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to calculate the optimal slope and intercept using Gradient Descent
&lt;/h2&gt;

&lt;p&gt;The quality of that line is measured using the Sum of Squared Residuals (SSR), which represents the total prediction error.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;SSR = sum( (y_observed - y_predicted)^2 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The best regression line is simply the line that produces the smallest SSR.&lt;/p&gt;

&lt;p&gt;When studying linear regression, it's easy to think that the slope and intercept magically appear from a formula. In reality, they are the values that minimize the prediction error. This is where Gradient Descent comes in.&lt;/p&gt;

&lt;p&gt;Instead of calculating the optimal slope and intercept directly using a closed-form equation, Gradient Descent starts with arbitrary values and gradually improves them. After each step, it measures how the SSR changes and adjusts the parameters in the direction that reduces the error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Gradient Descent Example
&lt;/h2&gt;

&lt;p&gt;Let's illustrate how Gradient Descent works using the exact same dataset of 4 points from Part 10:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Dataset
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Point 1:&lt;/strong&gt; (1, 2)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point 2:&lt;/strong&gt; (2, 3)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point 3:&lt;/strong&gt; (3, 5)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point 4:&lt;/strong&gt; (4, 4)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Simplifying the Problem
&lt;/h3&gt;

&lt;p&gt;To make the math easy to trace, we will hold the &lt;strong&gt;Slope (m)&lt;/strong&gt; constant at its optimal value of &lt;strong&gt;0.8&lt;/strong&gt; and focus purely on finding the optimal &lt;strong&gt;Intercept (b)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our prediction equation is:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;y_predicted = 0.8 * x + b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start with an initial guess for the intercept: &lt;code&gt;b = 0&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Calculating the Initial SSR (at b = 0)
&lt;/h3&gt;

&lt;p&gt;Let's find the predicted values and calculate the residuals (&lt;code&gt;observed - predicted&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Point 1 (1, 2):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 1 + 0 = 0.8&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_1 = 2 - 0.8 = 1.2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;For Point 2 (2, 3):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 2 + 0 = 1.6&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_2 = 3 - 1.6 = 1.4&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;For Point 3 (3, 5):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 3 + 0 = 2.4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_3 = 5 - 2.4 = 2.6&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;For Point 4 (4, 4):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 4 + 0 = 3.2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_4 = 4 - 3.2 = 0.8&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Now, sum the squared residuals:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;SSR = 1.2^2 + 1.4^2 + 2.6^2 + 0.8^2
    = 1.44 + 1.96 + 6.76 + 0.64
    = 10.8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Derivation of the Gradient (d(SSR)/db)
&lt;/h3&gt;

&lt;p&gt;To know which direction to move the intercept &lt;code&gt;b&lt;/code&gt; and by how much, we take the derivative of SSR with respect to &lt;code&gt;b&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;SSR = sum( (y_observed - (0.8 * x_observed + b))^2 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Applying the chain rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;d(SSR)/db = sum( 2 * (y_observed - (0.8 * x_observed + b)) * (-1) )
          = -2 * sum( y_observed - y_predicted )
          = -2 * sum( Residuals )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gradient is simply &lt;strong&gt;-2 times the sum of the residuals&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Updating the Intercept
&lt;/h3&gt;

&lt;p&gt;The update rule is:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;b_new = b_old - (Learning Rate * Gradient)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's choose a &lt;strong&gt;Learning Rate (LR)&lt;/strong&gt; of &lt;strong&gt;0.1&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gradient:&lt;/strong&gt; &lt;code&gt;d(SSR)/db = -2 * (1.2 + 1.4 + 2.6 + 0.8) = -2 * 6.0 = -12.0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Size:&lt;/strong&gt; &lt;code&gt;Gradient * LR = -12.0 * 0.1 = -1.2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Intercept:&lt;/strong&gt; &lt;code&gt;b_new = 0 - (-1.2) = 1.2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With &lt;code&gt;b = 1.2&lt;/code&gt;, the predictions are closer to the actual values.&lt;/li&gt;
&lt;li&gt;The new residuals are: &lt;code&gt;0.0&lt;/code&gt;, &lt;code&gt;0.2&lt;/code&gt;, &lt;code&gt;1.4&lt;/code&gt;, and &lt;code&gt;-0.4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSR:&lt;/strong&gt; &lt;code&gt;0.0^2 + 0.2^2 + 1.4^2 + (-0.4)^2 = 2.16&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradient:&lt;/strong&gt; &lt;code&gt;-2 * (0.0 + 0.2 + 1.4 - 0.4) = -2.4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Size:&lt;/strong&gt; &lt;code&gt;-2.4 * 0.1 = -0.24&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Intercept:&lt;/strong&gt; &lt;code&gt;b_new = 1.2 - (-0.24) = 1.44&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 3 (Convergence):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With &lt;code&gt;b = 1.44&lt;/code&gt;, the new residuals are: &lt;code&gt;-0.24&lt;/code&gt;, &lt;code&gt;-0.04&lt;/code&gt;, &lt;code&gt;1.16&lt;/code&gt;, and &lt;code&gt;-0.64&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradient:&lt;/strong&gt; &lt;code&gt;-2 * (-0.24 - 0.04 + 1.16 - 0.64) = -0.48&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Size:&lt;/strong&gt; &lt;code&gt;-0.48 * 0.1 = -0.048&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Intercept:&lt;/strong&gt; &lt;code&gt;b_new = 1.44 - (-0.048) = 1.488&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We repeat this loop. As we approach the optimal intercept, the residuals sum up closer to 0, which shrinks the gradient and steps.&lt;/li&gt;
&lt;li&gt;After several iterations, the gradient becomes 0, and the intercept converges to the exact optimal value of &lt;strong&gt;1.5&lt;/strong&gt; (where SSR reaches its minimum value of &lt;strong&gt;1.8&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We started with an arbitrary intercept of &lt;strong&gt;0&lt;/strong&gt; and adjusted it step-by-step. Each step was guided by the gradient, which told us exactly how much to change the intercept to reduce the prediction error (SSR). We repeated this process until the error reached its minimum.&lt;/p&gt;

&lt;p&gt;While this example focused on a simple linear regression with a single variable, this same principle applies to deep neural networks with millions of parameters. Gradient descent is the engine that drives learning in machine learning.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src=&quot;https://www.youtube.com/embed/4JW4rfheX3U&quot;&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnuphqwcp6bha9ol6pdo3.png&quot; alt=&quot;git-lrc&quot; width=&quot;360&quot; height=&quot;540&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use.&lt;/p&gt;

&lt;p&gt;&amp;#11088; &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt;&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/21640/from-linear-regression-to-gradient-descent</guid>
<pubDate>Tue, 30 Jun 2026 20:23:06 +0000</pubDate>
</item>
</channel>
</rss>