AI & Machine Learning Fundamentals

Chapter 8
Neural Networks and Deep Learning

Advance Diploma in Artificial Intelligence and Machine Learning
AI & Machine Learning Fundamentals   •   Advance Diploma

Neural Networks and Deep Learning

Introduction and Context

Neural networks are computational models inspired loosely by networks of biological neurons. Their practical importance arises from their ability to learn nonlinear relationships directly from data. Unlike many traditional models that require extensive manual feature engineering, deep neural networks can learn hierarchical representations: early layers may detect simple patterns, while later layers combine them into increasingly meaningful structures.

Neural networks support image classification, speech recognition, machine translation, recommendation, anomaly detection, medical diagnosis, robotics and generative AI. However, their effectiveness depends on appropriate data, architecture, optimization, evaluation and governance. A large network is not automatically an intelligent or reliable system.

Learning Objectives

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

  • explain perceptrons and multilayer feed-forward networks;
  • distinguish common activation and loss functions;
  • derive the central steps of backpropagation;
  • apply gradient-based optimization and interpret its limitations;
  • explain regularization and its relationship to generalization;
  • compare convolutional, recurrent and transformer architectures;
  • select an architecture for a practical problem;
  • evaluate neural-network risks, limitations and deployment requirements.

1. From Perceptrons to Multilayer Networks

A neuron receives an input vector, forms a weighted sum, adds a bias and applies an activation function. For input vector x = (x₁, …, xₙ), weights w = (w₁, …, wₙ), and bias b, the pre-activation is:

z = wᵀx + b = ∑ᵢ₌₁ⁿ wᵢxᵢ + b

The output is a = φ(z), where φ is an activation function. The bias shifts the decision boundary and is usually learned along with the weights.

A classical perceptron uses a step function:

ŷ = 1 if wᵀx + b ≥ 0; otherwise 0

It can represent a linear decision boundary. For binary inputs, it can learn AND and OR functions, but not XOR using a single unit, because XOR is not linearly separable. This limitation motivated multilayer networks.

A feed-forward multilayer perceptron (MLP) contains an input layer, one or more hidden layers and an output layer. For layer l:

a⁽ˡ⁾ = φ⁽ˡ⁾(W⁽ˡ⁾a⁽ˡ⁻¹⁾ + b⁽ˡ⁾)

Here, a⁽ˡ⁻¹⁾ is the previous layer's activation, W⁽ˡ⁾ is the weight matrix, b⁽ˡ⁾ is the bias vector and φ⁽ˡ⁾ is applied element by element. The input is a⁽⁰⁾ = x. Matrix dimensions must be compatible: if the previous layer has m units and the current layer has k units, W has shape k × m and b has shape k.

A network with no hidden layer and a linear output is a linear model. Hidden layers with nonlinear activations allow the network to approximate complex functions. The universal approximation theorem states, under technical assumptions, that a sufficiently wide network with a suitable nonconstant activation can approximate a broad class of continuous functions on compact domains. It does not say that a small network will learn the function, that training will be easy, or that the result will generalize well.

Feed-forward neural network InputHiddenOutput featureslearned representationsprediction
Feed-forward neural network with input, hidden and output layers
Figure: Feed-forward neural network with input, hidden and output layers

Figure: A fully connected feed-forward network. Each arrow represents a learnable connection; omitted arrows would correspond to architectural sparsity.

2. Activation Functions

Activation functions introduce nonlinearity. Without them, a composition of linear layers remains equivalent to one linear transformation.

FunctionDefinitionTypical useMain concern
Sigmoidσ(z)=1/(1+e-z)Binary probability outputSaturating gradients
Tanhtanh(z)=(ez-e-z)/(ez+e-z)Older hidden layers, centred outputsSaturation
ReLUmax(0,z)Common hidden-layer activationDead units for persistently negative inputs
Leaky ReLUmax(αz,z), α > 0Hidden layersChoice of α
Softmaxpᵢ=ezᵢ/∑ⱼezⱼMulticlass outputSensitive to large logits unless stabilized
GELUSmooth, approximately gated activationModern language and vision modelsMore computational complexity

For numerical stability, softmax is normally computed after subtracting the maximum logit: replacing each zᵢ by zᵢ − maxⱼ zⱼ leaves probabilities unchanged but reduces overflow risk.

The output activation must match the task. Use sigmoid for one independent binary target, softmax for mutually exclusive classes, and a linear output for unconstrained regression. Using softmax for multilabel classification is incorrect because multilabel outputs are not required to sum to one.

3. Loss Functions and Model Objectives

