Last lesson you picked the weights by hand. That doesn't scale. The whole point of machine learning is the machine finds the weights itself.
The recipe is shockingly simple:
- Start with random (or zero) weights.
- Show the neuron one example. Let it guess.
- If it's wrong, nudge each weight a little in the direction that would have made it right.
- Repeat over all examples, many times.
The nudge is one line: weight += learning_rate * (correct − guess) * input. If the guess was already right, (correct − guess) is 0, so nothing changes. Beautifully self-correcting.
A perceptron that learns AND
The neuron taught itself the AND gate — no human picked the weights this time. The loop just kept nudging until the errors stopped.
lr (learning rate) controls how big each nudge is. Too small → learns slowly. Too big → overshoots and never settles. This single knob will follow you all the way to training giant models.
Replace the AND data with the OR truth table and confirm the perceptron learns it. Then print the final weights. (Bonus: try lowering epochs to 2 — does it still learn?)