Introduction to Artificial Intelligence

Chapter 10
Neural Networks and Deep Learning

Bachelors Program in AI & ML
Introduction to Artificial Intelligence   •   Bachelor

Neural Networks and Deep Learning

Introduction and Context

Neural networks are computational models inspired loosely by the information-processing behavior of biological nervous systems. In artificial intelligence, they provide a flexible way to learn relationships between inputs and outputs from examples. A neural network may learn to classify an image, predict demand, recognize speech, translate text, or estimate risk without requiring a programmer to specify every decision rule explicitly.

The modern success of neural networks results from the interaction of three developments: large datasets, powerful parallel hardware such as graphics processing units (GPUs), and improved algorithms and architectures. Deep learning refers to neural networks containing multiple learnable layers that transform raw inputs into increasingly useful representations. The word “deep” describes the number of computational layers, not necessarily the size of the dataset or model.

This chapter develops the mathematical and practical foundations of neural networks, beginning with the perceptron and progressing to multilayer networks, convolutional neural networks, recurrent models, attention mechanisms, transformers, and transfer learning.

Learning Objectives

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

1. Explain the structure and limitations of a perceptron.

2. Describe how multilayer neural networks represent nonlinear functions.

3. Compare common activation functions and identify their practical uses.

4. Explain forward propagation, loss functions, backpropagation, and gradient-based optimization.

5. Describe the principles and applications of CNNs, recurrent networks, attention, and transformers.

6. Select appropriate architectures for common AI applications.

7. Explain transfer learning, overfitting, regularization, and responsible deployment considerations.

1. The Perceptron: A Foundational Model

A perceptron is a binary linear classifier. Given an input vector \(\mathbf{x}=(x_1,x_2,\ldots,x_d)\), weights \(\mathbf{w}\), and bias \(b\), it computes a weighted sum:

\[

z=\mathbf{w}^{T}\mathbf{x}+b=\sum_{i=1}^{d}w_i x_i+b.

\]

The classical perceptron applies a step function:

\[

\hat{y}=\begin{cases}1,&z\geq 0\\0,&z<0.

\end{cases}

\]

Here, \(x_i\) are features, \(w_i\) are learned parameters, \(b\) shifts the decision boundary, and \(\hat y\) is the predicted class. The model predicts class 1 on one side of the hyperplane \(\mathbf{w}^{T}\mathbf{x}+b=0\) and class 0 on the other side.

During training, for a labelled example with target \(y\), the perceptron rule updates parameters when the prediction is incorrect. One common form is:

\[

\mathbf{w}\leftarrow\mathbf{w}+\eta(y-\hat y)\mathbf{x},\qquad b\leftarrow b+\eta(y-\hat y),

\]

where \(\eta>0\) is the learning rate. This rule is applicable to linearly separable binary data under suitable presentation conditions. It does not guarantee a useful solution for data that cannot be separated by a hyperplane.

Worked Example: One Perceptron Update

Suppose \(\mathbf{x}=(2,1)\), \(y=1\), \(\mathbf{w}=(0.2,-0.4)\), \(b=-0.5\), and \(\eta=0.1\). Then

\[

z=(0.2)(2)+(-0.4)(1)-0.5=-0.5.

\]

Thus \(\hat y=0\). The error is \(y-\hat y=1\), so

\[

\mathbf{w}_{new}=(0.2,-0.4)+0.1(1)(2,1)=(0.4,-0.3),

\]

\[

b_{new}=-0.5+0.1=-0.4.

\]

The update moves the decision boundary toward classifying this example as class 1.

A major limitation is that a single perceptron cannot represent nonlinear patterns such as XOR. This limitation motivates networks with hidden layers and nonlinear activation functions.

2. Multilayer Neural Networks

A feed-forward neural network consists of an input layer, one or more hidden layers, and an output layer. Each neuron calculates a weighted sum followed by an activation function:

\[

\mathbf{a}^{(l)}=f^{(l)}(\mathbf{z}^{(l)}),\qquad

\mathbf{z}^{(l)}=\mathbf{W}^{(l)}\mathbf{a}^{(l-1)}+\mathbf{b}^{(l)}.

\]