A loss function measures disagreement between prediction and target. Training commonly minimizes the empirical risk:

J(θ) = (1/m)∑ᵢ₌₁ᵐ L(fθ(xᵢ), yᵢ) + λΩ(θ)

Here, θ denotes all parameters, m is the number of training examples, L is the example loss, Ω is a regularization penalty and λ ≥ 0 controls its strength. The first term measures data fit; the second discourages undesirable parameter configurations.

For regression, mean squared error is:

MSE = (1/m)∑ᵢ₌₁ᵐ(yᵢ − ŷᵢ)²

It is appropriate when large errors should receive disproportionately large penalties and is commonly associated with a conditional-mean prediction under a Gaussian-noise assumption. Mean absolute error, (1/m)∑|yᵢ−ŷᵢ|, is more robust to outliers and corresponds to a conditional-median interpretation under suitable assumptions.

For binary classification with y ∈ {0,1} and predicted probability p, binary cross-entropy is:

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

For K mutually exclusive classes and one-hot target y, categorical cross-entropy is:

L = −∑ₖ₌₁ᴷ yₖ log(pₖ)

Logarithms require probabilities in (0,1); implementations use stable combined sigmoid/log-loss or softmax/log-loss routines and avoid computing fragile probabilities separately.

4. Backpropagation

Backpropagation is an efficient application of the chain rule. It computes the gradient of the loss with respect to every parameter by propagating error signals from the output toward the input. It is not itself an optimization method; it supplies derivatives to an optimizer.

Consider one unit with z = wᵀx + b, a = φ(z), and loss L(a,y). The chain rule gives:

∂L/∂wᵢ = (∂L/∂a)(∂a/∂z)(∂z/∂wᵢ) = (∂L/∂a)φ′(z)xᵢ

Similarly, ∂L/∂b=(∂L/∂a)φ′(z). Define δ = ∂L/∂z. Then the parameter gradients are ∂L/∂w=δx and ∂L/∂b=δ. For a layer, the vector form is:

δ⁽ˡ⁾ = (W⁽ˡ⁺¹⁾ᵀδ⁽ˡ⁺¹⁾) ⊙ φ′⁽ˡ⁾(z⁽ˡ⁾)

where ⊙ denotes elementwise multiplication. The output-layer error is determined by the loss and output activation. A useful special case is softmax with cross-entropy, for which the derivative with respect to logits is p − y. This simplification assumes the standard matched softmax/cross-entropy formulation.

A training iteration has four stages: compute a forward pass, evaluate loss, compute gradients by reverse-mode differentiation, and update parameters. Automatic differentiation systems implement this process while retaining computational graphs or equivalent intermediate values.

5. Gradient-Based Optimization

The basic gradient-descent update is:

θₜ₊₁ = θₜ − η∇J(θₜ)

η > 0 is the learning rate. Full-batch gradient descent uses the entire training set; stochastic gradient descent (SGD) uses one example; mini-batch SGD uses a subset and is the dominant practical approach. Mini-batches provide computational efficiency and noisy gradients that can sometimes improve exploration.

Momentum maintains a velocity, for example:

vₜ = βvₜ₋₁ + ∇J(θₜ); θₜ₊₁ = θₜ − ηvₜ

where 0 ≤ β < 1. Adaptive methods such as Adam maintain estimates of first and second gradient moments. They often converge quickly, but learning-rate schedules, weight decay and validation performance still require careful tuning. No optimizer eliminates poor data, label leakage or inappropriate objectives.

Common symptoms provide diagnostic clues. A learning rate that is too high can cause divergence or oscillation; one that is too low can make training extremely slow. Exploding gradients produce unstable updates, while vanishing gradients make early layers learn slowly. Careful initialization, normalized inputs, suitable activations, residual connections, gradient clipping and normalization layers can help.

6. Generalization and Regularization

Training error and generalization error are distinct. A model may memorize the training data yet perform poorly on unseen cases. Regularization controls effective complexity rather than guaranteeing fairness or correctness.

L2 regularization adds λ∑ⱼθⱼ² to the objective and discourages large weights. L1 regularization adds λ∑ⱼ|θⱼ| and may produce sparse parameters. The exact effect depends on optimizer implementation; “weight decay” and L2 penalty coincide under plain SGD but are not identical for all adaptive optimizers.

