Chapter 2Differentiation
In this chapter, we review key differentiation concepts. In particular, we emphasize the fundamental role played by linear maps.
2.1 Univariate functions
2.1.1 Derivatives
To study functions, we need to capture their infinitesimal variations around points as defined by the notion of limit.
Definition 2.1 (Limit)
We say that is the limit of as approaches , denoted if, for any , there exists such that for any satisfying , we have .
We can also write as . Limits are preserved under additions and multiplications. Namely, if and , then denoting for any and , we have by definition of the limit, and . The preservation of the limit under addition and multiplication by a scalar is generally referred to as the linearity of the limit, a property that many definitions in the sequel inherit.
With the notion of limit, we can already delineate a class of “well-behaved” functions: functions whose limit at any point equals the value of the function at that point. Functions satisfying this property are called continuous.
Definition 2.2 (Continuous function)
A function is continuous at a point if A function is said to be continuous if it is continuous at all points in its domain.
Although the notion of continuity appears to be a benign assumption, several functions commonly used in machine learning, such as the Heaviside step function (displayed in the left panel of Fig. 2.2), are not continuous and require special treatment.
Remark 2.1 (Little notation)
In the following, we will make use of Landau’s little notation. We write if That is, the function dominates in the limit . For example, is continuous at if and only if
We now explain derivatives. Consider a function . As illustrated in Fig. 2.1, its value on an interval can be approximated by the secant between its values and , a linear function with slope . In the limit of an infinitesimal variation around , the secant converges to the tangent of at and the resulting slope defines the derivative of at . The definition below formalizes this intuition.
Definition 2.3 (Derivative)
The derivative of at is defined as provided that the limit exists. If is well-defined at a particular , we say that the function is differentiable at . If is differentiable at any , we say that it is differentiable everywhere or differentiable for short.

Figure 2.1: A function can be locally approximated around a point by a secant, a linear function with slope and intercept , crossing at with value and crossing at with value . Using and , we find that its slope is and the intercept is . The derivative of a function at a point is then defined as the limit of the slope when . It is the slope of the tangent of at . The value of the function at can then be locally approximated around by .
If is differentiable at a given , then it is necessarily continuous at as shown in the following proposition. Non-differentiability of a continuous function at a given point is generally illustrated by a kink, as shown in Fig. 2.2.
Proposition 2.1 (Differentiability implies continuity)
If is differentiable at , then it is continuous at .
Proof
In little notation, is differentiable at if there exists , such that Since as , is continuous at . ◻

