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

Limits, Derivatives, and Differentials

If linear algebra is the "data language" of machine learning, telling computers how to represent and organize data, then calculus is the "optimization behavior" of machine learning, telling computers how to learn and improve from data. From gradient descent in deep learning to motion simulation in physics engines, calculus is ubiquitous in modern computing technology and serves as a key bridge connecting traditional software development with artificial intelligence.

From Practical Problems to Mathematical Theory

The birth of calculus is one of the most exciting chapters in the history of mathematics. In the 17th century, the wave of the Scientific Revolution swept across Europe. The development of physics and astronomy raised numerous questions about motion and change: How do planets move around the sun? What trajectory does a cannonball follow? How do we define the instantaneous velocity of a moving object at a given moment? These questions troubled the greatest scientists of the time, because traditional mathematical tools could only handle "static" quantities and could not precisely describe "dynamic" changes.

In the 1660s, the English scientist Isaac Newton, while studying the motion of objects and planetary orbits, developed a mathematical method called the Method of Fluxions. Using this method, he successfully calculated instantaneous velocities of non-uniform motion, the slopes of tangent lines to curves, and the curvature of planetary orbits. Almost simultaneously, the German mathematician Gottfried Leibniz, while studying tangent lines and area problems for curves, independently developed a similar system of notation and methods.

The contribution of Newton and Leibniz lies in their unification of two seemingly unrelated types of problems -- finding tangents (differential problems) and finding areas (integral problems) -- under a single framework, and discovering the inverse relationship between them. This is the famous Fundamental Theorem of Calculus. This theorem reveals that differentiation and integration are inverse operations, much like the relationship between multiplication and division, or exponentiation and logarithms.

Although calculus was quickly applied to physics, astronomy, engineering, and other fields with great success after its birth, its theoretical foundation had long-standing gaps. The expositions of Newton and Leibniz were filled with the concept of infinitesimals -- quantities that are not zero yet close to zero. This concept was logically self-contradictory: if it is not zero, it can be further divided; if it is zero, it cannot serve as a denominator. This ambiguity drew sharp criticism. In 1734, The Analyst satirized that such reasoning was "neither correct mathematics nor reliable logic."

The rigorous formalization of calculus took nearly two hundred years of effort. In the early 19th century, the French mathematician Augustin-Louis Cauchy gave a rigorous definition of limits, placing calculus on the foundation of limits rather than vague infinitesimals. In the late 19th century, the German mathematician Karl Weierstrass further refined the definition of limits using the ε−δ\varepsilon-\deltaε−δ language, ultimately placing calculus on a solid logical foundation. Interestingly, in the 1960s, the American mathematician Abraham Robinson, through Non-standard Analysis, re-endowed infinitesimals with rigorous mathematical meaning -- but that is another story.

Calculus in Machine Learning

For software developers accustomed to object-oriented programming, design patterns, and microservice architectures, calculus may seem like a somewhat "out-of-place" abstract discipline. After all, in most software development work, we are more accustomed to discrete ways of thinking: data is discrete (integers, strings, booleans), operations are discrete (assignment, conditionals, loops), and state spaces are discrete (finite or countably infinite sets of states). This reflects the nature of computers -- a Turing Machine is a discrete state machine, and digital computers represent everything with finite bits. However, when software development enters the field of machine learning and artificial intelligence, the situation is entirely different. A key problem in machine learning is optimization -- finding optimal model parameters from vast amounts of data. This problem is continuous: the parameter space is continuous (the real numbers), the loss function is a continuous function, and the optimization process involves finding extrema in a continuous space. In this context, writing machine learning code requires the language of calculus to describe and solve problems. The core concept of calculus is the rate of change. The derivative captures how sensitively one quantity changes with respect to another -- when the input changes by a tiny amount, how much does the output change? This concept is ubiquitous in machine learning:

  • The derivative of the loss function with respect to a parameter tells us: if we increase the parameter slightly, will the loss increase or decrease? By how much? This guides how we adjust parameters.
  • The derivative of the activation function determines how gradients flow during backpropagation, affecting the training effectiveness of the network.
  • The learning rate is essentially a step-size parameter that controls the magnitude of each parameter update -- too large and we may overshoot the optimum, too small and convergence is too slow.
  • ...

Understanding these concepts not only helps us correctly use machine learning frameworks (such as PyTorch, TensorFlow) but also enables us to diagnose training problems, design better model architectures, and choose appropriate optimization strategies.

