Differentiation
local graph
I turn with terror and horror from this lamentable scourge of continuous functions with no derivatives.
— Charles Hermite
In the history of science, a few milestones are as significant as inventing the wheel. Even among these, differentiation is a highlight. With the invention of calculus, Newton created mechanics as we know it. Differentiation is all over science and engineering, and as it turns out, it’s a key component of machine learning as well.
Why? Because of optimization! As it turns out, the extremal points of a function can be characterized in terms of their derivative, and these extremal points can be iteratively found via gradient descent.
In this chapter, we’ll learn what differentiation is, what its origins are, and most importantly, how to use it in practice. Let’s go!
Bookmark
12.1 Differentiation in theory
Section titled “12.1 Differentiation in theory”Instead of jumping straight into the mathematical definition, let’s start our discussion with a straightforward example: a point-like object moving along a straight line. Its movement is fully described by the time-distance plot (Figure 12.1), which shows its distance from the starting point at a given time.

Figure 12.1: Time-distance plot of our moving object
Our goal is to calculate the object’s velocity at a given time. In high school, we learned that

To put this into a quantitative form, if f(t) denotes the time-distance function, and t1>t2 are two arbitrary points in time, then

Expressions like
are called differential quotients. Note that if the object moves backwards, the average velocity is negative. (As opposed to speed, which is always positive. Velocity is speed and direction.)
The average velocity has a simple geometric interpretation: if you replace the object’s motion with a constant velocity motion moving with that average, you’ll end up at exactly the same place. In graphical terms, this is equivalent of connecting (t1,f(t1)) and (t2,f(t2)) with a single line.
The average velocity is just the slope of this line. This is visualized by Figure 12.2.

Figure 12.2: Average velocity between t1 and t2
Given this, we can calculate the exact velocity at a single time point t, which we’ll denote with v(t). The idea is simple: the average speed in the small time-interval between t and t + Δt should get closer and closer to v(t) if Δt is small enough. (Δt can be negative as well.)
So,
v(t) = limΔt → 0 (f(t + Δt) - f(t)) / Δt, (12.1)
if the above limit exists. Figure 12.3 illustrates the limit defined by (12.1). There, we can see that as Δt gets closer and closer to 0, the slope of the line connecting (t,f(t)) to (t + Δt,f(t + Δt)) gets closer and closer to the slope of the tangent.

Figure 12.3: Approximating the speed at t
Following our geometric intuition, we see that v(t) is simply the slope of the tangent line of f at t. Keeping this in mind, we are ready to introduce the formal definition.
Definition 54. (Differentiability)
Let f : ℝ →ℝ be an arbitrary function. We say that f is differentiable at x0 ∈ℝ if the limit

exists. If so, this is called the derivative of f at x0.
In other words, if f describes a time-distance function of a moving object, then the derivative is simply its velocity.
Similar to continuity, differentiability is a local property. However, we’ll be more interested in functions that are differentiable (almost) everywhere. In those cases, the derivative is a function, often denoted with f′(x).
Sometimes it is confusing that x can denote the variable of f and the exact point where the derivative is taken. Here is a quick glossary of terms to clarify the difference between derivative and derivative function.
(x0): derivative of f with respect to the variable x at the point x0. This is a scalar, also denoted with f′(x0).
: derivative function of f with respect to the variable x. This is a function, also denoted with f′.
Remark 9. (Variables in the limit)
Don’t let the change in notation from t and t + Δt to x0 and x confuse you; the limit that defines the derivative means exactly the same as before:

Also note that

On occasion, we might even use x and y instead of x0 and x, writing

We’ll use whichever is more convenient.
Let’s see some examples!
Example 1. f(x) = x. For any x, we have

Thus, f(x) = x is differentiable everywhere and its derivative is the constant function f′(x) = 1.
Example 2. f(x) = x2. Here, we have