Here, \(l\) denotes a layer, \(\mathbf{a}^{(0)}=\mathbf{x}\), \(\mathbf{W}^{(l)}\) is a weight matrix, \(\mathbf{b}^{(l)}\) is a bias vector, and \(f^{(l)}\) is applied element by element. The output of one layer becomes the input to the next. This process is called forward propagation.

Without nonlinear activations, stacking layers does not increase expressive power: a composition of linear transformations is itself linear. Nonlinear activations allow a multilayer network to construct complex decision regions and approximate broad classes of functions. The universal approximation theorem states, under mathematical assumptions, that a feed-forward network with at least one suitable hidden layer can approximate continuous functions on compact domains to arbitrary accuracy, given enough hidden units. The theorem does not imply that training is easy, that the required network is small, or that the learned function will generalize well.

For binary classification, a network commonly produces one output logit \(z\), converts it to a probability with the sigmoid function, and uses binary cross-entropy loss:

\[

\sigma(z)=\frac{1}{1+e^{-z}},

\]

\[

L=-[y\log \hat y+(1-y)\log(1-\hat y)],\qquad \hat y=\sigma(z).

\]

For mutually exclusive multi-class classification with \(K\) classes, softmax produces

\[

p_k=\frac{e^{z_k}}{\sum_{j=1}^{K}e^{z_j}},

\]

and categorical cross-entropy for target class \(y\) is \(L=-\log p_y\). In implementation, logits are often passed directly to a numerically stable combined softmax-cross-entropy operation.

3. Activation Functions

Activation functions influence gradient flow, output interpretation, and optimization.

Sigmoid. The sigmoid maps real values to \((0,1)\), making it useful for a binary probability output. However, its derivative becomes very small for highly positive or negative inputs, contributing to vanishing gradients when used repeatedly in deep hidden layers.

Hyperbolic tangent.

\[

\tanh(z)=\frac{e^z-e^{-z}}{e^z+e^{-z}}.

\]

It maps values to \((-1,1)\) and is zero-centred, but it can also saturate.

Rectified linear unit (ReLU).

\[

\operatorname{ReLU}(z)=\max(0,z).

\]

ReLU is computationally simple and usually provides stronger gradients for positive inputs. A unit can become inactive if it receives negative inputs consistently; this is sometimes called the dying-ReLU problem.

Leaky ReLU.

\[

f(z)=\max(\alpha z,z),

\]

where a small \(\alpha\), such as 0.01, preserves a nonzero negative-side slope.

GELU. The Gaussian error linear unit is widely used in transformer architectures. It smoothly weights inputs according to their magnitude and often performs well in large language and vision models.

The output activation should match the task: sigmoid for a binary probability, softmax for mutually exclusive classes, and often a linear activation for an unrestricted regression output. A poor output choice can make the loss or predictions mathematically inappropriate.

4. Loss Functions and Learning

A loss function measures disagreement between a model prediction and a target. Training seeks parameters that minimize average loss over a training set:

\[

J(\theta)=\frac{1}{n}\sum_{i=1}^{n}L(f_\theta(\mathbf{x}_i),y_i),

\]

where \(\theta\) represents all weights and biases. The empirical objective may be augmented with a regularization term, such as \(\lambda\|\mathbf{w}\|_2^2\), to discourage excessively large weights.

For a regression problem, mean squared error is

\[

\operatorname{MSE}=\frac{1}{n}\sum_{i=1}^{n}(y_i-\hat y_i)^2.

\]

It is sensitive to large errors and is appropriate when squared deviations represent the application’s cost. Mean absolute error, \(\frac{1}{n}\sum_i|y_i-\hat y_i|\), is less sensitive to outliers but is not differentiable at zero; optimization libraries handle this using subgradients or smooth variants.

Gradient Descent

Gradient descent updates parameters in the direction that reduces the objective:

\[

\theta\leftarrow\theta-\eta\nabla_\theta J(\theta).

\]

The gradient \(\nabla_\theta J\) contains partial derivatives of the loss with respect to all parameters. In batch gradient descent, the gradient uses all training examples. In stochastic or mini-batch gradient descent, it uses one example or a small batch, producing a noisier but computationally efficient estimate.

