Introduction to AI and ML

Chapter 9
Neural Networks and Deep Learning

Diploma in Artificial Intelligence and Machine Learning
Introduction to AI and ML   •   Diploma

Neural Networks and Deep Learning

Introduction

Neural networks are computational models inspired loosely by biological nervous systems. In artificial intelligence, they learn relationships between inputs and outputs by adjusting numerical parameters from data. Deep learning refers to neural-network methods with multiple learnable layers. These models are especially effective when useful representations must be learned directly from images, speech, text, or other high-dimensional data.

A neural network does not “understand” data in the human sense. It performs a sequence of mathematical transformations whose parameters are learned through optimization. Its usefulness depends on the quality and representativeness of its training data, the suitability of its architecture, the choice of objective function, and appropriate evaluation.

Learning Objectives

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

  • explain artificial neurons, layers, weights, biases, and activation functions;
  • describe forward propagation and calculate a simple neuron output;
  • explain loss functions, gradient descent, and backpropagation;
  • identify causes and remedies of overfitting;
  • distinguish CNNs, sequence models, and transformers;
  • explain transfer learning and select suitable deep-learning applications;
  • recognize practical, ethical, and governance considerations in deployment.

1. The Artificial Neuron

An artificial neuron receives input values, multiplies each by a learnable weight, adds a bias, and applies an activation function. For input vector x = (x₁, x₂, …, xₙ), weights w = (w₁, w₂, …, wₙ), and bias b, the neuron first computes a weighted sum:

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

Here, z is the pre-activation value. The output is:

a = φ(z)

where φ is the activation function. Weights determine the influence of inputs; the bias shifts the response; and the activation function introduces non-linearity.

For a binary classification problem, a neuron might output a probability using the sigmoid function:

σ(z) = 1 ÷ (1 + e⁻ᶻ)

The sigmoid output lies between 0 and 1. It is often used in a binary output layer, although it is less commonly used in hidden layers of modern deep networks because its gradients can become very small.

Worked example: one neuron

Suppose x₁ = 2, x₂ = −1, w₁ = 0.6, w₂ = −0.4, and b = 0.2. Then:

z = (0.6 × 2) + [−0.4 × (−1)] + 0.2 = 1.8.

Using sigmoid activation, σ(1.8) ≈ 0.858. The neuron therefore produces an output of approximately 0.858. This value may be interpreted as a probability only when the model and training objective justify that interpretation.

2. Layers and Network Architecture

A neural network is organized into layers:

  • Input layer: receives features, pixels, tokens, or measurements. It normally performs no learned transformation.
  • Hidden layers: transform representations using weights, biases, and activations.
  • Output layer: produces a prediction, such as a class probability, numerical estimate, or sequence of tokens.

A network is called deep when it contains multiple hidden layers. Greater depth can allow the model to represent hierarchical features. For example, an image network may learn edges in early layers, shapes in intermediate layers, and object-level patterns in later layers.

A fully connected, or dense, layer connects each unit in one layer to every unit in the next. If a layer receives m inputs and has k neurons, it has m × k weights and k biases, for a total of mk + k parameters. Excessive parameter counts can increase computational cost and overfitting risk.

3. Activation Functions

Activation functions determine how a neuron responds and allow networks to model non-linear relationships. Without non-linear activations, a stack of linear layers is equivalent to a single linear transformation.

FunctionDefinitionTypical useMain issue
Sigmoidσ(z) = 1 ÷ (1 + e⁻ᶻ)Binary outputSaturating gradients
Tanhtanh(z)Some recurrent modelsSaturating gradients
ReLUmax(0, z)Hidden layersDead units for persistently negative inputs
Leaky ReLUmax(αz, z), α > 0Hidden layersRequires choice of α
Softmaxexp(zᵢ) ÷ ∑ⱼ exp(zⱼ)Multi-class outputSensitive to scale; not multilabel by itself

For a classification problem with C mutually exclusive classes, softmax converts logits z₁, …, z_C into probabilities:

pᵢ = exp(zᵢ) ÷ ∑ⱼ₌₁ᶜ exp(zⱼ)

