Skip to content
ImagineWorld

Derivatives and Gradients

node_idnode-book_mathematics-of-machine-learning_chapter-16-derivatives-and-gradients
node_typechapter_node
schema_profileworld.chapter_node.v1
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
statusactive
chapter_num16
chapter_titleDerivatives and Gradients
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Now that we understand why multivariate functions and high-dimensional spaces are more complex than the single-variable case we studied earlier, it’s time to see how to do things in the general case.

To recap quickly, our goal in machine learning is to optimize functions with millions of variables. For instance, think about a neural network N(x,w) trained for binary classification, where

  • x ∈ℝn is the input data,
  • w ∈ℝm is the vector compressing all of the weight parameters,
  • and N(x,w) ∈ [0,1] is the prediction, representing the probability of belonging to the positive class.

In the case of, say, binary cross-entropy loss, we have the loss function

 d L(w ) = − ∑ y log N (x ,w ), i i k=1

where xi is the i-th data point with ground truth yi ∈{0,1}. See, I told you that we have to write much more in multivariable calculus. (We’ll talk about binary cross-entropy loss in Chapter 20.)

Training the neural network is the same as finding a global minimum of L(w), if it exists. We have already seen how we can do optimization in a single variable:

  • figure out the direction of increase by calculating the derivative,
  • take a small step,
  • then iterate.

For this to work in multiple variables, we need to generalize the concept of the derivative. We can quickly discover the issue: since division with a vector is not defined, the difference quotient

f(x)−-f-(y-) x− y

makes no sense when f : ℝn →ℝ is a function of n variables and x,y ∈ℝn are n-dimensional vectors.

How can we make sense of it, then? This is what we’ll learn in the following chapter.

day-mode Bookmark

Let’s take a look at multivariable functions more closely! For the sake of simplicity, let f : ℝ2 →ℝ be our function of two variables. To emphasize the dependence on the individual variables, we often write

f(x1,x2), x1,x2 ∈ ℝ.

Here’s the trick: by fixing one of the variables, we obtain the two single-variable functions! That is, if x1 ∈ℝ2 is fixed, we have x→f(x1,x), and if x2 ∈ℝ2 is fixed, we have x→f(x,x2), both of which are well-defined univariate functions. Think about this as slicing the function graph with a plane parallel to the x−z or the y −z axes, as illustrated by Figure 16.1. The part cut out by the plane is a single-variable function.

PIC

Figure 16.1: Slicing the surface with the x −z plane

We can define the derivative of these functions by the limit of difference quotients. These are called the partial derivatives:

∂f-- f(x,x2)-−-f(x1,x2) ∂x1 (x1,x2 ) = xli→mx1 x − x1 , ∂f f(x ,x) − f(x ,x ) ----(x1,x2 ) = lim ---1---------1--2-. ∂x2 x→x2 x − x2

(Keep in mind that x1 signifies the variable in ∂f- ∂x1, but an actual scalar value in the argument of ∂f- ∂x1(x1,x2). This can be quite confusing, but you’ll soon learn to make sense of it.)

The definition is similar for general multivariable functions; we just have to write much more. There, the partial derivative of f : ℝn →ℝ at the point x = (x1,…,xn) with respect to the i-th variable is defined by

L(U,V ) = {f : U → V | f is linear}

(16.1)

One of the biggest challenges in multivariable calculus is to manage the ever-growing notational complexity. Just take a look at the difference quotient above:

f(x1,...,x,...,xn)-−-f(x1,...,xi,...,xn). x − xi

This is not the prettiest to look at, and this kind of notational complexity can pile up fast. Fortunately, linear algebra comes to the rescue! Not only can we compact the variables into the vector x = (x1,…,xn), we can use the standard basis

ei = (0,...,0, 1 ,0,...,0) ◟◝◜◞ i- th component

to write the difference quotients as

f(x+-hei)-−-f(x), h ∈ ℝ. h

Thus, (19.1) can be compacted. With this newly found form, we are ready to make a concise and formal definition for partial derivatives.

Definition 66. (Partial derivatives)

Let f : ℝn →ℝ be a function of n variables. The partial derivative of f at the point x = (x1,…,xn) with respect to the i-th variable is defined by

-∂f-(x ) = lim f(x-+-hei)−-f-(x-). ∂xi h→0 h

If the above limit exists, we say that f is partially differentiable with respect to the i-th variable xi.

The partial derivative is again a vector-scalar function. Because of this, it is often written as -∂- ∂xif, reflecting on the fact that the symbol ∂-- ∂xi can be thought of as a function that maps functions to functions. I know, this is a bit abstract, but you’ll get used to it quickly.

