5 Machine Learning

Artificial Intelligence ยท Unit 5 ยท 9 hrs

Machine Learning

Exam-focused notes for Machine Learning (Artificial Intelligence, CSC266): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.

Machine Learning in short

This unit is about learning: the general idea of an agent that improves with experience, the main categories of learning problem, a statistical learning method (Naive Bayes), a population-based search method (genetic algorithms), and neural networks, including the concrete learning rules used to train them.

Machine Learning: notes

Introduction to Machine Learning

Machine learning is the sub-field of AI concerned with giving a computer the ability to improve at a task from experience, without being given explicit step-by-step rules for that task. Instead of a programmer supplying both the program and the data and expecting an output, as in traditional programming, a machine learning system is given data and a desired output and produces the program (the model) itself; the model then looks for patterns in the data provided and uses them to make decisions on data it has not seen before. Typical applications include computer vision, natural language processing, forecasting (such as stock market trends), game playing and expert systems.

Concepts of Learning

A learning agent consists of four conceptual components (introduced already in the intelligent-agent design of an earlier unit): a performance element, which uses the agent's current knowledge base to act and produce output; a learning element, which receives feedback from the environment and updates the knowledge base; a critic, which compares the performance element's output against a fixed external standard and produces the feedback the learning element needs; and a problem generator, which proposes exploratory experiments so the agent keeps encountering informative new experience rather than only exploiting what it already knows.

Supervised, Unsupervised and Reinforcement Learning

