Teaching a machine by example
What a neural network is, why we need one, and where it's used, explained from zero with demos you can play with.
A neural network is a program that learns a rule from examples instead of being told the rule. You show it many inputs together with the right answers, and it slowly adjusts itself until its answers match.
This post explains it from zero. No maths background needed. Every idea comes with something you can click or drag.
Why do we need it?
Normally a programmer writes the rules. That works when the rule is easy to say out loud, like "add 18% tax to the price".
Now try writing down the rule for "is there a cat in this photo?" Every cat looks different. Nobody can write that rule. But we already have millions of photos labelled "cat" or "not cat".
| Normal program | Neural network | |
|---|---|---|
| You give it | the rules + an input | examples (inputs + right answers) |
| It gives you | an answer | a rule it found by itself |
| Good when | you can explain the rule | the rule is too complicated to write, but examples are easy to collect |
So the relationship is always the same: an input goes in, an answer comes out, and the network learns the connection between the two from past examples.
A neuron is one small decision
Think about how you decide whether to go for a walk. You check a few things: is it sunny, do you have free time, is it raining. Some things matter a lot to you, some matter a little, and some push you to stay home.
A neuron does exactly this. Each input gets a weight: how much that thing matters. A positive weight pushes toward "yes", a negative weight pushes toward "no". The neuron multiplies each input by its weight, adds everything up, and squashes the total into a yes-chance between 0% and 100%.
The bias is your mood before you look at anything. A lazy person starts negative and needs a good reason to go out.
Try it. Switch the inputs on and off, and drag the weights.
Sunny 1 ร 1.5 + Free time 1 ร 3.0 + Raining 0 ร โ4.0 + mood โ2.0
= 2.5 โ squashed to 92% chance
Yes, go for a walk (92%)
Notice what a weight is and isn't. The input is a fact about today ("it is raining": 1 or 0). The weight is how much you care about that fact. The input changes every day; the weight is the part that gets learned.
Learning means fixing the weights
Above, you chose the weights. The point of a neural network is that it finds the weights by itself.
Below are eight days from someone's diary: what the day was like and whether they actually went for a walk. The neuron starts knowing nothing. Every weight is zero, so it guesses 50% for everything.
Each time it practises, it looks at every day, checks how wrong its guess was, and nudges each weight a little in the direction that would have made the guess less wrong. That's all training is.
| Day | Sunny | Free | Rain | Really walked? | Network's guess | |
|---|---|---|---|---|---|---|
| 1 | yes | yes | no | walked | 50% walk | wrong |
| 2 | no | yes | no | walked | 50% walk | wrong |
| 3 | yes | no | no | stayed home | 50% walk | wrong |
| 4 | no | yes | yes | stayed home | 50% walk | wrong |
| 5 | yes | yes | yes | stayed home | 50% walk | wrong |
| 6 | no | no | no | stayed home | 50% walk | wrong |
| 7 | no | no | yes | stayed home | 50% walk | wrong |
| 8 | yes | no | yes | stayed home | 50% walk | wrong |
Click a day to see it in the diagram. Right now every weight is zero, so every guess is 50%.
Nobody wrote the rule "this person needs free time and hates rain". The network found it in the examples, and you can read it straight off the weights.
Hard rules need many neurons working together
Some rules are too tricky for one neuron. Think of a staircase light with a switch at the bottom and one at the top. Flip either switch and the light changes. The light is on when exactly one switch is up. A single neuron can never learn this, whatever weights it picks.
The fix is to put a layer of neurons in the middle, called the hidden layer. Each middle neuron learns a small piece of the pattern, and the last neuron combines the pieces. That is why it's called a network. Real networks do the same thing with thousands or billions of neurons stacked in many layers.
| Bottom | Top | Light should be | Network says | |
|---|---|---|---|---|
| down | down | off | 27% on | right |
| down | up | on | 25% on | wrong |
| up | down | on | 30% on | wrong |
| up | up | off | 27% on | right |
If it gets stuck, press Start over. Training starts from random weights, and an unlucky start sometimes gets stuck. Real networks have the same problem, and much of the craft of training them is about avoiding it.
Where it's used
Every use below is the same pattern you just played with: an input, an output, and a network that learned the connection from examples.
| Where | Input | Output | Learned from |
|---|---|---|---|
| Photo apps | a photo | "cat", "beach", "Mom" | millions of labelled photos |
| Spam filters | an email | spam or not spam | emails people marked as spam |
| Voice assistants | the sound of speech | written words | recordings with transcripts |
| Bank fraud alerts | a card payment | normal or suspicious | past payments that were fraud |
| Medical scans | an X-ray | "look closer here" | scans doctors already diagnosed |
| Recommendations | what you watched | what you'll like next | what millions of people watched |
| Translation | a Hindi sentence | an English sentence | texts that exist in both languages |
| Chat assistants | your question | the next words of a reply | huge amounts of text |
How it's used
It's the same three steps every time:
- Collect examples. Pairs of an input and the correct answer, like the diary days above. More and better examples give a better network.
- Train. Let the network guess, measure how wrong it was, and nudge the weights. Repeat thousands of times until the mistakes are small.
- Use it. Freeze the weights and give it new inputs it has never seen. It answers with the rule it learned.
When not to use one
If you can write the rule yourself (tax, sorting, dates), just write it. Be careful too when you have very few examples, or when you must explain exactly why each answer was given. A network learns only from its examples, so biased or wrong examples give biased or wrong answers.
That last point is why, at Headknot, every answer comes with the sources it was built from. When a machine learned something from examples, you should be able to check those examples.
The words people use
- Input: the facts you give it, like "sunny", the pixels of a photo, or the words of a sentence.
- Output: its answer, usually a chance like "92% cat".
- Neuron: one small decision maker. It multiplies each input by its weight, adds them up, and squashes the total to 0โ100%.
- Weight: how much one input matters. Positive pushes toward yes, negative toward no. Weights live on the connections and are what the network learns.
- Bias: the starting mood before looking at any input. It is learned too.
- Hidden layer: neurons in the middle that each learn a piece of a harder pattern.
- Training: guessing, measuring the mistake, and adjusting the weights, over and over.
- Loss (or error): a number for how wrong the guesses are. Training tries to make it small.