Modern optimizers include momentum, RMSProp, and Adam. Adam maintains moving estimates of first and second moments of gradients and adapts parameter-specific step sizes. It is a strong default, but it does not eliminate the need for learning-rate selection, validation, and monitoring.

5. Backpropagation

Backpropagation is an efficient application of the chain rule for calculating gradients in layered networks. It is not itself an optimizer; it supplies derivatives to an optimizer.

Consider a simple network with hidden pre-activation \(\mathbf{z}^{(1)}\), hidden activation \(\mathbf{a}^{(1)}=f(\mathbf{z}^{(1)})\), and output \(\hat y=g(\mathbf{z}^{(2)})\). The forward pass computes values from input to output. The backward pass computes error signals from output to input.

For a scalar output and loss \(L\), define

\[

\delta^{(2)}=\frac{\partial L}{\partial \mathbf{z}^{(2)}}.

\]

Then the output-layer gradients are

\[

\frac{\partial L}{\partial \mathbf{W}^{(2)}}=\delta^{(2)}(\mathbf{a}^{(1)})^T,

\qquad

\frac{\partial L}{\partial \mathbf{b}^{(2)}}=\delta^{(2)}.

\]

The hidden-layer error is propagated as

\[

\delta^{(1)}=((\mathbf{W}^{(2)})^T\delta^{(2)})\odot f'(\mathbf{z}^{(1)}),

\]

where \(\odot\) denotes element-wise multiplication. Then

\[

\frac{\partial L}{\partial \mathbf{W}^{(1)}}=\delta^{(1)}\mathbf{x}^T,

\qquad

\frac{\partial L}{\partial \mathbf{b}^{(1)}}=\delta^{(1)}.

\]

These equations assume compatible matrix dimensions and differentiability, or an appropriate subgradient, at the activation. In practice, software frameworks construct a computational graph and use automatic differentiation to perform equivalent calculations.

Training Workflow

A sound training process usually involves:

1. Splitting data into training, validation, and test sets.

2. Preprocessing features using statistics computed from the training set only.

3. Initializing parameters, often with variance-aware schemes such as Xavier or He initialization.

4. Repeating mini-batch forward propagation, loss calculation, backpropagation, and parameter updates.

5. Monitoring training and validation metrics.

6. Selecting hyperparameters using validation data and reporting final performance on an untouched test set.

If training performance improves while validation performance worsens, the model is likely overfitting. Remedies include more data, data augmentation, weight decay, dropout, early stopping, a smaller model, or improved data quality. Dropout randomly suppresses units during training and uses a corresponding scaling convention so that expected activations remain consistent at evaluation time.

6. Convolutional Neural Networks

Convolutional neural networks (CNNs) are particularly effective for images and other grid-like data. A convolutional layer applies learnable filters across local regions. For a two-dimensional single-channel input \(X\) and kernel \(K\), a cross-correlation operation commonly implemented in deep-learning libraries is

\[

S(i,j)=\sum_m\sum_n K(m,n)X(i+m,j+n)+b.

\]

Although often called convolution, this operation does not necessarily flip the kernel as in the strict mathematical definition of convolution. The output size depends on input dimensions, kernel size, stride, and padding. Multiple filters produce multiple feature maps.

CNNs exploit local connectivity, parameter sharing, and hierarchical representation. Early layers may detect edges, middle layers textures or parts, and deeper layers object-level patterns. Pooling or strided convolution can reduce spatial resolution and increase the receptive field, though excessive reduction may discard important information.

CNN applications include medical-image analysis, quality inspection, autonomous perception, satellite-image interpretation, and document recognition. Practical concerns include class imbalance, image leakage between training and test sets, sensitivity to distribution shifts, and the need to evaluate performance across relevant subgroups.

7. Recurrent Models and Sequence Learning

Sequential data has an order: examples include sentences, sensor streams, transaction histories, and speech. A recurrent neural network (RNN) maintains a hidden state that is updated at each time step:

\[

\mathbf{h}_t=\phi(\mathbf{W}_{xh}\mathbf{x}_t+\mathbf{W}_{hh}\mathbf{h}_{t-1}+\mathbf{b}_h),

\]

