Deep Learning

Chapter 2
Mathematical and Computational Foundations

Post Graduate Program in AI and ML
Deep Learning   •   Post Graduate

Mathematical and Computational Foundations

Introduction

Deep learning is often described as the construction of layered computational models, but its reliability depends on a deeper foundation. Neural networks represent data using vectors, matrices, tensors, and probability distributions; they learn by minimizing objective functions through calculus and numerical optimization; and they are implemented as computational graphs evaluated by automatic differentiation. A sound understanding of these foundations enables practitioners to design models, diagnose failures, assess uncertainty, and interpret computational results rather than treating deep learning as a collection of software commands.

This chapter develops the mathematical and computational language required for postgraduate study of deep learning. It connects linear algebra with representation, calculus with learning, probability and statistics with uncertainty and generalization, information theory with prediction and compression, and computational graphs with efficient gradient computation.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Represent data and transformations using vectors, matrices, and tensors.
  2. Explain norms, inner products, rank, eigenvalues, and singular-value decomposition in deep learning contexts.
  3. Compute gradients, Jacobians, Hessians, and directional derivatives for common functions.
  4. Interpret probability distributions, expectations, variance, covariance, likelihood, and Bayes’ rule.
  5. Relate entropy, cross-entropy, KL divergence, and mutual information to machine learning objectives.
  6. Construct and differentiate computational graphs using the chain rule.
  7. Distinguish reverse-mode automatic differentiation from numerical differentiation.
  8. Apply these concepts to model training and critically evaluate numerical and statistical limitations.

1. Mathematical Objects Used in Deep Learning

1.1 Scalars, vectors, matrices, and tensors

A scalar is a single number, such as a learning rate or loss value. A vector is an ordered collection of numbers. An input feature vector may be written as x ∈ ℝd, where d is the number of features. A matrix is a rectangular array, commonly used to represent a batch of examples, a linear transformation, or a set of parameters. A tensor is a general multidimensional array.

The shape of an object specifies the length of each axis. For example, a batch of colour images may have shape (B,C,H,W), representing batch size, channels, height, and width. Shape is not merely a programming detail: it determines whether operations are mathematically defined. A matrix A ∈ ℝm×n can multiply a vector x ∈ ℝn, producing Ax ∈ ℝm.

The transpose of a matrix exchanges rows and columns. For real-valued matrices, AT is defined by (AT)ij=Aji. Matrix multiplication is associative, but generally not commutative: (AB)C=A(BC), whereas AB ≠ BA in general.

1.2 Inner products, norms, and distance

The Euclidean inner product of two vectors is

xᵀy = ∑ᵢ₌₁ᵈ xᵢyᵢ

It measures alignment. The induced Euclidean norm is

‖x‖₂ = √(∑ᵢ₌₁ᵈ xᵢ²)

and the squared Euclidean distance between x and y is ‖x-y‖₂². The Cauchy–Schwarz inequality states |xTy| ≤ ‖x‖₂‖y‖₂, with equality when the vectors are linearly dependent.

The L1 norm, ‖x‖₁=∑ᵢ|xᵢ|, encourages sparse solutions in many optimization problems. The L2 norm is differentiable everywhere and is commonly used for weight decay. A general Lp norm is ‖x‖p=(∑ᵢ|xᵢ|p)1/p for p≥1. Norms must not be confused with probability distributions: a norm measures magnitude, whereas a distribution assigns probabilities.

1.3 Linear transformations and affine layers

A linear transformation satisfies additivity and homogeneity. In finite-dimensional spaces it can be represented by a matrix. A neural-network affine layer is

z = Wx + b

where x ∈ ℝn is the input, W ∈ ℝm×n is a parameter matrix, b ∈ ℝm is a bias vector, and z ∈ ℝm is the output. Strictly speaking, the bias makes the operation affine rather than linear. A nonlinear activation a=φ(z) is required for a multilayer network to represent functions that cannot be reduced to one affine transformation.

1.4 Rank, eigenvalues, and singular values

