How Does a Neural Network Actually Learn?
You've probably used a neural network today without noticing sometimes, the autocomplete on your phone, a spam filter, a chatbot, a photo app that recognizes faces in your gallery. Most explanations jump straight to calc
You've probably used a neural network today without noticing sometimes, the autocomplete on your phone, a spam filter, a chatbot, a photo app that recognizes faces in your gallery. Most explanations jump straight to calculus and Greek letters, and that's usually where people quit.
Here's the secret: a neural network learns the same way you learned to estimate how long your commute takes.
By the end of this post you'll understand neurons, weights, biases, and training. We'll build a tiny learning system in about 15 lines of plain Python, with no libraries.
The Everyday Version: Learning Your Commute
Think about how you know how long it takes to get to work.
Nobody gave you a formula. You may notice that:
Longer distance means more time (obviously).
Rain makes everything slower.
Even a 0 km trip takes a few minutes, because you still have to find your keys and lock the door.
You have never calculated any of this. You made guesses, got them wrong ("I thought 20 minutes, it took 35"), and adjusted your mental model. After enough trips, your guesses got good.
That loop is the whole idea:
A neural network does exactly this, just with numbers.
Part 1: The Neuron (A Fancy Calculator)
A neuron takes inputs, decides how much each one matters, adds them up, and produces an output.
For our commute example:
distance_km = 7
is_raining = 1 # 1 = yes, 0 = no
# How much does each input matter?
weight_distance = 5.0 # each km adds 5 minutes
weight_rain = 10.0 # rain adds 10 minutes
bias = 8.0 # baseline: keys, parking, lift...
predicted_minutes = (distance_km * weight_distance) + (is_raining * weight_rain) + bias
print(predicted_minutes) # 53.0
That's a neuron. Really. Three terms to remember:
- Inputs: the facts you feed in (distance, rain).
- Weights: how much each input matters. A big weight means "pay attention to this." A weight near zero means "ignore this."
- Bias: the starting point when every input is zero. It's the "keys and parking" time.
In real neural networks there are thousands or billions of these weights, and they're arranged in layers so the outputs of some neurons become the inputs of others. But every single one of them does the little calculation above.
Part 2: The Problem. We Don't Know the Right Weights
In the example above, I handed you perfect weights. In real life, nobody does. The whole point of learning is finding the weights yourself from examples.
Here's our data: past trips, with the real time each one took.
# (distance_km, is_raining, actual_minutes)
data = [
(2, 0, 18),
(5, 0, 33),
(8, 1, 58),
(3, 1, 33),
(10, 0, 58),
(6, 1, 48),
]
We'll start the network knowing nothing. All weights and the bias are zero. It's like a new employee on day one.
Part 3: The Learning Loop
Here's the full training code. Read it slowly, because it's the heart of everything:
w_km, w_rain, bias = 0.0, 0.0, 0.0 # knows nothing yet
learning_rate = 0.01 # how big each adjustment is
for epoch in range(1, 3001): # 3000 passes over the data
for km, rain, actual in data:
# 1. GUESS
prediction = (w_km * km) + (w_rain * rain) + bias
# 2. CHECK how wrong we were
error = prediction - actual
# 3. ADJUST each weight a little, in the direction that reduces the error
w_km -= learning_rate * error * km
w_rain -= learning_rate * error * rain
bias -= learning_rate * error
print(round(w_km, 2), round(w_rain, 2), round(bias, 2))
# 5.0 10.0 8.0
I ran this. The network recovered the exact rule (5 minutes per km, 10 for rain, 8 baseline) without ever being told it.
Here's what it looked like along the way:
| Training pass | Average squared error | km weight | rain weight | bias |
|---|---|---|---|---|
| 1 | 578.0 | 6.42 | 0.70 | 1.19 |
| 10 | 54.1 | 6.13 | 2.87 | 2.45 |
| 100 | 0.46 | 5.15 | 9.69 | 6.97 |
| 500 | 0.00 | 5.00 | 10.01 | 7.98 |
Watch the error collapse from 578 to nearly zero. That's learning.
Part 4: What's Actually Happening in Step 3?
This is the one line people find mysterious:
w_km -= learning_rate * error * km
Let's translate it into plain English.
error tells us which direction we were wrong.
Positive error: we predicted too high, so reduce the weight.
Negative error: we predicted too low, so increase the weight.
_km _blames the right input. If the trip was 10 km, the distance weight deserves more blame (or credit) than on a 2 km trip. If it didn't rain (rain = 0), then the rain weight gets zero blame, so it doesn't change at all. That's a lovely, fair piece of logic.
learning_rate is how bold each correction is. Too big and you overshoot wildly (imagine overcorrecting a steering wheel and swerving). Too small and learning takes forever. Think of it as how seriously you take each new mistake.
This technique has a name, gradient descent, but the name is just decoration. The idea is: nudge every dial slightly in whichever direction makes the mistake smaller. Do that thousands of times and you end up in a good place.
Think of walking downhill in thick fog. You can't see the valley, but you can feel the slope under your feet. Take a small step downhill, again and again, and you eventually reach the bottom.
Part 5: Does It Work on a Trip It Has Never Seen?
This is the real test. Learning that only memorizes old examples is useless.
# A brand-new trip: 7 km, raining
new_trip = (w_km * 7) + (w_rain * 1) + bias
print(round(new_trip)) # 53 minutes
The network never saw that exact trip, yet it predicted correctly. It didn't memorize, it generalized. This is why neural networks are useful.
Part 6: Why This Matters for Modern AI
Our model has 3 numbers to learn. The large language models behind modern chatbots have billions. But the training loop is conceptually the same:
- Our commute model A large language model
- Input: distance, rain Input: the words so far
- Output: predicted minutes Output: probability of the next word
- Error: predicted minus actual time Error: how surprised the model was by the real next word
- Adjust 3 numbers Adjust billions of numbers
- Repeat 3,000 times Repeat across huge amounts of text
When a chatbot writes a sentence, it's making a guess about what comes next, which is the same guess, check, adjust loop you just wrote, scaled up enormously.
That's the unglamorous truth: there's no hidden reasoning engine at the core of the training process, only a very large number of tiny corrections.
Part 7: Three Mistakes Beginners (and Experienced People) Often Do
Understanding the loop also helps you debug real projects:
Learning rate too high. Try learning_rate = 0.5 in the code above. The error explodes to infinity instead of shrinking. Your steering overcorrected.
Bad or biased data. If every trip in your data happened in the rain, the model could never learn what "no rain" means. A model can only learn what its examples show it. This is the root of most "why is my AI biased?" problems.
Memorizing instead of learning. With very little data and too many weights, a model can nail every training example and still fail on new ones. Always test on data it has never trained on.
What About "Hidden Layers" and "Activation Functions"?
Our single neuron can only learn straight-line relationships, like "more distance means proportionally more time." Real problems (recognizing a cat, understanding a sentence) are curvy and complicated.
So real networks stack many neurons in layers and add a small twist called an activation function, which lets the network bend its answers instead of drawing only straight lines. Layers let the network build understanding step by step: edges, then shapes, then ears and whiskers, then "cat."
But every neuron in every layer still does the same thing you saw today: weighted sum plus bias, then get nudged when it's wrong.
Key Takeaways:
A neuron is a weighted sum of inputs plus a bias.
Weights say how much each input matters. Bias is the baseline.
Training is a loop: guess, measure the error, nudge the weights, repeat.
The learning rate controls how big each nudge is.
A good model generalizes to new data, not just the data it studied.
Giant AI models are this same idea at a massive scale.
Try It Yourself
Copy the code and change things:
Add a third input, like is_rush_hour, and invent data for it.
Change the learning rate to 0.001, then 0.1, and watch what happens.
Add noise to the data (make 58 into 62) and see the weights settle on approximations instead of exact values. That's much closer to real life.
If you can follow those 15 lines, you understand the core of how neural networks learn. Everything else is engineering on top.
If this helped, share it with someone who thinks AI is magic. It's not, it's just very patient trial and error.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.