As usual, there are several alternative notations for the partial derivatives. Among others, the symbols

  • fxi(x),
  • Dif(x),
  • ∂if(x)

denote the i-th partial derivative of f at x. For simplicity, we’ll use the old-school -∂f ∂xi(x).

It’s best to start with a few examples to illustrate the concept of partial derivatives.

Example 1. Let

f(x1,x2) = x21 + x22.

To calculate, say, ∂f∕∂x1, we fix the second variable and treat x2 as a constant. Formally, we obtain the single-variable function

 1 2 2 f (x) := x + x2, x2 ∈ ℝ,

whose derivative gives the first partial derivative:

 1 ∂f-(x ,x ) = df-(x ) = 2x . ∂x1 1 2 dx 1 1

Similarly, we get that

∂f ∂x-(x1,x2) = 2x2. 2

Once you are comfortable with the mental gymnastics of fixing variables, you’ll be able to perform partial differentiation without writing out all the intermediate steps.

Example 2. Let’s see a more complicated example. Define

f(x1,x2) = sin(x21 + x2).

By fixing x2, we obtain a composite function. Thus the chain rule is used to calculate the first partial derivative:

 ∂f ----(x1,x2 ) = 2x1 cos(x21 + x2). ∂x1

Similarly, we obtain that

∂f-- 2 ∂x2(x1,x2) = cos(x1 + x2).

(I highly advise you to carry out the above calculations step by step as an exercise, even if you understand all the intermediate steps.)