Rigorous Definition of Limits and Continuity

Before formally approaching derivatives, let us first understand the concept of limits through the intuitive approach used in high school curricula. Limits are the prelude to calculus theory, describing the trend of a function's value as the independent variable approaches some value. Consider the simple function f(x)=x2−1x−1f(x) = \frac{x^2 - 1}{x - 1}f(x)=x−1x2−1​. When x=1x = 1x=1, the denominator is zero and the function value is undefined. However, if we observe how the function values change as xxx approaches 1:

xxxf(x)f(x)f(x)
0.91.9
0.991.99
0.9991.999
1.0012.001
1.012.01
1.12.1

We can see that as xxx approaches 1 from both sides, f(x)f(x)f(x) approaches 2. This is the intuitive meaning of a limit: as xxx gets arbitrarily close to some value aaa, the function value f(x)f(x)f(x) gets arbitrarily close to some value LLL, denoted as lim⁡x→af(x)=L\lim_{x \to a} f(x) = Llimx→a​f(x)=L. Note that the limit concerns the process of xxx "approaching" aaa, not the function value when xxx equals aaa. In the example above, f(1)f(1)f(1) does not even exist, but lim⁡x→1f(x)=2\lim_{x \to 1} f(x) = 2limx→1​f(x)=2 is fully determined.

While the intuitive understanding of limits in high school curricula helps build conceptual understanding, mathematics requires rigor. In the 19th century, the German mathematician Karl Weierstrass gave the rigorous definition of limits: Let a function fff be defined on some punctured neighborhood of a point aaa (a neighborhood that does not include the point aaa itself). If there exists a constant LLL such that for any arbitrarily small positive number ε\varepsilonε, there exists a positive number δ\deltaδ such that whenever 0<∣x−a∣<δ0 < |x - a| < \delta0<∣x−a∣<δ, we have ∣f(x)−L∣<ε|f(x) - L| < \varepsilon∣f(x)−L∣<ε, then LLL is called the limit of the function f(x)f(x)f(x) as x→ax \to ax→a. This is what is now called the ε−δ\varepsilon-\deltaε−δ language, which uses two inequalities to precisely characterize what "arbitrarily close" means mathematically:

  • ∣x−a∣<δ|x - a| < \delta∣x−a∣<δ means the distance between xxx and aaa is less than δ\deltaδ (xxx is sufficiently close to aaa)
  • ∣f(x)−L∣<ε|f(x) - L| < \varepsilon∣f(x)−L∣<ε means the distance between f(x)f(x)f(x) and LLL is less than ε\varepsilonε (f(x)f(x)f(x) is sufficiently close to LLL)

The picture described by the ε−δ\varepsilon-\deltaε−δ language is: no matter how close you want f(x)f(x)f(x) to be to LLL (given ε\varepsilonε), I can find a range where xxx is sufficiently close to aaa (determine δ\deltaδ) such that within this range f(x)f(x)f(x) achieves the desired closeness. For readers not specializing in mathematics, understanding the logical structure depicted by ε−δ\varepsilon-\deltaε−δ is more important than memorizing specific proofs. In practical applications, we mainly rely on intuitive understanding of limits and operational rules to solve problems.

Yes, limits also have operational rules. These rules tell us that limit operations can "penetrate" addition, subtraction, multiplication, and division -- we can take the limit of each part separately and then perform the corresponding operation. Let lim⁡x→af(x)=A\lim_{x \to a} f(x) = Alimx→a​f(x)=A, lim⁡x→ag(x)=B\lim_{x \to a} g(x) = Blimx→a​g(x)=B, then the following operational rules hold:

  • Addition: lim⁡x→a[f(x)+g(x)]=A+B\lim_{x \to a} [f(x) + g(x)] = A + Blimx→a​[f(x)+g(x)]=A+B
  • Subtraction: lim⁡x→a[f(x)−g(x)]=A−B\lim_{x \to a} [f(x) - g(x)] = A - Blimx→a​[f(x)−g(x)]=A−B
  • Multiplication: lim⁡x→a[f(x)⋅g(x)]=A⋅B\lim_{x \to a} [f(x) \cdot g(x)] = A \cdot Blimx→a​[f(x)⋅g(x)]=A⋅B
  • Division: lim⁡x→af(x)g(x)=AB\lim_{x \to a} \frac{f(x)}{g(x)} = \frac{A}{B}limx→a​g(x)f(x)​=BA​ (when B≠0B \neq 0B=0)