Figure 2.2: Illustration of discontinuity and non-differentiability. Left. A discontinuous function, with a jump in function value at . Center. A continuous but non-differentiable everywhere function, with kinks at and . Right. A differentiable everywhere function.
In addition to enabling the construction of a linear approximation of in a neighborhood of , since it is the slope of the tangent of at , the derivative informs us about the monotonicity of around . If is positive, the function is increasing around . Conversely, if is negative, the function is decreasing. Such information can be used to develop iterative algorithms such as gradient descent, which seeks to minimize by computing iterates of the form for , thereby moving along descent directions of around .
For several elementary functions such as , , , or , their derivatives can be obtained directly by applying the definition of the derivative in Eq. (2.1) as we now illustrate.
Example 2.1 (Derivative of power function)
Consider for , . For any , we have where, in the second line, we used the binomial theorem. Since and , we get .
Remark 2.2 (Functions on a subset of )
For simplicity, we presented the definition of the derivative for a function defined on the whole set of real numbers . If a function is defined on a subset of the real numbers, as it is the case for defined on , the derivative of at is defined by the limit in Eq. (2.1) provided that the function is well defined on a neighborhood of , that is, there exists such that for any . The function is then said to be differentiable everywhere or differentiable for short if it is differentiable at any point in the interior of , the set of points such that for sufficiently small. For points lying at the boundary of (such as and if ), one may define the right and left derivatives of at and , meaning that the limit is taken by approaching from the right or from the left.
2.1.2 Calculus rules
For a given and two functions and , the derivative of elementary operations on and such as their sums, products or compositions can easily be derived from the definition of the derivative, under appropriate conditions on the differentiability of and at . For example, if the derivatives of and exist at , then the derivatives of their weighted sum or product exist, and satisfy the rules where . The linearity can be verified directly from the linearity of the limits. For the product rule, in little notation, we have, as , hence the result.
If the derivatives of at and of at exist, then the derivative of the composition at exists and is given by We prove this result more generally in Proposition 2.2. As seen in the sequel, the linearity and the product rule can be seen as byproducts of the chain rule, making the chain rule the cornerstone of differentiation.
Consider a function that can be expressed using sums, products or compositions of elementary functions, such as . Its derivative can be computed by applying the aforementioned rules on the decomposition of into elementary operations and functions.
Example 2.2 (Applying rules of differentiation)
Consider . The derivative of at can be computed step by step as follows, denoting , We therefore obtain that .
Such a process is purely mechanical and lends itself to an automated procedure, which is the main idea of automatic differentiation presented in Chapter 8.
2.1.3 Leibniz’s notation
The notion of derivative was first introduced independently by Newton and Leibniz in the 18th century (Ball, 1960). The latter considered derivatives as the quotient of infinitesimal variations. Namely, denoting a variable depending on through , Leibniz considered the derivative of as the quotient where and denote infinitesimal variations of and respectively and the symbol denotes the evaluation of the derivative at a given point . This notation simplifies the statement of the chain rule first discovered by Leibniz (Rodriguez and Lopez Fernandez, 2010) as we have for and This hints that derivatives are multiplied when considering compositions. At evaluation, the chain rule in Leibniz notation recovers the formula presented above as The ability of Leibniz’s notation to capture the chain rule as a mere product of quotients made it popular throughout the centuries, especially in mechanics (Ball, 1960). The rationale behind Leibniz’s notation, the concept of “infinitesimal variations”, was questioned by later mathematicians for its potential logical issues (Ball, 1960). The notation first introduced by Euler and further popularized by Lagrange (Cajori, 1993) has then taken over in numerous mathematical textbooks. The concept of infinitesimal variations has been rigorously defined by using the set of hyperreal numbers. They extend the set of real numbers by considering each number as a sum of a non-infinitesimal part and an infinitesimal part (Hewitt, 1948). The formalism of infinitesimal variations further underlies the development of automatic differentiation algorithms through the concept of dual numbers.
2.2 Multivariate functions
2.2.1 Directional derivatives
Let us now consider a function with multi-dimensional input . The most important example in machine learning is a function which, to the parameters of a neural network, associates a loss value in . Variations of need to be defined along specific directions, such as the variation of around in the direction by an amount . This consideration naturally leads to the definition of the directional derivative.
Definition 2.4 (Directional derivative)
The directional derivative of at in the direction is given by provided that the limit exists.
We use the notation to emphasize that, for a given input , we can see as a function. This is essential to define the differentiability of (Definition 2.6) at and to later define linear maps (Section 2.3).
One example of directional derivative consists in computing the derivative of a function at in any of the canonical directions This allows us to define the notion of partial derivatives, denoted for This is also denoted in Leibniz’s notation as or . By moving along only the th coordinate of the function, the partial derivative is akin to differentiating the function around , letting all other coordinates fixed at their values .
2.2.2 Gradients
We now introduce the gradient vector, which gathers the partial derivatives. We first recall the definitions of linear map and linear form.
Definition 2.5 (Linear map, linear form)
A function is a linear map if for any , , A linear map with values in , , is called a linear form.
Linearity plays a crucial role in the differentiability of a function.
Definition 2.6 (Differentiability, single-output case)
A function is differentiable at if its directional derivative is defined along any direction, is linear in any direction , and if where .
We can now introduce the gradient.
Definition 2.7 (Gradient)
The gradient of a differentiable function at a point is defined as the vector of partial derivatives By linearity, the directional derivative of at in the direction is then given by
Here, denotes the inner product. We provide its definition in Euclidean spaces in Section 2.3.2.
In the definition above, the fact that the gradient can be used to compute the directional derivative is a mere consequence of the linearity of w.r.t. . However, in more abstract cases presented in later sections, the gradient is defined directly through this property.
As a simple example, any linear function of the form is differentiable as we have for any and in particular for . Moreover, its gradient is naturally given by .
More generally, to show that a function is differentiable and find its gradient, one approach is to approximate around . If we can find a vector such that then is differentiable at , since is linear. Moreover, is then the gradient of at .
Remark 2.3 (Gateaux and Fréchet differentiability)
Multiple definitions of differentiability exist. The one presented in Definition 2.6 is that of Fréchet differentiable functions. Alternatively, if has well-defined directional derivatives along any direction then the function is Gateaux differentiable. Note that the existence of directional derivatives in any direction is not a sufficient condition for the function to be differentiable. In other words, any Fréchet differentiable function is Gateaux differentiable, but the converse is not true. As a counter-example, one can verify that the function is Gateaux differentiable at but not (Fréchet) differentiable at (because the directional derivative at 0 is not linear).
Some authors also require Gateaux differentiable functions to have linear directional derivatives along any direction. These are still not Fréchet differentiable functions. Indeed, the limit in Definition 2.6 is over any vectors tending to (potentially in a pathological way), while directional derivatives look at such limits uniquely in terms of a single direction.
In the remainder of this chapter, all definitions of differentiability are in terms of Fréchet differentiability.
The next example illustrates how to compute the gradient of the logistic loss and validates its differentiability.
Example 2.3 (Gradient of logistic loss)
Consider the logistic loss , that measures the prediction error of the logits w.r.t. the correct label . Let us compute the gradient of this loss w.r.t. for fixed , i.e., we want to compute the gradient of . Let us decompose as with and the log-sum-exp function. The function is linear so differentiable with gradient . We therefore focus on . Denoting , using that , , and denoting the elementwise product, we get The above decomposition of shows that it is differentiable, and that , where Overall, we get that .
Linearity of gradients
The notion of differentiability for multi-input functions naturally inherits from the linearity of derivatives for single-input functions. For any and any multi-input functions differentiable at , the function is differentiable at and its gradient is
Why is the gradient useful?
When is differentiable, we say that is an ascent direction of from if Conversely, we say that is a descent direction of from if Using this definition, the gradient leads to the steepest ascent direction of from . To see why, we note that where we assumed . The gradient is orthogonal to the level set of the function (the set of points sharing the same value ) and points towards higher values of , as illustrated in Fig. 2.3. Conversely, the negative gradient points towards lower values of . This observation motivates the development of optimization algorithms such as gradient descent. It is based on iteratively performing the update , for . It therefore seeks for a minimizer of by moving along the steepest descent direction around given, up to a multiplicative factor, by . See also Definition 17.1 for more details.

Figure 2.3: The gradient of a function at is the normal vector to the tangent space of the level set and points towards points with higher function values.

