Multivariate Functions and Composite Function Differentiation
In the previous chapter, we learned about derivatives and differentials of single-variable functions, establishing the fundamental concept of the rate of change. However, most problems in machine learning involve multiple variables. For example, the parameters of a neural network may number in the millions or even billions, and the loss function is a multivariate function of these parameters. This chapter extends the concept of derivatives to multivariate functions, introducing core concepts such as partial derivatives, gradients, the chain rule, directional derivatives, and the Hessian matrix, laying the theoretical foundation for understanding optimization algorithms in machine learning.
Partial Derivatives
A Multivariate Function is a natural extension of a single-variable function. An -variable function maps inputs to a single output value. In the real world, multivariate functions are more common than univariate functions. For instance, in a neural network, the output of each layer is a multivariate function of multiple inputs; during neural network optimization, the loss function is a multivariate function of the model parameters; the predicted value corresponding to a feature vector , and so on.
When dealing with multivariate functions, a natural first thought is to simplify the problem by fixing variables -- considering how the function value changes if we vary only one variable while keeping all others constant. This is precisely the idea behind partial derivatives. Let be a function of two variables. The partial derivative of with respect to at the point is defined as the limit of the rate of change of the function value when is held fixed and undergoes a small change :
Similarly, the partial derivative with respect to is defined as:
When computing the partial derivative of with respect to , we treat as a constant and differentiate with respect to using ordinary derivative rules -- the computation is exactly the same as for single-variable derivatives. Therefore, the derivative rules introduced in the previous chapter still apply. Let . To find , treat as a constant: . To find , treat as a constant: .
Geometrically, the derivative is interpreted as the slope of the tangent line, and partial derivatives have an equally intuitive geometric interpretation. For a function of two variables , its graph is a surface in three-dimensional space. represents the "tangent slope" along the direction on the surface, while represents the "tangent slope" along the direction. More specifically, is the slope of the tangent line at the point to the curve formed by the intersection of the surface and the plane . This is equivalent to the tangent slope when we "fix and let only vary." In short, a partial derivative represents the rate of change of a function along a coordinate axis direction.
But what if we do not want to restrict ourselves to fixed coordinate axes and instead want to know the rate of change along an arbitrary direction? This is where the directional derivative comes in. Let be a function of two variables, and let be a unit vector (). The directional derivative of at the point in the direction is defined as:
Geometrically, the directional derivative represents the limit of the average rate of change of the function value when moving a small step from the point in the direction .
Gradients
Partial derivatives tell us the rate of change of a function along each coordinate axis direction. Combining the partial derivative information from all coordinate directions yields a vector called the gradient: let be a multivariate function. Its gradient is defined as:
where is called the gradient operator. With the gradient, we can view the directional derivative from another perspective -- the directional derivative is the dot product of the gradient and the direction vector:
This formula (which can be shown to be equivalent to the earlier definition after studying the Chain Rule) reveals an extremely important geometric property of the gradient: the gradient points in the direction of the steepest increase of the function. Recalling the definition and geometric properties of the dot product, let be the angle between the gradient and the direction vector . Then , and since , we have . Because attains its maximum value of when , the directional derivative reaches its maximum value when the direction vector is aligned with the gradient . In other words, the gradient direction is the direction in which the function value increases most rapidly.
As a concrete example, consider the function , which resembles a bowl-shaped surface (or an inverted hill), as shown in the figure below. At the point , the gradient is , and the magnitude of the gradient is .