Intuitively, "continuity" means the graph of a function can be drawn without lifting the pen. But only after rigorously clarifying limits can we precisely define continuity. Continuity is a mathematical concept describing that a function has "no breaks." Its mathematical definition states that a function fff is continuous at point aaa if and only if the following three conditions are satisfied:

  1. f(a)f(a)f(a) is defined
  2. lim⁡x→af(x)\lim_{x \to a} f(x)limx→a​f(x) exists
  3. lim⁡x→af(x)=f(a)\lim_{x \to a} f(x) = f(a)limx→a​f(x)=f(a)

The third condition unifies the limit value with the function value: "the limit equals the function value" is precisely what "continuity" truly means. Continuous functions have many useful properties. For example, the Intermediate Value Theorem tells us: if a continuous function fff takes values f(a)f(a)f(a) and f(b)f(b)f(b) on the interval [a,b][a, b][a,b], then for any value ccc between f(a)f(a)f(a) and f(b)f(b)f(b), there exists x∈(a,b)x \in (a, b)x∈(a,b) such that f(x)=cf(x) = cf(x)=c. This theorem is often used in numerical computation for finding roots of equations (such as the bisection method).

Definition and Geometric Meaning of Derivatives

We begin with the physical problem that Newton considered to introduce derivatives. Suppose an object moves along a straight line, and its position sss is a function of time ttt, s=s(t)s = s(t)s=s(t). Over the time interval [t0,t0+Δt][t_0, t_0 + \Delta t][t0​,t0​+Δt], the distance traveled by the object is s(t0+Δt)−s(t0)s(t_0 + \Delta t) - s(t_0)s(t0​+Δt)−s(t0​). The average velocity can then be expressed as:

vˉ=s(t0+Δt)−s(t0)Δt\bar{v} = \frac{s(t_0 + \Delta t) - s(t_0)}{\Delta t}vˉ=Δts(t0​+Δt)−s(t0​)​

This is the concept of the average rate of change: the change in the function value divided by the change in the independent variable. But how should we define the instantaneous velocity of the object at a specific instant t0t_0t0​? Intuitively, if we let the time interval Δt\Delta tΔt become smaller and smaller, the average velocity gets closer and closer to the instantaneous velocity. As Δt\Delta tΔt approaches zero, the limit of the average velocity is the instantaneous velocity:

v(t0)=lim⁡Δt→0s(t0+Δt)−s(t0)Δtv(t_0) = \lim_{\Delta t \to 0} \frac{s(t_0 + \Delta t) - s(t_0)}{\Delta t}v(t0​)=Δt→0lim​Δts(t0​+Δt)−s(t0​)​

More generally, suppose a function y=f(x)y = f(x)y=f(x) is defined on some neighborhood of a point x0x_0x0​. If the limit lim⁡Δx→0f(x0+Δx)−f(x0)Δx\lim_{\Delta x \to 0} \frac{f(x_0 + \Delta x) - f(x_0)}{\Delta x}limΔx→0​Δxf(x0​+Δx)−f(x0​)​ exists, then the function fff is said to be differentiable at x0x_0x0​, and this limit value is called the derivative of fff at x0x_0x0​, denoted by f′(x0)f'(x_0)f′(x0​) or dfdx∣x=x0\frac{df}{dx}\bigg|_{x=x_0}dxdf​​x=x0​​ (the former is Lagrange's notation, the latter is Leibniz's notation; both are still widely used today). The fraction f(x0+Δx)−f(x0)Δx\frac{f(x_0 + \Delta x) - f(x_0)}{\Delta x}Δxf(x0​+Δx)−f(x0​)​ in this definition is called the difference quotient, representing the average rate of change of the function over the interval [x0,x0+Δx][x_0, x_0 + \Delta x][x0​,x0​+Δx]. The derivative is the limit of the difference quotient as Δx→0\Delta x \to 0Δx→0, i.e., the instantaneous rate of change. Another equivalent definition of the derivative is:

f′(x0)=lim⁡x→x0f(x)−f(x0)x−x0f'(x_0) = \lim_{x \to x_0} \frac{f(x) - f(x_0)}{x - x_0}f′(x0​)=x→x0​lim​x−x0​f(x)−f(x0​)​

These two definitions are equivalent; simply let x=x0+Δxx = x_0 + \Delta xx=x0​+Δx to convert between them.