Figure 2.4: The directional derivative of a parametric curve at is the tangent to the curve at the point .
2.2.3 Jacobians
Let us now consider a multi-output function defined by , where . A typical example in machine learning is a neural network. The notion of directional derivative can be extended to such function by defining it as the vector composed of the coordinate-wise directional derivatives: where the limits (provided that they exist) are applied coordinate-wise. The directional derivative of in the direction is therefore the vector that gathers the directional derivative of each , i.e., . In particular, we can define the partial derivatives of at as the vectors As for the usual definition of the derivative, the directional derivative can provide a linear approximation of a function around a current input, as illustrated in Fig. 2.4 for a parametric curve .
Just as in the single-output case, differentiability is defined not only as the existence of directional derivatives in any direction but also by the linearity in the chosen direction.
Definition 2.8 (Differentiability, multi-output case)
A function is (Fréchet) differentiable at a point if its directional derivative is defined along any direction, is linear in any direction, and,
The partial derivatives of each coordinate’s function are gathered in the Jacobian matrix.
Definition 2.9 (Jacobian)
The Jacobian of a differentiable function at is defined as the matrix gathering partial derivatives of each coordinate’s function provided they exist, The Jacobian can be represented by stacking columns of partial derivatives or rows of gradients, By linearity, the directional derivative of at along any input direction is then given by
Notice that we use bold to indicate the Jacobian, seen as a matrix. The Jacobian matrix naturally generalizes the concepts of derivatives and gradients presented earlier. As for the single input case, to show that a function is differentiable, one approach is to approximate around . If we find a linear map such that then is differentiable at . Moreover, if is represented by matrix such that then .
As a simple example, any linear function for is differentiable, since all its coordinate-wise components are single-output linear functions, and the Jacobian of at any is given by .
Remark 2.4 (Special cases of the Jacobian)
For single-output functions , i.e., , the Jacobian matrix reduces to a row vector identified as the transpose of the gradient, For a single-input function , the Jacobian reduces to a single column vector of directional derivatives, denoted For a single-input single-output function , the Jacobian reduces to the derivative of , i.e.,
The next example illustrates the form of the Jacobian matrix for the element-wise application of a differentiable function , such as the softplus activation (see Section 4.4 for a review of activation functions). In this case, the Jacobian takes a simple diagonal matrix form. As a consequence, the directional derivative associated with this function is simply given by an element-wise product: a full matrix-vector product is not needed, as would suggest Definition 2.9. We will revisit this point in Section 2.3.
Example 2.4 (Jacobian matrix of the softplus activation)
Consider the element-wise application of the softplus defined for by Since is differentiable, each coordinate of this function is differentiable and the overall function is differentiable. The th coordinate of is independent of the th coordinate of for , so for . For , the result boils down to the derivative of at . That is, , where . The Jacobian of is therefore a diagonal matrix
Chain rule
Equipped with a generic definition of differentiability and the associated objects, gradients and Jacobians, we can now generalize the chain rule, previously introduced for single-input single-output functions.
Proposition 2.2 (Chain rule)
Consider and . If is differentiable at and is differentiable at , then the composition is differentiable at and its Jacobian is given by
Proof
We progressively approximate using the differentiability of at and at , Hence, is differentiable at with Jacobian . ◻
Proposition 2.2 can be seen as the cornerstone of any derivative computations. For example, it can be used to rederive the linearity and product rules associated to the derivatives of single-input single-output functions.
When is scalar-valued, combined with Remark 2.4, we obtain a simple expression for .
Proposition 2.3 (Chain rule, scalar-valued case)
Consider and . The gradient of the composition is given by
This is a very useful identity in machine learning, as we often need to compose a vector-valued model function and a scalar-valued loss function. We illustrate this with linear regression below.
Example 2.5 (Linear regression)
Consider a linear regression of inputs onto targets , using a parameter vector . The loss is defined as the sum of squared residuals, where and .
The function can be decomposed into a linear mapping and a squared error , so that . We can then apply the chain rule in Proposition 2.3 to get provided that and are differentiable at and , respectively.
The function is linear so differentiable with Jacobian . On the other hand the partial derivatives of are given by for . Therefore, is differentiable at any and its gradient is . By combining the two, we then get the gradient of as
2.3 Linear maps
The Jacobian matrix is useful as a representation of the partial derivatives. However, the core idea underlying the definition of differentiable functions, as well as their implementation in an autodiff framework, lies in the access to two key linear maps. These two maps encode infinitesimal variations along input or output directions and are referred to, respectively, as Jacobian-vector product (JVP) and Vector-jacobian product (VJP). This section formalizes these notions, in the context of Euclidean spaces.
2.3.1 The need for linear maps
So far, we have focused on functions , that take a vector as input and produce a vector as output. However, functions that use matrix or even tensor inputs/outputs are commonplace in neural networks. For example, consider the function of matrices of the form , where and . This function takes a matrix as input, not a vector. Of course, a matrix can always be “flattened” into a vector , by stacking the columns of . We denote this operation by and its inverse by . We can then equivalently write as , so that the previous framework applies. However, we will now see that this would be inefficient.
Indeed, the resulting Jacobian of at any consists in a matrix of size , which, after some computations, can be observed to be mostly filled with zeros. Getting the directional derivative of at in a direction would consist in (i) vectorizing into , (ii) computing the matrix-vector product at a cost of computations (ignoring the fact that the Jacobian has many zero entries), (iii) re-shaping the result into a matrix.
On the other hand, since is linear in its matrix input, we can infer that the directional derivative of at any in any direction is simply given by the function itself applied on . Namely, we have , which is simple to implement and clearly only requires operations. Note that the cost would have been the same, had we ignored the non-zero entries of . The point here is that by considering the operations associated to the differentiation of a function as linear maps rather than using the associated representation as a Jacobian matrix, we can efficiently exploit the underlying input or output space structure. To that end, we now recall the main abstractions necessary to extend the previous definitions in the context of Euclidean spaces.
2.3.2 Euclidean spaces
Linear spaces, a.k.a. vector spaces, are spaces equipped with and closed under an addition rule compatible with multiplication by a scalar (we limit ourselves to the field of reals). Namely, in a linear space , there exist operations and , such that for any , , and , we have and .
Euclidean spaces are linear spaces equipped with a basis . Any element can be decomposed as for some unique scalars . A canonical example of Euclidean space is the set of all vectors of size that we already covered. The set of matrices of size is also naturally a Euclidean space generated by the set of canonical matrices for filled with zero except at the th entry filled with one. For example, can be written . Euclidean spaces are naturally equipped with a notion of inner product.
Definition 2.10 (Inner product)
An inner product on a linear space is a function that is
bilinear: and are linear for any ,
symmetric: for any ,
positive definite: for any , and if and only if .
An inner product defines a norm .
The norm induced by an inner product defines a distance between , and therefore a notion of convergence.
For vectors, where , the inner product is the usual one For matrices, where , the inner product is the so-called Frobenius inner product. It is defined for any by where is the trace operator defined for square matrices . For tensors of order , which generalize matrices to , the inner product is defined similarly for by where is the th entry of .
2.3.3 Linear maps and their adjoints
The notion of linear map in Definition 2.5 naturally extends to Euclidean spaces. Namely, a function from a Euclidean space onto a Euclidean space is a linear map if for any and , we have . When and , there always exists a matrix such that . Therefore, we can think of as the “materialization” as a matrix of .
Example 2.6 (Linear map)
Consider the linear map , where , and . This is a function from to . We can always materialize the linear map as a matrix and write . However, applying a linear map on a vector often does not require materializing the corresponding matrix. In this example, we can simply write , since is a scalar value. This only requires an inner product and an element-wise multiplication, and is more efficient than materializing then computing , which requires an outer product and a matrix-vector multiplication.
We can define the adjoint operator of a linear map.
Definition 2.11 (Adjoint operator)
Given two Euclidean spaces and equipped with inner products and , the adjoint of a linear map is the unique linear map such that for any and ,
The adjoint can be thought of as the counterpart of the matrix transpose for linear maps. When , we have since
Example 2.7 (Adjoint linear map)
Using the linear map from the previous example, we have for all and , Therefore, the adjoint linear map is . This is a function from to . It can be materialized as the matrix . Applying can be done efficiently as .
2.3.4 Jacobian-vector products
We now define the directional derivative using linear maps, leading to the notion of Jacobian-vector product (JVP). This can be used to facilitate the treatment of functions on tensors or for further extensions to infinite-dimensional spaces. In the following, and denote two Euclidean spaces equipped with inner products and . We start by defining differentiability in general Euclidean spaces.
Definition 2.12 (Differentiability in Euclidean spaces)
A function is differentiable at a point if the directional derivative along is well-defined for any , linear in and if
We can now formally define the Jacobian-vector product.
Definition 2.13 (Jacobian-vector product)
For a differentiable function , the linear map , mapping to , is called the Jacobian-vector product (JVP). From this perspective, the function is a function from to a linear map from to . That is, we have
We emphasize again that the directional derivative is a value, while the JVP is a function. Strictly speaking, can belong to any Euclidean space and does not need to be limited to a vector, as the JVP acronym would suggest. We adopt the name JVP, as it is now standard.
Recovering the gradient
Previously, we saw that for differentiable functions with vector input and scalar output, the directional derivative is equal to the inner product between the direction and the gradient. The same applies when considering differentiable functions from a Euclidean space with single outputs, except that the gradient is now an element of the input space and the inner product is the one associated with the input space.
Proposition 2.4 (Gradient)
If a function is differentiable at , then there exists , called the gradient of at such that the directional derivative of at along any input direction is given by
In Euclidean spaces, the existence of the gradient can simply be shown by decomposing the partial derivative along a basis of . Such a definition generalizes to infinite-dimensional (e.g., Hilbert spaces) spaces as discussed in Section 2.3.9.
2.3.5 Vector-Jacobian products
Consider a function . Instead of variations of along an input direction , we may also consider the variations of along an output direction , namely, computing the gradient of the scalar-valued function Equivalently, we may compute the gradients of each coordinate function at , where is the th canonical vector in . The infinitesimal variations of at along any output direction are given by where is the Jacobian’s transpose. Using the definition of derivative as a limit, we may also write for where is the th canonical vector in .
For generic Euclidean spaces and , the counterpart of the transpose is the adjoint operator, leading to the notion of vector-Jacobian product.
Proposition 2.5 (Vector-Jacobian product)
If a function is differentiable at , then its infinitesimal variation along an output direction is given by the adjoint map of the JVP, called the vector-Jacobian product (VJP). It satisfies where we denoted . The function is a function from to a linear map from to . That is, we have
Proof
The chain rule presented in Proposition 2.2 naturally generalizes to Euclidean spaces (see Proposition 2.6). Since is linear, its directional derivative is itself. Therefore, the directional derivative of is As this is true for any , is the gradient of per Proposition 2.4. ◻
We illustrate the JVP and VJP linear maps in Fig. 2.5.

