How Does a Neural Network Actually Learn? A developer demonstrated how a neural network learns by building a 15-line pure-Python training loop with no libraries, using a commute-time prediction example. Starting from zero weights, the loop recovered the exact underlying rule — 5 minutes per km, 10 minutes for rain, and an 8-minute baseline — after 3,000 passes over six sample trips, with average squared error falling from 578 to near zero. 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: 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: 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.