Code a Perceptron from Scratch
A neuron in a few lines of Python.
Code a Perceptron from Scratch is a free Deep Learning Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Perceptron
A perceptron is one neuron plus a learning rule. It is the original trainable building block of neural networks, and you can code it from scratch. 🛠️
Start with Weights
First create the weights and a bias. Starting them at zero is fine for a perceptron; learning will move them where they need to be.
weights = [0.0, 0.0]
bias = 0.0Predict with a Step
The predict step computes the weighted sum, then applies the step function to return 0 or 1.
score = w[0]*x[0] + w[1]*x[1] + bias
return 1 if score >= 0 else 0Measure the Error
Compare the prediction to the true label. The error is simply target minus prediction: 0 when right, plus or minus one when wrong.
error = target - predictionThe Update Rule
The update rule nudges each weight by the error times the input. Wrong guesses push the weights toward the correct answer.
w[i] += lr * error * x[i]Update the Bias Too
The bias learns the same way, but its input is always 1. So you simply add the learning rate times the error.
bias += lr * errorThe Learning Rate
The learning rate scales how big each update is. Small values learn slowly but steadily; large ones can overshoot the answer.
lr = 0.1One Pass: an Epoch
Looping once over every training example is called an epoch. You usually repeat for several epochs until mistakes stop.
The Training Loop
Each step of training predicts, measures error, and updates the weights and bias. Repeat across the dataset to learn.
for x, target in data:
err = target - predict(x)
update(x, err)It Learns AND
Feed it the AND truth table and the perceptron converges fast. Both inputs must be 1 for it to output 1.
Convergence Guarantee
If the data is linearly separable, the perceptron is guaranteed to find a separating line. That promise is the convergence theorem.
Quick Check
Recall how a perceptron corrects itself.
Recap
A perceptron predicts with a step, measures error, and updates weights by lr times error times input. Repeat over epochs and it learns separable data. ✅
Frequently asked questions
Is the “Code a Perceptron from Scratch” lesson free?
Yes — the full text of “Code a Perceptron from Scratch” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Code a Perceptron from Scratch”?
A neuron in a few lines of Python. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Code a Perceptron from Scratch” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Weights, Bias & the Weighted Sum
- The Step Function & Linear Decisions
- Code a Perceptron from Scratch
- The XOR Problem: Why One Neuron Isn't Enough