\[

\hat{\mathbf{y}}_t=g(\mathbf{W}_{hy}\mathbf{h}_t+\mathbf{b}_y).

\]

The same weights are reused across time, allowing variable-length sequences. Training commonly uses backpropagation through time, which unfolds the recurrence over sequence positions. Long sequences can cause vanishing or exploding gradients because repeated multiplication by Jacobian matrices can shrink or amplify signals.

Long short-term memory (LSTM) and gated recurrent unit (GRU) architectures use gates to regulate information flow. An LSTM has input, forget, and output gates and a cell state designed to preserve information over longer intervals. Gating improves optimization but does not guarantee perfect long-range reasoning. RNNs remain useful for streaming and resource-constrained applications, although transformers have become dominant for many large-scale sequence tasks.

8. Attention and Transformers

Attention allows a model to assign different importance to different elements of an input sequence when producing a representation. In scaled dot-product attention, queries \(Q\), keys \(K\), and values \(V\) are combined as

\[

\operatorname{Attention}(Q,K,V)=\operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V,

\]

where \(d_k\) is the key-vector dimension. The scaling reduces excessively large dot products that could make the softmax overly concentrated.

A transformer uses self-attention, feed-forward sublayers, residual connections, normalization, and positional information. Self-attention allows each token to interact directly with other tokens in the sequence. Multi-head attention runs several attention mechanisms with different learned projections and combines their outputs. Encoder-only transformers are commonly used for representation and classification; decoder-only transformers generate sequences autoregressively; encoder-decoder transformers map one sequence to another, as in translation.

Transformers parallelize many training computations better than recurrent models, but standard full self-attention has approximately quadratic computational and memory cost in sequence length. This creates challenges for very long documents. Contemporary research addresses this through sparse attention, efficient kernels, retrieval, state-space approaches, and hierarchical processing.

Attention weights should not automatically be interpreted as faithful explanations of a model’s reasoning. Explanation claims require additional analysis, such as perturbation tests, feature attribution, counterfactual evaluation, or expert review.

9. Transfer Learning and Modern Practice

Transfer learning reuses knowledge learned from one task or dataset for another. A common workflow is to begin with a pretrained model, replace or adapt its output head, train the new head, and optionally fine-tune some or all of the base model. Freezing the base reduces computation and helps when the target dataset is small; full fine-tuning may produce better adaptation when domains differ sufficiently and data is available.

Related techniques include feature extraction, parameter-efficient fine-tuning, adapters, prompt-based adaptation, and low-rank updates. Transfer learning is powerful because early or intermediate representations may be reusable, but it can also transfer unwanted biases, spurious correlations, licensing restrictions, or privacy risks.

Best practices include:

  • Define the task, users, costs of errors, and deployment constraints before selecting an architecture.
  • Establish a simple baseline, such as logistic regression or a small CNN, before adopting a large model.
  • Ensure that data splits reflect deployment conditions and prevent leakage.
  • Normalize or standardize inputs consistently, using training-set statistics.
  • Track experiments, random seeds, data versions, model versions, and hyperparameters.
  • Evaluate calibration, subgroup performance, robustness, latency, memory, and energy use—not only average accuracy.
  • Use confidence thresholds, human review, monitoring, and rollback procedures in high-impact applications.

10. Illustrative Case: Defect Detection in Manufacturing

Background. A manufacturer wants to identify scratches and dents on metal components using camera images. Manual inspection is slow and variable, while missed defects can result in costly recalls.

Problem. The company has 40,000 labelled images from two factories. Defects are rare, lighting differs between factories, and images from the same production batch are highly similar. A random image-level split could place near-duplicate images in both training and test sets, producing an unrealistically high score.

Analysis. Engineers first define the operational objective: minimize missed critical defects while maintaining an acceptable false-alarm rate. They group the data by production batch and time, reserving later batches and one factory for evaluation. They inspect labels, measure class balance, and identify lighting and camera differences.

Approach. A small CNN provides a baseline. A pretrained image model is then adapted using transfer learning. During initial training, the pretrained backbone is frozen and a new classification head is trained. Selected upper layers are subsequently fine-tuned using a low learning rate. Augmentation simulates plausible lighting and orientation changes but does not create physically impossible defects. Because missed defects are more costly, the decision threshold is selected using validation data rather than automatically fixed at 0.5.

