I taught 2 neural networks to play snake
A reinforcement-learning agent takes on Snake. Choose a model below to watch it play!
About the project
SnakeRL trains an agent to play Snake by learning from the rewards (and consequences) of its actions.
Learning through play
Reinforcement learning is the process of teaching an agent to achieve a task through a core loop of observing state, choosing an action, observating the reward generated by that action, and updating the network based on its findings. In training the agent is fed an observation, which in this case consists not only of the state of the board, but also other information depending on the network architecture. These observations are then passed into the neural network which will output what it thinks the next best action is. The environment will then update with the input from the agent and the agent will be fed an observation of the new state of the environment and the reward of its action, which it will use to retune its weights and optimize the network so that it achieves the highest reward possible.
Two model perspectives
I created 2 different neural networks to play snake to see how they would both perform. The first on is a multilayer perceptron (MLP). This neural network consists of an input layer (where you pass in observations), hidden layer(script) (where the network extracts meaning from the observations), and the output layer (the models final choice). MLPs are a very important to machine learning and are used modern LLMs. The second network is a convolutional neural network (CNN). A CNN uses multidimensonal inputs (like images or 2D/3D grids), unlike MLPs which only take one dimensional data as input. A CNN works by using convolutional layers to extract different meanings from different parts of an input. For instance, with a CNN trained on an image, one layer may detect edges in the image, another may detect certain colours, and another layer may detect certain shapes. For playing snake, so far the MLP performs better, as it seems to be able to extract more meaning from the different observations given to it.