Mathematical and Computational FoundationsIntroductionDeep learning systems are built from a small set of mathematical and computational operations. A neural network receives numerical data, transforms it through weighted sums and nonlinear functions, and adjusts its parameters using derivatives and optimization. Although software libraries perform most calculations automatically, practitioners must understand the underlying mathematics to design models, interpret results, identify errors, and select appropriate methods. This chapter develops the foundations required for introductory neural-network computation: scalars, vectors, matrices, tensors, functions, derivatives, gradients, probability, statistics, and computational representation. The emphasis is on interpretation and practical use rather than formal proof. Learning ObjectivesBy the end of this chapter, you should be able to: - Distinguish scalars, vectors, matrices, and tensors.
- Perform basic vector and matrix operations and check dimensional compatibility.
- Explain functions, derivatives, partial derivatives, gradients, and the chain rule.
- Describe how neural networks use weighted sums and activation functions.
- Calculate basic probabilities, means, variances, standard deviations, and covariance.
- Explain how computational graphs support forward and backward propagation.
- Apply these ideas to a small neural-network problem.
- Recognize numerical, statistical, ethical, and implementation limitations.
1. Numerical Objects in Machine Learning1.1 Scalars and variablesA scalar is a single numerical value, such as a temperature, learning rate, or loss value. Scalars are commonly written using lowercase italic symbols such as x, y, or θ. A parameter is a value learned by a model; a hyperparameter is selected by the practitioner, such as the learning rate or batch size. A scalar may have units. For example, an input representing height may be measured in centimetres. Neural-network calculations are usually easier when inputs are converted to dimensionless, normalized values. Unit consistency remains important when interpreting the original data and designing features. 1.2 VectorsA vector is an ordered collection of numbers. A feature vector for one observation may be written as: x = [x₁, x₂, …, xₙ]ᵀ Here, n is the number of features and T denotes transpose. A vector with n entries has dimension n. In a programming library, it may be represented as a one-dimensional array, although the distinction between a row vector and a column vector is mathematically important. Two vectors of equal length can be added component by component: a + b = [a₁ + b₁, a₂ + b₂, …, aₙ + bₙ]ᵀ Scalar multiplication is also componentwise: c a = [c a₁, c a₂, …, c aₙ]ᵀ The dot product, also called the inner product, is: a · b = ∑ᵢ₌₁ⁿ aᵢbᵢ It is defined when a and b have the same length and produces a scalar. Geometrically, it measures alignment. If the dot product is zero, the vectors are orthogonal. The Euclidean norm measures vector length: ‖a‖₂ = √(∑ᵢ₌₁ⁿ aᵢ²) The formula assumes ordinary Euclidean geometry. Norms are useful for measuring parameter size, distances, and regularization penalties. Worked Example 1: Dot product and normLet a = [2, −1]ᵀ and b = [3, 4]ᵀ. Then: a · b = (2)(3) + (−1)(4) = 6 − 4 = 2 The length of a is: ‖a‖₂ = √(2² + (−1)²) = √5 ≈ 2.236 The dot product is positive, so the vectors have some directional alignment, although they are not parallel. 1.3 MatricesA matrix is a rectangular arrangement of numbers. A matrix with m rows and n columns has shape m × n: A ∈ ℝᵐˣⁿ The symbol ℝ indicates real-valued entries. A matrix may represent a dataset, a collection of model parameters, or a linear transformation. If each row is one observation and each column is one feature, a dataset containing m observations and n features has shape m × n. Matrix addition requires equal shapes. Matrix multiplication is defined when the number of columns in the first matrix equals the number of rows in the second. If A has shape m × n and B has shape n × p, then AB has shape m × p: (AB)ᵢⱼ = ∑ₖ₌₁ⁿ AᵢₖBₖⱼ Matrix multiplication is generally not commutative: AB usually differs from BA, and BA may not even be defined. The transpose changes rows into columns. For a matrix A, the entry in row i and column j of Aᵀ is the entry in row j and column i of A. A square matrix is symmetric when A = Aᵀ. Worked Example 2: Matrix multiplicationLet: A = [[1, 2], [3, 4]], B = [[5], [6]] A has shape 2 × 2 and B has shape 2 × 1, so AB is defined and has shape 2 × 1: AB = [[(1)(5)+(2)(6)], [(3)(5)+(4)(6)]] = [[17], [39]] This operation is the basic computational pattern of a fully connected neural layer. 1.4 TensorsA tensor is a general multidimensional numerical array. A scalar is a zero-dimensional tensor, a vector is one-dimensional, and a matrix is two-dimensional. Examples include: - A grayscale image: height × width.
- A colour image: height × width × channels.
- A batch of colour images: batch size × height × width × channels.
- A sequence of feature vectors: sequence length × feature dimension.
The shape and ordering of tensor dimensions must be known. For example, channels-first and channels-last image formats store the same conceptual information in different orders. Confusing these conventions can produce shape errors or, worse, incorrect results without an obvious error. A useful distinction is between shape and size. The shape describes each dimension, while the size is the total number of elements. A tensor of shape 8 × 28 × 28 × 1 contains 8 × 28 × 28 × 1 = 6,272 values. 2. Functions and Neural ComputationA function maps an input to an output. In neural networks, a function may represent a complete model, a layer, an activation function, or a loss function. A simple linear model is: f(x) = wᵀx + b where x is an n-dimensional input vector, w is an n-dimensional weight vector, b is a scalar bias, and wᵀx is a dot product. The output is a scalar. The bias shifts the output independently of the input. A neural-network layer commonly applies a nonlinear activation after the linear transformation: a = φ(Wx + b) Here W is a weight matrix, b is a bias vector, and φ is applied element by element. If x has shape n × 1 and the layer has m units, W has shape m × n, b has shape m × 1, and a has shape m × 1. Without nonlinear activation functions, stacking linear layers still produces one linear transformation. Nonlinearities allow networks to represent more complex relationships. Common activation functions include: | Function | Definition | Typical use or issue |
|---|
| Sigmoid | σ(z) = 1/(1 + e-z) | Binary output; can saturate | | Tanh | tanh(z) = (ez − e-z)/(ez + e-z) | Zero-centred activation; can saturate | | ReLU | ReLU(z) = max(0,z) | Hidden layers; simple and efficient | | Softmax | softmax(zᵢ) = ezᵢ/∑ⱼ ezⱼ | Multiclass probabilities |
Softmax outputs are nonnegative and sum to one, provided the exponentials are finite. In practice, numerical implementations subtract the maximum logit before exponentiation. This does not change the output probabilities but improves stability. 3. Derivatives and Gradients3.1 Derivative of a scalar functionThe derivative describes the instantaneous rate of change of a function. For a scalar function f(x), its derivative at x is defined by: f′(x) = limₕ→0 [f(x + h) − f(x)]/h This definition applies when the limit exists. The derivative is also the slope of the tangent line. In optimization, it indicates how changing an input or parameter changes an output. Important derivative rules include: (d/dx)c = 0 (d/dx)xⁿ = n xⁿ⁻¹ (d/dx)eˣ = eˣ (d/dx) ln x = 1/x, for x > 0 The sum rule states that the derivative of a sum is the sum of derivatives. The product rule is (fg)′ = f′g + fg′. The chain rule is especially important in neural networks: (d/dx) f(g(x)) = f′(g(x))g′(x) 3.2 Partial derivatives and gradientsA multivariable function has several inputs. A partial derivative measures change with respect to one input while holding the others constant. For a function J(w₁, w₂), the gradient is the vector of partial derivatives: ∇J = [∂J/∂w₁, ∂J/∂w₂]ᵀ The gradient points in the direction of greatest local increase. Therefore, gradient descent updates parameters in the opposite direction: w ← w − η∇J(w) where η > 0 is the learning rate. This update is a local method; it does not guarantee the globally best solution for a non-convex neural-network objective. A learning rate that is too large may cause unstable updates, while one that is too small may make training unnecessarily slow. The gradient and learning rate must be compatible with the scale of the objective and parameters. Worked Example 3: One-parameter gradient descentSuppose the objective is: J(w) = (w − 3)² Its derivative is: dJ/dw = 2(w − 3) At w = 1, the gradient is 2(1 − 3) = −4. With learning rate η = 0.1: wnew = 1 − 0.1(−4) = 1.4 The parameter moves toward 3, the minimum of this objective. The example is one-dimensional and convex; neural networks generally involve many parameters and more complicated objective surfaces. 3.3 Backpropagation and computational graphsA computational graph represents a calculation as a sequence of elementary operations. During the forward pass, inputs and parameters produce predictions and a loss. During the backward pass, the chain rule computes how the loss changes with respect to each parameter. Consider: u = wx + b ŷ = u² The derivatives are: ∂ŷ/∂u = 2u ∂u/∂w = x By the chain rule: ∂ŷ/∂w = (∂ŷ/∂u)(∂u/∂w) = 2ux Automatic differentiation systems implement this principle, usually without symbolically expanding a complete formula. Backpropagation is therefore an efficient application of the chain rule to a layered computation. 4. Loss Functions and OptimizationA loss function quantifies disagreement between a prediction and a target. For a single regression observation, mean squared error is often written: L = (y − ŷ)² where y is the target and ŷ is the prediction. For N observations, the mean squared error is: MSE = (1/N)∑ᵢ₌₁ᴺ(yᵢ − ŷᵢ)² The factor 1/N makes the value an average and keeps its scale less dependent on dataset size. MSE is nonnegative and penalizes large errors more strongly than small errors. Its units are the squared units of the target. For binary classification, binary cross-entropy is commonly used: L = −[y ln(p) + (1 − y)ln(1 − p)] where y ∈ {0,1} is the class label and p is the predicted probability of class 1, with 0 < p < 1. The logarithm makes highly confident incorrect predictions costly. Numerical implementations avoid evaluating ln(0) directly. Training usually minimizes an average loss over a batch. A batch gradient estimate is computationally efficient but may differ from the exact gradient over the full training set. This variation can sometimes help exploration but also makes training noisy. 5. Probability and Basic Statistics5.1 Probability foundationsA random variable represents an uncertain numerical outcome. For a discrete variable X, a probability mass function assigns probabilities to possible values. Probabilities satisfy: 0 ≤ P(A) ≤ 1 and: P(Ω) = 1 where A is an event and Ω is the complete sample space. For mutually exclusive events A and B: P(A ∪ B) = P(A) + P(B) Conditional probability is: P(A|B) = P(A ∩ B)/P(B), provided P(B) > 0 Bayes’ theorem follows from the definition of conditional probability: P(A|B) = P(B|A)P(A)/P(B) In classification, this relationship helps explain why class prevalence and evidence both affect predicted probabilities. Independence means that knowledge of one event does not change the probability of another. For independent events A and B: P(A ∩ B) = P(A)P(B) Independence is an assumption, not a default fact. Real-world features are often correlated. 5.2 Mean, variance, and standard deviationFor observations x₁, x₂, …, xₙ, the arithmetic mean is: x̄ = (1/n)∑ᵢ₌₁ⁿ xᵢ The population variance is: σ² = (1/n)∑ᵢ₌₁ⁿ(xᵢ − μ)² where μ is the population mean. When estimating variance from a sample, the commonly used unbiased estimator is: s² = [1/(n − 1)]∑ᵢ₌₁ⁿ(xᵢ − x̄)² The corresponding standard deviation is the nonnegative square root of variance. Variance has squared units; standard deviation has the same units as the original variable. Worked Example 4: Descriptive statisticsFor values 2, 4, and 6, the mean is: x̄ = (2 + 4 + 6)/3 = 4 The sum of squared deviations is (2 − 4)² + (4 − 4)² + (6 − 4)² = 4 + 0 + 4 = 8. The population variance is 8/3 ≈ 2.667, and the population standard deviation is √(8/3) ≈ 1.633. The sample variance would be 8/(3 − 1) = 4. 5.3 Covariance and correlationCovariance measures whether two variables vary together. The population covariance is: Cov(X,Y) = (1/n)∑ᵢ₌₁ⁿ(xᵢ − μₓ)(yᵢ − μᵧ) Its units are the product of the units of X and Y, so magnitude is difficult to compare across variable pairs. Pearson correlation standardizes covariance: ρₓᵧ = Cov(X,Y)/(σₓσᵧ) when both standard deviations are positive. Correlation lies between −1 and 1. It measures linear association, not causation, and a correlation near zero does not rule out nonlinear dependence. 5.4 Normalization and standardizationA common standardization transforms a feature x into: z = (x − μ)/σ where μ and σ are calculated from the training data and σ > 0. The transformed training feature has approximately mean zero and standard deviation one. The same training-set μ and σ must be used for validation and test data; recalculating them separately causes data leakage. Min–max scaling maps values to an interval, often [0,1]: x′ = (x − xmin)/(xmax − xmin) provided x_max ≠ x_min. Scaling is not automatically beneficial for every model, but it is often important for gradient-based neural-network training because features with very different magnitudes can produce poorly conditioned optimization. 6. A Small Neural-Network ApplicationConsider an illustrative binary classification task: predicting whether a loan application requires manual review using two standardized features, income score and debt score. Let: x = [0.8, −0.4]ᵀ Use one neuron with weights w = [0.5, −1.0]ᵀ and bias b = 0.1. The pre-activation is: z = wᵀx + b = (0.5)(0.8) + (−1.0)(−0.4) + 0.1 = 0.9 Using the sigmoid function: p = 1/(1 + e−0.9) ≈ 0.711 The model estimates a 0.711 probability for class 1. If the decision threshold is 0.5, it predicts class 1. The threshold is a decision rule and is not the same as the probability itself. Suppose the true label is y = 1. The binary cross-entropy loss is approximately: L = −ln(0.711) ≈ 0.341 For sigmoid output with binary cross-entropy, the derivative with respect to z simplifies to: dL/dz = p − y Thus dL/dz ≈ 0.711 − 1 = −0.289. The weight gradients are: ∂L/∂w = (p − y)x Therefore: ∂L/∂w₁ ≈ (−0.289)(0.8) = −0.231 ∂L/∂w₂ ≈ (−0.289)(−0.4) = 0.116 and the bias gradient is approximately −0.289. With learning rate 0.1, the updated first weight is 0.5 − 0.1(−0.231) ≈ 0.523, while the second is −1.0 − 0.1(0.116) ≈ −1.012. This example demonstrates the complete chain from input representation to prediction, loss, gradient, and update. 7. Computational ConsiderationsMathematical correctness does not guarantee computational reliability. Important practical issues include: - Shape checking: Confirm that matrix and tensor dimensions match before multiplication.
- Data types: Integer, single-precision, and double-precision representations have different ranges and memory costs.
- Overflow and underflow: Exponentials and logarithms can become numerically unstable for extreme values.
- Vectorization: Array operations are usually faster and clearer than unnecessary explicit loops.
- Reproducibility: Record random seeds, data versions, preprocessing parameters, and software versions where reproducibility matters.
- Memory management: Large tensors and batches may exceed available CPU or GPU memory.
- Gradient verification: For debugging, compare automatic gradients with finite-difference approximations on a very small example. This is a diagnostic technique, not normally the main training method.
A finite-difference approximation for a scalar function is: f′(x) ≈ [f(x + h) − f(x − h)]/(2h) for a sufficiently small nonzero h. If h is too large, truncation error increases; if too small, floating-point cancellation can dominate. 8. Case Study: Screening Images for Manufacturing DefectsBackgroundThis illustrative case concerns a factory that photographs components on a production line. The organization wants a neural network to classify images as acceptable or requiring inspection. ProblemImages have height 128, width 128, and three colour channels. A batch contains 32 images. Engineers must represent the data correctly, preprocess it without leakage, and understand the computation of a first layer. AnalysisUsing channels-last storage, the input tensor has shape 32 × 128 × 128 × 3. Each image contains 49,152 numerical values. Pixel values may initially be integers from 0 to 255. A common transformation is x′ = x/255, producing values in [0,1], assuming the original sensor encoding uses that range. The labels are binary. A final sigmoid unit can produce a probability of a defect, and binary cross-entropy can measure prediction error. During training, the dataset should be separated into training, validation, and test partitions before statistics such as normalization parameters are estimated. Augmentation may improve robustness, but transformations must preserve the meaning of a defect; an image flip is inappropriate if orientation itself is diagnostically important. Approach and solutionThe network performs a forward pass to produce probabilities, computes a loss, and uses backpropagation to update weights. Engineers monitor both training and validation performance. If training performance improves while validation performance worsens, overfitting may be occurring. A confusion matrix should be examined because a missed defect may be more costly than a false alarm. The decision threshold may therefore be selected using operational costs rather than fixed automatically at 0.5. OutcomeA technically successful model is not defined only by accuracy. It should meet agreed criteria for recall of defects, processing speed, calibration, reliability under changing lighting, and human review procedures. The final system should flag uncertain or out-of-distribution images rather than presenting every output as equally trustworthy. Learning pointsThe case demonstrates that tensor shape, scaling, probability interpretation, gradient optimization, evaluation statistics, and operational consequences are connected. It also shows why a model’s mathematical output must be interpreted within a broader engineering and governance process. 9. Limitations, Ethics, and Best PracticesMathematical models summarize data; they do not automatically discover truth or causality. Biased, incomplete, or historically discriminatory data can produce discriminatory predictions. In high-impact domains, practitioners should document data provenance, intended use, limitations, performance across relevant groups, and mechanisms for human oversight. Probability estimates should be calibrated when decisions depend on their numerical meaning. Privacy risks arise when training data contain personal or sensitive information. Security threats include adversarial inputs, data poisoning, and unauthorized model extraction. Governance should include access control, audit logs, retention rules, incident response, and compliance with applicable data-protection and sector-specific requirements. Recommended practice is to: - Define the prediction target and units clearly.
- Inspect distributions, missing values, outliers, and label quality.
- Split data before fitting preprocessing transformations.
- Track tensor shapes and parameter counts.
- Use numerically stable library implementations.
- Select metrics that reflect the real cost of errors.
- Compare against a simple baseline.
- Test on data representing deployment conditions.
- Document assumptions and reproducibility information.
- Maintain human review for consequential decisions.
Contemporary DevelopmentsModern deep-learning software combines automatic differentiation, accelerator hardware, mixed-precision arithmetic, and highly optimized tensor libraries. Automatic differentiation reduces manual derivative errors. GPUs and other accelerators perform many array operations in parallel. Mixed precision can improve speed and memory efficiency, but loss scaling and numerical monitoring may be needed to prevent underflow. Large models increasingly use vectorized representations, attention mechanisms, and distributed computation. These developments do not remove the need for foundational mathematics: tensor shapes, probability, gradients, numerical stability, and statistical evaluation remain essential for diagnosing and responsibly deploying advanced systems. Chapter SummaryNeural networks rely on scalars, vectors, matrices, and higher-dimensional tensors to represent data and parameters. Vector and matrix operations, especially dot products and matrix multiplication, implement the linear part of neural layers. Functions and nonlinear activation functions allow networks to represent complex relationships. Derivatives quantify local change, while partial derivatives form gradients. The chain rule provides the mathematical basis of backpropagation, and gradient descent uses gradients to update parameters. Loss functions such as mean squared error and binary cross-entropy convert prediction quality into an optimization objective. Probability supports uncertainty and classification, while means, variance, standard deviation, covariance, and correlation support data analysis and preprocessing. Standardization must be fitted on training data only. Computational practice requires attention to shapes, numerical precision, memory, reproducibility, and stable implementations. Ethical deployment additionally requires attention to bias, privacy, security, calibration, human oversight, and domain-specific consequences. Key TermsScalar: A single numerical value. Vector: An ordered one-dimensional collection of values. Matrix: A rectangular two-dimensional array. Tensor: A multidimensional numerical array. Dot product: The sum of pairwise products of two equal-length vectors. Norm: A function that measures the size or length of a vector. Transpose: An operation that exchanges rows and columns. Function: A mapping from inputs to outputs. Parameter: A model value learned from data. Bias: A trainable additive term in a neural layer. Activation function: A function applied to a layer’s pre-activation values. Derivative: The instantaneous rate of change of a function. Gradient: A vector of partial derivatives. Chain rule: A rule for differentiating composed functions. Backpropagation: Efficient gradient computation through a computational graph. Loss function: A numerical measure of prediction error. Probability: A numerical representation of uncertainty. Variance: The average squared deviation from a mean, with population or sample conventions. Covariance: A measure of joint variation between two variables. Standardization: Rescaling a feature by subtracting a mean and dividing by a standard deviation. Review QuestionsShort Answer Questions- What is the difference between a vector and a matrix?
- Under what condition is matrix multiplication AB defined?
- What does a gradient represent in optimization?
- Why are nonlinear activation functions needed in multilayer neural networks?
- Why should standardization parameters be calculated using training data only?
Long Answer Questions- Explain how a fully connected neural layer transforms an input vector, including the roles of weights, bias, and activation function.
- Describe the chain rule and explain how it enables backpropagation.
- Compare mean squared error and binary cross-entropy, including an appropriate use for each.
- Explain mean, variance, standard deviation, covariance, and correlation and identify an important limitation of correlation.
- Discuss the computational and ethical considerations involved in deploying a neural-network classifier.
Analytical / Application Questions- A layer receives an input vector of shape 4 × 1 and contains 3 units. Determine the shapes of its weight matrix, bias vector, and output vector. Explain your reasoning.
- For J(w) = (w − 5)², calculate the gradient at w = 2 and the updated value after one gradient-descent step with learning rate 0.2.
- A defect detector has high accuracy but misses many actual defects. Which additional metrics and decision-process changes should be investigated, and why?
Multiple Choice Questions with AnswersQuestion 1Which operation produces a scalar from two equal-length vectors? A. Dot product B. Matrix transpose C. Tensor reshaping D. Elementwise concatenation Correct Answer: A. Dot product Question 2If A has shape 3 × 4 and B has shape 4 × 2, what is the shape of AB? A. 4 × 4 B. 3 × 2 C. 3 × 4 D. 2 × 3 Correct Answer: B. 3 × 2 Question 3What is the main purpose of a nonlinear activation function in a neural network? A. To ensure all inputs are integers B. To eliminate the need for weights C. To allow representation of nonlinear relationships D. To make matrix multiplication commutative Correct Answer: C. To allow representation of nonlinear relationships Question 4For J(w) = (w − 3)², what is the gradient at w = 1? A. −4 B. −2 C. 2 D. 4 Correct Answer: A. −4 Question 5Which statement about gradient descent is correct? A. It updates parameters in the direction of the gradient to maximize loss B. It updates parameters opposite to the gradient to reduce loss locally C. It requires all gradients to be exactly zero before training begins D. It can be used only for one-parameter models Correct Answer: B. It updates parameters opposite to the gradient to reduce loss locally Question 6What does the standard deviation measure? A. The squared units of a feature B. The average of two variables C. The typical spread around the mean in the original units D. The probability that an observation is correct Correct Answer: C. The typical spread around the mean in the original units Question 7When standardizing validation data, which quantities should normally be used? A. Mean and standard deviation recalculated from the validation data B. Mean and standard deviation from the training data C. The minimum and maximum from the test data D. No transformation parameters Correct Answer: B. Mean and standard deviation from the training data Question 8For binary cross-entropy, what does p usually represent? A. The predicted probability of class 1 B. The number of model parameters C. The population variance D. The learning-rate multiplier Correct Answer: A. The predicted probability of class 1 Question 9If x = [0.8, −0.4]ᵀ and w = [0.5, −1.0]ᵀ with b = 0.1, what is wᵀx + b? A. 0.1 B. 0.5 C. 0.9 D. 1.7 Correct Answer: C. 0.9 Question 10What is the principal mathematical basis of backpropagation? A. The chain rule B. The commutative law of matrix multiplication C. The sample variance formula only D. The min–max scaling rule Correct Answer: A. The chain rule
|