All posts
machine-learningengineering

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.

Sonu Nigamยท FounderOct 31, 20265 min read

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 programNeural network
You give itthe rules + an inputexamples (inputs + right answers)
It gives youan answera rule it found by itself
Good whenyou can explain the rulethe 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.

Try it: should I go for a walk?1 neuron
1.53.0โˆ’4.092%SunnyFree timeRainingGo for a walk

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.

Watch it learn this person's habitstraining
0.00.00.050%SunnyFree timeRainingWalks?
rounds: 0 ยท right: 0/8 ยท bias 0.0
DaySunnyFreeRainReally walked?Network's guess
1yesyesnowalked50% walkwrong
2noyesnowalked50% walkwrong
3yesnonostayed home50% walkwrong
4noyesyesstayed home50% walkwrong
5yesyesyesstayed home50% walkwrong
6nononostayed home50% walkwrong
7nonoyesstayed home50% walkwrong
8yesnoyesstayed home50% walkwrong

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.

Teach a network the staircase light2 โ†’ 4 โ†’ 1 network
30%BottomTopLight on?hidden layer
Network says: light off (30%)
Untrained: random weights, so it's guessing.
BottomTopLight should beNetwork says
downdownoff27% onright
downupon25% onwrong
updownon30% onwrong
upupoff27% onright

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.

WhereInputOutputLearned from
Photo appsa photo"cat", "beach", "Mom"millions of labelled photos
Spam filtersan emailspam or not spamemails people marked as spam
Voice assistantsthe sound of speechwritten wordsrecordings with transcripts
Bank fraud alertsa card paymentnormal or suspiciouspast payments that were fraud
Medical scansan X-ray"look closer here"scans doctors already diagnosed
Recommendationswhat you watchedwhat you'll like nextwhat millions of people watched
Translationa Hindi sentencean English sentencetexts that exist in both languages
Chat assistantsyour questionthe next words of a replyhuge amounts of text

How it's used

It's the same three steps every time:

  1. Collect examples. Pairs of an input and the correct answer, like the diary days above. More and better examples give a better network.
  2. Train. Let the network guess, measure how wrong it was, and nudge the weights. Repeat thousands of times until the mistakes are small.
  3. 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.
Share this post