Figure 2.5: Jacobian-vector product (JVP) and vector-Jacobian product (VJP) , seen as linear maps.
2.3.6 Chain rule using linear maps
The chain rule presented before in terms of Jacobian matrices can readily be formulated to take advantage of the implementations of the JVP and VJP as linear maps.
Proposition 2.6 (Chain rule, general case)
Consider and , where , and are Euclidean spaces. If is differentiable at and is differentiable at , then the composition is differentiable at . Its JVP is given for all by and its VJP is given for all by
The proof follows the one of Proposition 2.2. This is illustrated in Fig. 2.6. When the last function is scalar-valued, which is often the case in machine learning, we obtain the following simplified result.
Proposition 2.7 (Chain rule, scalar case)
Consider and , the gradient of the composition is given by

Figure 2.6: Chain rule using JVP and VJP linear maps.
2.3.7 Functions of multiple inputs (fan-in)
Oftentimes, the inputs of a function do not belong to only one Euclidean space but to a product of them. An example is , which is defined on . In such a case, it is convenient to generalize the notion of partial derivatives to handle blocks of inputs.
Consider a function defined on , where . We denote the partial derivative with respect to the th input along as . Equipped with this notation, we can analyze how JVPs or VJPs are decomposed along several inputs.
Proposition 2.8 (Multiple inputs)
Consider a differentiable function of the form with signature , where and . Then the JVP with the input direction is given by The VJP with the output direction is given by
Example 2.8 (Matrix-vector product)
Consider , where and . This corresponds to setting and . For the JVP, letting and , we obtain We can also access the individual JVPs as For the VJP, letting , we obtain We can access the individual VJPs by
Remark 2.5 (Nested inputs)
It is sometimes convenient to group inputs into meaningful parts. For instance, if the input is naturally broken down into two parts , where is a text part and is an image part, and the network parameters are naturally grouped into three layers , we can write . This is mostly a convenience and we can again reduce it to a function of a single input, thanks to the linear map perspective in Euclidean spaces.
Remark 2.6 (Hiding away inputs)
It will often be convenient to ignore inputs when differentiating. We use the semicolon for this purpose. For instance, a function of the form (notice the semicolon) has signature because we treat and as constants. Therefore, the gradient is . On the other hand, the function (notice the comma) has signature so its gradient is . If we need to access partial gradients, we use indexing, e.g., or when there is no ambiguity.
2.3.8 Functions with multiple outputs (fan-out)
Similarly, it is often convenient to deal with functions that have multiple outputs.
Proposition 2.9 (Multiple outputs)
Consider a differentiable function of the form , with signatures and , where . Then the JVP with the input direction is given by The VJP with the output direction is
Combined with the chain rule, we obtain that the Jacobian of is and therefore the JVP is
2.3.9 Extensions to non-Euclidean linear spaces
So far, we focused on Euclidean spaces, i.e., linear spaces with a finite basis. However, the notions studied earlier can be generalized to more generic spaces.
For example, directional derivatives (see Definition 2.12) can be defined in any linear space equipped with a norm and complete with respect to this norm. Such spaces are called Banach spaces. Completeness is a technical assumption that requires that any Cauchy sequence converges (a Cauchy sequence is a sequence whose elements become arbitrarily close to each other as the sequence progresses). A function defined from a Banach space onto a Banach space is then called Gateaux differentiable if its directional derivative is defined along any direction (where limits are defined w.r.t. the norm in ). Some authors also require the directional derivative to be linear to define a Gateaux differentiable function.
Fréchet differentiability can also naturally be generalized to Banach spaces. The only difference is that, in generic Banach spaces, the linear map satisfying Definition 2.12 must be continuous, i.e., there must exist , such that , where is the norm in the Banach space .
The definitions of gradient and VJPs require in addition a notion of inner product. They can be defined in Hilbert spaces, that is, linear spaces equipped with an inner product and complete with respect to the norm induced by the inner product (they could also be defined in a Banach space by considering operations in the dual space, see, e.g. (Clarke et al., 2008)). The existence of the gradient is ensured by Riesz’s representation theorem which states that any continuous linear form in a Hilbert space can be represented by the inner product with a vector. Since for a differentiable function , the JVP is a linear form, Riesz’s representation theorem ensures the existence of the gradient as the element such that for any . The VJP is also well-defined as the adjoint of the JVP w.r.t. the inner product of the Hilbert space.
As an example, the space of squared integrable functions on is a Hilbert space equipped with the inner product Here, we cannot find a finite number of functions that can express all possible functions on . Therefore, this space is not a mere Euclidean space. Nevertheless, we can consider functions on this Hilbert space (called functionals to distinguish them from the elements of the space). The associated directional derivatives and gradients, can be defined and are called respectively, functional derivative and functional gradient, see, e.g., Frigyik et al. (2008) and references therein.
2.4 Second-order differentiation
2.4.1 Second derivatives
For a single-input, single-output differentiable function , its derivative at any point is itself a function . We may then consider the derivative of the derivative at any point: the second derivative.
Definition 2.14 (Second derivative)
The second derivative of a differentiable function at is defined as the derivative of at , that is, provided that the limit is well-defined. If the second derivative of a function is well-defined at , the function is said to be twice differentiable at . The second derivative is also denoted .
If has a small second derivative at a given , the derivative around is almost constant. That is, the function behaves like a line around , as illustrated in Fig. 2.7. Hence, the second derivative is usually interpreted as the curvature of the function at a given point.

