A one-feature model predicts 7 for a training example whose target is 8. If the weight is raised slightly, does the squared error go up or down, and how fast? Would raising the intercept instead have the same effect? Derivatives answer such questions for small changes. This lesson defines the derivative and the partial derivative, collects partial derivatives into the gradient, uses the chain rule to differentiate a squared prediction error through every intermediate step, and checks the result with a directional derivative and a finite difference.
Key terms
Derivative and differentiability
Definition Let be a real function defined on an open interval containing . The derivative of at is
when this limit exists as a finite number, with approaching through nonzero values from both sides. In that case is called differentiable at . Equivalently, there is a number with
where as . So near the function is approximated by a line with slope . The derivative has the unit of divided by the unit of . If the difference quotient has different one-sided limits at , then is not differentiable there, and a method that still needs a slope at must state a convention. For a convex function such as at , a standard choice is a subgradient, any number with for every real , which means any in .
In plain words The difference quotient is the average rate of change over a step of size , and the derivative is the value it settles on as the step shrinks. Being differentiable means that close enough to the graph looks like a straight line, so a small step changes the output by about . It says nothing about large steps.
Example For and any ,
which tends to as . So , and the limit argument is a proof for every , not a pattern read from a few numbers. The absolute value has the difference quotient at , which equals for and for . The one-sided limits disagree, so has no derivative at . Software often uses the subgradient there, which is a convention and not the value of a derivative.
Partial derivative
Definition Let be defined near the point . The partial derivative of with respect to at that point is the ordinary derivative at of the one-variable function , in which is held at ,
so the steps in the limit defining change alone. The partial derivative with respect to holds fixed in the same way. With more parameters, one coordinate moves and all the others stay fixed. In a training loss the data are held fixed as well.
In plain words Slice the loss surface along one coordinate axis and take the slope of the slice. Each partial derivative answers what happens when only one parameter moves while everything else stays put.
Example Fix one training example with feature and target . The model predicts , the residual on this page is prediction minus target, , and the loss is . The factor only simplifies the derivatives. At and the prediction is 7, the residual is and the loss is . Holding gives the slice , whose derivative equals at . Holding gives the slice , whose derivative equals at . So at this point and .
Gradient
Definition For a real function of a parameter vector , the gradient is the vector of partial derivatives in the same order as the parameters. On this page it is a column with the same shape as , so for
The function is differentiable at if its partial derivatives exist there and
with an error term satisfying as . Having all partial derivatives is not enough. The function equal to away from the origin, with , has both partial derivatives equal to at the origin, yet it equals at every point with , so it is not even continuous there. If the partial derivatives exist and are continuous on an open set containing , then is differentiable at . Polynomials, such as the loss on this page, are differentiable everywhere.
In plain words The gradient packs the one-at-a-time slopes into one vector. For a differentiable function, a small step that moves several parameters at once changes the value by about the dot product of the gradient with the step. The approximation is local, and curvature makes it less accurate for larger steps.
Example At and the gradient is . For the step it predicts a change of . The actual new prediction is , the residual is and the loss is , a change of from . The gap of is the curvature term , which shrinks like the square of the step.
Chain rule
Definition If is differentiable at and is differentiable at , then the composition is differentiable at and
For several variables, suppose intermediate quantities are differentiable functions of and is a differentiable function of . Then
A computational graph draws each quantity as a node and each direct dependency as an edge labeled with its local derivative. The derivative of the output with respect to a parameter is the sum, over all paths from the parameter to the output, of the product of the local derivatives along each path.
In plain words Rates multiply along a chain of dependencies, and contributions from separate routes add. Dropping one factor, such as the derivative of the prediction with respect to the weight, gives a wrong gradient even when every other step is right.
Example The graph for one example multiplies by , adds to give , subtracts to give , and squares and halves to give . The local derivatives are , , and , so
With and this gives , the gradient found from the slices. Expanding the loss as gives the same result, with the inner factor 3 for and 1 for . Forgetting the inner factor would make the two derivatives equal, which is wrong. With two training examples and total loss , the weight reaches the loss through both predictions, and the two paths add to .
Directional derivative
Definition The directional derivative of at along a vector is the rate of change of along the line at ,
where . If is differentiable at , the limit exists for every and
For a unit vector, with , this is the rate of change per unit of Euclidean distance in parameter space. For other vectors it is the rate per unit of . The partial derivative with respect to is the directional derivative along the coordinate vector . When , the Cauchy-Schwarz inequality shows that among unit vectors the rate is largest for and smallest, equal to , for the opposite vector. These steepest directions depend on the chosen coordinates and their scales.
In plain words Pick a straight path through parameter space and ask how fast the function changes as you set off along it. A step that moves several parameters at once mixes their partial derivatives, so it measures something different from any single one of them.
Example At and , with gradient , the unit vector gives
The direction , which raises both parameters together, has rate per unit of , or per unit distance. Neither number equals the partial derivative , which is the rate along . No unit direction decreases the loss faster than at the rate , reached along , the direction of .
Finite-difference approximation
Definition For a step , the forward difference and the central difference of at are
For a partial derivative, only the chosen coordinate is stepped and the others are held fixed. If has a continuous second derivative on , the forward difference differs from by for some in that interval. If has a continuous third derivative on , the central difference differs from by for some in that interval. In floating-point arithmetic a very small makes the two function values nearly equal, and their difference loses significant digits to rounding. Agreement between a finite difference and a formula is evidence that the formula or code is right at the tested points. It is not a proof, and it cannot show that a function is differentiable.
In plain words Nudge one parameter, measure how much the loss moves and divide by the nudge. It needs no derivative formula, which makes it a good independent test of a hand-derived or coded gradient, but it costs extra loss evaluations for every parameter. Too large a step picks up curvature and too small a step loses digits to rounding, so try a few step sizes and compare relative as well as absolute differences.
Example Hold and step around with . The loss values are and , so the central difference is , exact up to rounding because the loss is quadratic in . The forward difference is . Its error is exactly , since the second derivative in is . With rounding dominates instead, and in double-precision arithmetic both differences are off by about .
Check yourself
- Take a new example with and , the same loss with , and the parameters and . Find the prediction, the residual, the loss and the gradient, naming each chain-rule factor. Then check with a forward difference with step , holding fixed.
Show the answer
The prediction is , the residual is and the loss is . The factors are , , and . So and , and the gradient in the order is .
At the loss is , so the forward difference is . It differs from by , which is the curvature term , and a central difference with the same step gives up to rounding. The agreement supports the derivation at this point but does not prove it.
- For the same example, find the rate of change of the loss along the path that raises by and lowers by . Is it the partial derivative with respect to ?
Show the answer
The direction is , and the directional derivative is per unit of . A direct calculation agrees. Along the path the prediction is , so the residual is , the loss is and its derivative at is .
It is not the partial derivative with respect to , which is and moves only . On this path the drop in cancels part of the effect of raising , so the prediction rises by instead of . Because has length , the rate per unit distance is .
- Someone checks the derivative of at with a central difference and gets for every step . They conclude that . What is wrong?
Show the answer
The central difference is for every , but the derivative does not exist at . The forward difference and the backward difference show the two one-sided limits, and they disagree. The central difference is the average of these two, so it returns a tidy number at a corner where no derivative exists.
A finite difference cannot establish differentiability. If an algorithm needs a value at the corner, it has to state a convention, such as a subgradient in . The value is one valid subgradient, not the derivative.