So, f(x) = x2 is differentiable everywhere and f′(x) = 2x. Later, when talking about elementary functions, we’ll see the general case f(x) = xk.
Example 3. f(x) = jxj at x = 0. For this, we have

Since

this limit does not exist, as it is illustrated on Figure 12.4. This is our first example of a non-differentiable function. However, jxj is differentiable everywhere else.
It is worth drawing a picture here to enhance our understanding of differentiability. Recall that the value of the derivative at a given point equals the slope of the tangent line to the function’s graph.
Since jxj has a sharp corner at 0, the tangent line is not well-defined.

Figure 12.4: Tangent manifolds of f(x) = jxj at 0
Differentiability means no sharp corners in the graph, so differentiable functions are often called smooth. This is one reason we prefer differentiable functions: the rate of change is tractable.
Next, we’ll see an equivalent definition of differentiability, involving local approximation with a linear function. From this perspective, differentiability means manageable behavior: no wrinkles, corners, or sharp changes in value.
12.1.1 Equivalent forms of differentiation
Section titled “12.1.1 Equivalent forms of differentiation”To really understand derivatives and differentiation, we are going to take a look at it from another point of view: local linear approximations.
Approximation is a very natural idea in mathematics. Say, have you ever thought about what happens when you punch sin(2.18) into a calculator? We cannot express the function sin with finitely many additions and multiplications, so we have to approximate it. In practice, we use functions of the form

which, can be evaluated easily. These are called polynomials, and they are just a finite combination of additions and multiplications.
Can we just replace functions with polynomials to make computations easier? (Even at the cost of perfect precision.)
It turns out that we can, and differentiation is one way to do so. In essence, the derivative desribes the best local approximation with a linear function.
The following theorem makes this clear.
Theorem 77. (Differentiation as a local linear approximation)
Let f : ℝ →ℝ be an arbitrary function. The following are equivalent.
(a) f is differentiable at x0.
(b) there is an α ∈ℝ such that
f(x) = f(x₀) + α(x − x₀) + o(|x − x₀|) as x → x₀. (12.2)
Recall that the small O notation (see Definition 52) means that the function is an order of magnitude smaller around x0 than the function |x −x0|.
If exists, the α in the above theorem is the derivative f′(x0). In other words, f(x) can be locally written as
f(x0) + f′(x0)(x − x0) + o(|x − x0|). (12.3)
Proof. To show the equivalence of two statements, we have to prove that differentiation implies the desired property and vice versa. Although this might seem complicated, it is straightforward and entirely depends on how functions can be written as their limit plus an error term (Theorem 71).
(a) ⇒ (b). The existence of the limit

implies that we can write the slope of the approximating tangent in the form

where limx→x0error(x) = 0.
With some simple algebra, we obtain

Since the error term tends to zero as x goes to x0, error(x)(x −x0) = o(|x −x0|), which is what we wanted to show.
(b) ⇒ (a). Now, repeat what we did in the previous part, just in reverse order. We can rewrite

in the form

which, according to what we have used before, implies that

So, f is differentiable at x0 and its derivative is f′(x0) = α.
One huge advantage of this form is that it will be easily generalized to multivariate functions. Even though we are far from it, we can get a glimpse. Multivariate functions map vectors to scalars, so the ratio

is not even defined. (Since we can’t divide with a vector.) However, the expression

makes perfect sense, since ∇f(x0)T (x −x0) is a scalar. Here, ∇f(x0) denotes the gradient of f, that is, the multivariable version of derivatives. ∇f(x0) is an n-dimensional vector. Don’t worry if you are not familiar with this notation, we’ll cover everything in due time. The take-home message is that this alternative definition will be more convenient for us in the future.
Another advantage of the locally-best-approximation mindset is that Theorem 77 can be generalized to higher derivatives.
Check out the following theorem.
Theorem 78. (Taylor’s theorem)
Let f : ℝ →ℝ function that is n times differentiable at x0. Then