The derivative has a very intuitive geometric meaning: the slope of the tangent line. Consider the graph of the function y=f(x)y = f(x)y=f(x). At the point (x0,f(x0))(x_0, f(x_0))(x0​,f(x0​)), draw a tangent line. The slope of this tangent line is f′(x0)f'(x_0)f′(x0​). First, consider a secant line passing through the two points (x0,f(x0))(x_0, f(x_0))(x0​,f(x0​)) and (x0+Δx,f(x0+Δx))(x_0 + \Delta x, f(x_0 + \Delta x))(x0​+Δx,f(x0​+Δx)). The slope of the secant line is:

Secant slope=f(x0+Δx)−f(x0)Δx\text{Secant slope} = \frac{f(x_0 + \Delta x) - f(x_0)}{\Delta x}Secant slope=Δxf(x0​+Δx)−f(x0​)​

This is exactly the difference quotient. As Δx→0\Delta x \to 0Δx→0, the point (x0+Δx,f(x0+Δx))(x_0 + \Delta x, f(x_0 + \Delta x))(x0​+Δx,f(x0​+Δx)) approaches (x0,f(x0))(x_0, f(x_0))(x0​,f(x0​)) along the curve. Observe the figure below: as the two points through which the secant passes gradually get closer, the secant approaches the tangent, eventually coinciding with it. Therefore, the derivative f′(x0)f'(x_0)f′(x0​) is the slope of the tangent line.

Tangent and secant lines of a function

Figure: The process of a secant line gradually approaching a tangent line

Derivatives of Common Functions

Mastering the derivative formulas of basic functions is the foundation of differential calculus. Just as learning arithmetic requires memorizing the multiplication table first, being proficient in basic formulas greatly simplifies the differentiation process. This section introduces the derivative formulas for power functions, exponential functions, logarithmic functions, and trigonometric functions, as well as the rules for differentiating sums, differences, products, and quotients of functions. These formulas, combined with the operational rules, enable us to handle differentiation problems for most common functions. It is particularly noteworthy that these formulas appear frequently in machine learning. For instance, the property that the derivative of exe^xex equals itself makes it central to probability distributions (such as Softmax), while the derivative of ln⁡x\ln xlnx is indispensable in maximum likelihood estimation.

  • Power Functions

    For the power function f(x)=xnf(x) = x^nf(x)=xn (where nnn is a positive integer), its derivative is: ddxxn=nxn−1\frac{d}{dx} x^n = nx^{n-1}dxd​xn=nxn−1. This formula can be generalized to any real number nnn, for example:

    • f(x)=x1/2=xf(x) = x^{1/2} = \sqrt{x}f(x)=x1/2=x​, then f′(x)=12x−1/2=12xf'(x) = \frac{1}{2}x^{-1/2} = \frac{1}{2\sqrt{x}}f′(x)=21​x−1/2=2x​1​
    • f(x)=x−1=1xf(x) = x^{-1} = \frac{1}{x}f(x)=x−1=x1​, then f′(x)=−x−2=−1x2f'(x) = -x^{-2} = -\frac{1}{x^2}f′(x)=−x−2=−x21​
  • Exponential and Logarithmic Functions

    For the natural exponential function f(x)=exf(x) = e^xf(x)=ex: ddxex=ex\frac{d}{dx} e^x = e^xdxd​ex=ex. This is a very special property: the derivative of exe^xex is itself. This property gives exe^xex a central role in differential equations, probability theory, and other fields.

    For the general exponential function f(x)=axf(x) = a^xf(x)=ax (where a>0,a≠1a > 0, a \neq 1a>0,a=1): ddxax=axln⁡a\frac{d}{dx} a^x = a^x \ln adxd​ax=axlna

    For the natural logarithmic function f(x)=ln⁡xf(x) = \ln xf(x)=lnx: ddxln⁡x=1x\frac{d}{dx} \ln x = \frac{1}{x}dxd​lnx=x1​

    For the general logarithmic function f(x)=log⁡axf(x) = \log_a xf(x)=loga​x (where a>0,a≠1a > 0, a \neq 1a>0,a=1): ddxlog⁡ax=1xln⁡a\frac{d}{dx} \log_a x = \frac{1}{x \ln a}dxd​loga​x=xlna1​

  • Trigonometric Functions

    Derivatives of basic trigonometric functions:

    FunctionDerivative
    sin⁡x\sin xsinxcos⁡x\cos xcosx
    cos⁡x\cos xcosx−sin⁡x-\sin x−sinx
    tan⁡x\tan xtanxsec⁡2x=1cos⁡2x\sec^2 x = \frac{1}{\cos^2 x}sec2x=cos2x1​

    Note that the derivatives of sine and cosine form a cycle: (sin⁡x)′=cos⁡x(\sin x)' = \cos x(sinx)′=cosx, (cos⁡x)′=−sin⁡x(\cos x)' = -\sin x(cosx)′=−sinx, and differentiating twice more returns to sin⁡x\sin xsinx. This property is very useful when solving differential equations.

Similar to the operational rules for limits, there are corresponding derivative rules for addition, subtraction, multiplication, and division of functions:

  • Sum Rule: (f+g)′=f′+g′(f + g)' = f' + g'(f+g)′=f′+g′

  • Difference Rule: (f−g)′=f′−g′(f - g)' = f' - g'(f−g)′=f′−g′

  • Product Rule: (f⋅g)′=f′⋅g+f⋅g′(f \cdot g)' = f' \cdot g + f \cdot g'(f⋅g)′=f′⋅g+f⋅g′

  • Quotient Rule: (fg)′=f′⋅g−f⋅g′g2\left(\frac{f}{g}\right)' = \frac{f' \cdot g - f \cdot g'}{g^2}(gf​)′=g2f′⋅g−f⋅g′​

Differentials and Linear Approximation

A differential can be understood as another way of expressing the derivative. Suppose a function y=f(x)y = f(x)y=f(x) is differentiable at a point xxx. Then dy=f′(x)dxdy = f'(x) dxdy=f′(x)dx is called the differential of the function y=f(x)y = f(x)y=f(x) at xxx. Here, dxdxdx is the increment of the independent variable (an independent quantity), and dydydy is the differential of the dependent variable. The difference between the differential and the derivative is that the derivative is a ratio dydx\frac{dy}{dx}dxdy​, while the differential dydydy and dxdxdx are independent quantities. If the derivative of a function at xxx is 12, then its differential at xxx is dy=12 dxdy = 12 \, dxdy=12dx, meaning that if the independent variable has a small increment dxdxdx, the increment in the function value is approximately 12 times dxdxdx.

An important application of differentials is linear approximation, which aims to transform complex function calculations into relatively simple differential calculations. When ∣Δx∣|\Delta x|∣Δx∣ is small, the function increment Δy=f(x+Δx)−f(x)\Delta y = f(x + \Delta x) - f(x)Δy=f(x+Δx)−f(x) can be approximated by the differential dy=f′(x)Δxdy = f'(x) \Delta xdy=f′(x)Δx:

f(x+Δx)≈f(x)+f′(x)Δxf(x + \Delta x) \approx f(x) + f'(x) \Delta xf(x+Δx)≈f(x)+f′(x)Δx

Geometrically, this formula means: near the point (x,f(x))(x, f(x))(x,f(x)), we use the tangent line (a straight line) to approximate the curve. Linear approximation is very useful in engineering calculations. For example, to compute 4.01\sqrt{4.01}4.01​, let f(x)=xf(x) = \sqrt{x}f(x)=x​, take x=4x = 4x=4, Δx=0.01\Delta x = 0.01Δx=0.01, then:

4.01≈4+124×0.01=2+14×0.01=2.0025\sqrt{4.01} \approx \sqrt{4} + \frac{1}{2\sqrt{4}} \times 0.01 = 2 + \frac{1}{4} \times 0.01 = 2.00254.01​≈4​+24​1​×0.01=2+41​×0.01=2.0025

This result differs from the exact value 4.01≈2.002498\sqrt{4.01} \approx 2.0024984.01​≈2.002498 by only about 2×10−62 \times 10^{-6}2×10−6. The geometric significance of linear approximation is that on a tiny scale, we can use a straight line segment to approximately represent curves of various shapes -- as long as the scale is small enough, the two are approximately equal in length.

Higher-Order Derivatives

If the derivative f′f'f′ of a function fff is itself differentiable, we can differentiate f′f'f′ again to obtain the second derivative:

f′′(x)=ddx(dfdx)=d2fdx2f''(x) = \frac{d}{dx}\left(\frac{df}{dx}\right) = \frac{d^2 f}{dx^2}f′′(x)=dxd​(dxdf​)=dx2d2f​

