If you have unlocked a phone with your face, seen a translated web page, or asked a chatbot a question, you have used AI today. It can feel like magic, or like a mind in a box. It is neither. By the end of this lesson you will have built a tiny piece of AI yourself, and you will know what kind of thing it is.
Two ways to get a computer to do something
For most of computing history, getting a computer to do something meant writing down exactly what to do. A thermostat follows a rule a person wrote: if the room is colder than 19°C, turn on the heat. That works well when you can state the rule.
Now try writing the rule for “does this photo contain a cat?” Pointy ears? Some cats fold theirs. Fur? So do dogs. Whiskers? Not visible from behind. Every rule you write has exceptions, and the exceptions have exceptions.
Machine learningA way of building software where the behavior is learned from examples with known answers, instead of being written as rules by a person.Open in glossary takes a different route. Instead of writing the rule, you collect many examples where you already know the answer, such as thousands of photos labeled “cat” or “not cat”. Then an algorithm searches for a rule that fits those examples. The rule it finds is called a ModelThe rule a learning algorithm produces from data. Given an input, a model returns a prediction. In neural networks, the model is defined by its parameters.Open in glossary.
Traditional programming
Machine learning
Teach by example
Here is the idea at its smallest. Every dot below is an example with a known answer: blue or orange. Your job is to teach. The computer’s job is to guess the color of any new point, and the shading shows its guess everywhere on the plane at once.
Teach by example
Add labeled examples. The shading shows what the computer would guess for a new point anywhere on the plane.
Challenges
0 of 3 complete
- Place the test point where the vote is as close as it can be: won by a single neighbor (for example 3 to 2 with k = 5). (not yet)
- Load Ring, then find the smallest k at which the model starts getting its own examples wrong. (not yet)
- Clear the board and teach a shape of your own: at least 6 blue and 6 orange examples, every one guessed right with k = 3. (not yet)
Try this
- Add a few orange points inside the blue area. The shading bends around them right away.
- Set k to 1 and add a single blue point deep in orange territory. The computer believes it completely. Now raise k to 7: one odd example gets outvoted by its neighbors.
- Load Ring. No straight line separates the two colors, yet the examples alone describe the shape.
- Choose Test a point and drag it around. The dashed lines show which examples the computer consults.
The same idea in a few lines of codePython with NumPy
import numpy as np
# Six labeled examples: positions across and up, 0 = blue, 1 = orange.
points = np.array([[0.2, 0.7], [0.3, 0.6], [0.25, 0.8],
[0.7, 0.3], [0.8, 0.4], [0.6, 0.2]])
labels = np.array([0, 0, 0, 1, 1, 1])
def guess(query, k):
distances = np.linalg.norm(points - query, axis=1)
nearest = np.argsort(distances, kind="stable")[:k]
votes = np.bincount(labels[nearest], minlength=2)
return ("blue" if votes[0] > votes[1] else "orange"), votes
for k in (1, 3, 5):
color, votes = guess(np.array([0.5, 0.5]), k)
print(f"k={k}: {votes[0]} blue, {votes[1]} orange -> {color}")k=1: 1 blue, 0 orange -> blue
k=3: 1 blue, 2 orange -> orange
k=5: 2 blue, 3 orange -> orangeThe method you just used is called k-nearest neighborsA method that predicts the label of a new point by finding the k most similar labeled examples and taking a vote among them.Open in glossary. To guess the color of a new point, it finds the k closest examples and takes a vote. Nobody told it where the border between blue and orange is. The examples define the border, and when you change the examples, the behavior changes. That is the central idea of machine learning: behavior comes from data.
Notice the readout labeled Your examples it gets right. With k = 1 it is always 100%, because every example is its own nearest neighbor. That sounds good but tells you almost nothing about new points, because the model has simply memorized. Telling memorizing apart from learning is one of the most important ideas in the field, and the machine learning track is built around it.
What “learning” means here
It is tempting to imagine the computer knows what blue and orange mean. It does not. Each point is two numbers, its position across and up, and the model only ever sees those numbers. It finds a pattern in them and nothing more.
Real systems work the same way at a much larger scale. A photo becomes millions of numbers, one for each color channel of each pixel. A sentence becomes thousands of numbers. The models are vastly larger than our vote-counting one, and the way they find patterns is more sophisticated, but the shape of the process holds: numbers in, numbers out, behavior shaped by examples.
Where AI, machine learning, and deep learning fit
People use these words loosely, but they mean different things. Machine learning is one way of doing AI, and deep learning is one way of doing machine learning. Today’s generative AI is built with deep learning, though simpler generators exist; you will build one from word counts later in this track.
Artificial intelligence Making computers do things that seem to need intelligence, by any method, including rules people write by hand.
Machine learning Systems that learn their behavior from data.
Deep learning Machine learning with neural networks of many layers.
Generative AI Models that create text, images, or sound. Today's chatbots live here.
Artificial intelligence is the broad goal: getting computers to do things that seem to require intelligence. Early AI was mostly rules written by hand. IBM’s Deep Blue, which beat world chess champion Garry Kasparov in 1997, relied on fast search and a scoring function crafted largely by people.
Machine learning is the part of AI where behavior is learned from data, as in the demo above.
Deep learningMachine learning with neural networks that have many layers. It powers modern image recognition, speech recognition, translation, and chatbots.Open in glossary is machine learning with neural networks that have many layers. It is behind modern image recognition, speech recognition, translation, and the chatbots you have used. You will build a neural network from a single neuron upward later in this track.
Generative AIModels that produce new content, such as text, images, audio, or code, rather than only labeling or scoring existing content.Open in glossary describes models that produce new content, such as text, images, or audio, instead of only labeling it. Today’s generative models are built with deep learning.
Key ideas
- Traditional programs follow rules people write. Machine learning finds rules from examples with known answers.
- The learned rule is called a model. Change the examples and the model’s behavior changes.
- A model sees only numbers. Real systems turn photos and text into numbers, often millions of them.
- Getting your own examples right is easy. Getting new ones right is the real test.
- AI contains machine learning, which contains deep learning, which is how today’s generative AI is built.