Performance is reported using recall for the defect class, precision, confusion matrices, and precision-recall curves. The final test uses unseen batches and the second factory. An error analysis examines false negatives by defect type and lighting condition. The system sends uncertain cases to human inspectors rather than making an automatic reject decision.

Outcome. The model improves inspection speed and achieves useful recall, but performance drops under a new camera configuration. The company therefore adds camera-specific calibration, monitoring for image-distribution shifts, and a process for collecting and relabelling difficult examples.

Learning points. Appropriate data splitting is as important as architecture selection. Transfer learning can reduce training requirements, but deployment evaluation must reflect real operating conditions. Thresholds should reflect business and safety costs. Human oversight, monitoring, and documentation remain necessary even when average accuracy is high.

11. Limitations, Ethics, and Governance

Neural networks are powerful statistical systems, not guaranteed reasoning engines. They may learn correlations that fail under distribution shift, exploit shortcuts, reproduce dataset errors, or generate confident but incorrect outputs. Large models also require substantial computation, which has financial and environmental costs.

Ethical risks include discriminatory outcomes, privacy leakage, unauthorized use of personal data, unclear accountability, and automation bias. In high-impact domains such as healthcare, employment, education, credit, and public services, organizations should conduct impact assessments, document data provenance, test subgroup performance, provide appropriate human review, and establish appeal and incident-response mechanisms.

Governance should address model purpose, authorized users, security, access control, retention, auditability, versioning, monitoring, and retirement. Legal requirements vary by jurisdiction, but technical compliance alone is insufficient: organizations must connect model evaluation to rights, institutional responsibilities, and the consequences of error.

Contemporary Developments

Current neural-network practice includes multimodal models that combine text, images, audio, and video; foundation models pretrained on broad datasets; retrieval-augmented generation; diffusion models for generation; federated and privacy-preserving learning; and specialized accelerators. These developments increase capability but also intensify concerns about data provenance, copyright, misinformation, security, and concentration of computational resources.

A responsible practitioner should distinguish benchmark performance from reliable deployment. Reproducible evaluation, transparent limitations, human-centered design, and continuous monitoring are central to professional AI engineering.

Chapter Summary

Neural networks learn parameterized transformations from data. The perceptron provides a linear decision boundary, while multilayer networks use nonlinear activations to represent complex functions. Forward propagation computes predictions; a suitable loss measures error; backpropagation applies the chain rule to compute gradients; and optimization algorithms update parameters. Activation choice, initialization, regularization, data splitting, and validation strongly affect results.

CNNs exploit local structure and shared filters in images. RNNs represent sequences through recurrent states but may suffer from unstable long-range gradients. Attention directly relates sequence elements, and transformers organize attention with feed-forward layers, residual connections, normalization, and positional information. Transfer learning makes pretrained representations useful for new tasks, although it can transfer bias and fail under domain shift.

Successful applications require more than a high accuracy score. Data quality, leakage prevention, calibration, robustness, subgroup evaluation, cost-sensitive thresholds, monitoring, security, human oversight, and governance are essential to responsible deployment.

Key Terms

Perceptron: A binary linear classifier using weighted inputs and a threshold.

Weight: A learnable coefficient controlling the influence of an input or activation.

Bias: A learnable offset added before activation.

Activation function: A function that transforms a neuron’s pre-activation value.

Logit: An unnormalized score, often converted into a probability.

Forward propagation: Computing network activations from input to output.

Loss function: A numerical measure of prediction error.

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

Backpropagation: Efficient calculation of parameter gradients using the chain rule.

Epoch: One complete pass through the training dataset.

Overfitting: Learning training-specific patterns that do not generalize.

Regularization: A method that discourages undesirable model complexity.

Convolution: A local sliding operation using shared filter parameters.

Feature map: An activation map produced by a convolutional filter.

Recurrent neural network: A network that updates a hidden state across sequence steps.

Attention: A mechanism that weights information according to learned relevance.

Transformer: An architecture based primarily on attention and feed-forward blocks.