The second derivative has many valuable physical and geometric meanings. For example, in physics, if f(t)f(t)f(t) represents position as a function of time, then f′(t)f'(t)f′(t) is velocity, and the second derivative f′′(t)f''(t)f′′(t) represents acceleration. Geometrically, the second derivative reflects the concavity of a function:

  • If f′′(x)>0f''(x) > 0f′′(x)>0, the function is convex at xxx (bowl-shaped, opening upward)
  • If f′′(x)<0f''(x) < 0f′′(x)<0, the function is concave at xxx (bowl-shaped, opening downward)
  • If f′′(x)=0f''(x) = 0f′′(x)=0, then xxx may be an inflection point

In machine learning, the loss functions we typically wish to minimize are convex functions, where the second derivative is non-negative and the function graph is "bowl-shaped upward," meaning there is a unique global minimum. Determining the convexity or concavity of a function is very important for assessing whether optimization algorithms (such as gradient descent) can converge stably.

Continuing the generalization, for some functions we can compute third-order, fourth-order, and even higher-order derivatives. For example:

  • f(x)=exf(x) = e^xf(x)=ex, then f(n)(x)=exf^{(n)}(x) = e^xf(n)(x)=ex (every order of derivative is itself)
  • f(x)=sin⁡xf(x) = \sin xf(x)=sinx, then f′(x)=cos⁡xf'(x) = \cos xf′(x)=cosx, f′′(x)=−sin⁡xf''(x) = -\sin xf′′(x)=−sinx, f′′′(x)=−cos⁡xf'''(x) = -\cos xf′′′(x)=−cosx, f(4)(x)=sin⁡xf^{(4)}(x) = \sin xf(4)(x)=sinx (cycles every four orders)

Summary

Derivatives reveal a simple yet profound idea: any continuously changing quantity can be characterized by its trend of change at a given point. Starting from the instantaneous velocity problem that Newton pondered, derivatives push the average rate of change to its limit, yielding a precise expression for the instantaneous rate of change. This leap from a static ratio to a dynamic limit is not only a mathematical advancement but also a shift in way of thinking -- we can now use a single number to describe the tendency of a function to change at a particular point. Geometrically, the derivative is the slope of the tangent line, providing us with a way to understand curves (nonlinear) through straight lines (linear).

Differentials transform the abstract "rate of change" into a concrete "amount of change," allowing us to simplify complex function calculations through linear approximation. This idea is particularly evident in machine learning: Taylor expansions decompose complex functions into a series of polynomials, gradient descent relies on first-order derivatives to guide the optimization direction, and the convexity or concavity of loss functions is revealed by second-order derivatives -- all of these are direct applications of differential calculus in modern artificial intelligence. The concepts of limits, derivatives, differentials, and higher-order derivatives build progressively upon each other, forming a mathematical framework for understanding the continuously changing world and laying a solid foundation for the next chapter on multivariate differential calculus.