The probabilities sum to 1. Softmax is appropriate when exactly one class is treated as the correct class. For multilabel classification, independent sigmoid outputs are usually more suitable.

4. Forward Propagation

Forward propagation is the process of passing an input through the network to obtain a prediction. For layer l:

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

Here, W⁽ˡ⁾ is the weight matrix, b⁽ˡ⁾ is the bias vector, a⁽ˡ⁻¹⁾ is the previous layer’s activation, and φ⁽ˡ⁾ is the layer activation. The input is conventionally represented as a⁽⁰⁾. These equations assume compatible matrix dimensions and a consistent convention for whether examples are stored by rows or columns.

The output is compared with the target by a loss function. A loss is calculated for one example; a cost or empirical risk commonly refers to the average loss over a dataset.

5. Loss Functions

The loss function expresses how undesirable a prediction is.

Regression

For targets yᵢ and predictions ŷᵢ, mean squared error is:

MSE = (1 ÷ n)∑ᵢ₌₁ⁿ(yᵢ − ŷᵢ)²

It is appropriate when squared error is a meaningful penalty. Its units are the square of the target units.

Mean absolute error is:

MAE = (1 ÷ n)∑ᵢ₌₁ⁿ|yᵢ − ŷᵢ|

MAE has the same units as the target and is less affected by large errors than MSE.

Classification

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

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

For C-class single-label classification, categorical cross-entropy is:

L = −∑ᶜᵢ₌₁ yᵢ ln(pᵢ)

where yᵢ is 1 for the correct class and 0 otherwise. These losses strongly penalize confident incorrect predictions. Logarithms require probabilities strictly between 0 and 1 in the mathematical expression; implementations use numerical stabilization.

6. Gradient Descent and Optimization

Training seeks parameters θ—weights and biases—that minimize the average training loss. Gradient descent updates parameters in the direction opposite to the gradient:

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

θₜ is the parameter vector at iteration t, J is the objective, ∇θJ is its gradient, and η > 0 is the learning rate. The learning rate has no physical unit in ordinary normalized machine-learning problems, but it must be chosen to match the scale of the gradients and parameters.

Common training approaches are:

  • Batch gradient descent: uses the whole training set for each update.
  • Stochastic gradient descent: uses one example at a time.
  • Mini-batch gradient descent: uses a small batch and is the usual practical choice.
  • Adam: adapts step sizes using estimates of first and second moments of gradients. It often converges quickly, but still requires validation and learning-rate tuning.

An epoch is one complete pass through the training data. A batch is a subset processed before an update. Training loss alone is insufficient: validation performance is needed to detect poor generalization.

7. Backpropagation

Backpropagation efficiently computes derivatives of the loss with respect to all parameters by applying the chain rule from the output layer toward the input. It is not itself an optimizer; it supplies gradients to an optimizer.

For a parameter θ affecting intermediate quantities, the chain rule states:

∂L ÷ ∂θ = (∂L ÷ ∂a)(∂a ÷ ∂z)(∂z ÷ ∂θ)

Consider one neuron with z = wx + b, a = φ(z), and loss L(a, y). Then:

∂L ÷ ∂w = (∂L ÷ ∂a)φ′(z)x
∂L ÷ ∂b = (∂L ÷ ∂a)φ′(z)

The derivative φ′(z) measures how activation changes with its input. During backpropagation, intermediate values from the forward pass are reused. Automatic differentiation software applies these chain-rule operations while managing computational graphs.

Vanishing and exploding gradients

In deep or recurrent networks, repeated multiplication of derivatives can make gradients extremely small or very large. Vanishing gradients slow learning in early layers; exploding gradients create unstable updates. ReLU-like activations, careful initialization, normalization, residual connections, gradient clipping, and suitable architectures can help. No single remedy works in every model.

8. Generalization and Overfitting

A model generalizes when it performs well on unseen examples from the relevant data-generating process. Underfitting occurs when the model is too limited or insufficiently trained. Overfitting occurs when it learns training-specific noise or accidental patterns.