Supervised learning trains on a set of examples that already carry the correct output (a labelled training set), and the resulting model is then used to predict the label of new, unseen inputs; classification (for example, classifying a patient's records as showing a disease present or absent) is a standard example, and the process has two stages, building the model on training data and then testing its accuracy on held-out test data. Unsupervised learning, sometimes called predictive learning, trains on data with no labels at all, and instead looks for hidden structure in the data itself, such as clustering similar records together or discovering a new target market; it is closer to how infants learn from raw experience with no one telling them the right answer. A method that falls between the two, using a small amount of labelled data together with a much larger amount of unlabelled data, is able to improve on pure unsupervised learning while needing far less labelling effort than pure supervised learning. Reinforcement learning trains an agent through reward and punishment rather than labelled examples: the agent (learner) interacts with an environment by taking actions and receiving a reward or penalty signal in return, gradually learning which actions earn the most reward, as in a system recommending videos or news items based on how a user reacts. Reinforcement can be positive, strengthening a behaviour and increasing how often it is repeated (this sustains change well but can drown out the wanted behaviour if the reward is present in every state), or negative, strengthening a behaviour because an unpleasant condition is removed as a result of it (this raises the frequency of the behaviour but only to the minimum level needed to avoid the unpleasant condition).

What this unit covers

  • Introduction to Machine Learning
  • Concepts of Learning
  • Supervised, Unsupervised and Reinforcement Learning
  • Statistical-based Learning: Naive Bayes Model
  • Learning by Genetic Algorithm
  • Learning with Neural Networks: Introduction, Biological Neural Networks Vs. Artificial Neural Networks (ANN), Mathematical Model of ANN, Types of ANN: Feed-forward, Recurrent, Single Layered, Multi-Layered, Application of Artificial Neural Networks, Learning by Training ANN, Supervised vs. Unsupervised Learning, Hebbian Learning, Perceptron Learning, Back-propagation Learning

Learning with Neural Networks

2081 ยท 10 marks

How can you relate synapse, dendrite, and axon in biological neural networks with the elements of artificial neural networks? Create a multi-layer ANN with input layer, hidden layer, and output layer. Assume necessary inputs and weights to the ANN and illustrate a single iteration of backpropagation algorithm to train the ANN.[10]

Show solved answer

Artificial Neural Networks: Biological Analogy and Backpropagation

Given

The question requires us to assume inputs and weights, so the worked example below uses these values:

Inputs: $x_1 = 0.5$, $x_2 = 0.3$ Target: $t = 1.0$

Learning rate: $\alpha = 0.5$ Activation: Sigmoid $g(x) = \dfrac{1}{1+e^{-x}}$, derivative $g'(x) = g(x)(1-g(x))$ Biases: $0$

Initial weights:

WeightConnectionValue
$w_{13}$$x_1 \to h_1$0.4
$w_{23}$$x_2 \to h_1$0.2
$w_{14}$$x_1 \to h_2$0.3
$w_{24}$$x_2 \to h_2$0.5
$w_{35}$$h_1 \to o_1$0.6
$w_{45}$$h_2 \to o_1$0.7

Part 1: Biological vs Artificial Neural Network Mapping

Biological ElementArtificial ElementFunction
DendriteInput connections carrying $x_j$Receive incoming signals
SynapseWeight $w_{ij}$Modulate signal strength; learning adjusts these
AxonNeuron output lineTransmits processed signal onward
Soma (cell body)Summation $\Sigma$ + activation $g(\cdot)$Integrates weighted inputs and fires

Summary: dendrites $\to$ inputs, synapses $\to$ weights, soma $\to$ summation + activation, axon $\to$ output. Learning in ANN = adjusting synaptic weights.


Part 2: Network Architecture

A 2-2-1 multilayer network:

 x1 โ”€w13โ”€โ”        โ”Œโ”€w35โ”€โ”
         โ”œโ”€โ”€> h1 โ”€โ”ค     โ”‚
 x2 โ”€w23โ”€โ”˜        โ”‚     โ”œโ”€โ”€> o1 โ”€โ”€> y
                  โ”‚     โ”‚
 x1 โ”€w14โ”€โ”        โ”‚     โ”‚
         โ”œโ”€โ”€> h2 โ”€โ”ดโ”€w45โ”€โ”˜
 x2 โ”€w24โ”€โ”˜

Part 3: Single Iteration of Backpropagation

Step 1: Forward pass - hidden layer

$$net_{h1} = 0.4(0.5) + 0.2(0.3) = 0.20 + 0.06 = 0.26$$ $$h_1 = g(0.26) = \frac{1}{1+e^{-0.26}} = \frac{1}{1.7711} = 0.5646$$

$$net_{h2} = 0.3(0.5) + 0.5(0.3) = 0.15 + 0.15 = 0.30$$ $$h_2 = g(0.30) = \frac{1}{1+e^{-0.30}} = \frac{1}{1.7408} = 0.5744$$

Step 2: Forward pass - output layer

$$net_{o1} = 0.6(0.5646) + 0.7(0.5744) = 0.3388 + 0.4021 = 0.7409$$ $$y = g(0.7409) = \frac{1}{1+e^{-0.7409}} = \frac{1}{1.4767} = 0.6772$$

Step 3: Output error and delta

$$\delta_{o1} = (t - y)\cdot y(1-y) = (1.0 - 0.6772)(0.6772)(1-0.6772)$$ $$= 0.3228 \times 0.6772 \times 0.3228 = 0.07057$$

Step 4: Hidden layer deltas

$$\delta_{h1} = h_1(1-h_1),(w_{35},\delta_{o1})$$ $$= 0.5646(0.4354)(0.6 \times 0.07057) = 0.2458 \times 0.04234 = 0.01041$$

$$\delta_{h2} = h_2(1-h_2),(w_{45},\delta_{o1})$$ $$= 0.5744(0.4256)(0.7 \times 0.07057) = 0.2445 \times 0.04940 = 0.01208$$

Step 5: Weight updates $;w^{new} = w^{old} + \alpha,\delta,(\text{input to that weight})$

Output layer: $$w_{35}^{new} = 0.6 + 0.5(0.07057)(0.5646) = 0.6 + 0.01992 = 0.6199$$ $$w_{45}^{new} = 0.7 + 0.5(0.07057)(0.5744) = 0.7 + 0.02027 = 0.7203$$

Hidden layer: $$w_{13}^{new} = 0.4 + 0.5(0.01041)(0.5) = 0.4 + 0.00260 = 0.4026$$ $$w_{23}^{new} = 0.2 + 0.5(0.01041)(0.3) = 0.2 + 0.00156 = 0.2016$$ $$w_{14}^{new} = 0.3 + 0.5(0.01208)(0.5) = 0.3 + 0.00302 = 0.3030$$ $$w_{24}^{new} = 0.5 + 0.5(0.01208)(0.3) = 0.5 + 0.00181 = 0.5018$$

Result of one iteration

WeightOldNew
$w_{13}$0.40.4026
$w_{23}$0.20.2016
$w_{14}$0.30.3030
$w_{24}$0.50.5018
$w_{35}$0.60.6199
$w_{45}$0.70.7203

Output moved from $y = 0.6772$ toward target $t = 1.0$; repeating iterations reduces error.


Common mistake: treating $\delta = t - y = 0.3229$ as the output delta. The sigmoid derivative must be included, giving $\delta_{o1} = (t-y),y(1-y) = 0.0706$, and that is the value used in the weight updates. The forward pass gives $h_1 \approx 0.565$, $h_2 \approx 0.574$ and $y \approx 0.677$.

Open this answer on its own page โ†’

2081 ยท 5 marks

What is reinforcement learning? Configure an ANN neuron to simulate OR gate. [5]

Show solved answer

Reinforcement Learning and ANN Neuron for OR Gate

Given

This is a conceptual + design question. The relevant "data" is:

  • Task 1: Define reinforcement learning.
  • Task 2: Configure a single ANN neuron (perceptron) to implement the OR logic function.
  • OR gate truth table (standard, not given but implied):
$x_1$$x_2$$y$
000
011
101
111

No numeric parameters are supplied; the weights, bias, and threshold must be chosen by design.


Solution

Part 1: Reinforcement Learning

Reinforcement Learning (RL) is a machine learning paradigm in which an agent learns to make decisions by interacting with an environment through trial and error. The agent performs actions, observes the resulting state, and receives a reward (positive) or penalty (negative) as feedback. Its goal is to learn a policy (a mapping from states to actions) that maximizes the cumulative reward over time.

Key elements:

ElementMeaning
AgentThe learner / decision maker
EnvironmentThe world the agent interacts with
StateCurrent situation of the agent
ActionChoice made by the agent
RewardFeedback signal (reinforcement)
PolicyStrategy for choosing actions

There is no labelled training data as in supervised learning; instead learning is guided by the reward signal.

Types:

  • Positive reinforcement - a favourable outcome strengthens and increases the frequency of a behaviour.
  • Negative reinforcement - removing an unfavourable condition strengthens a behaviour.

Examples: game-playing agents (chess, Go), robot navigation, recommendation systems.


Part 2: ANN Neuron (Perceptron) for OR Gate

A single neuron computes a weighted sum plus bias, then applies a step activation function:

$$ \text{Net} = w_1 x_1 + w_2 x_2 + b $$

$$ y = \begin{cases} 1 & \text{if } \text{Net} \ge 0 \ 0 & \text{if } \text{Net} < 0 \end{cases} $$

Chosen parameters:

$$ w_1 = 1,\qquad w_2 = 1,\qquad b = -0.5 $$

This is equivalent to using weights $w_1 = w_2 = 1$ with a threshold $\theta = 0.5$ (since $\text{Net} \ge 0 \Leftrightarrow w_1x_1 + w_2x_2 \ge 0.5$).

Verification:

$x_1$$x_2$$\text{Net}=x_1+x_2-0.5$$\text{Net}\ge 0$?Output $y$Required
00$-0.5$No00 โœ“
01$0.5$Yes11 โœ“
10$0.5$Yes11 โœ“
11$1.5$Yes11 โœ“

All four outputs match the OR truth table.

Neuron diagram:

        w1 = 1
x1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
               โ”œโ”€โ”€โ–บ[ ฮฃ + b ]โ”€โ”€โ–บ Step โ”€โ”€โ–บ y
x2 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   (b = -0.5)
        w2 = 1

Result: With $w_1 = w_2 = 1$ and bias $b = -0.5$ (threshold $\theta = 0.5$), a single perceptron correctly realizes the OR gate.


Common mistake: stating bias $b = 0$ together with threshold $\theta = 0.5$. These are inconsistent: $b = 0$ corresponds to $\theta = 0$, and the bias matching $\theta = 0.5$ is $b = -0.5$. Applying the $\theta = 0.5$ rule in the verification table still gives the correct logical behaviour and weights.

Open this answer on its own page โ†’

2080 ยท 10 marks

What is the role of activation function in ANN. How sigmoid function works? Discuss about perceptron learning.[10]

--- An activation function determines the output of a neuron given its input. It introduces non-linearity into the network, enabling ANN to learn complex patterns. Role Description ------------------- Non-linearity Allows the network to learn non-linear mappings between inputs and outputs Output ...

Open this answer on its own page โ†’

2079 ยท 5 marks

Give an example of reinforcement learning. Explain the types of ANN. [5]

--- Reinforcement Learning is a type of dynamic learning that trains an agent using reward and punishment. The agent learns by interacting with its environment. It consists of three components: - Agent (the learner) - Environment (what the agent interacts with) - Action (what the agent can do) Wh...

Open this answer on its own page โ†’

2078 ยท 10 marks

Describe mathematical model of neural network. What does it means to train a neural network? Write algorithm for preceptron learning.[10]

--- A neural network is inspired by the biological neuron. The mathematical model of a single artificial neuron (also called a perceptron or node) consists of the following components: Component Description ------------------------ Inputs xโ‚, xโ‚‚, ..., xโ‚™ (features/signals) Weights wโ‚, wโ‚‚, ..., wโ‚™...

Open this answer on its own page โ†’

2076 ยท 10 marks

Define mathematical model of artificial neural network. Discuss how Hebbian learning algorithm can be used to train a neural network. Support your answer with an example.[10]

An Artificial Neural Network (ANN) is inspired by the biological neural network of the human brain. The mathematical model of a single artificial neuron (also called a McCulloch-Pitts neuron, first proposed in 1943) consists of the following components: Symbol Meaning ----------------- xโ‚, xโ‚‚, .....

Open this answer on its own page โ†’

Supervised, Unsupervised and Reinforcement Learning

2081 ยท 5 marks

What is reinforcement learning? Configure an ANN neuron to simulate OR gate. [5]

Show solved answer

Reinforcement Learning and ANN Neuron for OR Gate

Given Data

  • Task 1: Define reinforcement learning (conceptual).
  • Task 2: Configure a single ANN neuron (perceptron) to simulate the OR gate.
  • OR gate truth table (standard, boolean logic):
$x_1$$x_2$$y = x_1 \lor x_2$
000
011
101
111

No numeric parameters are supplied, so weights, bias, and threshold are to be designed.


Part 1: Reinforcement Learning

Reinforcement Learning (RL) is a machine learning paradigm in which an agent learns to make decisions by interacting with an environment. The agent performs actions, observes the resulting state, and receives feedback in the form of a reward (for good actions) or a penalty/punishment (for bad actions). The goal of the agent is to learn a policy (a mapping from states to actions) that maximizes the cumulative reward over time.

Unlike supervised learning, there is no labelled dataset; the agent learns purely by trial and error guided by reward signals.

Key elements:

ComponentMeaning
AgentThe learner / decision maker
EnvironmentThe system the agent interacts with
State ($s$)Current situation of the environment
Action ($a$)Choice made by the agent
Reward ($r$)Feedback signal (positive or negative)
Policy ($\pi$)Strategy mapping states to actions

Types of reinforcement:

  • Positive reinforcement: rewarding desirable behaviour so it occurs more often.
  • Negative reinforcement: removing an unpleasant condition to strengthen desirable behaviour.

Examples: game-playing agents (Chess, Go), robot navigation, recommendation systems.


Part 2: ANN Neuron (Perceptron) for OR Gate

A single neuron computes:

$$y = f!\left(\sum_i w_i x_i + b\right) = f(w_1 x_1 + w_2 x_2 + b)$$

Using a step activation function with threshold $\theta$:

$$f(net) = \begin{cases} 1 & \text{if } net \geq \theta \ 0 & \text{otherwise} \end{cases}$$

Chosen parameters

  • $w_1 = 1$
  • $w_2 = 1$
  • Threshold $\theta = 1$ (equivalently bias $b = -1$ with firing condition $net \geq 0$)

Verification against truth table

Case 1: $x_1=0,\ x_2=0$ $$net = (1)(0)+(1)(0) = 0 < 1 \Rightarrow y = 0\ \checkmark$$

Case 2: $x_1=0,\ x_2=1$ $$net = (1)(0)+(1)(1) = 1 \geq 1 \Rightarrow y = 1\ \checkmark$$

Case 3: $x_1=1,\ x_2=0$ $$net = (1)(1)+(1)(0) = 1 \geq 1 \Rightarrow y = 1\ \checkmark$$

Case 4: $x_1=1,\ x_2=1$ $$net = (1)(1)+(1)(1) = 2 \geq 1 \Rightarrow y = 1\ \checkmark$$

All four outputs match the OR gate truth table.

Neuron diagram

x1 ---(w1=1)---\
                (ฮฃ) --> net --> [Step, ฮธ=1] --> y
x2 ---(w2=1)---/

Conclusion

A single perceptron with $w_1 = 1$, $w_2 = 1$, and threshold $\theta = 1$ (bias $b = -1$) correctly simulates the OR gate.

Open this answer on its own page โ†’

2080.1 ยท 10 marks

Differentiate supervised learning from unsupervised? Discuss how Naive Bayes Model can be used for machine learning? Support your answer with example.[10]

--- The system is supplied with a set of training examples consisting of inputs and corresponding outputs (labelled data). It can take what it has learnt in the past and apply that to new data using labelled examples to predict future patterns and events. Classification is a classic example. Also...

Open this answer on its own page โ†’

Learning by Genetic Algorithm

2080 ยท 5 marks

Define selection, crossover, and mutation operations in genetic algorithm. [5]

--- Selection is the operator that determines which individuals from the current population are chosen as parents to produce the next generation. - Individuals with higher fitness scores are given preference and are more likely to pass their genes to the next generation. - This simulates the prin...

Open this answer on its own page โ†’

2078 ยท 5 marks

What is iscrossver operation in genetic algorithm? Given following chromosomes show the result of one-point and two point crossover. C1 = 01100010, C2 = 10101100. Choose appropriate crossover points as per your own suggestions. [5]

Show solved answer

Crossover Operation in Genetic Algorithm

Given Data

  • Chromosome $C_1 = 01100010$
  • Chromosome $C_2 = 10101100$
  • Each chromosome has 8 bits.
  • Crossover points to be chosen by student.

Position indexing:

C1 = 0 1 1 0 0 0 1 0
C2 = 1 0 1 0 1 1 0 0
     1 2 3 4 5 6 7 8

Definition of Crossover

Crossover (also called recombination) is a genetic operator in a Genetic Algorithm that combines the genetic material of two parent chromosomes to produce one or more offspring. Two parents are selected from the mating pool, one or more crossover points are chosen, and the segments of the parents are exchanged around these points. Crossover is the primary operator responsible for exploring new regions of the search space by recombining good building blocks (schemas) from both parents, thereby helping the population converge toward better solutions.


One-Point Crossover

Chosen crossover point: after position 4.

Split at position 4:

ChromosomeHead (pos 1-4)Tail (pos 5-8)
$C_1$01100010
$C_2$10101100

Swap the tails:

C1 = 0110 | 0010
C2 = 1010 | 1100
          ^  (cut after position 4)

O1 = 0110 | 1100  = 01101100
O2 = 1010 | 0010  = 10100010

Offspring: $O_1 = 01101100$, $O_2 = 10100010$.

Verification of bit copy:

  • O1 head = C1 head 0110 โœ“; O1 tail = C2 tail 1100 โœ“
  • O2 head = C2 head 1010 โœ“; O2 tail = C1 tail 0010 โœ“

Two-Point Crossover

Chosen crossover points: after position 2 and after position 6.

Split into three segments:

ChromosomeSeg 1 (1-2)Seg 2 (3-6)Seg 3 (7-8)
$C_1$01100010
$C_2$10101100

Check the middle segment of C1 (pos 3-6): C1 = 0 1 1 0 0 0 1 0 โ†’ 1000 โœ“ Middle segment of C2 (pos 3-6): C2 = 1 0 1 0 1 1 0 0 โ†’ 1011 โœ“

Swap the middle segment:

C1 = 01 | 1000 | 10
C2 = 10 | 1011 | 00
       ^        ^
  cut after 2   cut after 6

O1 = 01 | 1011 | 10 = 01101110
O2 = 10 | 1000 | 00 = 10100000

Offspring: $O_1 = 01101110$, $O_2 = 10100000$.


Summary Table

OperationCrossover PointsOffspring 1Offspring 2
One-Pointafter pos 40110110010100010
Two-Pointafter pos 2 & 60110111010100000

Crossover recombines features of both parents to generate new candidate solutions, which is essential for the genetic algorithm to search effectively and converge toward an optimal solution.

Open this answer on its own page โ†’

2076 ยท 5 marks

Write an algorithm for learning by Genetic Approach. [5]

Genetic Algorithms (GAs) are adaptive heuristic search algorithms that belong to the larger part of evolutionary algorithms. They are based on the idea of natural selection and genetics. Historical data are provided to find better solutions; GAs are used to generate high-quality solutions for opt...

Open this answer on its own page โ†’

2080.1 ยท 5 marks

Discuss how genetic algorithm works? [5]

Genetic Algorithms (GAs) are adaptive heuristic search algorithms that belong to the larger part of evolutionary algorithms. They are based on the idea of natural selection and genetics. Historical data are provided to find better solutions, or simply GAs are used to generate high quality solutio...

Open this answer on its own page โ†’