<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged affects</title>
<link>https://coderlegion.com/tag/affects</link>
<description>Powered by Question2Answer</description>
<item>
<title>Understanding Backpropagation: Calculating Gradients for Hidden Layer Weights and Biases</title>
<link>https://coderlegion.com/21631/understanding-backpropagation-calculating-gradients-for-hidden-layer-weights-biases</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, we derived formulas for updating the output layer weights w3, w4, and bias b3. Now, we will understand how to calculate the gradients for the hidden layer parameters: w1, b1, w2, and b2.&lt;/p&gt;

&lt;h2&gt;
  
  
  How are w1, b1, w2, and b2 connected to the prediction?
&lt;/h2&gt;

&lt;p&gt;To find the gradients of the parameters in the hidden layer, we need to trace how changing these values affects the final prediction and the error (SSR).&lt;/p&gt;

&lt;p&gt;Let's recall the structure of our neural network:&lt;/p&gt;

&lt;p&gt;For the top neuron:&lt;/p&gt;

&lt;p&gt;x1 = input * w1 + b1&lt;/p&gt;

&lt;p&gt;y1 = f(x1) = log(1 + e^x1) &lt;em&gt;(using the softplus function)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For the bottom neuron:&lt;/p&gt;

&lt;p&gt;x2 = input * w2 + b2&lt;/p&gt;

&lt;p&gt;y2 = f(x2) = log(1 + e^x2) &lt;em&gt;(using the softplus function)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finally, the prediction:&lt;/p&gt;

&lt;p&gt;Predicted = y1 * w3 + y2 * w4 + b3&lt;/p&gt;

&lt;p&gt;And the prediction error:&lt;/p&gt;

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

&lt;p&gt;Since w1, b1, w2, and b2 are not directly connected to the output prediction, we must use the chain rule to backpropagate the error from the output layer back to the hidden layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying the Chain Rule to the Hidden Layer
&lt;/h2&gt;

&lt;p&gt;Let's calculate the gradient for the top neuron's weight w1 first.&lt;/p&gt;

&lt;p&gt;A change in w1 affects x1, which affects the output y1, which affects the predicted value, which finally affects the SSR.&lt;/p&gt;

&lt;p&gt;So, by the chain rule:&lt;/p&gt;

&lt;p&gt;dSSR/dw1 = dSSR/d(predicted) * d(predicted)/dy1 * dy1/dx1 * dx1/dw1&lt;/p&gt;

&lt;p&gt;Let's calculate each of these values:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. dSSR/d(predicted)
&lt;/h3&gt;

&lt;p&gt;As we saw in the previous articles, this is the derivative of SSR with respect to the predicted value:&lt;/p&gt;

&lt;p&gt;dSSR/d(predicted) = -2 * (Observed - Predicted)&lt;/p&gt;

&lt;h3&gt;
  
  
  2. d(predicted)/dy1
&lt;/h3&gt;

&lt;p&gt;Since Predicted = y1 * w3 + y2 * w4 + b3, and all other terms are treated as constants w.r.t y1:&lt;/p&gt;

&lt;p&gt;d(predicted)/dy1 = w3&lt;/p&gt;

&lt;h3&gt;
  
  
  3. dy1/dx1
&lt;/h3&gt;

&lt;p&gt;Since y1 = log(1 + e^x1), the derivative of the softplus function is the logistic sigmoid function:&lt;/p&gt;

&lt;p&gt;dy1/dx1 = e^x1 / (1 + e^x1)&lt;/p&gt;

&lt;h3&gt;
  
  
  4. dx1/dw1
&lt;/h3&gt;

&lt;p&gt;Since x1 = input * w1 + b1, differentiating w.r.t w1 gives:&lt;/p&gt;

&lt;p&gt;dx1/dw1 = input&lt;/p&gt;

&lt;h3&gt;
  
  
  Final formula for dSSR/dw1:
&lt;/h3&gt;

&lt;p&gt;Multiplying these parts together, we get:&lt;/p&gt;

&lt;p&gt;dSSR/dw1 = -2 * (Observed - Predicted) * w3 * (e^x1 / (1 + e^x1)) * input&lt;/p&gt;

&lt;h2&gt;
  
  
  Deriving the Gradient for Bias b1
&lt;/h2&gt;

&lt;p&gt;Similarly, for the top neuron's bias b1:&lt;/p&gt;

&lt;p&gt;dSSR/db1 = dSSR/d(predicted) * d(predicted)/dy1 * dy1/dx1 * dx1/db1&lt;/p&gt;

&lt;p&gt;The only term that changes here is the last one:&lt;/p&gt;

&lt;p&gt;dx1/db1 = 1 (since x1 = input * w1 + b1, derivative w.r.t b1 is 1)&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;p&gt;dSSR/db1 = -2 * (Observed - Predicted) * w3 * (e^x1 / (1 + e^x1)) * 1&lt;/p&gt;

&lt;h2&gt;
  
  
  Deriving the Gradients for the Bottom Neuron (w2 and b2)
&lt;/h2&gt;

&lt;p&gt;Following the same logic, we can find the gradients for the bottom neuron's parameters:&lt;/p&gt;

&lt;h3&gt;
  
  
  For weight w2:
&lt;/h3&gt;

&lt;p&gt;dSSR/dw2 = dSSR/d(predicted) * d(predicted)/dy2 * dy2/dx2 * dx2/dw2&lt;/p&gt;

&lt;p&gt;dSSR/dw2 = -2 * (Observed - Predicted) * w4 * (e^x2 / (1 + e^x2)) * input&lt;/p&gt;

&lt;h3&gt;
  
  
  For bias b2:
&lt;/h3&gt;

&lt;p&gt;dSSR/db2 = dSSR/d(predicted) * d(predicted)/dy2 * dy2/dx2 * dx2/db2&lt;/p&gt;

&lt;p&gt;dSSR/db2 = -2 * (Observed - Predicted) * w4 * (e^x2 / (1 + e^x2)) * 1&lt;/p&gt;

&lt;h2&gt;
  
  
  Improving Prediction with self Learning
&lt;/h2&gt;

&lt;p&gt;Once we calculate all these derivatives (dSSR/dw1, dSSR/db1, dSSR/dw2, dSSR/db2), we can update the hidden layer weights and biases using gradient descent:&lt;/p&gt;

&lt;p&gt;Step size w1 = derivation w1 * Learning rate&lt;br&gt;
New w1 = old w1 - Step size w1&lt;/p&gt;

&lt;p&gt;Step size b1 = derivation b1 * Learning rate&lt;br&gt;
New b1 = old b1 - Step size b1&lt;/p&gt;

&lt;p&gt;Step size w2 = derivation w2 * Learning rate&lt;br&gt;
New w2 = old w2 - Step size w2&lt;/p&gt;

&lt;p&gt;Step size b2 = derivation b2 * Learning rate&lt;br&gt;
New b2 = old b2 - Step size b2&lt;/p&gt;

&lt;p&gt;By doing this repeatedly, the model minimizes the error and converges to the optimal values for all weights and biases.&lt;/p&gt;

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

&lt;p&gt;We have successfully derived the formulas to calculate the gradients for w1, b1, w2, and b2. Combined with the output layer derivations, we now have the math for the entire neural network's backpropagation!&lt;/p&gt;

&lt;p&gt;In the next article, we will see how to implement this in code.&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/21631/understanding-backpropagation-calculating-gradients-for-hidden-layer-weights-biases</guid>
<pubDate>Tue, 30 Jun 2026 20:23:01 +0000</pubDate>
</item>
</channel>
</rss>