Typical evidence of overfitting is decreasing training loss alongside increasing validation loss. Important remedies include:

  • collecting more representative data;
  • data augmentation, such as realistic image transformations;
  • reducing model capacity;
  • weight regularization;
  • dropout;
  • early stopping based on validation performance;
  • careful feature and architecture selection;
  • preventing train–test leakage.

L2 regularization adds a penalty proportional to the squared weights:

Jᵣₑg = J + λ∑ⱼ θⱼ²

where λ ≥ 0 controls penalty strength and θⱼ are parameters included in the penalty. The bias may or may not be regularized depending on the implementation. Larger λ generally encourages smaller weights but can cause underfitting if excessive.

Dropout randomly omits units during training according to a specified probability, encouraging distributed representations. At inference, the network uses the full architecture with appropriately scaled activations, usually handled automatically by the framework.

9. Major Deep-Learning Architectures

Convolutional neural networks

CNNs exploit local structure and parameter sharing. A convolutional filter slides across an input and computes local weighted combinations. In images, this allows the same detector to recognize a feature at different positions. CNNs commonly include convolution, activation, pooling or strided operations, and task-specific output layers.

CNNs are suitable for image classification, object detection, segmentation, medical-image analysis, quality inspection, and some audio representations. Their limitations include sensitivity to distribution shifts, dependence on sufficient labeled data, and difficulty explaining individual decisions.

Sequence models

Recurrent neural networks process ordered observations. A simple recurrent update can be written:

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

xₜ is the input at time t, hₜ is the hidden state, and Wₓ, Wₕ, and b are learned parameters. LSTM and GRU architectures introduce gates that help preserve or discard information. Sequence models have been used for speech, sensor data, forecasting, and language, but long-range dependencies and sequential computation can limit them.

Transformers

Transformers use attention to relate elements of a sequence. In scaled dot-product attention:

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

Q, K, and V are query, key, and value matrices; dₖ is the key-vector dimension. The scaling reduces excessively large dot products. Self-attention allows each token to use information from other tokens, while positional information represents order. Transformers support parallel training and have become central to large language models, vision transformers, speech systems, and multimodal models.

Their challenges include high computational and memory costs for long sequences, large data requirements, possible hallucination or spurious correlations, and substantial energy and infrastructure demands.

10. Transfer Learning

Transfer learning starts with a model trained on one task or dataset and adapts it to a related task. A common workflow is:

  1. select a pretrained model;
  2. replace or add the task-specific output head;
  3. freeze most layers initially;
  4. train the new head;
  5. optionally unfreeze selected layers and fine-tune using a small learning rate;
  6. evaluate on an untouched test set.

Transfer learning can reduce data, training time, and computational requirements. It is not automatically safe: the source and target domains may differ, pretrained data may contain bias, and fine-tuning can cause catastrophic forgetting or leakage if data splits are poorly designed.

11. Selecting an Appropriate Application

Deep learning is especially suitable when:

  • the input is high-dimensional or unstructured;
  • large, representative datasets are available;
  • the task contains complex nonlinear patterns;
  • computational resources and monitoring are feasible;
  • the value of improved performance exceeds development and governance costs.

Traditional statistical or machine-learning methods may be preferable for small tabular datasets, highly interpretable decisions, strict resource constraints, or situations where a simpler model performs equally well. A responsible selection process considers accuracy, latency, cost, interpretability, robustness, privacy, fairness, and consequences of error.

Applied Case: Illustrative Medical-Image Triage System

Background

A hospital wants to prioritize chest radiographs for review when they may contain signs of an urgent condition. The system is intended to assist radiographers and radiologists, not replace clinical judgment.

Problem

Urgent cases are relatively uncommon, labels may contain disagreement, and images originate from different scanners and hospitals. A model that performs well on one hospital’s retrospective data may fail after deployment because of distribution shift.

Analysis

A CNN or pretrained vision transformer could learn image representations. The dataset should be divided at the patient level, preventing images from the same patient appearing in both training and test sets. The team should examine sensitivity, specificity, precision, calibration, and performance across demographic and acquisition subgroups. Accuracy alone may be misleading because urgent cases are imbalanced.

Approach and solution

