Ten single-variable derivatives that exercise the product, quotient and chain rules on the functions machine learning uses — exponentials, logs, sigmoid, tanh, softplus, nested trig, xˣ and an implicit curve — with worked solutions and the mistakes that drop the inner derivative or flip the quotient rule.
Before you start
Three rules and a short table of basic derivatives are enough to differentiate every function on this page. The work goes wrong in a few places that recur: an inner derivative left off, a quotient rule with its numerator reversed, a product rule that multiplies the two derivatives, a power rule used on a variable exponent. These ten problems run from single rules to nested ones, then to xx and a curve given by an equation.
f′(x) and dxd both mean the derivative with respect to .x.u and v stand for any differentiable functions of .x.
Product rule: .(uv)′=u′v+uv′.
Quotient rule: ,(u/v)′=(u′v−uv′)/v2, where .v=0.
Chain rule: .(f∘g)′=f′(g)g′.
Basic derivatives: (xn)′=nxn−1 for a constant ;n;;(ex)′=ex;(lnx)′=1/x for ;x>0;;(sinx)′=cosx;.(cosx)′=−sinx.
ln is the natural log; σ is the logistic sigmoid, ;σ(x)=1/(1+e−x);.tanhx=(ex−e−x)/(ex+e−x).y names the function of x being differentiated when it is not given by an explicit formula (Problems 7 and 10).
Write the derivative of the outer function evaluated at the inner one, then multiply by the inner derivative — the factor the mistakes below drop.
Problems
·
Differentiate .x3ex.
·
Differentiate .1+x2x.
·
Differentiate .sin(3x2).
··
Differentiate ln(1+ex) (softplus) and simplify.
··
Differentiate σ(x)=1+e−x1 and write the result in terms of σ(x) alone.
··
Differentiate tanhx=ex+e−xex−e−x and write the result in terms of .tanhx. Then show .tanhx=2σ(2x)−1.
··
Differentiate xx for .x>0.
···
Differentiate ln(1+exex) and simplify to a single sigmoid.
···
Differentiate 1+sin2(2x) and simplify with a double-angle identity.
···
The curve x2+xy+y2=7 defines y as a function of x near most of its points. Find .dxdy.
Answers
ex(x3+3x2)
(1+x2)21−x2
6xcos(3x2)
σ(x)
σ′(x)=σ(x)(1−σ(x))
;1−tanh2x; and 2σ(2x)−1=1+e−2x2−1=1+e−2x1−e−2x=tanhx
xx(lnx+1)
σ(−x)
1+sin2(2x)sin4x
,dxdy=−x+2y2x+y, valid where x+2y=0
Worked solutions
Problem 1
Differentiate .x3ex.
,u=x3,;v=ex;,u′=3x2,.v′=ex.The function is a product of two factors whose derivatives are in the table, so the product rule applies.
.(x3ex)′=3x2⋅ex+x3⋅ex.Product rule, :u′v+uv′: each term differentiates one factor and leaves the other alone.
ex(x3+3x2)Both terms contain ,ex, so it factors out.
Problem 2
Differentiate .1+x2x.
,u=x,;v=1+x2;,u′=1,.v′=2x.The function is a quotient, so name the numerator and the denominator and differentiate each.
.(1+x2x)′=(1+x2)21⋅(1+x2)−x⋅2x.Quotient rule: u′v first, uv′ subtracted, over .v2.
.1⋅(1+x2)−x⋅2x=1+x2−2x2=1−x2.Expand the numerator and collect the x2 terms.
(1+x2)21−x2Sanity check: the function rises to its maximum 21 at x=1 and falls after it, and the derivative is 0 at ,x=1, positive for ∣x∣<1 and negative for .x>1.
Problem 3
Differentiate .sin(3x2).
Outer function ,sin, inner function .g(x)=3x2.sin is applied to ,3x2, not to ,x, so this is a composition and needs the chain rule.
The derivative of ,sin, evaluated at the inner function: .cos(3x2).,(sint)′=cost, and the chain rule evaluates it at .t=3x2.
The inner derivative: .g′(x)=6x.Power rule with the constant 3 carried along.
6xcos(3x2)Chain rule, :f′(g)g′: the product of steps 2 and 3, with the factor 6x written first.
Problem 4
Differentiate ln(1+ex) (softplus) and simplify.
Outer function ,ln, inner function .1+ex.The log is applied to ,1+ex, not to ,x, so the chain rule applies.
The derivative of ,ln, evaluated at the inner function: .1+ex1.(lnt)′=1/t at ,t=1+ex, which is positive for every .x.
The inner derivative: .(1+ex)′=ex.The constant 1 has derivative 0.
.dxdln(1+ex)=1+exex.Chain rule: step 2 times step 3.
.1+exex=e−x+11.Divide top and bottom by ,ex, which is never 0, so that x appears only as ,e−x, the form in the definition of .σ.
σ(x)1/(1+e−x) is the definition of :σ(x): softplus is an antiderivative of the sigmoid.
Problem 5
Differentiate σ(x)=1+e−x1 and write the result in terms of σ(x) alone.
.σ=(1+e−x)−1.Written as a power of an inner function, σ needs only the chain rule and the power rule, not the quotient rule.
The derivative of ,t−1, evaluated at the inner function: .−(1+e−x)−2.,(t−1)′=−t−2, at .t=1+e−x.
The inner derivative: .(1+e−x)′=e−x⋅(−1)=−e−x.e−x is itself a composition: the derivative of et at t=−x is ,e−x, and .(−x)′=−1.
.σ′(x)=−(1+e−x)−2⋅(−e−x)=(1+e−x)2e−x.Chain rule: step 2 times step 3; the two minus signs cancel.
.(1+e−x)2e−x=σ(x)⋅σ(x)e−x.Each factor 1/(1+e−x) is ,σ(x), so the square in the denominator gives .σ(x)2.
.e−x=σ(x)1−1.1/σ(x)=1+e−x by the definition of ;σ; subtract 1.
.σ(x)2(σ(x)1−1)=σ(x)−σ(x)2.Substitute step 6 into step 5 and multiply out.
σ′(x)=σ(x)(1−σ(x))Factor out .σ(x). The derivative is largest, ,41, where ,σ(x)=21, at .x=0.
Problem 6
Differentiate tanhx=ex+e−xex−e−x and write the result in terms of .tanhx. Then show .tanhx=2σ(2x)−1.
,u=ex−e−x,;v=ex+e−x;,u′=ex+e−x,.v′=ex−e−x.(e−x)′=−e−x (Problem 5, step 3), so differentiating flips the sign of each e−x term: u′=v and .v′=u.
.dxdtanhx=(ex+e−x)2(ex+e−x)2−(ex−e−x)2.Quotient rule: u′v=v2 and .uv′=u2.
.v2v2−u2=1−(vu)2.Split the fraction into two terms; the first is 1.
.dxdtanhx=1−tanh2x.u/v is tanhx by definition.
.2σ(2x)−1=1+e−2x2−1=1+e−2x2−(1+e−2x)=1+e−2x1−e−2x.The definition of σ at ,2x, then put 1 over the same denominator.
.1+e−2x1−e−2x=ex+e−xex−e−x.Multiply top and bottom by :ex:.ex⋅e−2x=e−x.
;1−tanh2x; and 2σ(2x)−1=1+e−2x2−1=1+e−2x1−e−2x=tanhxSteps 4 to 6. So tanh is a sigmoid stretched to the range (−1,1) and squeezed horizontally by 2.
Problem 7
Differentiate xx for .x>0.
Let .y=xx. Then .lny=xlnx.The exponent is the variable, so the power rule, which needs a constant exponent, does not apply; the log turns the exponent into a factor. x>0 makes ,y>0, so lny is defined.
.dxdlny=y1⋅y′.y is a function of ,x, so this is a chain rule: the derivative of ln evaluated at y is ,1/y, times the inner derivative .y′.
.dxd(xlnx)=1⋅lnx+x⋅x1=lnx+1.Product rule with u=x and ;v=lnx;(lnx)′=1/x for .x>0.
,yy′=lnx+1, so .y′=y(lnx+1).The two sides of step 1 are equal for every ,x>0, so their derivatives are equal; multiply by .y.
xx(lnx+1)Substitute .y=xx.
Another route: through the exponential
xx=exlnx for .x>0.,x=elnx, and raising both sides to the power x multiplies the exponent by .x.
The derivative of ,et, evaluated at the inner function: .exlnx.(et)′=et at .t=xlnx.
The inner derivative: .(xlnx)′=lnx+1.Product rule, as in step 3 of the first route.
xx(lnx+1)Chain rule: step 2 times step 3, and .exlnx=xx.
Problem 8
Differentiate ln(1+exex) and simplify to a single sigmoid.
.ln1+exex=lnex−ln(1+ex)=x−ln(1+ex).Simplify before differentiating: the log of a quotient is a difference of logs, and .lnex=x. That replaces a chain rule wrapped around a quotient rule with two terms whose derivatives are already known.
.dxd(x−ln(1+ex))=1−σ(x).x has derivative 1, and the derivative of ln(1+ex) is σ(x) (Problem 4).
.1−σ(x)=1−1+e−x1=1+e−xe−x.Put 1 over the denominator .1+e−x.
.1+e−xe−x=ex+11.Divide top and bottom by .e−x.
σ(−x)1/(1+ex) is the definition of σ evaluated at .−x. Since ex/(1+ex)=σ(x) (Problem 4, step 5), this also says .dxdlnσ(x)=1−σ(x)=σ(−x).
Problem 9
Differentiate 1+sin2(2x) and simplify with a double-angle identity.
Four layers, outermost first: ,t, then ,1+s2, then ,sinw, then .w=2x.Reading the function from the outside in lists the compositions; the chain rule needs one factor per layer.
The derivative of ,t, evaluated at the inner function :1+sin22x:.21+sin22x1.(t)′=1/(2t) at ,t=1+sin22x, which is at least 1, so the square root is never 0.
The derivative of ,1+s2, evaluated at :sin2x:.2sin2x.(1+s2)′=2s at .s=sin2x.
The derivative of ,sinw, evaluated at :2x:;cos2x; and .(2x)′=2.(sinw)′=cosw at ,w=2x, and the innermost derivative is the constant 2.
.dxd1+sin22x=21+sin22x2sin2xcos2x⋅2.Chain rule through all four layers: the product of the factors in steps 2 to 4.
.2sin2xcos2x=sin4x.Double-angle identity sin2θ=2sinθcosθ with .θ=2x.
1+sin2(2x)sin4xSubstitute step 6 into the numerator of step 5; the remaining factor 2 cancels the 2 in the denominator.
Problem 10
The curve x2+xy+y2=7 defines y as a function of x near most of its points. Find .dxdy.
Treat y as a function of x and differentiate both sides of x2+xy+y2=7 with respect to .x.The equation holds at every x in a stretch of the curve, so the derivatives of the two sides are equal there. Write y′ for .dy/dx.
.dxdx2=2x.Power rule.
.dxd(xy)=1⋅y+xy′.Product rule with u=x and ;v=y;y depends on ,x, so its derivative y′ stays.
.dxdy2=2y⋅y′.Chain rule: the derivative of t2 evaluated at y is ,2y, times the inner derivative .y′.
.2x+y+xy′+2yy′=0.Add steps 2 to 4; the right side, 7, is constant.
.y′(x+2y)=−(2x+y).Collect the terms containing y′ on one side.
,dxdy=−x+2y2x+y, valid where x+2y=0Divide by ,x+2y, which is allowed only where it is not 0. Where x+2y=0 the curve meets ,y=−x/2, so 43x2=7 and ;x=±28/3; the tangent there is vertical, and near those two points y is not a function of .x.
Where this goes wrong
1. Forgetting the inner derivative
dxdsinx=cosx is the first derivative most people memorize, and it is easy to reuse it with anything in the place of .x.
sin(3x2) has outer function sin and inner function 3x2Right so far: the function is a composition.
“The derivative of sin is ,cos, so the derivative of sin(anything) is .cos(anything).”The analogy that causes the mistake: dxdsinx=cosx applied with 3x2 in the place of .x.
dxdsin(3x2)=cos(3x2)This is the outer derivative evaluated at the inner function, the chain rule's first factor, with the second factor, ,(3x2)′=6x, left off. The correct derivative is 6xcos(3x2) (Problem 3). dxdsinx=cosx is the case where the inner function is x and its derivative is 1, which is why the factor never shows up there.
2. Quotient rule with the numerator the wrong way round
The product rule's two terms can be added in either order, and the quotient rule looks like it has the same two terms.
,u=x,,v=1+x2,,u′=1,v′=2xRight so far: the pieces are those of Problem 2.
“The numerator is the two cross terms, one minus the other.”The analogy that causes the mistake: remembering the terms of the product rule without the order the minus sign imposes.
(1+x2x)′=(1+x2)2x⋅2x−(1+x2)That is ;uv′−u′v; the quotient rule is .u′v−uv′. Swapping the two terms of a difference flips its sign, so the whole answer comes out negated: (1+x2)2x2−1 instead of .(1+x2)21−x2. A check at x=0 catches it: the function rises through the origin, so the derivative there must be positive, and this line gives .−1.
3. Product rule as the product of the derivatives
For a sum, the derivative is the sum of the derivatives, and it is natural to expect the same for a product.
,u=x3,,v=ex,,u′=3x2,v′=exRight so far: the pieces are those of Problem 1.
“The derivative of a product is the product of the derivatives.”The analogy that causes the mistake: the sum rule carried over to products.
(x3ex)′=3x2exThat is ,u′v′, and because v′=ex=v it also equals ,u′v, one of the product rule's two terms, which makes it look half-right. The other term is ,uv′=x3ex, and the derivative is ex(x3+3x2) (Problem 1). A check with x⋅x=x2 shows the analogy fails: the product of the derivatives is ,1⋅1=1, but .(x2)′=2x.
4. Power rule on a variable exponent
xx looks like ,xn, and (xn)′=nxn−1 is the rule people reach for first.
xx for x>0Right so far: the function and its domain.
“xx is x to a power, so bring the power down and subtract 1.”The analogy that causes the mistake: the power rule applied with .n=x.
(xx)′=x⋅xx−1The power rule needs a constant exponent; here the exponent changes with ,x, and its contribution, the term ,xxlnx, is lost. This line simplifies to ,xx, which is not the derivative: at x=1 it gives 1, the correct value, but at x=e it gives ee where the derivative is .2ee. Take logs first (Problem 7): .xx(lnx+1).
5. Derivative of a log without the inside
(lnx)′=1/x is short and easy to reuse with any argument in the place of .x.
ln(1+ex) has outer function ln and inner function u=1+exRight so far: the function is a composition.
“The derivative of ln of something is one over that something.”The analogy that causes the mistake: (lnx)′=1/x applied with 1+ex in the place of .x.
dxdln(1+ex)=1+ex1This is only the outer derivative evaluated at the inner function. The chain rule gives ,(lnu)′=u′/u, and u′=ex here, so the derivative is 1+exex=σ(x) (Problem 4). The wrong line is ,σ(−x), which falls where the true derivative rises: softplus is increasing, and its slope must approach 1 as x grows, not 0.
Print this set: differentiation-rules.pdf (problems, answers, and worked solutions on separate pages).