Transfer learning: Reusing knowledge from a pretrained model for another task.

Data leakage: Information from evaluation data or future conditions improperly influencing training.

Calibration: Agreement between predicted probabilities and observed frequencies.

Review Questions

Short Answer Questions

1. What limitation of a single perceptron motivates the use of hidden layers?

2. Why are nonlinear activation functions necessary in multilayer networks?

3. Distinguish backpropagation from gradient descent.

4. State two properties that make CNNs suitable for image data.

5. What is the purpose of a validation set?

Long Answer Questions

1. Explain the structure and training process of a multilayer feed-forward neural network.

2. Compare sigmoid, tanh, ReLU, and Leaky ReLU, including their advantages and limitations.

3. Explain how CNNs learn hierarchical visual representations.

4. Compare RNNs and transformers for sequence modelling.

5. Discuss transfer learning, including its benefits, risks, and recommended workflow.

Analytical / Application Questions

1. A binary classifier has excellent training accuracy but poor validation recall for a minority defect class. Diagnose possible causes and propose a technically and operationally appropriate improvement plan.

2. Design an evaluation strategy for a neural model used in loan-risk assessment. Explain how you would address leakage, subgroup performance, calibration, human review, and monitoring.

3. A company must classify short text messages on a device with limited memory and intermittent connectivity. Compare a small RNN, a compact transformer, and transfer learning, then justify a deployment choice using accuracy, latency, privacy, and maintenance criteria.

Multiple Choice Questions with Answers

Question 1

What is the primary limitation of a single perceptron?

A. It cannot process numerical inputs

B. It cannot represent nonlinearly separable decision boundaries

C. It cannot use weights

D. It cannot be trained from labelled examples

Correct Answer: B. It cannot represent nonlinearly separable decision boundaries

Question 2

Which activation is most commonly associated with a nonnegative hidden-unit output and the dying-unit problem?

A. ReLU

B. Softmax

C. Sigmoid

D. Linear

Correct Answer: A. ReLU

Question 3

What is the role of backpropagation?

A. To randomly initialize all weights

B. To compute gradients efficiently using the chain rule

C. To split data into training and test sets

D. To convert logits into class labels

Correct Answer: B. To compute gradients efficiently using the chain rule

Question 4

For mutually exclusive K-class classification, which output transformation produces probabilities summing to one?

A. ReLU

B. Sigmoid applied independently without normalization

C. Softmax

D. Tanh

Correct Answer: C. Softmax

Question 5

Which property of CNNs reduces the number of parameters compared with connecting every pixel to every neuron?

A. Parameter sharing across local regions

B. Using only binary labels

C. Removing all nonlinearities

D. Avoiding gradient computation

Correct Answer: A. Parameter sharing across local regions

Question 6

What difficulty can occur when training an RNN over very long sequences?

A. The input must always have exactly two features

B. Gradients may vanish or explode

C. The model cannot reuse weights

D. Softmax becomes impossible

Correct Answer: B. Gradients may vanish or explode

Question 7

In scaled dot-product attention, why is QK^T divided by the square root of the key dimension?

A. To make all values exactly zero

B. To reduce excessively large dot products before softmax

C. To remove positional information

D. To force the model to be recurrent

Correct Answer: B. To reduce excessively large dot products before softmax

Question 8

Which data split should normally be used for final unbiased performance reporting?

A. The training set used for parameter fitting

B. The validation set used for hyperparameter selection

C. An untouched test set

D. A set constructed from model predictions

Correct Answer: C. An untouched test set

Question 9

What is a central benefit of transfer learning?

A. It guarantees fairness in every target domain

B. It reuses representations learned from a related pretrained task

C. It eliminates the need for evaluation data

D. It prevents all distribution shifts

Correct Answer: B. It reuses representations learned from a related pretrained task

Question 10

In the manufacturing case, why were images grouped by production batch when splitting data?

A. To increase the number of duplicate images

B. To prevent near-duplicate information from leaking across splits

C. To ensure every batch had identical lighting

D. To avoid using a validation set

Correct Answer: B. To prevent near-duplicate information from leaking across splits

Introduction to Artificial Intelligence
Chapter 10 of 15