The rank of a matrix is the dimension of its column space. Low-rank structure can reveal redundancy in data and motivates dimensionality reduction. A nonzero vector v is an eigenvector of square matrix A if

Av = λv

where λ is the corresponding eigenvalue. Eigenvalue analysis is particularly useful for studying covariance matrices, optimization curvature, and dynamical stability.

Singular-value decomposition (SVD) applies to any real matrix A ∈ ℝm×n:

A = UΣVᵀ

where U and V have orthonormal columns and Σ contains nonnegative singular values. A rank-r approximation retains the largest r singular values. SVD supports compression, noise filtering, analysis of ill-conditioning, and understanding of learned representations.

1.5 Broadcasting and tensor operations

Frameworks commonly allow broadcasting: a lower-dimensional array is conceptually replicated across compatible axes. For example, adding a bias vector of shape (m,)] ] to every row of a batch output of shape [[MATH:(B,m) is valid. Broadcasting is convenient but can hide shape errors. Professional practice requires explicitly documenting axis order, batch conventions, and whether reductions are sums or means.

Tensor dimensions flowing through a neural network layer Input X(B, n) W, b Affine Z(B, m) φ Activation A(B, m)
Tensor dimensions flowing through a neural network layer
Figure: Tensor dimensions flowing through a neural network layer

Figure: Shapes and transformations in a batched affine layer.

2. Calculus for Learning

2.1 Derivatives and gradients

For a scalar function f:ℝ→ℝ, the derivative at x is the limiting rate of change

f′(x) = limₕ→₀ [f(x+h)-f(x)]/h

A function is differentiable at a point when this limit exists. For a scalar-valued function of a vector, L:ℝd→ℝ, the gradient is

∇ₓL = [∂L/∂x₁, …, ∂L/∂xd]ᵀ

The gradient points in the direction of greatest local increase. Thus gradient descent uses

θt+1 = θt − η∇_θJ(θt)

where θ is the parameter vector, J is the objective, η>0 is the learning rate, and differentiability is assumed at the current point. The update is a local approximation; it does not guarantee global minimization for non-convex neural-network objectives.

For a small perturbation ∆x,

f(x+∆x) ≈ f(x) + ∇f(x)ᵀ∆x

This first-order approximation explains both optimization progress and sensitivity to perturbations.

2.2 Chain rule and Jacobians

If y=f(u) and u=g(x), the chain rule gives

dy/dx = (dy/du)(du/dx)

For vector-valued functions, the Jacobian is the matrix of first partial derivatives. If y∈ℝm and x∈ℝn, then Jij=∂yi/∂xj and J∈ℝm×n. Under compatible dimensions,

Jf∘g(x) = Jf(g(x)) Jg(x)

Jacobians describe local sensitivity, but explicitly constructing them can be expensive. Automatic differentiation instead propagates required products through a graph.

2.3 Hessians and curvature

The Hessian of a twice-differentiable scalar function is

H(x) = ∇²f(x), Hᵢⱼ = ∂²f/(∂xᵢ∂xⱼ)

Near a point, a second-order approximation is

f(x+∆x) ≈ f(x)+∇f(x)ᵀ∆x + ½∆xᵀH(x)∆x

The Hessian indicates curvature. Positive definite curvature implies a strict local minimum; an indefinite Hessian indicates saddle-like curvature. Deep-learning objectives are often non-convex, high-dimensional, and expensive to analyze, so practical optimizers usually use first-order information or approximations to curvature.

2.4 Common derivatives

For f(x)=x², f′(x)=2x. For the sigmoid function

σ(x)=1/(1+e^(−x)), σ′(x)=σ(x)[1−σ(x)]

For the rectified linear unit, ReLU(x)=max(0,x), the derivative is 1 for x>0 and 0 for x<0; at zero it is not classically differentiable, and implementations select a subgradient convention.

For a vector z, softmax is

softmax(z)ᵢ = ezᵢ/∑ⱼezⱼ

It produces nonnegative values summing to one. In practice, numerical stability is improved by subtracting maxj zj before exponentiation; this does not change the result because the common factor cancels.

3. Probability and Statistics

3.1 Random variables and distributions

A random variable maps outcomes to numerical values. A discrete probability mass function satisfies P(X=x)≥0 and ∑xP(X=x)=1. A continuous density p(x) satisfies p(x)≥0 and ∫p(x)dx=1; a density value is not itself a probability, and probabilities are obtained by integration over intervals.

The expectation of a discrete variable is

E[X] = ∑ₓ xP(X=x)

and for a continuous variable,

E[X] = ∫x p(x) dx

Variance is Var(X)=E[(X−E[X])²]. Equivalently, Var(X)=E[X²]−E[X]². Standard deviation is σX=√Var(X), in the same units as X. Covariance between X and Y is

Cov(X,Y)=E[(X−E[X])(Y−E[Y])]

Correlation is ρXY=Cov(X,Y)/(σXσY) when both standard deviations are positive. Correlation is dimensionless and measures linear association; it does not establish causation.

3.2 Conditional probability and Bayes’ rule

For P(B)>0, conditional probability is P(A|B)=P(A∩B)/P(B). Bayes’ rule is

P(A|B)=P(B|A)P(A)/P(B)

In supervised learning, P(y|x,θ) describes a predictive model. The likelihood of parameters given independent observations is

L(θ)=∏ᵢ P(yᵢ|xᵢ,θ)

Maximizing likelihood is equivalent to minimizing negative log-likelihood, because the logarithm is strictly increasing:

−log L(θ)=−∑ᵢ log P(yᵢ|xᵢ,θ)

Independence is an assumption about the factorization, not a claim that all real-world observations are intrinsically unrelated.

3.3 Estimation and generalization

A dataset is commonly divided into training, validation, and test sets. The empirical risk is

Ĵ(θ)=1/n ∑ᵢ₌₁ⁿ ℓ(f_θ(xᵢ),yᵢ)

where ℓ is a loss function. The expected population risk is J(θ)=E(X,Y)∼P[ℓ(f_θ(X),Y). Training minimizes an estimate of population risk, so a low training loss does not guarantee good performance on new data. Overfitting occurs when model fitting captures idiosyncrasies of the sample rather than stable structure.

The sample mean X̄=(1/n)∑Xi is an estimator of the population mean. If observations are independent with finite variance σ², then Var(X̄)=σ²/n. This relationship explains why larger samples generally reduce estimation noise, although dependence and distribution shift can invalidate simple interpretations.

4. Information Theory and Learning Objectives

4.1 Entropy and cross-entropy

For a discrete random variable with probabilities pi, Shannon entropy is

H(p)=−∑ᵢ pᵢ log pᵢ

with the convention 0 log 0=0. The logarithm base determines units: base 2 gives bits, while the natural logarithm gives nats. Entropy measures uncertainty in a distribution, not the accuracy of a particular prediction.

Cross-entropy between target distribution p and model distribution q is

H(p,q)=−∑ᵢ pᵢ log qᵢ

For one-hot classification, where class k is correct, this reduces to −log qk. Assigning very small probability to the observed class incurs a large penalty.

4.2 KL divergence and mutual information

Kullback–Leibler divergence is

DKL(p‖q)=∑ᵢ pᵢ log[pᵢ/qᵢ]

It is nonnegative but generally asymmetric, so it is not a metric. It is finite only when qi>0 wherever pi>0. The identity

H(p,q)=H(p)+DKL(p‖q)

shows that minimizing cross-entropy with respect to q minimizes divergence from the target distribution, since H(p) is fixed.

Mutual information measures dependence:

I(X;Y)=∑ₓ,ᵧ p(x,y) log[p(x,y)/(p(x)p(y))]

It is zero exactly when X and Y are independent, under the usual discrete formulation. Mutual information appears in representation learning, feature selection, and privacy analysis, but estimating it reliably from high-dimensional data is difficult.

5. Computational Graphs and Automatic Differentiation

A computational graph expresses a function as elementary operations. Nodes store values, and directed edges indicate dependencies. During the forward pass, values are computed and often cached. During the backward pass, derivatives are propagated using the chain rule.

Consider the scalar function L=(wx+b−y)². Let z=wx+b−y and L=z². Then

∂L/∂z = 2z, ∂L/∂w = 2z x, ∂L/∂b = 2z

The local derivative ∂L/∂z is multiplied by the derivative of each preceding operation. This is reverse-mode automatic differentiation: it computes gradients of a scalar output with respect to many inputs efficiently. It is therefore especially suitable for neural-network training, where the loss is scalar and the parameter count may be very large.

Forward-mode differentiation propagates tangents from inputs to outputs and is efficient when there are few inputs and many outputs. Reverse mode is efficient in the opposite situation. Automatic differentiation is not symbolic algebra and is not the same as finite differences. It applies exact chain-rule operations up to floating-point arithmetic.

Finite-difference checking approximates a derivative, for example

∂f/∂x ≈ [f(x+ε)−f(x−ε)]/(2ε)

for small ε. It is useful for debugging but can suffer from cancellation for very small ε and truncation error for large ε. Gradient checking should be performed on small models with suitable tolerances, not treated as a training method.

Vanishing and exploding gradients

In a deep composition, gradients involve products of Jacobians. If typical singular values are substantially below one, products can shrink; if substantially above one, they can grow. Saturating activations, poor initialization, long recurrent sequences, and extreme parameter scales can therefore produce vanishing or exploding gradients. Residual connections, normalization, suitable initialization, gated architectures, gradient clipping, and careful learning-rate selection are practical responses, but none is universally sufficient.

6. Worked Example: A One-Neuron Gradient Update

Consider a neuron with input x=2, weight w=0.5, bias b=0.1, target y=1, and squared loss L=(wx+b−y)². The learning rate is η=0.1.

First, the prediction error is

z=0.5×2+0.1−1=−0.1.

Therefore L=(−0.1)²=0.01. By the chain rule,

∂L/∂w=2zx=2(−0.1)(2)=−0.4,

and ∂L/∂b=2z=−0.2. The gradient-descent updates are

wnew=0.5−0.1(−0.4)=0.54,

bnew=0.1−0.1(−0.2)=0.12.

The new pre-activation is 0.54×2+0.12=1.20, so the new loss is (1.20−1)²=0.04. In this particular step the loss increased, despite using the negative gradient. This is possible because the learning rate is too large for the local curvature or because the quadratic objective is being overshot. The example illustrates that the descent direction is locally improving for sufficiently small steps, not that every chosen step size must reduce the loss.

7. Applied Case: Predictive Maintenance for Industrial Pumps

Background

This illustrative case concerns a manufacturer monitoring pumps using vibration, temperature, pressure, and rotational-speed sensors. The objective is to predict failure within the next 24 hours while avoiding unnecessary maintenance alerts.

Problem

Sensor streams have different units and scales. Measurements arrive in batches, contain missing values, and are correlated over time. A binary classifier must output a calibrated probability of failure. False negatives are costly because they may cause production downtime; false positives are costly because they trigger inspections.

Analysis and approach

Each observation is represented as a tensor with dimensions corresponding to batch, time window, and sensor channels. Training and test periods are separated chronologically to reduce leakage. Continuous features are standardized using training-set statistics only:

x′=(x−μtrain)/σtrain

where μtrain and σtrain>0 are calculated on the training data. A neural model produces a logit s, converted to failure probability by σ(s). Binary cross-entropy for label y∈{0,1} is

L=−[y log p +(1−y)log(1−p)]

Class imbalance is addressed through carefully documented weighting or sampling, with validation performed using precision–recall curves, recall at an operational false-alarm rate, calibration, and expected maintenance cost rather than accuracy alone.

The computational graph includes normalization, temporal feature extraction, an affine output layer, sigmoid probability, and loss. Automatic differentiation computes parameter gradients. Gradient norms and activation statistics are monitored to detect numerical instability. Missingness indicators are retained when absence itself may carry operational meaning.

Outcome and learning points

A technically strong model is not automatically a useful maintenance system. Its threshold must reflect asymmetric costs, predictions must be monitored for sensor drift, and operators should receive interpretable evidence such as influential time windows or sensor groups. The deployment process must define who can override an alert, how interventions are logged, and how performance is reassessed after equipment or operating conditions change.

The case demonstrates that mathematical foundations influence every stage: tensor shapes govern implementation, statistics govern preprocessing and evaluation, probability governs risk interpretation, information theory motivates the loss, and automatic differentiation enables training.

8. Limitations, Governance, and Best Practices

Floating-point arithmetic introduces rounding, overflow, underflow, and accumulation error. Stable softmax and log-sum-exp implementations should be used; subtracting the maximum logit before exponentiation preserves softmax probabilities while avoiding unnecessary overflow. Mixed-precision training can improve speed and memory use, but loss scaling and validation are required to prevent underflow.

Data leakage occurs when information unavailable at prediction time enters training or preprocessing. Common examples include fitting normalization on the full dataset, randomly splitting temporally dependent records, or using post-outcome variables. Reproducibility requires recording data versions, preprocessing, random seeds where meaningful, software and hardware environments, and evaluation protocols.

Mathematical correctness does not remove ethical risk. Models may encode historical discrimination, expose sensitive information, or create unsafe automation. Practitioners should assess representativeness, subgroup performance, privacy, security, explainability, human oversight, and applicable regulation. Uncertainty should be communicated honestly: a softmax score is not automatically a calibrated probability, and confidence in a prediction may degrade under distribution shift.

Best practice is to verify tensor shapes, test gradients, use numerically stable losses, separate training and evaluation data, inspect distributions before and after preprocessing, report uncertainty and subgroup metrics, and connect optimization metrics to the real decision cost.

Chapter Summary

Deep learning depends on a unified mathematical and computational foundation. Vectors, matrices, tensors, norms, rank, eigenvalues, and SVD describe data and transformations. Derivatives, gradients, Jacobians, and Hessians explain sensitivity, optimization, and curvature. Probability and statistics provide the language of uncertainty, likelihood, estimation, and generalization. Entropy, cross-entropy, KL divergence, and mutual information connect prediction to information. Computational graphs organize forward evaluation, while reverse-mode automatic differentiation efficiently computes gradients through the chain rule. Numerical stability, data leakage, distribution shift, calibration, fairness, privacy, and human oversight are essential considerations when these tools are applied in practice.

Key Terms

Scalar: A single numerical value.

Vector: An ordered one-dimensional array of values.

Matrix: A rectangular two-dimensional array.

Tensor: A multidimensional numerical array.

Norm: A function measuring the magnitude of a vector or tensor.

Inner product: A scalar product that measures alignment between vectors.

Rank: The dimension of a matrix’s column or row space.

Eigenvalue: A scalar scaling factor associated with an eigenvector.

Singular-value decomposition: Factorization of a matrix into orthogonal factors and singular values.

Gradient: Vector of first partial derivatives of a scalar function.

Jacobian: Matrix of first partial derivatives of a vector-valued function.

Hessian: Matrix of second partial derivatives of a scalar function.

Likelihood: Probability of observed data considered as a function of model parameters.

Covariance: Measure of joint variation between two random variables.

Cross-entropy: Expected negative log probability assigned by one distribution to outcomes from another.

KL divergence: Nonnegative, generally asymmetric measure of distributional discrepancy.

Computational graph: Directed representation of elementary operations composing a function.

Automatic differentiation: Chain-rule-based computation of derivatives through a computational graph.

Gradient descent: Iterative optimization method that moves opposite the objective gradient.

Distribution shift: Change between the data distribution used for development and that encountered in deployment.

Review Questions

Short Answer Questions

  1. Why is an affine neural-network layer not strictly a linear transformation when it includes a bias?
  2. Define the gradient of a scalar-valued function of a vector.
  3. State one difference between variance and standard deviation.
  4. Why is KL divergence not generally considered a distance metric?
  5. What is the principal computational advantage of reverse-mode automatic differentiation in neural-network training?

Long Answer Questions

  1. Explain how matrix multiplication, nonlinear activation, and tensor shape interact in a multilayer neural network.
  2. Derive the gradient-descent update from a first-order Taylor approximation and discuss why the learning rate matters.
  3. Explain likelihood, negative log-likelihood, and cross-entropy in supervised classification.
  4. Compare forward-mode and reverse-mode automatic differentiation, including their preferred input-output regimes.
  5. Discuss how numerical stability, data leakage, and distribution shift can affect a deployed deep-learning system.

Analytical / Application Questions

  1. For L=(wx+b−y)², derive ∂L/∂w and ∂L/∂b using the chain rule. Explain how the result changes if the loss is averaged over a batch.
  2. A classifier assigns probabilities 0.7 and 0.3 to two classes, while the true class is the first. Calculate the one-hot cross-entropy using the natural logarithm and interpret the result.
  3. Design an evaluation plan for the predictive-maintenance case that addresses class imbalance, temporal leakage, calibration, asymmetric costs, and post-deployment distribution shift.

Multiple Choice Questions with Answers

Question 1

Which expression represents the Euclidean norm of a vector x?

A. ∑ᵢ xᵢ

B. √(∑ᵢ xᵢ²)

C. ∏ᵢ xᵢ²

D. maxᵢ xᵢ

Correct Answer: B. √(∑ᵢ xᵢ²)

Question 2

For z=Wx+b, where W∈ℝm×n and x∈ℝn, what is the shape of z?

A. (n,m)

B. (m,n)

C. (m,)

D. (n,)

Correct Answer: C. (m,)

Question 3

What does the gradient of a scalar function indicate locally?

A. The direction of greatest increase

B. The exact global minimum

C. The probability of an event

D. The matrix rank

Correct Answer: A. The direction of greatest increase

Question 4

If σ(x)=1/(1+e^(−x)), which is its derivative?

A. σ(x)

B. 1−σ(x)

C. σ(x)(1−σ(x))

D. e^(−x)

Correct Answer: C. σ(x)(1−σ(x))

Question 5

For independent observations, which expression is the likelihood of parameters θ?

A. ∑ᵢP(yᵢ|xᵢ,θ)

B. ∏ᵢP(yᵢ|xᵢ,θ)

C. P(θ)−P(y)

D. E[θ ]

Correct Answer: B. ∏ᵢP(yᵢ|xᵢ,θ)

Question 6

For a one-hot target whose correct class is k, what is the cross-entropy loss?

A. −log qk

B. log(1−qk)

C. qk²

D. −qk

Correct Answer: A. −log qk

Question 7

Which statement about KL divergence is correct?

A. It is always symmetric.

B. It can be negative for valid distributions.

C. It is nonnegative but generally asymmetric.

D. It is identical to Euclidean distance.

Correct Answer: C. It is nonnegative but generally asymmetric.

Question 8

Why is reverse-mode automatic differentiation especially suitable for neural-network training?

A. It requires no chain rule.

B. It efficiently differentiates many parameters with respect to a scalar loss.

C. It avoids all floating-point error.

D. It computes only numerical approximations.

Correct Answer: B. It efficiently differentiates many parameters with respect to a scalar loss.

Question 9

For L=(wx+b−y)², with x=2 and error wx+b−y=−0.1, what is ∂L/∂w?

A. −0.1

B. −0.2

C. −0.4

D. 0.4

Correct Answer: C. −0.4

Question 10

Which practice best prevents preprocessing leakage?

A. Compute normalization statistics using all available data.

B. Compute normalization statistics using the training set only.

C. Normalize the test labels using training predictions.

D. Randomly duplicate test observations in training.

Correct Answer: B. Compute normalization statistics using the training set only.

Deep Learning
Chapter 2 of 15