Other methods include:

  • Early stopping: stop when validation performance ceases to improve, using a validation set not used for parameter fitting.
  • Dropout: randomly omit units during training and rescale activations according to the implementation; it is disabled or adjusted during inference.
  • Data augmentation: generate label-preserving transformations, such as image crops or noise injection where scientifically justified.
  • Batch normalization and related methods: normalize intermediate activations using batch statistics during training and stored statistics during inference.
  • Transfer learning: initialize from a model trained on a related task, then freeze or fine-tune selected layers.

Data splitting must prevent leakage. For temporal, patient-level or user-level data, random row-wise splitting can put highly related records in both training and test sets and produce misleading results.

7. Convolutional Neural Networks

Convolutional neural networks (CNNs) exploit local connectivity and parameter sharing. A two-dimensional cross-correlation, commonly called convolution in deep-learning software, can be written as:

Y[i,j] = b + ∑₍u,v₎ K[u,v]X[i+u,j+v]

The output size for one spatial dimension is:

nout = floor((nin + 2p − d(k−1) − 1)/s + 1)

where n_in is input size, k is kernel size, p is padding, s is stride and d is dilation. This formula assumes a discrete sliding kernel and the stated padding convention. Channels add another summation over input channels, and multiple filters produce multiple output channels.

CNNs are effective for images because nearby pixels often have related information and the same feature detector can be useful across locations. Pooling or strided convolution reduces spatial resolution and increases receptive fields. Modern architectures commonly use residual connections, which provide an identity shortcut and improve optimization in deep networks.

CNN limitations include sensitivity to distribution shift, difficulty expressing long-range relationships without sufficient depth or specialized mechanisms, and potentially high computational cost. Data augmentation and careful calibration are important in safety-relevant vision systems.

8. Recurrent Neural Networks

Recurrent neural networks process sequences by maintaining a hidden state:

hₜ = φ(Wₓxₜ + Wₕhₜ₋₁ + b)

Here, xₜ is the input at time t, hₜ is the hidden state and Wₕ is a recurrent weight matrix shared across time. Outputs may be produced at every time step or only at the final step.

Repeated multiplication through time can make gradients vanish or explode. Long short-term memory (LSTM) and gated recurrent unit (GRU) models introduce gates that regulate information retention and forgetting. Truncated backpropagation through time limits the number of time steps used for gradient computation, reducing memory requirements but potentially losing long-range learning signals.

RNNs remain useful for streaming and resource-constrained applications, but their sequential computation limits parallelism. They also struggle with very long dependencies compared with attention-based models.

9. Attention and Transformers

Attention allows each token to form a weighted combination of other token representations. Scaled dot-product attention is:

Attention(Q,K,V) = softmax(QKᵀ/√dₖ)V

Q, K and V are query, key and value matrices, and dₖ is the key dimension. Scaling by √dₖ helps prevent dot products from becoming excessively large, which would make softmax gradients small. Multi-head attention uses several projections, enabling different relational patterns to be learned in parallel.

A transformer typically contains repeated attention and position-wise feed-forward blocks, residual connections and normalization. Because attention alone does not encode order, positional information is added through learned positional embeddings, sinusoidal encodings or relative-position methods.

Self-attention has quadratic pairwise interaction cost in sequence length in its standard form: for sequence length n, the attention score matrix has O(n²) entries. This creates memory and computation challenges for long contexts. Encoder-only models are suited to representation and classification, decoder-only models generate autoregressively, and encoder-decoder models map one sequence to another.

Transformers have enabled large-scale pretraining followed by fine-tuning or prompting. Their capabilities depend on data quality, objective design, scale and alignment procedures; fluent output is not proof of factual accuracy.

10. Worked Example: One Gradient Update

Suppose a single neuron uses x = 2, w = 0.5, b = 0, a linear activation, target y = 3 and squared loss L = ½(ŷ − y)². The prediction is:

ŷ=wx+b=0.5×2=1.

The error is ŷ − y = −2. Since dL/dŷ = ŷ − y, the gradients are:

∂L/∂w=(−2)(2)=−4, and ∂L/∂b=−2.

With learning rate η = 0.1, gradient descent gives:

wnew=0.5−0.1(−4)=0.9, and bnew=0−0.1(−2)=0.2.

The updated prediction for x = 2 is 0.9×2+0.2=2.0, closer to the target 3. This example uses one observation, a linear output and the half-squared-error convention; changing any of these assumptions changes the gradients.

11. Illustrative Case: Defect Detection in Manufacturing

Background. A manufacturer wants to detect surface defects from camera images on a production line. Missed defects are costly, but excessive false alarms interrupt production.

Problem. Images vary with lighting, camera position and product batches. Defect examples are rare and several images may come from the same physical item.

