Artificial Intelligence (AI)
Graphs and the two search algorithms that find routes through them, and how machines learn: neural networks, back propagation, regression and the three kinds of learning.
Graphs: how an AI problem is drawn
Many AI problems become one question on a graph: what is the best path from here to there?
A graph is a set of nodes (also called vertices) joined by edges (lines). Think of towns joined by roads.
An edge can have:
- a weight: a value such as a distance, a cost or a time
- a direction
Graphs describe anything made of connections:
- roads between towns
- links in a network
- moves in a game
- the nodes of a neural network
Example:The graph below has 10 nodes and 16 edges. There are 44 paths from S to G that never visit a node twice.
Trying them all is too slow. So AI needs an algorithm that finds the best path without trying every one.
Dijkstra's algorithm
Dijkstra's algorithm finds the shortest distance from the start node to every other node.
First, set up the distances:
- The start node gets 0.
- Every other node gets infinity (∞).
Then repeat two steps:
- Choose the unvisited node with the smallest distance. Mark it visited.
- Look at each of its neighbours. Is the distance through this node smaller than the one recorded? Then replace it, and write this node as the neighbour's previous node.
Stop when the destination is visited. Then read the path backwards, from previous node to previous node.
Watch out:Dijkstra does not know where the goal is, so it spreads out in every direction. Here it visits all 10 nodes, and G is the last.
Node S P Q R A B C D E G
Distance 0 3 3 6 3 3 6 8 8 11
Previous - S S P S S A A B D
Visit order: S A B P Q C R D E G
Back from G: G D A S
Path: S → A → D → G, cost 11A*: Dijkstra with a sense of direction
A* is Dijkstra plus a guess of the distance left, so it tries nodes towards the goal first.
A* adds a heuristic, h: an estimate of how far a node is from the goal. For each node A* keeps these values:
| Meaning | |
|---|---|
g | the distance from the start to the node |
h | the estimate of the distance from the node to the goal |
f = g + h | the two added together |
A* always expands the node with the smallest f. So nodes heading towards the goal are tried first. Nodes leading away are often never examined.
| Dijkstra | A* | |
|---|---|---|
| Nodes examined here | visits 10 | expands 7; never P, Q or R |
| Path found | S → A → D → G | S → A → D → G |
| Cost | 11 | 11 |
Remember:If h never overestimates the real distance left, A* still finds the shortest path.
Node g h f = g + h
S 0 8 8
A 3 6 9
B 3 6 9
C 6 4 10
D 8 2 10
E 8 2 10
G 11 0 11
P 3 10 13 never expanded
Q 3 10 13 never expandedMachine learning and its three categories
In machine learning a system learns from data or experience, instead of a programmer giving it every rule.
Machine learning is AI in which a system gets better at a task by learning. It learns from data or experience. A programmer does not give it every rule.
There are three categories:
| Learns from | Produces | Example | |
|---|---|---|---|
| Supervised learning | labelled examples: inputs paired with the correct outputs (input → output) | predictions: it maps new inputs to outputs | spam or not spam; a house's price |
| Unsupervised learning | data with no labels | the groups and patterns it found by itself | customers with similar habits |
| Reinforcement learning | trial and error in an environment: its actions earn rewards or penalties | the best action in each state, the one that earns the most reward over time | a robot learning to walk |
Remember:Labels mean supervised. No labels means unsupervised. Rewards mean reinforcement.
Artificial neural networks
A neural network learns by changing the weights on its connections until its outputs are right.
An artificial neural network is modelled on the human brain. It has layers of connected nodes:
- an input layer
- one or more hidden layers (layers between the input and the output)
- an output layer
Every connection has a weight (a number).
What one node does:
- It multiplies each input by its connection's weight.
- It adds up the results and passes the total on.
- Real networks also pass the total through an activation function (an extra function applied to the total).
What the network knows is held in its weights. Training changes the weights until the outputs are right. So the network learns from examples. Nobody programs it with rules.
Exam:How have neural networks helped machine learning? They can learn patterns too complex for anyone to write down.
Back propagation of errors
Back propagation sends the error backwards through the network and changes each weight to make the error smaller.
Back propagation of errors trains a network in a loop of three steps:
- Forward: feed in an example. The network gives an output.
- Compare: compare the output with the correct answer, the target. This gives the error.
- Backward: send the error back through the network, layer by layer. Change each weight in the direction that makes the error smaller.
How much does a weight change? It depends on how much that weight added to the error.
Repeat the loop for many examples, many times, until the error is small enough.
Example:The figure trains one example with a learning rate (the size of each change) of 0.5. The error falls from 0.625 to 0.306 after one step, then to 0.046.
forward
h = w1·x1 + w2·x2 = 0.75
y = w3·h = 0.375
error
target − y = 1 − 0.375 = 0.625
backward
change in w3 = rate × error × h
= 0.5 × 0.625 × 0.75 = 0.234375
change in w1 = rate × error × w3 × x1
= 0.5 × 0.625 × 0.5 × 1 = 0.15625
change in w2 = rate × error × w3 × x2
= 0.5 × 0.625 × 0.5 × 0.5 = 0.078125
new
w1 = 0.65625 w2 = 0.578125
w3 = 0.734375
again
y = 0.694, error = 0.306 (smaller)Regression
Regression predicts a number by fitting a function to the training data with the smallest total error.
Regression is a supervised learning method. It predicts a number, such as a price or a mark. It does not predict a category.
It fits a mathematical function to the training data. The total error between the function and the points must be as small as possible.
The simplest kind is linear regression:
- The function is a straight line, y = mx + c.
- The line is chosen to make the sum of the squared errors smallest. Each error is the gap between a point and the line.
- The fitted line then predicts y for a new x.
Example:For the six points in the figure, the best line is y = 1.086x + 1.2. Its sum of squared errors is 3.371. At x = 7 it predicts 8.8.
Deep learning
Deep learning uses neural networks with many hidden layers that learn features from raw data, simple to complex.
Deep learning is machine learning that uses artificial neural networks with many hidden layers.
Each layer extracts features (useful details) from the output of the layer before. It builds from simple features to complex ones. The network learns these features by itself, from the raw data.
Example:In photos of people, early layers find edges. Later layers find shapes. The last layers find faces.
| Why use deep learning | The costs | |
|---|---|---|
| Data | works well on unstructured data such as images, sound and text | needs huge amounts of training data |
| Power | does better than other methods when there is a lot of data | needs a lot of processing power |
| Answers | can find patterns people cannot see | it can be hard to explain why it gave an answer |
Reinforcement learning
In reinforcement learning an agent learns by doing: actions earn rewards or penalties, and it learns which work best.
In reinforcement learning, the agent is the program that learns. It acts in an environment, which is in some state (situation) at each moment.
The agent repeats a loop:
- In the current state, it chooses an action.
- The environment gives it a reward or a penalty.
- It updates what it knows about how good that action is in that state.
At first the agent explores almost at random. Over many attempts, the best actions earn the highest values. The agent follows them more and more.
Use it where there is no labelled data, but results can be scored:
- games
- robots learning to walk
- self-driving cars trained in a simulation
Which method, and why
Choose the method from what you have: answers, no answers, rewards, big unstructured data, or a known map.
Use machine learning when the rules are too complex or too many to write by hand. Use it also when the answers must get better as more data arrives.
| What you have | Choose |
|---|---|
| examples with the right answers | supervised learning |
| data but no answers, and you want to find structure in it | unsupervised learning |
| no examples, but actions can be rewarded | reinforcement learning |
| large, unstructured data, such as photos or speech | deep learning |
| a known map of connections, and the question is a route | a graph algorithm, not learning |
Match each description to the term it defines.
PICK ONE, THEN PICK ITS GROUP
- empty
- empty
- empty
- empty
KEY TERMS
CHECK YOURSELF
1.Training a model on emails already labelled 'spam' or 'not spam' is:
2.Why does A* usually examine fewer nodes than Dijkstra?
3.In A*, which node is expanded next?
4.What makes deep learning 'deep'?
5.In back propagation of errors, what is sent backwards through the network?
6.Which task is regression used for?
7.A robot learns to walk by trying moves and getting a reward when it stays upright. This is: