Designing Machine Learning ApplicationsDesigning Machine Learning Applications
Home
Discus
Home
Discus
  • Contents
  • Preface

    • About the Author
    • About DMLA
  • Mathematical Foundations

    • Linear Algebra

      • Vector Basics
      • Matrix Basics
    • Calculus

      • Limits, Derivatives, and Differentials
      • Multivariate Functions and Composite Function Differentiation
    • Statistics and Probability

      • Probability Basics
      • Statistical Inference
  • Classical Statistical Learning

    • Linear Models

      • Linear Regression
      • Logistic Regression
      • Regularization and Generalized Linear Models
    • Bayesian Methods

      • Naive Bayes
      • Bayesian Network
      • EM Algorithm
    • Support Vector Machines

      • Support Vector Machine
      • Kernel Trick
    • Decision Trees and Ensembles

      • Decision Trees
      • Random Forest
      • Boosting
    • Unsupervised Learning

      • Clustering
      • Dimensionality Reduction
  • Neural Networks and Deep Learning

    • Neural Network Architectures

      • Fundamentals of Neural Networks
      • Linear Perceptron
      • Multi-Layer Perceptron
      • Forward Propagation
      • Backpropagation
      • Activation Functions and Loss Functions
    • Optimization

      • Gradient Descent
      • Adaptive Optimizers
    • Deep Network Stability

      • Weight Initialization
      • Dropout Regularization
      • Batch Normalization
    • Convolutional Neural Networks

      • CNN Basics
      • AlexNet and the CNN Revival
      • VGG and GoogLeNet
      • ResNet Residual Network
      • Lab: AlexNet Image Classification
    • Generative Models

      • Variational Autoencoder
      • Generative Adversarial Network
      • Lab: DCGAN Image Generation
    • Sequence Models

      • Word Embedding and Representation Learning
      • RNN Fundamentals
      • LSTM and GRU Gating Mechanisms
      • Seq2Seq Sequence Mapping
      • Lab: LSTM Poetry Generation
  • The Language Model Singularity

    • Transformer Architecture

      • Transformer Fundamentals
      • Transformer Evolution and Variants
      • Language Models and Tokenization
      • Lab: Transformer Model Training
    • Pretraining and Fine-Tuning

      • Pretraining Data Engineering
      • Scaling Laws
      • Distributed Training Infrastructure
      • Supervised Fine-Tuning
      • Lab: SFT Model Conversation
    • Alignment Training

      • Reinforcement Learning from Human Feedback
      • Evolution of Alignment Methods
      • Lab: DPO Alignment Training
    • Reasoning Capabilities

      • Chain of Thought and Reasoning Models
      • Test-Time Compute Scaling
      • Inference Efficiency Optimization
      • Lab: LLM Inference Optimization
    • Multimodal Fusion and Safety

      • Multimodal Large Language Models
      • Model Evaluation and Safety
      • Lab: VLM Training
  • AI Infrastructure and Engineering

    • Model Serving

      • Inference Service Architecture
      • Request Scheduling and Batching
      • GPU Resource Management
      • Lab: Deploying LLM Inference Service
    • MLOps Practices

      • Data Versioning
      • Experiment Tracking and Model Registry
      • Hyperparameter Optimization
      • Model Performance Monitoring
      • Drift Detection
  • Agentic Application Systems

    • Vector Retrieval and RAG

      • Embedding and Vector Retrieval
      • Retrieval Quality Evaluation and Optimization
      • Retrieval-Augmented Generation
      • Lab: Building a Knowledge Base Q&A System
    • Building Agent Applications

      • From LLM to Agent
      • Tool Use
      • Planning and Reasoning
      • Memory Systems
      • Agent Collaboration and Communication
      • Orchestration and Fault Tolerance
      • Lab: Research Agent Collaboration System
  • Appendix

    • Building the Sandbox Environment
    • NumPy Practice

      • Data Processing Practice
      • Calculus Computation Practice
      • Probability and Statistics Practice

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 nnn-variable function fff maps nnn inputs (x1,x2,…,xn)(x_1, x_2, \ldots, x_n)(x1​,x2​,…,xn​) 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 L(θ1,θ2,…,θn)L(\theta_1, \theta_2, \ldots, \theta_n)L(θ1​,θ2​,…,θn​) is a multivariate function of the model parameters; the predicted value f(x1,x2,…,xn)f(x_1, x_2, \ldots, x_n)f(x1​,x2​,…,xn​) corresponding to a feature vector (x1,x2,…,xn)(x_1, x_2, \ldots, x_n)(x1​,x2​,…,xn​), 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 z=f(x,y)z = f(x, y)z=f(x,y) be a function of two variables. The partial derivative of fff with respect to xxx at the point (x0,y0)(x_0, y_0)(x0​,y0​) is defined as the limit of the rate of change of the function value when yyy is held fixed and xxx undergoes a small change Δx\Delta xΔx:

∂f∂x=lim⁡Δx→0f(x0+Δx,y0)−f(x0,y0)Δx\frac{\partial f}{\partial x} = \lim_{\Delta x \to 0} \frac{f(x_0 + \Delta x, y_0) - f(x_0, y_0)}{\Delta x}∂x∂f​=Δx→0lim​Δxf(x0​+Δx,y0​)−f(x0​,y0​)​

Similarly, the partial derivative with respect to yyy is defined as:

∂f∂y=lim⁡Δy→0f(x0,y0+Δy)−f(x0,y0)Δy\frac{\partial f}{\partial y} = \lim_{\Delta y \to 0} \frac{f(x_0, y_0 + \Delta y) - f(x_0, y_0)}{\Delta y}∂y∂f​=Δy→0lim​Δyf(x0​,y0​+Δy)−f(x0​,y0​)​

When computing the partial derivative of f(x,y)f(x, y)f(x,y) with respect to xxx, we treat yyy as a constant and differentiate with respect to xxx 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 f(x,y)=x2y+3xy2f(x, y) = x^2 y + 3xy^2f(x,y)=x2y+3xy2. To find ∂f∂x\frac{\partial f}{\partial x}∂x∂f​, treat yyy as a constant: ∂f∂x=2xy+3y2\frac{\partial f}{\partial x} = 2xy + 3y^2∂x∂f​=2xy+3y2. To find ∂f∂y\frac{\partial f}{\partial y}∂y∂f​, treat xxx as a constant: ∂f∂y=x2+6xy\frac{\partial f}{\partial y} = x^2 + 6xy∂y∂f​=x2+6xy.

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 z=f(x,y)z = f(x, y)z=f(x,y), its graph is a surface in three-dimensional space. ∂f∂x\frac{\partial f}{\partial x}∂x∂f​ represents the "tangent slope" along the xxx direction on the surface, while ∂f∂y\frac{\partial f}{\partial y}∂y∂f​ represents the "tangent slope" along the yyy direction. More specifically, ∂f∂x(x0,y0)\frac{\partial f}{\partial x}(x_0, y_0)∂x∂f​(x0​,y0​) is the slope of the tangent line at the point (x0,y0,f(x0,y0))(x_0, y_0, f(x_0, y_0))(x0​,y0​,f(x0​,y0​)) to the curve formed by the intersection of the surface and the plane y=y0y = y_0y=y0​. This is equivalent to the tangent slope when we "fix yyy and let only xxx 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 f(x,y)f(x, y)f(x,y) be a function of two variables, and let u=(u1,u2)\mathbf{u} = (u_1, u_2)u=(u1​,u2​) be a unit vector (∥u∥=1\|\mathbf{u}\| = 1∥u∥=1). The directional derivative of fff at the point (x0,y0)(x_0, y_0)(x0​,y0​) in the direction u\mathbf{u}u is defined as:

Duf(x0,y0)=lim⁡h→0f(x0+hu1,y0+hu2)−f(x0,y0)hD_{\mathbf{u}} f(x_0, y_0) = \lim_{h \to 0} \frac{f(x_0 + h u_1, y_0 + h u_2) - f(x_0, y_0)}{h}Du​f(x0​,y0​)=h→0lim​hf(x0​+hu1​,y0​+hu2​)−f(x0​,y0​)​

Geometrically, the directional derivative represents the limit of the average rate of change of the function value when moving a small step hhh from the point (x0,y0)(x_0, y_0)(x0​,y0​) in the direction u\mathbf{u}u.

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 f(x1,x2,…,xn)f(x_1, x_2, \ldots, x_n)f(x1​,x2​,…,xn​) be a multivariate function. Its gradient is defined as:

∇f=(∂f∂x1,∂f∂x2,…,∂f∂xn)\nabla f = \left(\frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \ldots, \frac{\partial f}{\partial x_n}\right)∇f=(∂x1​∂f​,∂x2​∂f​,…,∂xn​∂f​)

