Snakes, Neurons, and a Dash of Scalar Autograd
Smarty Snakes, a little toy version of Snake for you.
You may embark on the age old quest of consuming the most abstract circles food, without running into your own tail. But unlike the Nokia game of yore, you’re one snake amongst several this time. And one extra feature I’ll get to in a moment ;D
To play, tap or click onto the game, then:
- On desktop, use the arrow keys to turn. Pressing the up arrow will make you go “North” on the game board.
- On mobile, tap on the left or right side of the screen. Tapping left will make the snake turn left.
Yes, that means the controls, and thus game, are fundamentally different between desktop and mobile. Absolute vs relative control schemes. Stroke of genius, bestowed upon mine head by grayscale LCD snakes long past? Nah. It kinda just ended up that way and I like the variation.
Ok, so why make a new version of Snake? Hardly enthralling, some might say — and, btw, how dare they.
Well in this game, the other snakes are controlled via a neural network. And an untrained one at that!
As you play the game, the other snakes learn from your actions. The neural network is trained live on your moves and decisions. And this means you can create some rather dumb snakes if you like!
It may not be super obvious there’s a neural network underneath, as the network’s design could use more tuning. But I’ve burned enough time on this already, so here it is.
The most impactful decision in this neural network’s design is the inputs. I’d love to just throw the whole game board in there and call it a day, but the underlying neural engine is too slow. More on that in a bit.
So instead, this game’s AI has one job: decide if a snake should change directions at any given moment. And this runs on only nine inputs:
- Obstacle to the left
- Obstacle ahead
- Obstacle to the right
- Nearest food, cells ahead
- Nearest food, cells to the right
- Nearest food, whether ahead or behind
- Nearest food, whether right or left
- Nearest other snake’s head, cells ahead
- Nearest other snake’s head, cells to the right
Don’t think about these too much. The exact choices here were made by Claude while doing A/B experiments and I have no reason to change them in this v1.
What this actually is about
Ok, so I’m saying performance sucks and the “special sauce” AI is barely designed too??
Yurp. Uh, is that a pitchfork you have?
My actual goal here was to learn how to create a neural network inference and training engine from scratch. The game is just a fun proof-of-concept to test it.
I followed Andrej Karpathy’s micrograd, making a version in Rust to be compiled into WebAssembly.
Karpathy did an incredible job with micrograd, I can’t recommend it enough. If you’re at all curious about how neural networks are trained and used, it’s absolutely worth a look. I particularly love the build up from arbitrary math expressions and the visualization of backpropagation. A literal reminder that calculus is awesome is just a bonus!
micrograd, and my version dubbed “sullgrad”, are scalar autograd engines. This means they both work on individual math terms at a time, instead of batching work up via matrix operations. This, combined with my own coding “ability”, and the confines of browser engines running on the CPU, contributes to the slowness of it all. But… it does work!
If you’d like to see the code:
- sullgrad, my tribute to micrograd
- smarty-snakes Rust crate, the game internals
- SmartySnakes Svelte component, the game rendering
I’d like to step aside from the snakes for a final moment to share some meta thoughts.
My previous post on this site was in 2018, eight years ago!! I always wanted to come back with updates, and am elated to finally deliver. I shall schedule a self-pat on the back at the next available opportunity. Hooray.
Some things have happened since 2018. COVID-19, for starters. Many new people exist, with some of them learning at a 3rd grade level. A lovely human named Robert fell in love, eventually marrying the future mother of their children, Samantha. Well, statistically at least, idk about that last one.
Most notably for this humble site, society is witnessing the beginnings of artificial general intelligence. With that in mind, I’d like to be transparent via sharing my personal philosophy here:
- I do not use any AI when writing prose meant for human consumption.
- I do use agents to code software. I do review every change and strive to continuously improve the quality bar. Sometimes use of agents should be minimized, such as when learning, designing critical systems, or crafting meaningful interfaces.
- It’s important to label what was produced by AI, or via the lack thereof. I don’t have the specific answer to this yet, and am iterating on my own approach here.
Thus, this post is all of my own words.
And with those parting thoughts, until next time! I have no idea if the snake game can be “won”, but perhaps worth a try? I expect a written report on my desk by Monday.