The team fine-tunes a pretrained image model using training data and selects thresholds using a validation set. The final test set remains untouched until the design is fixed. Deployment includes human review, uncertainty or low-confidence routing, audit logs, drift monitoring, periodic recalibration, and a mechanism for reporting errors. Images and metadata are governed under applicable health-data and privacy requirements.

Outcome

The system reduces time to review for selected cases in a controlled pilot, but it is not authorized to make autonomous diagnoses. Monitoring shows lower performance on a subgroup, leading to additional data collection and model revision before expansion.

Learning points

The architecture is only one part of the solution. Data provenance, clinically meaningful evaluation, workflow design, human accountability, privacy, and post-deployment monitoring are equally important.

12. Practical Development Workflow

A professional deep-learning project normally follows these stages:

  1. define the decision, users, risks, and success metrics;
  2. collect, document, clean, and label data;
  3. create leakage-resistant training, validation, and test splits;
  4. establish a simple baseline;
  5. select and configure an architecture;
  6. train while recording experiments and random seeds where feasible;
  7. tune using validation data only;
  8. evaluate overall and subgroup performance;
  9. test robustness, security, latency, and resource usage;
  10. deploy cautiously with monitoring, rollback, and governance.

Data normalization should be computed from training data and then applied consistently to validation and test data. Hyperparameter tuning must not repeatedly use the final test set, because this gradually turns it into a training resource.

13. Ethics, Governance, and Limitations

Deep-learning systems can reproduce historical discrimination, expose sensitive information, or make confidently incorrect predictions. Important controls include data minimization, access control, documentation, consent or lawful data use, bias assessment, explainability appropriate to the users, human oversight, incident response, and clear responsibility.

Interpretability has several meanings: a model may provide feature importance, examples, saliency maps, confidence estimates, or a simpler surrogate explanation. None should automatically be treated as a proof of causality. Explanations must be evaluated for faithfulness and usefulness.

Security threats include data poisoning, evasion attacks, model extraction, prompt manipulation in generative systems, and leakage of training information. Governance should cover model versioning, approval, monitoring, change control, and retirement. Applicable laws and standards vary by jurisdiction and sector; technical teams must consult current institutional and regulatory requirements.

Contemporary Developments

Current practice increasingly combines pretrained foundation models, multimodal learning, parameter-efficient fine-tuning, retrieval-augmented systems, synthetic data, distillation, and edge deployment. These developments improve accessibility but do not remove the need for validation. Smaller distilled models may reduce latency and energy use, while quantization can reduce memory requirements at the possible cost of accuracy. Generative models require additional controls for factuality, copyright, privacy, misuse, and human review.

Chapter Summary

Artificial neurons compute weighted sums, add biases, and apply activation functions. Layers combine these operations into networks capable of learning nonlinear mappings. Forward propagation produces predictions; loss functions measure error; backpropagation computes gradients through the chain rule; and optimization updates parameters. Generalization is threatened by overfitting, which can be addressed through data quality, regularization, augmentation, dropout, early stopping, and careful validation.

CNNs exploit local spatial structure, sequence models represent ordered information, and transformers use attention to model relationships across elements. Transfer learning reuses representations learned from a source task. Deep learning is most valuable when data, complexity, and deployment resources justify it. Responsible practice requires leakage-resistant evaluation, subgroup analysis, privacy protection, human oversight, security, monitoring, and governance throughout the system lifecycle.

Key Terms

Artificial neuron: A computational unit that transforms weighted inputs through an activation function.

Weight: A learned parameter controlling an input’s influence.

Bias: A learned offset added to a weighted sum.

Activation function: A function that introduces nonlinear transformation.

Layer: A set of computational units operating at one stage of a network.

Logit: An unnormalized output before conversion to probabilities.

Forward propagation: Passing inputs through a network to obtain predictions.

Loss function: A numerical measure of prediction error.

Gradient: The vector of partial derivatives of an objective with respect to parameters.

Backpropagation: Efficient gradient computation using the chain rule.

Learning rate: A hyperparameter controlling optimization step size.

Epoch: One complete pass through training data.

Overfitting: Learning patterns that fail to generalize to new data.