Analysis. A random image split would risk leakage if images from one item appear in both training and test sets. Accuracy would also be misleading because most images are non-defective. The project therefore defines item-level splits, records defect prevalence, and reports recall, precision, F1 score, confusion matrices and calibration. If a defect is safety-critical, recall may receive priority, but the operating threshold must reflect inspection capacity and the relative costs of errors.

Approach. Engineers begin with a pretrained CNN, apply physically plausible augmentation, use class-weighted or sampled training, and tune the decision threshold on validation data. They inspect false positives by lighting condition and test performance on a later production batch. A human review pathway is retained for uncertain predictions.

Outcome. In this illustrative scenario, the model improves screening speed, but deployment is conditional on monitoring drift, auditing subgroup and batch performance, and maintaining a documented rollback procedure. The model is an aid to inspection, not an unquestionable authority.

Learning points. Architecture selection is only one component of the solution. Data governance, evaluation design, threshold selection, human factors and post-deployment monitoring determine whether a neural system creates operational value.

12. Responsible Deployment and Professional Practice

Neural networks can reproduce sampling bias, encode historical discrimination and expose sensitive information. Practitioners should document data origin, consent or lawful basis, labelling procedures, known gaps, model version, intended use and prohibited use. Privacy-preserving design may include minimization, access control, encryption and appropriate anonymization, although anonymization is not automatically irreversible.

High-impact applications require human oversight, contestability, security testing and records of decisions. Threats include adversarial examples, data poisoning, model extraction and prompt or input manipulation. A model card or system card can communicate limitations, evaluation populations and appropriate use. Monitoring should examine data drift, concept drift, latency, calibration, error rates and fairness indicators.

Explainability techniques can support debugging and communication, but post hoc explanations are not guaranteed to reveal the model's true causal reasoning. Explanations should therefore be treated as evidence with limitations, not as proof of correctness.

13. Best-Practice Workflow

A technically disciplined project generally follows this sequence:

  1. Define the decision, users, harms and success criteria.
  2. Acquire representative, legally usable and sufficiently labelled data.
  3. Establish a simple baseline before selecting a complex network.
  4. Split data according to the real dependency structure.
  5. Normalize inputs and choose an output/loss pair consistent with the task.
  6. Train using reproducible configuration, monitoring and validation.
  7. Tune hyperparameters without repeatedly overfitting the test set.
  8. Evaluate aggregate and subgroup performance, calibration and robustness.
  9. Test operational constraints such as latency, memory and failure handling.
  10. Deploy gradually with monitoring, documentation and a rollback plan.

14. Challenges and Contemporary Directions

Important challenges include computational cost, energy consumption, data scarcity, distribution shift, interpretability, uncertainty estimation and brittle behaviour outside the training distribution. Large models may display emergent capabilities but can also amplify spurious correlations and generate confident errors.

Current directions include efficient attention, model compression, pruning, quantization, parameter-efficient fine-tuning, multimodal learning, retrieval-augmented systems, diffusion-based generative models, federated learning and neural operators. These are active research areas rather than universal solutions. Their suitability must be judged against accuracy, safety, cost, privacy and maintainability.

Chapter Summary

Neural networks learn parameterized nonlinear transformations through layers of weighted combinations and activation functions. Perceptrons provide the foundation but are limited to linear separation; multilayer networks overcome this limitation through hidden nonlinear layers. Activation functions and task-matched loss functions determine how predictions and gradients behave. Backpropagation applies the chain rule efficiently, while SGD and adaptive optimizers update parameters using estimated gradients. Regularization, sound data splitting and validation support generalization.

CNNs exploit local structure and shared filters for spatial data. RNNs represent sequential state but face long-range gradient and parallelism limitations. Transformers use attention to model relationships across positions and have become central to modern language and multimodal systems. Successful professional deployment requires more than model accuracy: it requires representative data, appropriate evaluation, security, fairness, privacy, human oversight and continuous monitoring.

Key Terms

Perceptron: A weighted linear threshold unit.

Bias: A learnable offset added to a neuron's weighted input.

Activation function: A function introducing nonlinearity into a neural layer.

Logit: An unconstrained pre-activation, often converted to probabilities.

Multilayer perceptron: A feed-forward network with one or more hidden layers.

Loss function: A quantitative measure of prediction error.

Backpropagation: Reverse-mode chain-rule computation of parameter gradients.

Gradient descent: Optimization by moving parameters opposite the loss gradient.

Mini-batch: A subset of training examples used for one update.

Regularization: A method that discourages overfitting or undesirable complexity.