Figure 2.7: Points at which the second derivative is small are points along which the function is well approximated by its tangent line. On the other hand, points with large second derivative tend to be badly approximated by the tangent line.
2.4.2 Second directional derivatives
For a multi-input function , we saw that the directional derivative encodes infinitesimal variations of along a given direction. To analyze the second derivative, the curvature of the function at a given point , we can consider the variations along a pair of directions, as defined below.
Definition 2.15 (Second directional derivative)
The second directional derivative of at along is defined as the directional derivative of along , that is, provided that is well-defined around and that the limit exists.
Of particular interest are the variations of a function around the canonical directions: the second partial derivatives, defined as for , the th and th canonical directions in , respectively. In Leibniz notation, the second partial derivatives are denoted
2.4.3 Hessians
For a multi-input function, twice differentiability is simply defined as the differentiability of any directional derivative w.r.t. .
Definition 2.16 (Twice differentiability)
A function is twice differentiable at if it is differentiable and is also differentiable at .
As a result, the second directional derivative is a bilinear form.
Definition 2.17 (Bilinear map, bilinear form)
A function is a bilinear map if is linear for any and is linear for any . That is, for and . A bilinear map with values in , , is called a bilinear form.
The second partial derivatives are gathered in the Hessian and the second directional derivatives can be computed from it.
Definition 2.18 (Hessian)
The Hessian of a twice differentiable function at is the matrix gathering all second partial derivatives, provided that all second partial derivatives are well-defined.
The second directional derivative at is bilinear in any direction and . Therefore,
Given the gradient of , the Hessian is equivalent to the transpose of the Jacobian of the gradient. By slightly generalizing the notation to denote the transpose of the Jacobian of a function (which matches its definition for single-output functions), we have that the Hessian can be expressed as , which justifies its notation.
Similarly as for the differentiability of a function , twice differentiability of at is equivalent to having the second partial derivatives not only defined but also continuous in a neighborhood of . Remarkably, by requiring twice differentiability, i.e., continuous second partial derivatives, the Hessian is guaranteed to be symmetric (Schwarz, 1873).
Proposition 2.10 (Symmetry of the Hessian)
If a function is twice differentiable at , then its Hessian is symmetric, that is, for any .
The symmetry of the Hessian means that it can alternatively be written as , i.e., the Jacobian of the gradient of .
2.4.4 Hessian-vector products
Similarly to the Jacobian, we can exploit the formal definition of the Hessian as a bilinear form to extend its definition to Euclidean spaces. In particular, we can define the notion of Hessian-vector product.
Definition 2.19 (Hessian-vector product)
If a function defined on a Euclidean space with inner product , is twice differentiable at , then for any , there exists , called the Hessian-vector product (HVP) of at along , such that for any , In particular for , the HVP is .
From an autodiff point of view, the HVP can be implemented in four different ways, as explained in Section 9.1.
2.4.5 Second-order Jacobians
The previous definitions naturally extend to multi-output functions , where , and . The second directional derivative is defined by gathering the second derivatives of each coordinate’s function. That is, for , The function is twice differentiable if and only if all its coordinates are twice differentiable. The second directional derivative is then a bilinear map. We can then compute second directional derivatives as When and , so that , the bilinear map can be materialized as a tensor the “second-order Jacobian” of . However, similarly to the Hessian, it is usually more convenient to apply the bilinear map to prescribed vectors and than to materialize the second partial derivatives as a tensor.
2.5 Higher-order differentiation
2.5.1 Higher-order derivatives
Derivatives can be extended to any order. Formally, the th derivative can be defined inductively as follows for a single-input, single-output function.
Definition 2.20 (th order derivative)
The th derivative of a function at is defined as provided that is differentiable around and that the limit exists. In such a case, the function is said to be times differentiable at .
2.5.2 Higher-order directional derivatives
For a multi-input function , we can naturally extend the notion of directional derivative as follows.
Definition 2.21 (th order directional derivative)
The th directional derivative of at along is defined as
A multi-input function is -times differentiable if it is differentiable and its directional derivative along any direction is differentiable. As a consequence the th directional derivative is a multilinear form.
Definition 2.22 (Multilinear map, multilinear form)
A function is a multilinear map if it is linear in each coordinate given all others fixed, that is, if is linear in for any . It is a multilinear form if it has values in .
The th order directional derivative is then given by The th order partial derivatives can be materialized as an th order tensor
2.5.3 Higher-order Jacobians
All above definitions extend directly to the case of multi-output functions , where . The th directional derivatives are then The function is then times differentiable if it is differentiable and its directional derivative along any direction is differentiable. As a consequence, the th directional derivative is a multilinear map. The th directional derivative can be decomposed into partial derivatives as When and , the th order partial derivatives can be materialized by an th order tensor
2.5.4 Taylor expansions
With Landau’s little notation, we have seen that if a function is differentiable, it is approximated by a linear function in , Such an expansion of the function up to its first derivative is called the first-order Taylor expansion of around .
If the function is twice differentiable, we can approximate it by a quadratic in , leading to the second-order Taylor expansion of around , Compared to the first-order Taylor approximation, it is naturally more accurate around , as reflected by the fact that for .
More generally, we can build the th order Taylor expansion of a times differentiable function around by Note that, using the change of variable , it is often convenient to write the th Taylor expansion of around as Taylor expansions will prove useful in Chapter 7 for computing derivatives by finite differences.
2.6 Differential geometry
In this chapter, we progressively generalized the notion of derivative from real numbers to vectors and variables living in a linear space (a.k.a. vector space), either finite dimensional or infinite dimensional. We can further generalize these notions by considering a local notion of linearity. This is formalized by smooth manifolds in differential geometry, whose terminology is commonly adopted in the automatic differentiation literature and software. In this section, we give a brief overview of derivatives on smooth manifolds (simply referred to as manifolds), and refer to Boumal (2023) for a complete introduction.
2.6.1 Differentiability on manifolds
Essentially, a manifold is a set that can be locally approximated by a Euclidean space. The most common example is a sphere like the Earth. Seen from the Moon, the Earth is not a plane, but locally, at a human level, it can be seen as a flat surface. Euclidean spaces are also trivial examples of manifolds. A formal characterization of the sphere as a manifold is presented in Example 2.9. For now, we may think of a “manifold” as some set (e.g., the sphere) contained in some ambient Euclidean space; note however that manifolds can be defined generally without being contained in a Euclidean space (Boumal, 2023, 8). Differentiability in manifolds is simply inherited from the notion of differentiability in the ambient Euclidean space.
Definition 2.23 (Differentiability of restricted functions)
Let and be manifolds. A function defined from to , with and Euclidean spaces, is differentiable if is the restriction of a differentiable function , so that coincides with on .
Our objective is to formalize the directional derivatives and gradients for functions defined on manifolds. This formalization leads to the definitions of tangent spaces and cotangent spaces, and the associated generalizations of JVP and VJP operators as pushforward and pullback operators, respectively.
2.6.2 Tangent spaces and pushforward operators
To generalize the notion of directional derivatives of a function , the one property we want to preserve is the chain rule. Rather than starting from the variations of at a given point along a direction, we start with the variations of along curves. Namely, on a manifold like the sphere in , we can look at curves passing through at time , that is, . For single-input functions like , we denote for simplicity . The directional derivative of a function must typically serve to define the derivative of at , such that . In the case of the sphere, as illustrated in Fig. 2.8, the derivative of a curve passing through a point is always tangent to the sphere at . The tangent plane to the sphere at then captures all possible relevant vectors to pass to the JVP we are building. To define the directional derivative of a function on a manifold, we therefore restrict ourselves to an operator defined on the tangent space , whose definition below is simplified for our purposes.
Definition 2.24 (Tangent space)
The tangent space of a manifold at is defined as
In the case of the sphere in Fig. 2.8, the tangent space is a plane, that is, a Euclidean space. This property is generally true: tangent spaces are Euclidean spaces, enabling us to define directional derivatives as linear maps. Now, if is differentiable and goes from a manifold to a manifold , then is a differentiable curve in . Therefore, is the derivative of a curve passing through at and is tangent to at . Hence, the directional derivative of at can be defined as a function from the tangent space of at onto the tangent space of at . Overall, we built the directional derivative (JVP) by considering how a composition of with any curve pushes forward the derivative of into the derivative of . The resulting JVP is called a pushforward operator in differentiable geometry.
Definition 2.25 (Pushforward operator)
Given two manifolds and , the pushforward operator of a differentiable function at is the linear map defined by for any such that , where is a differentiable curve passing through at , i.e., .