where ∇\nabla∇ 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:

Duf=∇f⋅uD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}Du​f=∇f⋅u

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 θ\thetaθ be the angle between the gradient ∇f\nabla f∇f and the direction vector u\mathbf{u}u. Then Duf=∥∇f∥∥u∥cos⁡θD_{\mathbf{u}} f = \|\nabla f\| \|\mathbf{u}\| \cos\thetaDu​f=∥∇f∥∥u∥cosθ, and since ∥u∥=1\|\mathbf{u}\| = 1∥u∥=1, we have Duf=∥∇f∥cos⁡θD_{\mathbf{u}} f = \|\nabla f\| \cos\thetaDu​f=∥∇f∥cosθ. Because cos⁡θ\cos\thetacosθ attains its maximum value of 111 when θ=0\theta = 0θ=0, the directional derivative reaches its maximum value ∥∇f∥\|\nabla f\|∥∇f∥ when the direction vector u\mathbf{u}u is aligned with the gradient ∇f\nabla f∇f. In other words, the gradient direction is the direction in which the function value increases most rapidly.

As a concrete example, consider the function f(x,y)=x2+y2f(x, y) = x^2 + y^2f(x,y)=x2+y2, which resembles a bowl-shaped surface (or an inverted hill), as shown in the figure below. At the point (1,0.5)(1, 0.5)(1,0.5), the gradient is ∇f=(2x,2y)=(2,1)\nabla f = (2x, 2y) = (2, 1)∇f=(2x,2y)=(2,1), and the magnitude of the gradient is ∣∣∇f∣∣=22+12=5≈2.24||\nabla f|| = \sqrt{2^2 + 1^2} = \sqrt{5} \approx 2.24∣∣∇f∣∣=22+12​=5​≈2.24.

Gradient Example

Figure: Gradient Example

The calculation shows that moving in the gradient direction (2,1)(2, 1)(2,1) increases the function value by approximately 2.242.242.24 per unit distance (the largest among all directions). Moving in the negative gradient direction (−2,−1)(-2, -1)(−2,−1) decreases the function value by approximately 2.242.242.24 per unit distance (the "steepest descent" among all directions). Moving in a direction perpendicular to the gradient (e.g., (1,−2)(1, -2)(1,−2)) 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 L(θ)L(\theta)L(θ), where θ=(θ1,θ2,…,θn)\theta = (\theta_1, \theta_2, \ldots, \theta_n)θ=(θ1​,θ2​,…,θn​) are the model parameters. The update rule of the gradient descent algorithm is:

θt+1=θt−η∇L(θt)\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t)θt+1​=θt​−η∇L(θt​)

Here, η\etaη is the learning rate, which controls the step size; ∇L(θt)\nabla L(\theta_t)∇L(θt​) 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 f(x)f(x)f(x); they are frequently the result of nesting multiple functions. For instance, consider an airplane climbing upward: its position x(t)x(t)x(t) changes over time, while the temperature T(x)T(x)T(x) varies with altitude. The temperature around the airplane T(t)=T(x(t))T(t) = T(x(t))T(t)=T(x(t)) is a composite function: time ttt first affects the position xxx, which in turn affects the temperature TTT. If we want to know the "rate of change of temperature with respect to time" dTdt\frac{dT}{dt}dtdT​, we cannot differentiate T(t)T(t)T(t) directly -- we must decompose it layer by layer: first, how temperature changes with position dTdx\frac{dT}{dx}dxdT​, then how position changes with time dxdt\frac{dx}{dt}dtdx​. This requires the chain rule.

A composite function is a function whose output serves as the input to another function. Let u=g(x)u = g(x)u=g(x) and y=f(u)y = f(u)y=f(u). Then y=f(g(x))y = f(g(x))y=f(g(x)) is called the composite function of fff and ggg, denoted f∘gf \circ gf∘g. Composite functions "chain together" multiple simple functions to form complex functional relationships. For example, y=sin⁡(x2)y = \sin(x^2)y=sin(x2) is composed of u=x2u = x^2u=x2 and y=sin⁡(u)y = \sin(u)y=sin(u); y=ex2+1y = e^{x^2 + 1}y=ex2+1 is composed of u=x2+1u = x^2 + 1u=x2+1 and y=euy = e^uy=eu. 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 yyy with respect to xxx equals the rate of change of yyy with respect to the intermediate variable uuu, multiplied by the rate of change of uuu with respect to xxx. This is like a chain reaction: a change in xxx first affects uuu, which then affects yyy through uuu. For example, let y=f(u)y = f(u)y=f(u) and u=g(x)u = g(x)u=g(x); then y=f(g(x))y = f(g(x))y=f(g(x)) is a composite function of xxx, and its derivative is: dydx=dydu⋅dudx\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx}dxdy​=dudy​⋅dxdu​. In function notation: (f∘g)′(x)=f′(g(x))⋅g′(x)(f \circ g)'(x) = f'(g(x)) \cdot g'(x)(f∘g)′(x)=f′(g(x))⋅g′(x).

