Running live in your browser
A neural network that learns Snake.
No PyTorch, no black box. I wrote the forward pass, the loss and backprop by hand in NumPy, gave the network a game of Snake and nothing else. Pick a checkpoint and watch it get smarter.
- shape
- 11 → 64 → 3
- method
- Q-learning, replay, Adam
- stack
- NumPy only, hand-written backprop
- hosting
- static, $0
Fig. 1
Live game
Score0
Best0
Games0
Avg 100
Fig. 2
What the network sees
excitatoryinhibitoryfilled = firing now
Fig. 3
Training
score per game, 400 games, 10-game averageBest training game
Greedy avg · 30
Weights963
Notes
How it works
01Forward pass
11 yes/no inputs: danger ahead, right and left, heading, and where the food is. One hidden layer picks one of 3 moves.
h = max(0, x @ W1 + b1) q = h @ W2 + b2 # action = argmax(q)
02Q-learning
Eating is +10, crashing is −10. The network learns to predict how good each move is, from its own past games.
y = r + 0.9 * max(Q(s')) loss = (Q(s,a) - y)^2 # replay buffer, Adam
03Backprop
Every crash sends an error backwards through the layers. Each gradient is written by hand.
dq = 2*(q - y) dW2 = h.T @ dq dh = (dq @ W2.T) * (h > 0) dW1 = x.T @ dh