Exercises

  1. Must a differentiable function always be continuous? Is a continuous function always differentiable?

    Reference Answer

    If a function fff is differentiable at a point x0x_0x0​, then fff is continuous at x0x_0x0​.

    Proof Sketch: Differentiability implies that lim⁡Δx→0f(x0+Δx)−f(x0)Δx\lim_{\Delta x \to 0} \frac{f(x_0 + \Delta x) - f(x_0)}{\Delta x}limΔx→0​Δxf(x0​+Δx)−f(x0​)​ exists. To prove continuity, we need to show lim⁡Δx→0[f(x0+Δx)−f(x0)]=0\lim_{\Delta x \to 0} [f(x_0 + \Delta x) - f(x_0)] = 0limΔx→0​[f(x0​+Δx)−f(x0​)]=0.

    f(x0+Δx)−f(x0)=f(x0+Δx)−f(x0)Δx⋅Δxf(x_0 + \Delta x) - f(x_0) = \frac{f(x_0 + \Delta x) - f(x_0)}{\Delta x} \cdot \Delta xf(x0​+Δx)−f(x0​)=Δxf(x0​+Δx)−f(x0​)​⋅Δx

    As Δx→0\Delta x \to 0Δx→0, the first term approaches f′(x0)f'(x_0)f′(x0​) (the derivative exists), and the second term approaches 0, so the product approaches 0.

    However, continuity does not imply differentiability. A classic counterexample is f(x)=∣x∣f(x) = |x|f(x)=∣x∣ at x=0x = 0x=0: the function is continuous, but it has a "corner" at this point where the left and right derivatives are not equal, and therefore it is not differentiable.

  2. Using the definition of a limit, prove that lim⁡x→2(3x+1)=7\lim_{x \to 2} (3x + 1) = 7limx→2​(3x+1)=7.

    Reference Answer

    To prove lim⁡x→2(3x+1)=7\lim_{x \to 2} (3x + 1) = 7limx→2​(3x+1)=7, we need to show that for any ε>0\varepsilon > 0ε>0, there exists δ>0\delta > 0δ>0 such that whenever 0<∣x−2∣<δ0 < |x - 2| < \delta0<∣x−2∣<δ, we have ∣(3x+1)−7∣<ε|(3x + 1) - 7| < \varepsilon∣(3x+1)−7∣<ε.

    Compute: ∣(3x+1)−7∣=∣3x−6∣=3∣x−2∣|(3x + 1) - 7| = |3x - 6| = 3|x - 2|∣(3x+1)−7∣=∣3x−6∣=3∣x−2∣

    To ensure 3∣x−2∣<ε3|x - 2| < \varepsilon3∣x−2∣<ε, we only need ∣x−2∣<ε3|x - 2| < \frac{\varepsilon}{3}∣x−2∣<3ε​.

    Therefore, take δ=ε3\delta = \frac{\varepsilon}{3}δ=3ε​. When 0<∣x−2∣<δ0 < |x - 2| < \delta0<∣x−2∣<δ, we have ∣(3x+1)−7∣=3∣x−2∣<3⋅ε3=ε|(3x + 1) - 7| = 3|x - 2| < 3 \cdot \frac{\varepsilon}{3} = \varepsilon∣(3x+1)−7∣=3∣x−2∣<3⋅3ε​=ε.

    This proves lim⁡x→2(3x+1)=7\lim_{x \to 2} (3x + 1) = 7limx→2​(3x+1)=7.

  3. Using the definition of the derivative, find f′(1)f'(1)f′(1) for f(x)=x3f(x) = x^3f(x)=x3.

    Reference Answer

    By the definition of the derivative: f′(1)=lim⁡Δx→0f(1+Δx)−f(1)Δxf'(1) = \lim_{\Delta x \to 0} \frac{f(1 + \Delta x) - f(1)}{\Delta x}f′(1)=limΔx→0​Δxf(1+Δx)−f(1)​

    Compute:

    • f(1)=13=1f(1) = 1^3 = 1f(1)=13=1
    • f(1+Δx)=(1+Δx)3=1+3Δx+3(Δx)2+(Δx)3f(1 + \Delta x) = (1 + \Delta x)^3 = 1 + 3\Delta x + 3(\Delta x)^2 + (\Delta x)^3f(1+Δx)=(1+Δx)3=1+3Δx+3(Δx)2+(Δx)3

    Therefore:

    f′(1)=lim⁡Δx→0(1+3Δx+3(Δx)2+(Δx)3)−1Δx=lim⁡Δx→03Δx+3(Δx)2+(Δx)3Δxf'(1) = \lim_{\Delta x \to 0} \frac{(1 + 3\Delta x + 3(\Delta x)^2 + (\Delta x)^3) - 1}{\Delta x} = \lim_{\Delta x \to 0} \frac{3\Delta x + 3(\Delta x)^2 + (\Delta x)^3}{\Delta x}f′(1)=Δx→0lim​Δx(1+3Δx+3(Δx)2+(Δx)3)−1​=Δx→0lim​Δx3Δx+3(Δx)2+(Δx)3​
    =lim⁡Δx→0[3+3Δx+(Δx)2]=3= \lim_{\Delta x \to 0} [3 + 3\Delta x + (\Delta x)^2] = 3=Δx→0lim​[3+3Δx+(Δx)2]=3

    Or directly verify using the power rule: f′(x)=3x2f'(x) = 3x^2f′(x)=3x2, so f′(1)=3f'(1) = 3f′(1)=3.

  4. Find the derivatives of the following functions:

    • f(x)=x4−3x2+2x−5f(x) = x^4 - 3x^2 + 2x - 5f(x)=x4−3x2+2x−5
    • g(x)=exsin⁡xg(x) = e^x \sin xg(x)=exsinx
    • h(x)=ln⁡xxh(x) = \frac{\ln x}{x}h(x)=xlnx​
    Reference Answer
    • Apply the power rule and sum/difference rules:
    f′(x)=4x3−6x+2f'(x) = 4x^3 - 6x + 2f′(x)=4x3−6x+2
    • Apply the product rule (f⋅g)′=f′⋅g+f⋅g′(f \cdot g)' = f' \cdot g + f \cdot g'(f⋅g)′=f′⋅g+f⋅g′:
    g′(x)=ddx(ex)⋅sin⁡x+ex⋅ddx(sin⁡x)=exsin⁡x+excos⁡x=ex(sin⁡x+cos⁡x)g'(x) = \frac{d}{dx}(e^x) \cdot \sin x + e^x \cdot \frac{d}{dx}(\sin x) = e^x \sin x + e^x \cos x = e^x(\sin x + \cos x)g′(x)=dxd​(ex)⋅sinx+ex⋅dxd​(sinx)=exsinx+excosx=ex(sinx+cosx)
    • Apply the quotient rule (fg)′=f′⋅g−f⋅g′g2\left(\frac{f}{g}\right)' = \frac{f' \cdot g - f \cdot g'}{g^2}(gf​)′=g2f′⋅g−f⋅g′​:
    h′(x)=1x⋅x−ln⁡x⋅1x2=1−ln⁡xx2h'(x) = \frac{\frac{1}{x} \cdot x - \ln x \cdot 1}{x^2} = \frac{1 - \ln x}{x^2}h′(x)=x2x1​⋅x−lnx⋅1​=x21−lnx​
  5. Let f(x)=x3−3xf(x) = x^3 - 3xf(x)=x3−3x. Find:

    • The intervals where the function is increasing and decreasing
    • The intervals of concavity and inflection points
    Reference Answer

    First, find the first derivative: f′(x)=3x2−3=3(x2−1)f'(x) = 3x^2 - 3 = 3(x^2 - 1)f′(x)=3x2−3=3(x2−1)

    Set f′(x)=0f'(x) = 0f′(x)=0, obtaining x=±1x = \pm 1x=±1.

    • When x<−1x < -1x<−1 or x>1x > 1x>1, f′(x)>0f'(x) > 0f′(x)>0, the function is increasing
    • When −1<x<1-1 < x < 1−1<x<1, f′(x)<0f'(x) < 0f′(x)<0, the function is decreasing

    Find the second derivative: f′′(x)=6xf''(x) = 6xf′′(x)=6x

    Set f′′(x)=0f''(x) = 0f′′(x)=0, obtaining x=0x = 0x=0.

    • When x<0x < 0x<0, f′′(x)<0f''(x) < 0f′′(x)<0, the function is concave
    • When x>0x > 0x>0, f′′(x)>0f''(x) > 0f′′(x)>0, the function is convex

    Therefore, x=0x = 0x=0 is an inflection point, with coordinates (0,0)(0, 0)(0,0).

  6. Use linear approximation to estimate sin⁡(0.1)\sin(0.1)sin(0.1) (in radians), and compare the error with the exact value.

    Reference Answer

    Let f(x)=sin⁡xf(x) = \sin xf(x)=sinx, take x0=0x_0 = 0x0​=0, Δx=0.1\Delta x = 0.1Δx=0.1.

    Linear approximation formula: f(x0+Δx)≈f(x0)+f′(x0)⋅Δxf(x_0 + \Delta x) \approx f(x_0) + f'(x_0) \cdot \Delta xf(x0​+Δx)≈f(x0​)+f′(x0​)⋅Δx

    Compute:

    • f(0)=sin⁡0=0f(0) = \sin 0 = 0f(0)=sin0=0
    • f′(x)=cos⁡xf'(x) = \cos xf′(x)=cosx, so f′(0)=cos⁡0=1f'(0) = \cos 0 = 1f′(0)=cos0=1

    Therefore: sin⁡(0.1)≈0+1×0.1=0.1\sin(0.1) \approx 0 + 1 \times 0.1 = 0.1sin(0.1)≈0+1×0.1=0.1

    Exact value: sin⁡(0.1)≈0.099833\sin(0.1) \approx 0.099833sin(0.1)≈0.099833

    Error: ∣0.1−0.099833∣≈0.000167≈1.67×10−4|0.1 - 0.099833| \approx 0.000167 \approx 1.67 \times 10^{-4}∣0.1−0.099833∣≈0.000167≈1.67×10−4

    Relative error: 0.0001670.099833≈0.17%\frac{0.000167}{0.099833} \approx 0.17\%0.0998330.000167​≈0.17%

    We can see that for small angles, sin⁡x≈x\sin x \approx xsinx≈x is a very good approximation.

Words: 4,047
Updated 2026-07-28
Last Updated:
Contributors: icyfenix, Claude
Prev
Matrix Basics
Next
Multivariate Functions and Composite Function Differentiation