holds, where f(k)(x0) denotes the k-th derivative of f at x0.
(Note that the zeroth derivative f(0) equals to f.)
In other words, Theorem 78 says that if f is differentiable enough times, it can be written as a polynomial plus a small error term. For infinitely differentiable functions, Taylor’s theorem gives rise to the famous Taylor expansion.
Definition 55. (Taylor expansion)
Let f : ℝ → ℝ be a function that is differentiable at x0 infinitely many times. The series defined by

is called the Taylor expansion of f around x0.
To give you an example, as (ex)′ = ex, the Taylor expansion of ex around 0 is

Note that the equality sign is not an accident: the Taylor expansion of ex equals ex! (This is not always the case.) Now we see why e = ∑ k=0∞
, as we hinted earlier when discussing sequences and series.
In other words,

meaning that on any interval [ −α,α] and for any arbitrarily small 𝜀>0,

holds if n is large enough. In practice, this polynomial is evaluated to approximate the value of ex.
12.1.2 Differentiation and continuity
Section titled “12.1.2 Differentiation and continuity”As the following theorem states, differentiation is a more strict condition than continuity.
Theorem 79.
Differentiable functions are continuous.
If f : ℝ →ℝ is differentiable at a, it is also continuous there.
Proof. We’ll use Theorem 77 to prove the result. With the general form (12.2), we have

which shows the continuity of f at x0.
Note that the previous theorem is not true the other way around: a function can be continuous, but not differentiable. (As the example f(x) = jxj at x = 0 shows.)
This can be taken to the extremes: there are functions that are continuous everywhere but differentiable nowhere. One of the first examples was provided by Weierstrass (from the Bolzano-Weierstrass theorem). The function itself is defined by the infinite sum

where a ∈ (0,1), b is a positive odd integer, and ab>1 + 3π∕2.
I agree, this definition feels totally random, and you are probably wondering: how did the author come up with it? To get a grip on this function, imagine this as the superposition of cosine waves with smaller and smaller amplitude but higher and higher frequency. Remember that differentiation implies “no sharp corners”? This definition puts a sharp corner at every point on the real line.
Its graph is a fractal curve with self-similarity, as illustrated below.

Figure 12.5: Graph of the Weierstrass function. Source: https://en.wikipedia.org/wiki/Weierstrass_function
Examples such as this inspired the opening quote of the section:
I turn with terror and horror from this lamentable scourge of continuous functions with no derivatives. — Charles Hermite
19th-century mathematicians certainly did not think much about nondifferentiable functions. However, there are much more of them than differentiable ones. We won’t go into the details, but amongst all continuous functions, the set of ones that are differentiable at at least one point is meagre. Meagre is a proper technical term for sets, and although we don’t need to know what it means exactly, its name implies that it is extremely small.
Now that we understand what the derivative is, it’s time to put theory into practice. How do we compute derivatives, and how do we work with them in machine learning? We’ll see in the next section.
Bookmark
12.2 Differentiation in practice
Section titled “12.2 Differentiation in practice”During our first encounter with differentiation, we saw that computing derivatives by the definition