As a concrete example, suppose y=sin⁡(x2)y = \sin(x^2)y=sin(x2) and we want to find dydx\frac{dy}{dx}dxdy​. First, let u=x2u = x^2u=x2, so y=sin⁡uy = \sin uy=sinu. By the chain rule: $dydx=dydu⋅dudx=cos⁡u⋅2x=2xcos⁡(x2)\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx} = \cos u \cdot 2x = 2x \cos(x^2)dxdy​=dudy​⋅dxdu​=cosu⋅2x=2xcos(x2)$

Combining multivariate functions with composite functions yields a more general form of the chain rule. Let z=f(x,y)z = f(x, y)z=f(x,y), where x=x(t)x = x(t)x=x(t) and y=y(t)y = y(t)y=y(t). Then zzz becomes a function of ttt through xxx and yyy: z=f(x(t),y(t))z = f(x(t), y(t))z=f(x(t),y(t)). In this case, dzdt=∂f∂x⋅dxdt+∂f∂y⋅dydt\frac{dz}{dt} = \frac{\partial f}{\partial x} \cdot \frac{dx}{dt} + \frac{\partial f}{\partial y} \cdot \frac{dy}{dt}dtdz​=∂x∂f​⋅dtdx​+∂y∂f​⋅dtdy​. 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 [a,b][a, b][a,b]?"

  • The indefinite integral is the inverse operation of differentiation. Given a function f(x)f(x)f(x), we seek its antiderivative F(x)F(x)F(x) such that F′(x)=f(x)F'(x) = f(x)F′(x)=f(x). For example, given f(x)=2xf(x) = 2xf(x)=2x, its indefinite integral is F(x)=x2+CF(x) = x^2 + CF(x)=x2+C (where CCC is an arbitrary constant), because (x2+C)′=2x(x^2 + C)' = 2x(x2+C)′=2x. 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 f(x)f(x)f(x) be bounded on the interval [a,b][a, b][a,b]. Partition the interval into nnn subintervals. On each subinterval [xi−1,xi][x_{i-1}, x_i][xi−1​,xi​], choose an arbitrary point ξi\xi_iξi​ and form the sum: ∑i=1nf(ξi)Δxi\sum_{i=1}^{n} f(\xi_i) \Delta x_i∑i=1n​f(ξi​)Δxi​. As the partition becomes infinitely fine (all Δxi→0\Delta x_i \to 0Δxi​→0), if this sum approaches a definite limit, then this limit is called the definite integral of f(x)f(x)f(x) over [a,b][a, b][a,b], denoted:

∫abf(x) dx\int_a^b f(x) \, dx∫ab​f(x)dx

