# How does the neural-network know how to tweak weights for a specific neuron?

I know backpropagation uses cost and gradient descent to tweak the weights to minimize the cost. But how does it know which weights to give more weight to in the first place? Is there something inside each neuron in the hidden layers that defines how this is an important neuron for the correct result in some way? How does the network know how to tweak those weights for that specific neuron?

### tl;dr

The whole point of gradient descent is to assess the contribution of each parameter towards the loss. This information is uncovered through the gradient of the loss w.r.t each parameter.

### A deeper look...

Suppose we have a NN with parameters $$w_{i}, \; i={1, 2, ...}$$. This NN makes some predictions, which we compare to the actual targets and compute a loss $$J$$. The loss (or cost) function tells us how far off we are from the target. This is what we want to reduce, so that the predictions fall closer to the target.

By computing the partial derivative of the loss function $$J$$ w.r.t a parameter $$w_i$$

$$\frac{\partial J}{\partial w_i}$$

the NN uncovers two pieces of information:

• The slope of $$J$$ w.r.t $$w_i$$, which tells the NN how much $$w_i$$ affects $$J$$.
• Its sign, which tells the NN which way to tweak $$w_i$$ to decrease (or increase) the value of $$J$$.

By making the parameter updates depend on the derivative

$$w_i^{new} \leftarrow w_i^{old} - \lambda \frac{\partial J}{\partial w_i}$$

the NN causes parameters that affect the loss the most, to be updated the most.

You can think of this as: parameters that are more to blame for the network's mistakes (i.e. contribute more towards the loss) are forced to change the most, in the direction that will decrease the loss.

• @NeilSlater The first derivative can be positive or negative. If the first derivative is zero, it does not tell you anything about the nature of the extremum, which can be a minimum or maximum (or a saddle point). If we assume that $\lambda > 0$, then, if the first derivative is positive, we will be decreasing the weights. If it is negative, he will be increasing them. – nbro Nov 23 '19 at 15:50
• @NeilSlater The derivative tells you the rate of change of the loss function. Depending on the sign of the derivative and depending on whether you use gradient descent or ascent (i.e. depending on whether you use $-$ or $+$ in the update step, respectively), you can increase or decrease the loss. I don't think that this answer is strictly incorrect (even though the answerer provides the update step for gradient descent). I've updated the answer to include "or increase". – nbro Nov 23 '19 at 21:12
• @NeilSlater Your assumption is wrong. In general, the derivative function $f'$ tells you how the function $f$ is changing. Depending on the specific value of the derivative function $f'$ at a specific point $x$, $f'(x)$, you can know how $f$ is changing, it can be increasing or decreasing. More specifically, $f'(x)$ corresponds to the slope of the tangent line that goes through $f$ at $x$. This tangent can have a positive or negative slope, or it can also have a zero slope (e.g. at a maximum). See ugrad.math.ubc.ca/coursedoc/math100/notes/derivs/deriv6.html. – nbro Nov 23 '19 at 22:59
• @NeilSlater So, no, the sign of the derivative does not tell you the direction that will increase the function, but it tells you how the function is changing, so it tells you how the function is increasing or decreasing. – nbro Nov 23 '19 at 23:02
• @NeilSlater Wow, I'm a bit late to the party, but when I say "Its sign, which tells the NN which way to tweak $w_i$ to decrease the value of $J$", I mean that by knowing the sign you know how to change $w_i$ so that you can change $J$ and then I follow with the update equation that shows exactly that. I see what you're trying to say (in your first comment) and you are right about that, but I don't see why what I said is grammatically incorrect. I think of it like saying "by having a compass you know which way to travel south" and then you'd correct me by saying "but a compass points north". – Djib2011 Nov 24 '19 at 11:23