Figure: Gradient Example
The calculation shows that moving in the gradient direction increases the function value by approximately per unit distance (the largest among all directions). Moving in the negative gradient direction decreases the function value by approximately per unit distance (the "steepest descent" among all directions). Moving in a direction perpendicular to the gradient (e.g., ) leaves the function value unchanged, as this is precisely the tangent direction of the contour line. Correspondingly, the negative gradient direction is the direction of steepest descent. This leads to the core idea of gradient descent in machine learning: moving in the negative gradient direction allows us to find the minimum of a function most quickly.
This geometric property of the gradient is very important for our subsequent study. In the context of machine learning, the optimization objective is typically to minimize the loss function. Let the loss function be , where are the model parameters. The update rule of the gradient descent algorithm is:
Here, is the learning rate, which controls the step size; is the gradient of the loss function at the current parameter values, indicating the direction of steepest increase; the negative sign means moving in the negative gradient direction, i.e., progressing along the direction of steepest decrease of the loss function. Understanding the geometric meaning of the gradient is the theoretical foundation for understanding the convergence behavior of gradient descent in machine learning, choosing appropriate learning rates, and diagnosing training issues.
Composite Functions and the Chain Rule
When introducing partial derivatives and gradients earlier, we skipped the explanation of the dot product relationship between the gradient and the direction vector, because doing so requires the concept of a path composite function (see Exercise 1 for the detailed derivation). In practice, functions are often not as simple as ; they are frequently the result of nesting multiple functions. For instance, consider an airplane climbing upward: its position changes over time, while the temperature varies with altitude. The temperature around the airplane is a composite function: time first affects the position , which in turn affects the temperature . If we want to know the "rate of change of temperature with respect to time" , we cannot differentiate directly -- we must decompose it layer by layer: first, how temperature changes with position , then how position changes with time . This requires the chain rule.
A composite function is a function whose output serves as the input to another function. Let and . Then is called the composite function of and , denoted . Composite functions "chain together" multiple simple functions to form complex functional relationships. For example, is composed of and ; is composed of and . In neural network models, input data undergoes a series of transformations layer by layer, ultimately producing predictions and a loss value. Each layer is a function, and the entire network is a deeply composite function.
The Chain Rule is the powerful tool for differentiating composite functions. It tells us that the derivative of a composite function equals the product of the derivatives of each layer: the rate of change of with respect to equals the rate of change of with respect to the intermediate variable , multiplied by the rate of change of with respect to . This is like a chain reaction: a change in first affects , which then affects through . For example, let and ; then is a composite function of , and its derivative is: . In function notation: .
As a concrete example, suppose and we want to find . First, let , so . By the chain rule: $$
Combining multivariate functions with composite functions yields a more general form of the chain rule. Let , where and . Then becomes a function of through and : . In this case, . The meaning of this expression is that the total rate of change equals the sum of the contributions from each path.
Integration
In machine learning, the primary focus is on optimization problems, so differentiation takes center stage. However, integration, as another important concept in calculus, also has widespread applications in probability theory, information theory, and other fields. Differentiation studies the "local rate of change" -- how fast a function value changes at a given point. Integration studies the "global accumulation" -- the overall effect of a function over an interval. These two seemingly opposite problems are closely connected through the Fundamental Theorem of Calculus.
The concept of integration originated from an ancient and practical problem: how to calculate the area under a curve. For example, computing the cross-sectional area of a river to estimate flow rate, or calculating the area of irregular land. For shapes bounded by straight lines (triangles, rectangles), area formulas have long been known. But for regions bounded by curves, traditional geometric methods are inadequate. The key idea behind integration is partition, approximate, and take the limit: divide the irregular region into many small pieces, approximate each piece with a regular shape (such as a rectangle), then sum them up. As the partition becomes infinitely fine, the approximation approaches the exact value. This idea not only solves area problems but also extends to a broader class of "accumulation" problems: accumulating distance (from velocity to displacement), accumulating mass (from density to total mass), accumulating probability (from probability density to probability), and so on.
Integration is divided into two main categories: Definite Integral and Indefinite Integral:
The definite integral calculates the cumulative effect of a function over a specific interval. It yields a numerical value, not a function. The definite integral answers the question: "How much does the function accumulate over the interval ?"
The indefinite integral is the inverse operation of differentiation. Given a function , we seek its antiderivative such that . For example, given , its indefinite integral is (where is an arbitrary constant), because . The indefinite integral answers the question: "Which function has this function as its derivative?"
With this background, we now give the rigorous definition of the definite integral. Let be bounded on the interval . Partition the interval into subintervals. On each subinterval , choose an arbitrary point and form the sum: . As the partition becomes infinitely fine (all ), if this sum approaches a definite limit, then this limit is called the definite integral of over , denoted:
Here, is called the lower limit of integration, the upper limit of integration, the integrand, and indicates the variable of integration. This definition beautifully captures the central idea of integration: "partition, approximate, and take the limit." It also intuitively explains the geometric meaning of the definite integral: the definite integral represents the signed area between the function curve and the -axis (signed area means that when , the area is positive, and when , the area is negative). The definite integral is the algebraic sum of these signed areas.
Fundamental Theorem of Calculus
Differentiation and integration are closely linked through the Fundamental Theorem of Calculus. This theorem serves as the bridge between two important parts of calculus theory, demonstrating that differentiation and integration are inverse operations: differentiation finds rates of change, while integration finds accumulated quantities. One is "breaking apart," the other is "putting together" -- they are two sides of the same coin. This laid the foundation for subsequent developments such as differential equations and the calculus of variations. At the same time, the Fundamental Theorem greatly simplifies the computation of definite integrals, transforming them from a complex limiting process into simply finding an antiderivative and evaluating it at the endpoints. The entire Fundamental Theorem of Calculus consists of two parts:
First Fundamental Theorem (Relationship between differentiation and integration)
Let be continuous on , and define the "integral function" . Then is differentiable on , and its derivative is the integrand itself: .
This theorem tells us: the derivative of an integral is the integrand. In other words, integration is the inverse operation of differentiation. If we integrate to obtain , and then differentiate , we return to . The geometric intuition makes it easy to understand why this theorem holds: represents the area under the curve from to . When increases by a small amount , the area increases by approximately (approximated as a rectangle of width and height ). Therefore, the rate of change of the area (i.e., the derivative) is precisely the height .
Second Fundamental Theorem (Newton-Leibniz formula)
Let be continuous on , and let be any antiderivative of (i.e., ). Then: .
This formula, also known as the Newton-Leibniz formula, is one of the most famous formulas in calculus. It tells us: to compute a definite integral, we only need to find an antiderivative of the integrand and evaluate it at the endpoints. This dramatically simplifies the computation of integrals -- what originally required the complex process of "partition, approximate, and take the limit" now only requires finding an antiderivative and computing the difference.
Let us demonstrate how the Newton-Leibniz formula simplifies integral calculation with a concrete example. Suppose we want to compute . We can use either of the following two methods:
- Method 1 (partition and take the limit): Divide the interval into equal parts, each of width . Take the right endpoints and form the sum. As , the limit is :
- Method 2 (Newton-Leibniz formula): The antiderivative of is (verification: ). Therefore:
Summary
When mathematics moves from studying "how one variable affects a result" to "how multiple variables working together determine an outcome," partial derivatives provide a natural entry point: fix all other variables and observe the effect of only one variable. This dimension-reduction strategy decomposes complex multivariate problems into familiar univariate problems, embodying the scientific wisdom of simplifying complexity. Partial derivatives characterize the rate of change along coordinate axes, while the gradient assembles these scattered pieces of information into a single vector, revealing a "panoramic view" of how the function changes in all directions. The gradient points in the direction of steepest increase of the function -- this geometric property, seemingly simple, is the soul of the gradient descent algorithm in machine learning. Multivariate functions introduce another dimension of nested multivariate relationships, and the chain rule provides us with a tool for disentangling this complexity. The total rate of change equals the sum of contributions from each path -- this is a divide-and-conquer approach that machine learning has fully adopted. Neural networks are quintessential examples of deeply composite functions, and the backpropagation algorithm is essentially a systematic application of the chain rule.
Exercises
Prove that the definition of the directional derivative is equivalent to .
Reference Answer
This is essentially a problem of differentiating a composite function: let . The directional derivative is . Define the path functions and . By the multivariate chain rule:Compute the path derivatives: , . Substituting gives:
The right-hand side is precisely the dot product of the vector with the vector . The former is the gradient , and the latter is the direction vector . Therefore:
Let . Find , , and .
Reference Answer
To find : treat as a constant,
To find : treat as a constant,
Gradient:
At the point :
Let , , . Use the chain rule to find .
Reference Answer
Method 1 (chain rule):
Compute:
Therefore:
Method 2 (direct substitution for verification):
Both methods yield the same result.
Let . Compute the directional derivative of at the point in the direction .
Reference Answer
First compute the gradient:
At the point :
Directional derivative:
Explanation: The direction is perpendicular to the gradient, so the function value does not change along this direction. This is precisely the tangent direction of the contour line.
Determine the convexity of the function .
Reference Answer
Compute the first-order partial derivatives:
Compute the second-order partial derivatives:
Hessian matrix:
Compute the eigenvalues:
Solving yields: , both positive.
Conclusion: The Hessian matrix is positive definite, so the function is strictly convex.
Let . Compute and explain its significance in probability theory.
Reference Answer
This integral is the famous Gaussian integral:
Significance in probability theory:
The probability density function of the standard normal distribution isSince , the total area under the probability density function is 1, ensuring the normalization of probability.
The Gaussian integral appears widely in machine learning, for example:
- Gaussian kernel functions (RBF kernel)
- KL divergence calculations in variational inference
- Parameter estimation for Gaussian distributions
Prove: If and the Hessian matrix is positive definite at , then is a local minimum of .
Reference Answer
This is a key conclusion of the second-order sufficient condition.
Proof sketch:
- means is a critical point
- Positive definiteness of the Hessian matrix implies that near , the function can be approximated by a quadratic function:
- Since is positive definite, for any nonzero , we have
- Therefore holds for sufficiently small
- This shows is a local minimum
This conclusion has important applications in optimization algorithms: after finding a point where the gradient is zero, checking the positive definiteness of the Hessian matrix allows us to determine whether it is a local minimum, a local maximum, or a saddle point.