Regularization: A method that discourages undesirable model complexity.

CNN: A neural network using convolution and local parameter sharing.

Recurrent neural network: A sequence model maintaining a hidden state over time.

Transformer: An architecture based primarily on attention mechanisms.

Transfer learning: Adapting knowledge from a pretrained model to another task.

Data leakage: Unintended use of information unavailable at prediction time.

Calibration: Agreement between predicted probabilities and observed frequencies.

Review Questions

Short Answer Questions

  1. What roles do weights and biases play in an artificial neuron?
  2. Why are nonlinear activation functions needed in multilayer networks?
  3. Distinguish a loss function from an optimizer.
  4. What is the purpose of a validation set?
  5. State one strength and one limitation of transformers.

Long Answer Questions

  1. Explain forward propagation and backpropagation, including the role of the chain rule.
  2. Compare sigmoid, ReLU, and softmax, including appropriate uses and limitations.
  3. Explain overfitting and discuss at least four practical remedies.
  4. Compare CNNs, recurrent networks, and transformers for different data types.
  5. Describe a responsible workflow for developing and deploying a deep-learning system.

Analytical / Application Questions

  1. A classifier has excellent training performance but poor validation performance. Diagnose likely causes and propose an ordered intervention plan.
  2. Select an architecture for classifying factory-camera images and justify your choice using data structure, performance, cost, and monitoring considerations.
  3. Design an evaluation plan for a healthcare model where urgent cases are rare. Explain why accuracy alone is insufficient and identify safeguards for deployment.

Multiple Choice Questions with Answers

Question 1

Which expression represents the output of an artificial neuron before activation?

A. z = ∑ᵢ wᵢxᵢ + b

B. z = ∑ᵢ xᵢ ÷ wᵢ

C. z = φ(x) − b

D. z = wᵢ ÷ xᵢ

Correct Answer: A. z = ∑ᵢ wᵢxᵢ + b

Question 2

Which activation is commonly used to produce probabilities for mutually exclusive classes?

A. ReLU

B. Softmax

C. Tanh

D. Linear activation

Correct Answer: B. Softmax

Question 3

What is the primary purpose of backpropagation?

A. To initialize all weights equally

B. To compute gradients of the loss with respect to parameters

C. To divide data into training and test sets

D. To convert images into text

Correct Answer: B. To compute gradients of the loss with respect to parameters

Question 4

In gradient descent, a parameter is generally updated in which direction?

A. The direction of the positive gradient

B. The direction opposite the gradient

C. A randomly selected direction only

D. The direction of increasing loss

Correct Answer: B. The direction opposite the gradient

Question 5

Which pattern most strongly suggests overfitting?

A. Both training and validation loss decrease together

B. Training loss decreases while validation loss increases

C. Both losses remain constant at zero

D. Validation performance is always higher because of leakage

Correct Answer: B. Training loss decreases while validation loss increases

Question 6

What structural property do CNNs primarily exploit for images?

A. Local spatial structure and parameter sharing

B. Only sequential word order

C. Random connections between all pixels

D. Removal of all nonlinearities

Correct Answer: A. Local spatial structure and parameter sharing

Question 7

What mechanism is central to transformers?

A. Pooling without learned parameters

B. Self-attention

C. Only recurrent hidden states

D. Manual feature selection

Correct Answer: B. Self-attention

Question 8

Why is patient-level splitting important in the medical-image case?

A. It guarantees perfect accuracy

B. It prevents related images from causing leakage across splits

C. It removes the need for validation data

D. It makes labels unnecessary

Correct Answer: B. It prevents related images from causing leakage across splits

Question 9

Which technique reuses a pretrained model for a related task?

A. Transfer learning

B. Data leakage

C. Random guessing

D. One-hot encoding

Correct Answer: A. Transfer learning

Question 10

Why can accuracy be misleading for a rare-event classifier?

A. Accuracy cannot be calculated for classifiers

B. A model can appear accurate while missing most minority cases

C. Accuracy always equals recall

D. Rare events make validation unnecessary

Correct Answer: B. A model can appear accurate while missing most minority cases

Introduction to AI and ML
Chapter 9 of 14