The Smallest Brain You Can Build: A Perceptron in Python

288 points - today at 12:28 AM

Source

Comments

romaniv today at 2:13 PM
I think it should be quite obvious that perceptrons are far from the smallest units that are capable of learning. They store many bytes of information, require a non-local update process, need numeric (i.e. symbolic) inputs and involve relatively complex computations. You can go much simpler. For example:

https://medium.com/@VictorBanev/the-simplest-learning-machin...

This is a description of a 5-line algorithm that learns and stores approximate probability of an event using just 1 byte of persistent memory.

utopiah today at 3:31 PM
One day I'll write about my 1-liner physics engine...

    let gravity = setInterval( _ => { if (projectile.object3D.position.y > 0) projectile.object3D.position.y \*= .99 }, 100)
Jokes aside I find that providing ridiculously short toy examples that provide the very limited foundation of a concept are extremely empowering in pedagogy. You "get" it right away because it "fits" in your mind, then you dare tinker with it and quickly see how limited it is, then get excited again. It's a powerful trick to learn more IMHO.
kzrdude today at 10:51 AM
I think Karpathy's microgpt blogpost is the best in this genre in a long time, and it also includes a multi layer perceptron. It's a step up in the hierarchy, so reading both is helpful, of course.

https://karpathy.github.io/2026/02/12/microgpt/

rahen today at 6:31 AM
In the early days of machine learning (before the first AI winter), networks like this were often implemented and trained in hardware: https://en.wikipedia.org/wiki/ADALINE

That was the first thing that came to mind when I read "the smallest brain you can build". Nowadays, that "small brain" would likely be built on a breadboard using op-amps instead.

zkmon today at 5:20 AM
The IF statement is the root creator of software programming. It has the ability to compare two values against each other and branch out to blocks of instructions. So it is perceiving (reading), decision making and routing - all that which differentiate life from inanimate objects. The AI agents perform the exact same loop, by delegating the first two steps to a model.

Going further backwards, the transistor (or a PNP junction) is the hardware level enabler of the IF statement. The action (switching) driven by the current which in turn controls other switches, is the first manifestation of "observe and act" by inanimate things at the speed of electricity.

Mechanical equivalents existed ofcourse - speed of a governer which controls the flow of fuel which in turn controls the speed of the governer.

ankit84 today at 3:42 AM
I learnt a lot today from the interactive demo. You have the best clarity and right skill to educate
ninalanyon today at 2:28 PM
Is this something that could be scaled up and used, for instance, to recognize features in images?

Or to put it another way are there any local only tools that can be trained on my own set of images to automatically tag new images? Tools that do not already have built in classes of image.

I take a lot of photographs and it would be handy to reduce the drudgery of tagging them so to say broadly what the subject was so that they are easier to find later.

vain today at 5:37 PM
Shameless plug of my own interactive version of this (ai assisted, but probably not slop) https://sourceobscure.com/perceptron/
trekhleb today at 3:32 AM
Nice and minimalistic

I played with similar approach in JavaScript and built a NanoNeuron https://github.com/trekhleb/nano-neuron (it is more verbose than Python though)

virajk_31 today at 11:07 AM
Not a ML expert, but ML tutorials shall start with something like this... Good read. Thanks.
techteach00 today at 3:18 PM
Is this too high level for my 8th grade comp sci class?
Bimos today at 4:41 AM
> A perceptron *is* the smallest brain you can build.

> In 1958, a researcher named Frank Rosenblatt built a machine *he called* the perceptron.

> It was *inspired* by a single brain cell, a neuron.

esafak today at 12:53 AM
If you want to learn the fundamentals of ML I recommend a book, such as Deep Learning: Foundations and Concepts by Chris Bishop. If you insist on staying online, one option is https://course.fast.ai/

If you don't know ML I don't think you're going to learn much through ad hoc demos.

opem today at 10:32 AM
I have still so many questions left, but regardless of that it was a great read. Thanks for sharing!
warengonzaga today at 9:03 AM
This is amazing insight, thanks for sharing!
deleted today at 12:28 AM
charcircuit today at 3:59 AM
I can build a smaller brain.

f(x) = 0.

sspoisk today at 3:05 PM
[flagged]
deleted today at 9:43 AM
infoinlet today at 10:13 AM
[flagged]
knightops_dev today at 2:19 AM
[flagged]
jkwang today at 3:36 AM
[flagged]
haeseong today at 4:12 AM
[dead]
EvanXue today at 3:05 AM
[flagged]
karinatran today at 2:56 AM
[dead]
hbwang2076 today at 1:29 AM
[dead]
b33j0r today at 1:19 AM
Okay, itโ€™s conscious. But can it run doom? I rest my case.