Here, aaa is called the lower limit of integration, bbb the upper limit of integration, f(x)f(x)f(x) the integrand, and dxdxdx 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 xxx-axis (signed area means that when f(x)>0f(x) > 0f(x)>0, the area is positive, and when f(x)<0f(x) < 0f(x)<0, 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 f(x)f(x)f(x) be continuous on [a,b][a, b][a,b], and define the "integral function" F(x)=∫axf(t) dtF(x) = \int_a^x f(t) \, dtF(x)=∫ax​f(t)dt. Then F(x)F(x)F(x) is differentiable on [a,b][a, b][a,b], and its derivative is the integrand itself: F′(x)=f(x)F'(x) = f(x)F′(x)=f(x).

    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 fff to obtain FFF, and then differentiate FFF, we return to fff. The geometric intuition makes it easy to understand why this theorem holds: F(x)=∫axf(t) dtF(x) = \int_a^x f(t) \, dtF(x)=∫ax​f(t)dt represents the area under the curve y=f(t)y = f(t)y=f(t) from aaa to xxx. When xxx increases by a small amount Δx\Delta xΔx, the area increases by approximately f(x)⋅Δxf(x) \cdot \Delta xf(x)⋅Δx (approximated as a rectangle of width Δx\Delta xΔx and height f(x)f(x)f(x)). Therefore, the rate of change of the area (i.e., the derivative) is precisely the height f(x)f(x)f(x).

  • Second Fundamental Theorem (Newton-Leibniz formula)

    Let f(x)f(x)f(x) be continuous on [a,b][a, b][a,b], and let G(x)G(x)G(x) be any antiderivative of f(x)f(x)f(x) (i.e., G′(x)=f(x)G'(x) = f(x)G′(x)=f(x)). Then: ∫abf(x) dx=G(b)−G(a)\int_a^b f(x) \, dx = G(b) - G(a)∫ab​f(x)dx=G(b)−G(a).

    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 ∫012x dx\int_0^1 2x \, dx∫01​2xdx. We can use either of the following two methods:

  • Method 1 (partition and take the limit): Divide the interval into nnn equal parts, each of width Δx=1/n\Delta x = 1/nΔx=1/n. Take the right endpoints and form the sum. As n→∞n \to \inftyn→∞, the limit is 111:
∑i=1nf(xi)Δx=∑i=1n2in⋅1n=2n2∑i=1ni=2n2⋅n(n+1)2=n+1n\sum_{i=1}^{n} f(x_i) \Delta x = \sum_{i=1}^{n} \frac{2i}{n} \cdot \frac{1}{n} = \frac{2}{n^2} \sum_{i=1}^{n} i = \frac{2}{n^2} \cdot \frac{n(n+1)}{2} = \frac{n+1}{n}i=1∑n​f(xi​)Δx=i=1∑n​n2i​⋅n1​=n22​i=1∑n​i=n22​⋅2n(n+1)​=nn+1​
  • Method 2 (Newton-Leibniz formula): The antiderivative of f(x)=2xf(x) = 2xf(x)=2x is G(x)=x2G(x) = x^2G(x)=x2 (verification: (x2)′=2x(x^2)' = 2x(x2)′=2x). Therefore:
∫012x dx=G(1)−G(0)=12−02=1\int_0^1 2x \, dx = G(1) - G(0) = 1^2 - 0^2 = 1∫01​2xdx=G(1)−G(0)=12−02=1

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

  1. Prove that the definition of the directional derivative Duf(x0,y0)=lim⁡h→0f(x0+hu1,y0+hu2)−f(x0,y0)hD_{\mathbf{u}} f(x_0, y_0) = \lim_{h \to 0} \frac{f(x_0 + h u_1, y_0 + h u_2) - f(x_0, y_0)}{h}Du​f(x0​,y0​)=limh→0​hf(x0​+hu1​,y0​+hu2​)−f(x0​,y0​)​ is equivalent to Duf=∇f⋅uD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}Du​f=∇f⋅u.

    Reference Answer This is essentially a problem of differentiating a composite function: let g(h)=f(x0+hu1,y0+hu2)g(h) = f(x_0 + h u_1, y_0 + h u_2)g(h)=f(x0​+hu1​,y0​+hu2​). The directional derivative is g′(0)g'(0)g′(0). Define the path functions x(h)=x0+hu1x(h) = x_0 + h u_1x(h)=x0​+hu1​ and y(h)=y0+hu2y(h) = y_0 + h u_2y(h)=y0​+hu2​. By the multivariate chain rule:
    dgdh=∂f∂x⋅dxdh+∂f∂y⋅dydh\frac{dg}{dh} = \frac{\partial f}{\partial x} \cdot \frac{dx}{dh} + \frac{\partial f}{\partial y} \cdot \frac{dy}{dh}dhdg​=∂x∂f​⋅dhdx​+∂y∂f​⋅dhdy​

    Compute the path derivatives: dxdh=u1\frac{dx}{dh} = u_1dhdx​=u1​, dydh=u2\frac{dy}{dh} = u_2dhdy​=u2​. Substituting gives:

    Duf=∂f∂x⋅u1+∂f∂y⋅u2D_{\mathbf{u}} f = \frac{\partial f}{\partial x} \cdot u_1 + \frac{\partial f}{\partial y} \cdot u_2Du​f=∂x∂f​⋅u1​+∂y∂f​⋅u2​

    The right-hand side is precisely the dot product of the vector (∂f∂x,∂f∂y)(\frac{\partial f}{\partial x}, \frac{\partial f}{\partial y})(∂x∂f​,∂y∂f​) with the vector (u1,u2)(u_1, u_2)(u1​,u2​). The former is the gradient ∇f\nabla f∇f, and the latter is the direction vector u\mathbf{u}u. Therefore:

    Duf=∇f⋅uD_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}Du​f=∇f⋅u
  2. Let f(x,y)=x2y+y3f(x, y) = x^2 y + y^3f(x,y)=x2y+y3. Find ∂f∂x\frac{\partial f}{\partial x}∂x∂f​, ∂f∂y\frac{\partial f}{\partial y}∂y∂f​, and ∇f\nabla f∇f.

    Reference Answer

    To find ∂f∂x\frac{\partial f}{\partial x}∂x∂f​: treat yyy as a constant, ∂f∂x=2xy\frac{\partial f}{\partial x} = 2xy∂x∂f​=2xy

    To find ∂f∂y\frac{\partial f}{\partial y}∂y∂f​: treat xxx as a constant, ∂f∂y=x2+3y2\frac{\partial f}{\partial y} = x^2 + 3y^2∂y∂f​=x2+3y2

    Gradient: ∇f=(2xy,x2+3y2)\nabla f = (2xy, x^2 + 3y^2)∇f=(2xy,x2+3y2)

    At the point (1,2)(1, 2)(1,2): ∇f(1,2)=(4,13)\nabla f(1, 2) = (4, 13)∇f(1,2)=(4,13)

  3. Let z=x2+y2z = x^2 + y^2z=x2+y2, x=t+1x = t + 1x=t+1, y=t2y = t^2y=t2. Use the chain rule to find dzdt\frac{dz}{dt}dtdz​.

    Reference Answer

    Method 1 (chain rule):

    dzdt=∂z∂x⋅dxdt+∂z∂y⋅dydt\frac{dz}{dt} = \frac{\partial z}{\partial x} \cdot \frac{dx}{dt} + \frac{\partial z}{\partial y} \cdot \frac{dy}{dt}dtdz​=∂x∂z​⋅dtdx​+∂y∂z​⋅dtdy​

    Compute:

    • ∂z∂x=2x\frac{\partial z}{\partial x} = 2x∂x∂z​=2x
    • ∂z∂y=2y\frac{\partial z}{\partial y} = 2y∂y∂z​=2y
    • dxdt=1\frac{dx}{dt} = 1dtdx​=1
    • dydt=2t\frac{dy}{dt} = 2tdtdy​=2t

    Therefore: dzdt=2x⋅1+2y⋅2t=2(t+1)+2t2⋅2t=2t+2+4t3\frac{dz}{dt} = 2x \cdot 1 + 2y \cdot 2t = 2(t+1) + 2t^2 \cdot 2t = 2t + 2 + 4t^3dtdz​=2x⋅1+2y⋅2t=2(t+1)+2t2⋅2t=2t+2+4t3

    Method 2 (direct substitution for verification):
    z=(t+1)2+t4=t2+2t+1+t4z = (t+1)^2 + t^4 = t^2 + 2t + 1 + t^4z=(t+1)2+t4=t2+2t+1+t4
    dzdt=2t+2+4t3\frac{dz}{dt} = 2t + 2 + 4t^3dtdz​=2t+2+4t3

    Both methods yield the same result.

  4. Let f(x,y)=x2−y2f(x, y) = x^2 - y^2f(x,y)=x2−y2. Compute the directional derivative of fff at the point (1,1)(1, 1)(1,1) in the direction u=(12,12)\mathbf{u} = (\frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}})u=(2​1​,2​1​).

    Reference Answer

    First compute the gradient: ∇f=(2x,−2y)\nabla f = (2x, -2y)∇f=(2x,−2y)

    At the point (1,1)(1, 1)(1,1): ∇f(1,1)=(2,−2)\nabla f(1, 1) = (2, -2)∇f(1,1)=(2,−2)

    Directional derivative: Duf=∇f⋅u=(2,−2)⋅(12,12)=22−22=0D_{\mathbf{u}} f = \nabla f \cdot \mathbf{u} = (2, -2) \cdot (\frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}}) = \frac{2}{\sqrt{2}} - \frac{2}{\sqrt{2}} = 0Du​f=∇f⋅u=(2,−2)⋅(2​1​,2​1​)=2​2​−2​2​=0

    Explanation: The direction u\mathbf{u}u 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.

  5. Determine the convexity of the function f(x,y)=x2+2y2+2xyf(x, y) = x^2 + 2y^2 + 2xyf(x,y)=x2+2y2+2xy.

    Reference Answer

    Compute the first-order partial derivatives:

    • ∂f∂x=2x+2y\frac{\partial f}{\partial x} = 2x + 2y∂x∂f​=2x+2y
    • ∂f∂y=4y+2x\frac{\partial f}{\partial y} = 4y + 2x∂y∂f​=4y+2x

    Compute the second-order partial derivatives:

    • ∂2f∂x2=2\frac{\partial^2 f}{\partial x^2} = 2∂x2∂2f​=2
    • ∂2f∂y2=4\frac{\partial^2 f}{\partial y^2} = 4∂y2∂2f​=4
    • ∂2f∂x∂y=2\frac{\partial^2 f}{\partial x \partial y} = 2∂x∂y∂2f​=2
    • ∂2f∂y∂x=2\frac{\partial^2 f}{\partial y \partial x} = 2∂y∂x∂2f​=2

    Hessian matrix: H=[2224]\mathbf{H} = \begin{bmatrix} 2 & 2 \\ 2 & 4 \end{bmatrix}H=[22​24​]

    Compute the eigenvalues:
    det⁡(H−λI)=∣2−λ224−λ∣=(2−λ)(4−λ)−4=λ2−6λ+4=0\det(\mathbf{H} - \lambda \mathbf{I}) = \begin{vmatrix} 2-\lambda & 2 \\ 2 & 4-\lambda \end{vmatrix} = (2-\lambda)(4-\lambda) - 4 = \lambda^2 - 6\lambda + 4 = 0det(H−λI)=​2−λ2​24−λ​​=(2−λ)(4−λ)−4=λ2−6λ+4=0

    Solving yields: λ=3±5\lambda = 3 \pm \sqrt{5}λ=3±5​, both positive.

    Conclusion: The Hessian matrix is positive definite, so the function is strictly convex.

  6. Let f(x)=e−x2f(x) = e^{-x^2}f(x)=e−x2. Compute ∫−∞∞f(x) dx\int_{-\infty}^{\infty} f(x) \, dx∫−∞∞​f(x)dx and explain its significance in probability theory.

    Reference Answer

    This integral is the famous Gaussian integral: ∫−∞∞e−x2 dx=π\int_{-\infty}^{\infty} e^{-x^2} \, dx = \sqrt{\pi}∫−∞∞​e−x2dx=π​

    Significance in probability theory:
    The probability density function of the standard normal distribution is ϕ(x)=12πe−x2/2\phi(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2}ϕ(x)=2π​1​e−x2/2

    Since ∫−∞∞ϕ(x) dx=1\int_{-\infty}^{\infty} \phi(x) \, dx = 1∫−∞∞​ϕ(x)dx=1, 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
  7. Prove: If ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0}∇f(x∗)=0 and the Hessian matrix H\mathbf{H}H is positive definite at x∗\mathbf{x}^*x∗, then x∗\mathbf{x}^*x∗ is a local minimum of fff.

    Reference Answer

    This is a key conclusion of the second-order sufficient condition.

    Proof sketch:

    1. ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0}∇f(x∗)=0 means x∗\mathbf{x}^*x∗ is a critical point
    2. Positive definiteness of the Hessian matrix implies that near x∗\mathbf{x}^*x∗, the function can be approximated by a quadratic function: f(x∗+h)≈f(x∗)+12hTHhf(\mathbf{x}^* + \mathbf{h}) \approx f(\mathbf{x}^*) + \frac{1}{2}\mathbf{h}^T \mathbf{H} \mathbf{h}f(x∗+h)≈f(x∗)+21​hTHh
    3. Since H\mathbf{H}H is positive definite, for any nonzero h\mathbf{h}h, we have hTHh>0\mathbf{h}^T \mathbf{H} \mathbf{h} > 0hTHh>0
    4. Therefore f(x∗+h)>f(x∗)f(\mathbf{x}^* + \mathbf{h}) > f(\mathbf{x}^*)f(x∗+h)>f(x∗) holds for sufficiently small h\mathbf{h}h
    5. This shows x∗\mathbf{x}^*x∗ 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.

Words: 3,860
Updated 2026-07-28
Last Updated:
Contributors: icyfenix, Claude
Prev
Limits, Derivatives, and Differentials
Next
Probability Basics