can be really hard in practice if we encounter convoluted functions such as f(x) = cos(x)sin(ex). Similar to convergent sequences and limits, using the definition of differentiation won’t get us far—the complexity piles on fast. So, we have to find ways to decompose the complexity into its fundamental building blocks.
12.2.1 Rules of differentiation
Section titled “12.2.1 Rules of differentiation”First, we’ll look at the simplest of operations: scalar multiplication, addition, multiplication, and division.
Theorem 80. (Rules of differentiation)
Let f : ℝ → ℝ and g : ℝ → ℝ be two arbitrary functions and let x ∈ ℝ. Suppose that both f and g is differentiable at x. Then
(a) (cf)′(x) = cf′(x) for all c ∈ℝ,
(b) (f + g)′(x) = f′(x) + g′(x),
(c) (fg)′(x) = f′(x)g(x) + f(x)g′(x) (the product rule),
(d) (
)′(x) =
if g(x)≠0 (the quotient rule).
Proof. (a) and (b) is a direct consequence of the Theorem 70.
To show (c), we have to do a bit of algebra:
![lim f(x)g(x)−-f-(y)g(y)-= lim f(x)g(x)−--f(y)g-(x-)+-f(y)g(x)−-f-(y)g(y)- y→x x − y x→y x − y f(x)g(x)− f(y)g (x ) f(y)g(x)− f (y)g (y) = lyim→x -------x−--y-------+ lxi→my -------x−-y-------- = lim [f(x)−-f(y)g(x)]+ f (y ) lim g(x)−-g(y)- y→x x − y x→y x − y = f′(x)g(x)+ f(x)g′(x),](https://static.packt-cdn.com/products/9781837027873/graphics/media/file1188.png)
from which (c) follows.
For (d), we are going to start with the special case of (1∕g)′. We have

from which the general case follows by applying (c) to f and 1∕g.
There is one operation which we haven’t covered in the previous theorem: function composition. In the study of neural networks, composition plays an essential role. Each layer can be thought of as a function, which are composed together to form the entire network.
Theorem 81. (Chain rule/Leibniz rule)
Let f : ℝ→ℝ and g : ℝ→ℝ be two arbitrary functions and let x ∈ ℝ. Suppose that g is differentiable at x and f is differentiable at g(x). Then

holds.
Proof. First, we rewrite the differential quotient into the following form:

Because g is differentiable at x, it is also continuous there, so limy→xg(y) = g(x). So, the first term can be rewritten as

Since g is differentiable at x, the second term is g′(x). Thus, we have

which is what we had to show.
As neural networks are just huge composed functions, their derivative is calculated with the repeated application of the chain rule. (Although the derivatives of its layers are vectors and matrices since they are multivariable functions.)
12.2.2 Derivatives of elementary functions
Section titled “12.2.2 Derivatives of elementary functions”Following the already familiar pattern, now we calculate the derivatives for the most important class: elementary functions. There are a few that we will encounter all the time, like in the mean squared error, cross-entropy, Kullback-Leibler divergence, etc.
Theorem 82. (Derivatives of elementary functions)
(a) (x0)′ = 0 and (xn)′ = nxn−1, where n ∈ℤ ∖ 0,
(b) (sinx)′ = cosx and (cosx)′ = −sinx,
(c) (ex)′ = ex,
(d) (log x)′ =$$
.
You don’t necessarily have to know how to prove these. I’ll include the proof of (a), but feel free to skip it, especially if this is your first encounter with calculus. What you have to remember, though, are the derivatives themselves. (However, I’ll refer back to this part when necessary.)
Proof. (a) It is easy to see that for n = 0, the derivative (x0)′ = 0. The case n = 1 is also simple: calculating the differential quotient shows that (x)′ = 1. For the case n ≥ 2, we are going to employ a small trick. Writing out the differential quotient for f(x) = xn, we obtain

which we want to simplify. If you don’t have a lot of experience in math, it might seem like magic, but xn −yn can be written as

This can be seen easily by calculating the product:
![n∑−1 n−1−k k n n−1 n−1 (x − y) x y .= x + [x y + ⋅⋅⋅+ xy ] k=0 − [xn− 1y + ⋅⋅⋅+ xyn− 1]− yn = xn − yn.](https://static.packt-cdn.com/products/9781837027873/graphics/media/file1199.png)
Thus, we have

So, (xn)′ = nxn−1. With this and the rules of differentiation, we can calculate the derivative of any polynomial p(x) = ∑ k=0npkxk as

The case n>0 follows from x−n = 1∕xn using the rules of differentiation.
With these rules under our belt, we can calculate the derivatives for some of the most famous activation functions.
The most classical one, the sigmoid function is defined by

Since it is an elementary function, it is differentiable everywhere. To calculate its derivative, we can use the quotient rule:

(12.4)
Now that we have the sigmoid and its derivative, let’s plot them together!
def sigmoid(x): return 1/(1 + np.exp(-x))
def sigmoid_prime(x): return sigmoid(x) - sigmoid(x)**2import numpy as npimport matplotlib.pyplot as plt
xs = np.linspace(-10, 10, 1000)
with plt.style.context("seaborn-v0_8"): plt.title("Sigmoid and its derivative") plt.plot(xs, [sigmoid(x) for x in xs], label="Sigmoid") plt.plot(xs, [sigmoid_prime(x) for x in xs], label="Sigmoid prime") plt.legend() plt.tight_layout() plt.show()
Figure 12.6: Sigmoid and its derivative
Another popular activation function is the ReLU, defined by

Let’s plot its graph first!
def relu(x): if x > 0: return x else: return 0xs = np.linspace(-5, 5, 1000)
with plt.style.context("seaborn-v0_8"): plt.title("Graph of the ReLU function") plt.plot(xs, [relu(x) for x in xs], label="ReLU") plt.legend() plt.tight_layout() plt.show()
Figure 12.7: Graph of the ReLU function
By looking at it, we can suspect that it is not differentiable at 0. Indeed, since

the limit of the differential quotient doesn’t exist.
However, besides 0, it is differentiable and

Even though ReLU is not differentiable at 0, this is not a problem in practice. When performing backpropagation, it is extremely unlikely that ReLU′(x) will receive 0 as its input. Even if this is the case, the derivative can be artificially extended to zero by defining it as 0.
12.2.3 Higher-order derivatives
Section titled “12.2.3 Higher-order derivatives”One last thing to do before we move on is to talk about higher-order derivatives. Because derivatives are functions, it is a completely natural idea to calculate the derivative of derivatives. As we will see when studying the basics of optimization in Chapter 14, the second derivatives contain quite a lot of essential information regarding minima and maxima.
The n-th derivative of f is denoted with f(n), where f(0) = f. There are a few rules regarding them that are worth keeping in mind. Although, we have to note that a derivative function is not always differentiable, as the example

shows. Now, about those rules regarding higher-order derivatives.
Theorem 83.
Let f : ℝ →ℝ and g : ℝ →ℝ be two arbitrary functions.
(a) (f + g)(n) = f(n) + g(n)
(b) (fg)(n) = ∑ k=0n
f(n−k)g(k)
Proof. (a) trivially follows from the linearity of differentiation.
Regarding (b), we are going to use proof by induction. For n = 1, the statement simply says that (fg)′ = f′g + fg′, as we have seen before.
Now, we assume that it is true for n and deduce the n + 1 case. For this, we have
![∑n ( ) (fg)(n+1) = ((fg)(n))′ = n (f(n−k)g(k))′ k=0 k ∑n ( ) = n [f(n−k+1)g(k) + f(n−k)g(k+1)] k=0 k ∑n ( ) ∑n ( ) = n f(n− k+1)g (k) + n f(n−k)g(k+1) k=0 k k=0 k ( ) n ( ) n− 1( ) ( ) = n f (n+1)g + [∑ n f(n+1−k)g(k)]+ [∑ n f(n−k)g(k+1)]+ n fg(n+1). 0 k k n k=1 k=0](https://static.packt-cdn.com/products/9781837027873/graphics/media/file1211.png)
First, we note that
=
= 1 and
=
= 1. Second, the recursive relation for binomial coefficients says that

With a simple reindexing, we have

so we can join the two sums together and obtain
![( ) n ( ) ( ) ( ) (fg)(n+1) = n + 1 f(n+1)g + ∑ [ n + n f(n+1−k)g(k)]+ n+ 1 fg(n+1) 0 k k − 1 n+ 1 ( ) k=1 n∑+1 n + 1 (n+1−k) (k) = k f g , k=0](https://static.packt-cdn.com/products/9781837027873/graphics/media/file1218.png)
which is what we had to show.
12.2.4 Extending the Function base class
Section titled “12.2.4 Extending the Function base class”Now that we have several tools under our belt to calculate derivatives, it’s time to think about implementations. Since we have our own Function base class (Section 9.2.3), a natural idea is to implement the derivative as a method. This is a simple solution that is in line with object-oriented principles as well, so we should go for it!
class Function: def __init__(self): pass
def __call__(self, *args, **kwargs): pass
# new interface element for # computing the derivative def prime(self): pass
def parameters(self): return dict()To see a concrete example, let’s revisit the sigmoid function, whose derivative is given by (12.4):

class Sigmoid(Function): def __call__(self, x): return 1/(1 + np.exp(-x))
def prime(self, x): return self(x) - self(x)**2Simple implementation, powerful functionality. Now that we have the derivatives covered, let’s move towards calculating the derivative of more complex functions!
12.2.5 The derivative of compositions
Section titled “12.2.5 The derivative of compositions”At this point, I have probably emphasized the importance of function compositions and the chain rule (Theorem 81) dozens of times. We have finally reached a point when we are ready to implement a simple neural network and compute its derivative! (Of course, our methods will be far more refined in the end, but still, this is a milestone.)
How can we calculate the derivative for a composition of n functions?
To see the pattern, let’s map out the first few cases. For n = 2, we have the good old chain rule

For n = 3, we have

Among the multitude of parentheses, we can notice a pattern. First, we should calculate the value of the composed function f3 ∘f2 ∘f1 at x while storing the intermediate results, then pass these to the appropriate derivatives and take the product of the result.
class Composition(Function): def __init__(self, *functions): self.functions = functions
def __call__(self, x): for f in self.functions: x = f(x)
return x
def prime(self, x): forward_pass = [x]
for f in self.functions: try: x = f(x) forward_pass.append(x) except ValueError as e: print(f"Error in function {f}: {e}") return np.nan
forward_pass.pop() # removing the last element, as we won’t need it
derivative = np.prod([f.prime(x) for f, x in zip(self.functions, forward_pass)])
return derivativeTo see if our implementation works, we should test it on a simple test case, say for

The derivative of the composition (f3 ∘f2 ∘f1)(x) = 24x should be constant 24.
class Linear(Function): def __init__(self, a, b): self.a = a self.b = b
def __call__(self, x): return self.a*x + self.b
def prime(self, x): return self.a
def parameters(self): return {"a": self.a, "b": self.b}f = Composition(Linear(2, 0), Linear(3, 0), Linear(4, 0))
xs = np.linspace(-10, 10, 1000)ys = [f.prime(x) for x in xs]
with plt.style.context("seaborn-v0_8"): plt.title("The derivative of f(x) = 24x) plt.plot(xs, ys, label="f prime") plt.legend() plt.tight_layout() plt.show()
Figure 12.8: The derivative of f(x) = 24x
Success! Even though we’re only dealing with single-variable functions for now, our Composition is going to be the skeleton for neural networks.
12.2.6 Numerical differentiation
Section titled “12.2.6 Numerical differentiation”So far, we have seen that in the cases when at least some formula is available for the function in question, we can apply the rules of differentiation (see Theorem 80) to obtain the derivative.
However, in practice, this is often not the case. For instance, think about the case when the function represents a recorded audio signal. If we can’t compute the derivative exactly, a natural idea is to approximate it, that is, provide an estimate that is sufficiently close to the real value.
For the sake of example, suppose that we don’t know the exact formula of our function to be differentiated, which is secretly the good old sine function.
def f(x): return np.sin(x)Recall that, by definition, the derivative is given by

Since we can’t take limits inside a computer (as computers can’t deal with infinity), the second best thing to do is to approximate this by

where h>0 is an arbitrarily small but fixed quantity. Δhf(x) is called the forward difference quotient. In theory, Δhf(x) ≈f′(x) holds when h is sufficiently small. Let’s see how they perform!
def delta(f, h, x): return (f(x + h) - f(x))/h
def f_prime(x): return np.cos(x)hs = [3.0, 1.0, 0.1]xs = np.linspace(-5, 5, 100)f_prime_ys = [f_prime(x) for x in xs]
with plt.style.context("seaborn-v0_8"): _colormap = plt.cm.hot_r plt.figure(figsize=(10, 5)) plt.title("Approximating the derivative with finite differences)
true_color = _colormap(0.99) # Get a fixed color for the true derivative for i, h in enumerate(hs): ys = [delta(f, h, x) for x in xs] blend_ratio = 1 - (len(hs) - i) / len(hs) # Progressively blend closer to the true color approx_color = _colormap(blend_ratio) plt.plot(xs, ys, label=f"h = {h}" color=approx_color)
plt.plot(xs, f_prime_ys, label="the true derivative" color=true_color, linewidth=2) plt.legend() plt.tight_layout() plt.show()
Figure 12.9: Approximating the derivative with finite differences
Although the Δhf(x) functions seem to be close f′(x), when h is small, there is a plethora of potential issues.
For one, Δhf(x) =
only approximates the derivative from the right of x, as h>0. To solve this, one might use the backward difference quotient

but that seem to have the same problems. The crux of the issue is that if f is differentiable at some x, then

but only if h is very small, and the “good enough” choice for h can vary from point to point.
A middle ground is provided by the so-called symmetric difference quotients, defined by

which is the average of forward and backward differences: δhf(x) =
. These three approximators are called finite differences. With their repeated application, we can approximate higher-order derivatives as well.
Even though symmetric differences are provably better, the approximation errors can still be significantly amplified in the long run.
All things considered, we are not going to use finite differences for machine learning in practice. However, as we’ll see, the gradient descent method is simply a forward difference approximation of a special differential equation.
Bookmark
12.3 Summary
Section titled “12.3 Summary”This chapter taught us about differentiation, the key component of optimizing functions. Yes, even functions with millions of variables.
Even though we focused on univariate functions (for now), we managed to build a deep understanding of differentiation. For instance, we’ve learned that the derivative

describes the slope of the tangent line drawn to the graph of f at x, which describes the velocity if f is the trajectory of a one-dimensional motion. From the perspective of physics, the derivative describes the rate of change.
However, from the perspective of mathematics, differentiation offers much more than the rate of change: we’ve seen that a differentiable function can be written in the form

around some x0 ∈ℝ. In other words, locally speaking, a differentiable function is a linear part plus a small error term. Unlike the limit-of-quotients definition, this will generalize for multiple variables without an issue. Moreover, we can apply a similar idea to obtain the so-called Taylor expansion

allowing us to approximate transcendental functions like log x,sinx,cosx,ex with polynomials.
In addition to the theory, we also learned about computing derivatives in practice.
This is done by either 1) decomposing complex functions into their building blocks, then calculating the derivative using the rules

or 2) approximating the derivative with finite differences like

where h>0 is a small constant. The former method is the foundation of backpropagation, while the latter lies at the heart of gradient descent. These are the methods that truly enable training huge neural networks.
Now that we understand differentiation, it’s time to talk about its counterpart: integration. Let’s go!
Bookmark
12.4 Problems
Section titled “12.4 Problems”Problem 1. Calculate the derivative of the tanh(x) function defined by

Problem 2. Define the function

Find the derivative of f(x). Is f′(x) differentiable everywhere?
Problem 3. Define the function

Show that
(a) f is differentiable at 0, and f′(0) = 0, (b) and f is nowhere else differentiable.
Problem 4. Calculate the derivatives of the following functions.
(a) f(x) = e−
(b) f(x) = x2esin x (c) f(x) = sin(cosx2) (d) f(x) = 
Problem 5. Find the Taylor expansion of the following functions around 0.
(a) f(x) = sinx (b) f(x) = cosx (c) f(x) = log x (d) f(x) = e{-x2}
Problem 6. Find the Taylor expansion of the function

around 0. Is the Taylor expansion of f equal to f?