Figure 2.8: A differentiable function defined from a sphere to a sphere defines a push-forward operator that maps tangent vectors (derivatives of functions on the sphere passing through ) in the tangent space to tangent vectors of at in the tangent space .
2.6.3 Cotangent spaces and pullback operators
To generalize the JVP, we composed with any single-input function giving values on the manifold. The derivative of any such is then pushed forward from to by the action of . To define the VJP, we take a symmetric approach. We consider all single-output differentiable functions defined on with for some . We then want to pull back the derivatives of when precomposing it by . Therefore, the space on which the VJP acts is the space of directional derivatives of any at , defining the cotangent space.
Definition 2.26 (Cotangent space)
The cotangent space of a manifold at is defined as
Note that elements of the cotangent space are linear mappings, not vectors. This distinction is important to define the pullback operator as an operator on functions as done in measure theory. From a linear algebra viewpoint, the cotangent space is exactly the dual space of , that is, the set of linear maps from to , called linear forms. As is a Euclidean space, its dual space is also a Euclidean space. The pullback operator is then defined as the operator that gives access to directional derivatives of given the directional derivative of at .
Definition 2.27 (Pullback operator)
Given two manifolds and , the pullback operator of a differentiable function at is the linear map defined by for any such that , for a differentiable function .
Contrary to the pushforward operator that acts on vectors, the pullback operator acts on linear forms. Hence, the slight difference in notation between and , the adjoint operator of . To properly define the adjoint operator , we need a notion of inner product. Since tangent spaces are Euclidean spaces, we can define an inner product for each and , making a Riemannian manifold. Equipped with these inner products, the cotangent space can be identified with the tangent space, and we can define gradients.
Definition 2.28 (Gradients in Riemannian manifolds)
Let be a Riemannian manifold equipped with inner products . For any cotangent vector , with , there exists a unique tangent vector such that In particular for any differentiable function , we can define the gradient of as the unique tangent vector such that
Therefore, rather than pulling back directional derivatives, we can pull back gradients. The corresponding operator is then naturally the adjoint of the pushforward operator. Namely, given two Riemannian manifolds and , and a differentiable function , we have for represented by .
| Function | ||
|---|---|---|
| Push-forward | ||
| Pullback | ||
| Adjoint of pushforward |
Table 2.1: For a differentiable function defined from a manifold onto a manifold , the JVP is generalized with the notion of pushforward . The counterpart of the pushforward is the pullback operation that acts on linear forms in the tangent spaces. For Riemannian manifolds, the pullback operation can be identified with the adjoint operator of the pushforward operator as any linear form is represented by a vector.
Example 2.9 (The sphere as a manifold)
The sphere in is defined as the set of points , satisfying , with JVP .
Let us first understand why we may locally see the sphere as a space of dimension . Take the hemisphere , it can be entirely described by using the first coordinates of each point, the last one is simply given by . Formally, for any vector of coordinates such that , we can define and . We have then , that is . We can then describe the hemisphere as the image of the unit ball in dimension through the mapping , that is, . In particular, any open set on the hemisphere can be described as the mapping of an open set in . The reasoning applies to all hemispheres with different mappings, hence the local aspect of this viewpoint.
The tangent space can be naturally characterized in terms of the constraining function . Namely, the curve such that satisfies for any , . Hence, differentiating the implicit equation, we have That is, is in the null space of , denoted The tangent space of at is then We naturally recover that the tangent space is a Euclidean space of dimension , defined as the set of points orthogonal to .
2.7 Generalized derivatives
While we largely focus on differentiable functions in this book, it is important to characterize non-differentiable functions. We distinguish here two cases: continuous functions and non-continuous functions. For the former case, there exist generalizations of the notion of directional derivative, gradient and Jacobian, presented below. For non-continuous functions, even if derivatives exist almost everywhere, they may be uninformative. For example, piecewise-constant functions, encountered in e.g. control flows (Chapter 5), are almost everywhere differentiable but with zero derivatives. In such cases, surrogate functions can be defined to ensure the differentiability of a program (Part IV).
2.7.1 Rademacher’s theorem
We first recall the definition of (locally) Lipschitz continuous function.
Definition 2.29 ((Locally) Lipschitz continuous function)
A function , is Lipschitz continuous if there exists such that for any , A function is locally Lipschitz continuous if for any , there exists a neighborhood of such that restricted to is Lipschitz continuous.
Rademacher’s theorem (Rademacher, 1919) then ensures that is differentiable almost everywhere.
Proposition 2.11 (Rademacher’s theorem)
Let and denote Euclidean spaces. If is locally Lipschitz-continuous, then is almost everywhere differentiable, that is, the set of points in at which is not differentiable is of (Lebesgue) measure zero.
See also Morrey Jr (2009) for a standard proof.
2.7.2 Clarke derivatives
Rademacher’s theorem hints that the definitions of directional derivatives, gradients and Jacobians may be generalized to locally Lipschitz continuous functions. This is what Clarke (1975) did in his seminal work, which laid the foundation of nonsmooth analysis. The first building block is a notion of generalized directional derivative.
Definition 2.30 (Clarke generalized directional derivative)
The Clarke generalized directional derivative of a locally Lipschitz continuous function at in the direction is provided that the limit exists, where means that approaches by non-negative values, and where the limit superior is defined as for the ball centered at of radius .
There are two differences with the usual definition of a directional derivative: (i) we consider slopes of the function in a neighborhood of the point rather than at the given point, (ii) we take a limit superior rather than a usual limit. The first point is rather natural in the light of Rademacher’s theorem: we can properly characterize variations on points where the function is differentiable, therefore we may take the limits of these slopes as a candidate slope for the point of interest. The second point is more technical but essential: it allows us to characterize the directional derivative as the supremum of some linear forms (Clarke et al., 2008). These linear forms in turn define a set of generalized gradients (Clarke et al., 2008, 2).
Definition 2.31 (Clarke generalized gradient)
A Clarke generalized gradient of a locally Lipschitz function at is a point such that The set of Clarke generalized gradients is called the Clarke subdifferential of at .
Definition 2.30 and Definition 2.31 can be used in non-Euclidean spaces, such as Banach or Hilbert spaces (Clarke et al., 2008). In Euclidean spaces, the Clarke generalized gradients can be characterized more simply thanks to Rademacher’s theorem (Clarke et al., 2008, Theorem 8.1). Namely, as shown below, they can be defined as a convex combination of limits of gradients of evaluated at a sequence in that converges to .
Proposition 2.12 (Characterization of Clarke generalized gradients)
Let be a locally Lipschitz continuous and denote the set of points at which is not differentiable (Proposition 2.11). An element is a Clarke generalized gradient of at if and only if
In the above, the convex hull of a set , the set of convex combinations of elements of , is denoted
The Jacobian of a function between two Euclidean spaces can be generalized similarly (Clarke et al., 2008, Section 3.3).
Definition 2.32 (Clarke generalized Jacobian)
Let be locally Lipschitz continuous and denote the set of points at which is not differentiable (Proposition 2.11). A Clarke generalized Jacobian of at is an element of
For a continuously differentiable function or , there is a unique generalized gradient, recovering the usual gradient (Clarke et al., 2008, Proposition 3.1, page 78). The chain rule can be generalized to these objects (Clarke et al., 2008). Recently, (Bolte et al., 2022; Bolte and Pauwels; 2020) further generalized Clarke gradients through the definition of conservative gradients to define automatic differentiation schemes for nonsmooth functions.
2.8 Summary
The usual definition of derivatives of real-valued univariate functions extends to multivariate functions through the notion of directional derivative at in the direction .
To take advantage of the representation of using the canonical bases , the definition of differentiable functions requires the linearity of the directional derivative w.r.t. the direction .
This requirement gives rise to the notion of gradient , the vector that gathers the partial derivatives and further defines the steepest ascent direction at .
For vector-input vector-output functions , the directional derivative leads to the definition of Jacobian matrix , the matrix which gathers all partial derivatives (notice that we use bold ). The chain rule is then the product of Jacobian matrices.
These notions can be extended to general Euclidean spaces, such as the spaces of matrices or tensors. For functions of the form , the gradient is . More generally, for functions of the form , the Jacobian can be seen as a linear map (notice the non-bold ). The directional derivative at naturally defines a linear map , where is called the Jacobian vector product (JVP) and captures the infinitesimal variation at along the input direction .
Its adjoint defines another linear map called the vector Jacobian product (VJP) and captures the infinitesimal variation at along the output direction . The chain rule is then the composition of these linear maps.
For the particular case when we compose a scalar-valued function (such as a loss function) with a vector-valued function (such as a network function), the gradient is given by . This is why being able to apply the adjoint to a gradient, which as we shall see can be done with reverse-mode autodiff, is so pervasive in machine learning.
The definitions of JVP and VJP operators can further be generalized in the context of differentiable geometry. In that framework, the JVP amounts to the pushforward operator that acts on tangent vectors. The VJP amounts to the pullback operator that acts on cotangent vectors.
We also saw that the Hessian matrix of a function from to is denoted . It is symmetric if the second partial derivatives are continuous. Seen as linear map, the Hessian leads to the notion of Hessian-vector product (HVP), which we saw can be reduced to the JVP or the VJP of .
The main take-away message of this chapter is that computing the directional derivative or the gradient of compositions of functions does not require computing intermediate Jacobians but only to evaluate linear maps (JVPs or VJPs) associated with these intermediate functions. The goal of automatic differentiation, presented in Chapter 8, is precisely to provide an efficient implementation of these maps for computation chains or more generally for computation graphs.