Dropout: Training-time random omission of units.

Convolution: A local, weight-sharing operation over structured data.

Receptive field: The input region influencing a unit's activation.

Recurrent neural network: A sequence model with a state propagated over time.

Attention: Weighted interaction between queries, keys and values.

Transformer: An architecture built primarily from attention and feed-forward blocks.

Generalization: Performance on previously unseen data.

Data leakage: Use of information during training that would not be available at prediction time.

Calibration: Agreement between predicted probabilities and observed frequencies.

Distribution shift: Change in data or target relationships between development and deployment.

Review Questions

5 Short Answer Questions

  1. Why can a single perceptron not represent XOR?
  2. What role does an activation function play in a multilayer network?
  3. Distinguish backpropagation from gradient descent.
  4. Why is softmax generally inappropriate for multilabel classification?
  5. State one advantage and one limitation of transformers compared with recurrent networks.

5 Long Answer Questions

  1. Explain the forward pass and backpropagation process in a multilayer perceptron, including the role of the chain rule.
  2. Compare MSE, binary cross-entropy and categorical cross-entropy, including suitable output activations.
  3. Discuss overfitting and evaluate at least four regularization strategies.
  4. Explain how CNNs exploit spatial structure and derive the meaning of the main convolution output-size parameters.
  5. Compare RNN, LSTM, CNN and transformer architectures for sequence or spatial applications.

3 Analytical / Application Questions

  1. A medical classifier has high accuracy but poor recall for a rare disease. Diagnose possible causes and propose an evaluation and thresholding plan.
  2. Design a leakage-resistant training and testing strategy for a defect-detection CNN when many images come from the same manufactured item.
  3. An attention model performs well in the laboratory but poorly after deployment. Develop a structured investigation covering data drift, calibration, subgroup performance, security and rollback.

Multiple Choice Questions with Answers

Question 1

Which limitation applies to a single classical perceptron?

A. It cannot perform any binary classification

B. It can represent only linearly separable decision boundaries

C. It cannot use a bias term

D. It always requires convolution

Correct Answer: B. It can represent only linearly separable decision boundaries

Question 2

For mutually exclusive multiclass classification, which output and loss pairing is generally appropriate?

A. Linear output with mean absolute error

B. Sigmoid outputs with categorical cross-entropy

C. Softmax output with categorical cross-entropy

D. ReLU output with hinge-free regression loss

Correct Answer: C. Softmax output with categorical cross-entropy

Question 3

What is the main purpose of backpropagation?

A. To initialize weights randomly

B. To compute parameter gradients efficiently

C. To select the test set

D. To replace the loss function

Correct Answer: B. To compute parameter gradients efficiently

Question 4

For one neuron with x = 2, w = 0.5, b = 0 and target y = 3, using L = ½(ŷ − y)², what is ∂L/∂w?

A. −4

B. −2

C. 2

D. 4

Correct Answer: A. −4

Question 5

Which operation is characteristic of CNNs?

A. Local filtering with shared parameters

B. Mandatory sequential processing of every token

C. Removal of all spatial relationships

D. Conversion of every target into a probability sum of one

Correct Answer: A. Local filtering with shared parameters

Question 6

Why is the factor √dₖ used in scaled dot-product attention?

A. To increase sequence length

B. To prevent dot products from becoming excessively large

C. To eliminate positional information

D. To make all values equal

Correct Answer: B. To prevent dot products from becoming excessively large

Question 7

Which method directly discourages large parameter magnitudes through a squared penalty?

A. L1 regularization

B. Dropout

C. L2 regularization

D. Max pooling

Correct Answer: C. L2 regularization

Question 8

What is a major risk of randomly splitting related records between training and test sets?

A. The model cannot calculate gradients

B. The test estimate may be inflated by data leakage

C. The activation function becomes linear

D. The batch size becomes zero

Correct Answer: B. The test estimate may be inflated by data leakage

Question 9

What does the standard self-attention score matrix require with sequence length n?

A. O(n) pairwise entries

B. O(log n) pairwise entries

C. O(n²) pairwise entries

D. O(1) pairwise entries

Correct Answer: C. O(n²) pairwise entries

Question 10

Which practice is most appropriate for a safety-relevant deployed neural model?

A. Use accuracy alone and disable monitoring

B. Hide known limitations from users

C. Retain human oversight, monitor drift and maintain rollback capability

D. Tune the test set repeatedly until performance improves

Correct Answer: C. Retain human oversight, monitor drift and maintain rollback capability

AI & Machine Learning Fundamentals
Chapter 8 of 14