Example 3. Finally, let’s see a function that is partially differentiable in one variable but not in the other. Define the function

 ( |{ − 1 if x2 <0, f(x1,x2) = |( 1 otherwise.

As f(x1,x2) does not depend on x1, we can see that by fixing x2, the resulting function is constant. Thus,

-∂f-(x ,x ) = 0 ∂x1 1 2

holds everywhere. However, in x2, there is a discontinuity at 0; thus, ∂f- ∂x2 is undefined there.

If a function is partially differentiable in every variable, we can compact the derivatives together in a single vector to form the gradient.

Definition 67. (The gradient)

Let f : ℝn → ℝ be a function that is partially differentiable in all of its variables. Then, its gradient is defined by the (column) vector

 ⌊ ⌋ ∂∂x1f (x ) ||-∂-f (x )|| ∇f(x ) := ||∂x2 . || ∈ ℝn ×1. |⌈ .. |⌉ -∂- ∂xnf (x )

A few remarks are in order. First, the symbol ∇ is called nabla, a symbol that was conceived to denote gradients.

Second, the gradient can be thought of as a vector-vector function. To see that, consider the already familiar function f(x1,x2) = x12 + x22. The gradient of f is

 ⌊ ⌋ 2x1 ∇f (x1,x2) = ⌈ ⌉ , 2x2

or

∇f (x ) = 2x

in vectorized form. We can visualize this by drawing the vector ∇f(x1,x2) at each point (x1,x2) ∈ℝ2.

PIC

Figure 16.2: The vector field given by the gradient of x12 + x22

Thus, you can think about ∇f as a vector-vector function ∇f : ℝn →ℝn. The gradient at a given point x is obtained by evaluating this function, yielding (∇f)(x).

For clarity, the parentheses are omitted, arriving at the all familiar notation ∇f(x).

The partial derivatives of a vector-scalar function f : ℝn →ℝ are vector-scalar functions themselves. Thus, we can perform partial differentiation one more time!

If they exist, the second order partial derivatives are defined by

L(U,V ) = {f : U → V | f is linear}

(16.2)

where a ∈ℝn is an arbitrary vector. (When the second partial differentiation takes place with respect to the same variable, (16.2) is abbreviated by ∂2f ∂x2i(a).)

The definition begs the question: is the order of differentiation interchangeable? That is, does

 2 2 --∂-f--(a) = -∂-f--(a) ∂xi∂xj ∂xj∂xi

hold? The answer is quite surprising: the order is interchangeable under some mild assumptions, but not in the general case. There is a famous theorem about it which we won’t prove, but it’s essential to know.

Theorem 98.

Let f : ℝn → ℝ be an arbitrary vector-scalar function and let a ∈ ℝn. If there is a small ball B(𝜀,a) ⊆ℝn centered at a such that f has continuous second-order partial derivatives at all points of B(𝜀,a), then

--∂2f-- -∂2f--- ∂xi ∂xj(a) = ∂xj∂xi(a)

holds for all i = 1,…,n.

Theorem 98 is known as either Schwarz’s theorem, Clairaut’s theorem, or Young’s theorem.

Partial derivatives seem to generalize the notion of differentiability for multivariable functions. However, something is missing. Let’s revisit the single-variable case for a moment.

Recall that according to Theorem 77, the differentiability of a single-variable function f : ℝ →ℝ at a given point a is equivalent to a local approximation of f by the linear function

l(x ) = f(a) + f′(a)(x − a).

If x is close to a, l(x) is also close to f(x). Moreover, this is the best linear approximation we can do around a. In a single variable, this is equivalent to differentiation.

This gives us an idea: even though difference quotients like f(x)−f(y) x−y do not exist in multiple variables, the best local approximation with a multivariable linear function does!

Thus, the notion of total differentiability is born.

Definition 68. (Total differentiability)

Let f : ℝn →ℝ be a function of n variables. We say that f is totally differentiable (or sometimes just differentiable for short) at a ∈ℝn if there exists a row vector Df(a) ∈ℝ1×n such that

f(x) = f(a) + Df(a)(x − a) + o(∥x − a∥) (16.3)

holds for all x ∈B(𝜀,a), where 𝜀>0 and B(𝜀,a) is defined by

B(𝜀,a) = {x ∈ ℝn : ∥x − a∥} <𝜀.

(In other words, B(𝜀,a) is a ball of radius 𝜀>0 around a.) When exists, the vector Df(a) is called the total derivative of f at a.

Recall that when it is not stated explicitly, we use column vectors, because we want to write our linear transformations in the form Ax, where A ∈ℝm×n and x ∈ℝn×1. Thus, the “dimensionology” of the formula

 f(x) = f(a) + Df (a )(x− a )+o(∥x − a∥) ∈ ℝ1×1 ◟◝1◜×◞1 ◟◝◜1◞×1 ◟-◝1◜× ◞n ◟-◝◜n×◞1 ∈ℝ ∈ℝ ∈ℝ ∈ℝ

works out. (Don’t be fooled, ℝ1×1 is a scalar.)

Let’s unravel the notion of total differentiability. The form (16.3) implies that a totally differentiable function f equals to the linear part f(a) + Df(a)(x −a) plus a small error.

The surface given by the linear part is called the tangent plane. We can visualize it for functions of two variables.

PIC

Figure 16.3: The tangent plane

Unsurprisingly, the partial and total derivatives share an intimate connection.

Theorem 99. (Total derivative and the partial derivatives)

Let f : ℝn →ℝ be a function that is totally differentiable at a ∈ℝn. Then, all of its partial derivatives exist at a and

f(x) = f(a) + ∇f(a)T (x − a) + o(∥x − a∥) (16.4)

holds for all a in some B(𝜀,a), 𝜀>0. (That is, Df(a) = ∇f(a)T .)

In other words, the equation (16.4) gives that the coefficients of the best linear approximation are equal to the partial derivatives.

Proof. Because f is totally differentiable at a, the definition gives that f can be written in the form

f(x) = f(a)+ Df (a)(x− a)+ o(∥x − a∥),

where Df(a) = (d1,…,dn) is the vector that describes the coefficients of the linear part.

Our goal is to show that

 f-(a-+-hei)−-f-(a) hli→m0 h = di,

where ei is the unit (column) vector whose i-th component is 1, while the others are 0.

Let’s do a quick calculation! Based on what we know, we have

f(a-+-hei)−-f(a)-= Df-(a)hei +-o(∥hei∥) h h = Df (a)ei + o(1) = di + o(1),

thus confirming that limh→0f(a+hei)−-f(a)- h = di, which is what we had to show.

What’s all the hassle with total differentiation, then? Theorem 99 tells us that total differentiability is a stronger condition than partial differentiability.

Surprisingly, the other direction is not true: the existence of partial derivatives does not imply total differentiability, as the example

 ( |{ f (x, y) = 1 if x = 0 or y = 0, |( 0 otherwise

illustrates. This function has all its partial derivatives at 0, yet the total derivative does not exist. (You can convince yourself by either drawing a figure, or noting that the function 1 −dT x can never be o(∥x∥), regardless of the choice of d.)

Remark 11. (The total derivative as an operator)

Just like for single-variable functions, the total derivative of f : ℝn →ℝ is a function Df : ℝn →ℝn.

At the highest level of abstraction, we can think about the total derivative as an operator that maps a vector-scalar function to a vector-vector function:

 n ℝ n ℝn D : (ℝ ) → (ℝ ) , D : f ↦→ D , f

where AB denotes the set of all functions mapping A to B.

You are not required to understand this at all, but trust me, the more abstract your thinking is, the more powerful you’ll be.

So far, we have talked about two kinds of derivatives: partial derivatives that describe the rate of change along a fixed axis, and total derivatives that give the best linear approximation of the function at a given point.

Partial derivatives are only concerned with a few particular directions. However, this is not the end of the story in multiple variables. With the standard orthonormal basis vectors ei, the partial derivatives are defined by

L(U,V ) = {f : U → V | f is linear}

(16.5)

As we saw earlier, these describe the rate of change along the dimensions. However, the standard orthonormal vectors are just a few special directions.

What about an arbitrary direction v? Can we define the derivative along these? Sure! There is nothing stopping us from replacing ei with v in (16.5). Thus, directional derivatives are born.

Definition 69. (Directional derivatives)

Let f : ℝn →ℝ be a function of n variables and let v ∈ℝn be an arbitrary vector. The directional derivative of f along v is defined by the limit

∂f f(a + hv) − f(a) ∂v-:= lhim→0 -------h--------.

Good news: the directional derivatives can be described in terms of the gradient!

Theorem 100.

Let f : ℝn →ℝ be a function of n variables. If f is totally differentiable at a ∈ℝn, then its directional derivatives exist in all directions, and

∂f(a) = ∇f (a)Tv. ∂v

Proof. Because of the total differentiability, Theorem 103 gives that

f (x ) = f (a )+ ∇f (a)T(x − a)+ o(∥x − a∥)

around a. Thus,

f(a+ hv )− f (a ) h∇f (a)Tv + o(h) ----------------= ---------------- h h = ∇f (a)Tv + o(1),

giving that

∂f-(a) = lim f-(a-+-hv-)−-f(a) ∂v h→0 h = lim ∇f (a)Tv + o(1) h→0 = ∇f (a)T v,

as we needed to show.

In other words, Theorem 100 gives that no matter the direction v, the directional derivative can be written in terms of the gradient and v. If you think about this for a minute, this is quite amazing: the rates of change along n special directions determine the rate of change in any other direction.

In one variable, we have learned that if the derivative of f is positive at some a, then f is increasing around a. (If the derivative is negative, f is decreasing.) If we think about the derivative f′(a) as a one-dimensional vector, then the derivative points towards the direction of increase.

Is this true in higher dimensions? Yes, and this is what makes gradient descent work.

Theorem 101. (The gradient determines the direction of the increase)

Let f : ℝn →ℝ be a function of n variables, and suppose that f is totally differentiable at a ∈ℝn.

Then

L(U,V ) = {f : U → V | f is linear}

(16.6)

I know, (16.6) is pretty overloaded, so let’s unpack it. First, let’s start with the mysterious argmax. For a given function f,

argmaxx ∈Sf(x)

denotes the values that maximize f on the set S. As the maximum may not be unique, argmax can yield a set. (The definition of argmin is the same, but with minimum instead of maximum.)

Thus, in English, (16.6) states that the unit direction that maximizes the directional derivative at a ∈ℝn is the normalized gradient. Now we are ready to see the proof!

Proof. Do you remember the Cauchy-Schwarz inequality (Theorem 8)? It was a long time ago, so let’s recall! In the vector space ℝn, the Cauchy-Schwarz inequality tells us that for any x,y ∈ℝn,

xT y ≤ ∥x∥∥y∥.

Now, as Theorem 100 implies, the directional derivatives can be written as

∂f- T ∂v(a) = ∇f (a) v.

Combined with the Cauchy-Schwarz inequality, we get that

∂f ---(a) = ∇f (a)T v ∂v ≤ ∥ ∇f (a )∥∥v ∥.

By restricting the directions to unit vectors,

L(U,V ) = {f : U → V | f is linear}

(16.7)

follows. Thus, the directional derivatives must be less than or equal to the gradient’s norm. (At least, along a direction vector with unit length.)

However, by letting v0 = ∇f(a)∕∥∇f(a)∥, we obtain that

∂f--(a ) = ∇f (a)Tv0 ∂v0 ∇f (a)T∇f (a) = ---∥∇f-(a)∥-- 2 = ∥∇f-(a)∥- ∥∇f (a)∥ = ∥∇f (a)∥.

Thus, with the choice v0 = -∇f(a)-, equality can be attained in (16.7). This means that maximizes the directional derivative at a, which is what we had to prove.

With that, we have the basics of differentiation in multiple variables under our belt. To sum up, we have learned that the difference quotient definition of the derivative does not generalize directly for multiple variables, but we can fix all but one variables to make the difference quotient work, thus obtaining partial derivatives.

On the other hand, the linear approximation definition works in multiple dimensions, but instead of

 ′ f(a)+ f (a)(x− a), f : ℝ → ℝ, x,a ∈ ℝ,

like we had in one variable, we obtain

f (a )+ ∇f (a)T(x− a), f : ℝn → ℝ, x,a ∈ ℝn,

where the analogue of the derivative is the gradient vector ∇f(a) ∈ℝn.

Even when we were studying differentiation in one variable for the first time, I told you that the local linear approximation definition would be useful someday. That time is now, and we are reaping the benefits. Soon, we’ll see gradient descent in its full glory.

day-mode Bookmark

16.2 Derivatives of vector-valued functions

Section titled “16.2 Derivatives of vector-valued functions”

In a single variable, defining higher-order derivatives is easy. We simply have to keep repeating differentiation:

 ′′ ′ ′ f (x) = (f (x)), f′′′(x) = (f′′(x))′,

and so on. However, this is not that straightforward with multivariable functions.

So far, we have only talked about gradients, the generalization of the derivative for vector-scalar functions.

As ∇f(a) is a column vector, the gradient is a vector-vector function ∇ : ℝn →ℝn. We only know how to compute the derivative of vector-scalar functions. It’s time to change that!

Curves, often describing the solutions of dynamical systems, are one of the most important objects in mathematics. We don’t explicitly use them in machine learning, but they are underneath algorithms such as gradient descent. (Where we traverse a discretized curve leading to a local minimum.)

Formally, a curve – that is, a scalar-vector function – is given by a function

 ⌊γ (t)⌋ | 1 | n ||γ2(t)|| n(×1) γ : ℝ → ℝ , γ (t) = || .. || ∈ ℝ , ⌈ . ⌉ γn(t)

where the γi : ℝ →ℝ functions are good old single-variable scalar-scalar functions. As the independent variable often represents time, it is customary to denote it with t.

We can differentiate γ componentwise:

 ⌊ ⌋ γ′1(t) ||γ′(t)|| γ′(t) := || 2. || ∈ ℝn(×1). |⌈ .. |⌉ ′ γn(t)

If we indeed imagine γ(t) as a trajectory in space, γ′(t) is the tangent vector to γ at t. Since the differentiation is componentwise, Theorem 77 implies that if γ is differentiable at some a ∈ℝ,

γ(t) = γ(a) + γ′(t)T (t − a) + o(|t − a|) (16.8)

there. The equation (16.8) is a true vectorized formula: some components are vectors, and some are scalars. Yet, this is simple and makes perfect sense to us. Hiding the complexities of vectors and matrices is the true power of linear algebra.

It is easy to see that for any two curves γ,η : ℝ →ℝn, differentiation is additive, as (γ + η)′ = γ′ + η′. What happens when we compose a scalar-vector function with a vector-scalar one?

This situation is commonplace in machine learning. If, say, L : ℝn →ℝ describes the loss function and γ : ℝ →ℝn is our trajectory in the parameter space ℝn, the composite function f(γ(t)) describes the model loss at time t. Thus, to compute (f ∘γ)′, we have to generalize the chain rule.

Theorem 102. (The chain rule for composing scalar-vector and vector-scalar functions)

Let γ : ℝ →ℝn and f : ℝn →ℝ be arbitrary functions. If γ is differentiable at some a ∈ℝ and f is differentiable at γ(a), then f ∘γ : ℝ →ℝ is also differentiable at a and

(f ∘γ)′(a) = ∇f (γ(a))Tγ′(a )

there.

Proof. As f is differentiable at γ(a), Theorem 99 gives

f(γ(t)) = f (γ (a )) + ∇f (γ(a))T (γ (t)− γ (a )) + o(∥γ(t) − γ(a)∥).

Thus,

(f ∘γ )′(a) = lim f(γ(t))−-f(γ(a)) t→a t− a T γ(t)−-γ(a)- = ∇f (γ(a)) lit→ma [ t− a + o(1)] T ′ = ∇f (γ(a)) γ (a),

which is what we had to prove.

Now, our task is to extend the derivative for vector-vector functions, so let f : ℝn →ℝm be one. By writing out the output of f explicitly, we can decompose it into multiple components:

 ⌊ ⌋ | f1(x )| f(x) = |⌈ ... |⌉ ∈ ℝm (×1) fm(x )

where fi : ℝn →ℝ are vector-scalar functions.

The natural idea is to compute the partial derivatives for fi, compacting them into a matrix. And so we shall!

Definition 70. (The Jacobian matrix)

Let f : ℝn →ℝm be an arbitrary vector-vector function, and suppose that

f(x) = (f (x ),...,f (x)), 1 m

where all fi : ℝn → ℝ are (partially) differentiable at some a ∈ ℝn. The matrix

 ⌊ ∂f1 ∂f1 ∂f1 ⌋ | ∂x1(a) ∂x2(a) ... ∂xn(a)| | ∂f2(a) ∂f2(a) ... ∂f2(a)| Jf(a) := || ∂x1. ∂x2. . ∂xn. || ∈ ℝm ×n |⌈ .. .. .. .. |⌉ ∂fm- ∂fm- ∂fm-- ∂x1 (a) ∂x2 (a) ... ∂xn(a)

is called the Jacobian of f at a.

In other words, the rows of the Jacobian are the gradients of fi:

 ⌊ T ⌋ | ∇f1(a) | || ∇f2(a)T || || . || Jf(a) = | .. |. ||∇f (a )T || ⌈ m ⌉

I have good news: the best local linear approximation of f around a is given by

f(x ) = f(a)+ J (a)(x − a)+ o(∥x − a∥), f

if the best local linear approximation exists. Thus, the Jacobian is a proper generalization of the gradient.

We can use the Jacobian to generalize the notion of second-order derivatives for vector-scalar functions: by computing the Jacobian of the gradient, we obtain a special matrix, the analogue of the second derivative.

Definition 71. (The Hessian matrix)

Let f : ℝn →ℝ be an arbitrary vector-scalar function, and suppose that all of its second-order partial derivatives exist at a ∈ℝn.

The matrix

 ⌊ ⌋ ∂2f2(a) -∂2f-(a) ... -∂2f-(a) || ∂x21 ∂x1∂2x2 ∂x1∂2xn || || ∂∂x2f∂x1(a ) ∂∂xf2(a) ... ∂x∂2∂fxn(a)|| n×n Hf (a) := | .. 2.. .. .. | ∈ ℝ |⌈ . . . . |⌉ -∂2f--(a ) -∂2f-(a) ... ∂2f2(a) ∂xn∂x1 ∂xn∂x2 ∂xn

is called the Hessian of f at a.

In other words,

Hf (a) = J ∇f(a)T

holds by definition. Moreover, if f behaves nicely (for instance, all second-order partial derivatives exist and are continuous), Theorem 98 implies that the Hessian is symmetric; that is, Hf(a) = Hf(a)T .

16.2.3 The total derivative for vector-vector functions

Section titled “16.2.3 The total derivative for vector-vector functions”

One last generalization. (I promise.) Recall that the existence of the gradient (that is, partial differentiability) doesn’t imply total differentiability for vector-scalar functions, as the example

 ( |{ f (x, y) = 1 if x = 0 or y = 0, |( 0 otherwise

shows at zero.

This is true for vector-vector functions as well, as the Jacobian is the generalization of the gradient, not the total derivative.

It is best to rip the band-aid off quickly and define the total derivative for vector-vector functions. The definition will be a bit abstract, but trust me, the investment will pay off when talking about the chain rule. (Which is the foundation of backpropagation, the algorithm that makes gradient descent computationally feasible.)

Definition 72. (Total differentiability of vector-vector functions)

Let f : ℝn →ℝm be an arbitrary vector-vector function. We say that f is totally differentiable (or sometimes just differentiable in short) at a ∈ℝn if there exists a matrixDf(a) ∈ℝm×n such that

f(x) = f(a) + Df(a)(x − a) + o(∥x − a∥) (16.9)

holds for all x ∈B(𝜀,a), where 𝜀>0 and B(𝜀,a) is defined by

B(𝜀,a) = {x ∈ ℝn : ∥x − a∥ <𝜀}.

(In other words, B(𝜀,a) is a ball of radius 𝜀>0 around a.) When exists, the matrix Df(a) is called the total derivative of f at a.

Notice that Definition 72 is almost verbatim to Definition 68, except that the “derivative” is a matrix this time.

You are probably not surprised to hear that its relation with the Jacobian is the same as the gradient and the total derivative in the vector-scalar case.

Theorem 103. (Total derivative and the partial derivatives)

Let f : ℝn →ℝm be a function that is totally differentiable at a ∈ℝn. Then, all of its partial derivatives exist at a and

Df (a) = Jf(a ).

The proof is almost identical to the one of Theorem 99, with more complex notations. I strongly recommend you work it out line by line, as this kind of mental gymnastics helps significantly to get used to matrices in practice.

Componentwise, the total derivative can be written as

 ⌊ ∂f1 ∂f1 ∂f1- ⌋ | ∂x1(a) ∂x2(a) ... ∂xn (a )| | ∂f2(a) ∂f2(a) ... ∂f2-(a )| Df(a) = || ∂x1. ∂x2. . ∂xn. ||∈ ℝm ×n. |⌈ .. .. .. .. |⌉ ∂fm-(a) ∂fm(a) ... ∂fm-(a) ∂x1 ∂x2 ∂xn

By introducing the notation

 ⌊ ⌋ ∂∂fx1(a ) || ∂fi2 || || ∂xi(a )|| -∂f(a) = | ... | ∈ ℝm ×1, ∂xi || ∂f || |⌈ ∂mxi (a)|⌉

the total derivative Df(a) can be written in the block-forms

 [-∂f -∂f -∂f ] Df (a ) = ∂x1(a) ∂x2(a) ... ∂xn(a)

and

 ⌊ ⌋ ∇f1 (a )T || T || D (a ) = || ∇f2 (a ) || . f | ... | ⌈ ⌉ ∇fm (a)T

16.2.4 Derivatives and function operations

Section titled “16.2.4 Derivatives and function operations”

We have generalized the notion of derivatives as far as possible for us. Now it’s time to study their relations with the two essential function operations: addition and composition. (As there is no vector multiplication in higher dimensional spaces, the product and ratio of vector-vector functions are undefined.)

Let’s start with the simpler one: addition.

Theorem 104. (Linearity of the total derivative)

Let f,g : ℝn →ℝm be two vector-vector functions that are differentiable at some a ∈ℝn, and let α,β ∈ℝ be two arbitrary scalars.

Then, αf + βg is also differentiable at a and

D αf+ βg(a) = αDf (a)+ βDg (a )

there.

Proof. Because of the total differentiability, (16.9) implies that

αf(x) + βg(x) = αf(a) + βg(a) + (αD (a )+ βD (a))(x − a) f g + o(∥x− a∥),

which implies

Dαf+βg(a) = αDf (a)+ βDg (a).

This is what we had to show.

Linearity is always nice, but what we need is the ultimate generalization of the chain rule. We previously saw the special case of composing a scalar-vector and a vector-vector function (see Theorem 102), but we need to go one step further.

The multivariable chain rule is extremely important in machine learning. A neural network is a composite function, with layers acting as components. During gradient descent, we use the chain rule to calculate the derivative of this composition.

Theorem 105. (Multivariable chain rule)

Let f : ℝm →ℝl and g : ℝn →ℝm be two vector-vector functions. If g is totally differentiable at a ∈ℝn and f is totally differentiable at g(a), then f ∘g is also totally differentiable at a and

Df∘g(a) = Df(g(a)) Dg(a) (16.10)

holds.

To our advantage, the derivative of a composed function (16.10) is given by the product of two matrices. Since matrix multiplication can be done lightning fast, this is good news.

We will see two proofs for Theorem 105. One is done with a faster-than-light engine, while the other shows much more by reducing the general case to Theorem 102. Both provide a ton of insight. Let’s start with the heavy machinery.

Proof. (First method.)

As f is totally differentiable at g(a), the equation (16.9) implies

f(g(x)) = f(g(a))+ D (g (a ))(g(x) − g(a))+ o(∥g(x)− g(a)∥). f

In turn, again because of the total differentiability of g at a, we have

g(x)− g(a) = Dg (a)(x− a)+ o(∥x − a∥).

Thus, we can continue our calculation by

f(g (x )) = f(g(a))+ Df (g(a))(g(x )− g(a))+ o(∥g(x) − g(a)∥) = f(g(a))+ D (g(a))D (a)(x − a) f g + Df (g (a ))[o(∥x− a ∥)+ o(∥g(x)− g(a)∥)], ◟-----------------◝◜----------------◞ =o (∥x−a∥)

showing that f ∘g is totally differentiable at a with total derivative

Df∘g(a) = Df(g (a ))Dg (a),

which is what we needed to show.

Now, about that second proof.

Proof. (Second method.)

Let’s unpack Df∘g(a) a bit. Writing out the components of f ∘g, we have

 ⌊ ⌋ | (f ∘g )1(x )| | (f ∘g )2(x )| (f ∘ g)(x) = || . || ∈ ℝl, x ∈ ℝn. |⌈ .. |⌉ (f ∘ g)(x) l

By definition, the i-th row and j-th column of Df∘g(a) is

 ∂ (f ∘g )i (Df∘g(a))i,j = ---∂x---(a). j

If you look at it long enough, you’ll realize that ∂(f∘g) --∂xji(a) is the derivative of a single variable function. Indeed, the function to be differentiated is the composition of the curve

γ : t ↦→ g(a1,...,aj−1,t,aj+1,...,an)

and the vector-scalar function fi : ℝm → ℝ. Thus, the chain rule for the composition of scalar-vector and vector-scalar functions (given by Theorem 102) can be applied:

∂(f ∘g)i T ∂ --∂x----(a) = ∇fi(g(a)) ∂x--g(a), j j

where ∂∂xjg(a) is the componentwise derivative

 ⌊ ⌋ ∂g1(a) || ∂∂g2xj(a)|| -∂-- || -∂xj--|| ∂xj g(a) = | .. | . |⌈ . |⌉ ∂gm∂(xa) j

To sum up, we have

This is the element in the i-th row and j-th column of the matrix product Df(g(a))Dg(a), hence

which is what we had to show.

With the concept of total derivatives for vector-vector functions and the general chain rule under our belt, we are ready to actually do things with multivariable functions. Thus, our next stop lays the foundations of optimization.

day-mode Bookmark

You know by now: half the success in mathematics is picking the right representations and notations. Although multivariable calculus can seem insanely complex, it’s a cakewalk if we have a good understanding of linear algebra. This is why we started our entire journey with vectors and matrices! Going from f(x1,…,xn) to f(x) is a big deal.

In this chapter, we have learned that differentiation in multiple dimensions is slightly more complicated than in the single-variable case. First, we have the partial derivatives defined by

∂f-(a) = lim f(a+-hei)-−-f(a), a ∈ ℝn, ∂xi h→0 h

where ei is the vector whose i-th component is one, while the others are zero. We can think about ∂∂fx- i as the derivative of the single-variable function obtained by fixing all but the i-th variable of f. Together, the partial derivatives form the gradient:

 ⌊-∂- ⌋ |∂x1f (a )| ||∂∂x2f (a )|| n×1 ∇f (a ) := || .. || ∈ ℝ . ⌈ . ⌉ -∂-f (a ) ∂xn

However, the partial derivatives are not exactly the perfect analogue of the univariate derivatives. There, we learned that the derivative is the best local linear approximation, and this is the version that can be generalized to multiple variables. Thus, we say that f is totally differentiable at a ∈ℝn if it can be written in the form

f(x) = f(a) + ∇f (a )T (x − a) + o(∥x− a ∥).

In machine learning, one of the most essential tools is the multivariable chain rule

Df∘g(a) = Df(g (a))Dg (a),

which is used to compute the derivatives in practice. Without the chain rule, we wouldn’t have any effective method to compute the gradient. In turn, as the name suggests, the gradient is the cornerstone of gradient descent. We already understand the single-variable version, so it’s time to dive deep into the general one. See you in the next chapter!

day-mode Bookmark

Problem 1. Compute the partial derivatives and the Hessian matrix of the following functions.

(a) f(x1,x2) = x13x22 + 2x1x2 + x23 (b) f(x1,x2) = ex12−x2 + sin(x1x2) (c) f(x1,x2) = ln(x12 + x22) + x1ex2 (d) f(x1,x2) = cos(x1x2) + x12 sin(x2) (e) f(x1,x2) = f(x1,x2) = x2+x2 x11−x22

Problem 2. Compute the Jacobian matrix of the following functions.

(a)

 ⌊ ⌋ x21x2 + ex2 f(x1,x2) = ⌈ x2⌉ sin(x1x2)+ x1e

(b)

 ⌊ ⌋ ln(x21 + x22) + x1x2 f(x1,x2) = ⌈ 2 x1 ⌉ cos(x1)+ x 2e

(c)

 ⌊ ⌋ x3 − x2 f(x1,x2) = ⌈ 1 2 ⌉ ex1x2 + x1 cos(x2 )

(d)

 ⌊ ⌋ ⌈ tan (x1x2 )+ x32 ⌉ f(x1,x2 ) = ∘x2-+-x2-+ sin(x ) 1 2 1

(e)

 ⌊ ⌋ x ex2 − ln(1 + x2) f(x1,x2) = ⌈ 1 1 ⌉ x22cos(x1)+ x1x2

Problem 3. Let f(x1,x2) = x1∘ |x2|-. Show that f is partially differentiable but not totally differentiable at (0,0).