What's the best neural network setup for a Snake game AI?

Hey folks, I’m trying to make an AI that plays Snake. I’m pretty new to this machine learning stuff. My game board is 10x10, and I’ve got three layers of info:

  1. Snake body parts
  2. Apple location
  3. Snake head

I’m using 1s and 0s to mark these. The AI needs to pick from four moves: up, down, left, or right.

I’m not sure how to set up the neural network for this. Any tips on picking the right structure? Also, does my way of showing the game info to the AI make sense?

I’ve tried a few things, but nothing’s really working great. Here’s a basic attempt:

import numpy as np

def create_network():
    input_layer = np.random.rand(300, 64)
    hidden_layer = np.random.rand(64, 32)
    output_layer = np.random.rand(32, 4)
    return [input_layer, hidden_layer, output_layer]

def make_move(game_state, network):
    # Super simple forward pass
    x = game_state.flatten()
    for layer in network:
        x = np.dot(x, layer)
        x = np.maximum(x, 0)  # ReLU
    return np.argmax(x)

# This is just a placeholder, not a real game
game = np.zeros((3, 10, 10))
ai = create_network()
next_move = make_move(game, ai)

Any ideas on how to improve this? Thanks!

I’ve worked on a similar project before, and I can share some insights that might help you out. Your approach of using three layers to represent the game state is a good start, but you might want to consider adding more information to help the AI make better decisions.

For the neural network structure, I found that a deeper network with more hidden layers worked better for Snake. Something like 3-4 hidden layers with 128-256 neurons each seemed to give good results. Also, consider using convolutional layers at the start of your network to better process the spatial information of the game board.

One thing that really improved my AI’s performance was adding some pre-processed features to the input. For example, you could include the distance and direction to the nearest wall, the length of the snake, and whether moving in each direction would result in immediate death.

As for the output, instead of directly choosing a move, you might want to output a value for each possible move. This allows for more nuanced decision-making and can be used with reinforcement learning techniques like Q-learning.

Remember, training an AI for Snake can take a lot of time and iterations. Don’t get discouraged if it doesn’t work right away. Keep tweaking and experimenting, and you’ll get there!

yo, i tried somethin similar. ur setup’s not bad but maybe try addin some extra info like distance to walls n stuff. also, deeper networks can help - try like 3-4 hidden layers with more neurons.

don’t forget to mess with different activation functions too. ReLU’s cool but maybe check out LeakyReLU or ELU.

good luck with ur project, bro!

hey man, i’ve been playin around with snake AIs too. ur approach is decent, but maybe try usin a convolutional network? it’s good for spottin patterns on grids. also, try rewardin the AI for gettin closer to the apple, not just eatin it. that helped mine learn faster. good luck with ur project!

For a Snake game AI, I’ve found that a combination of convolutional and dense layers works well. Start with a few conv layers to process the spatial data, then flatten and feed into dense layers. Consider using a network structure like: Conv2D → Conv2D → Flatten → Dense → Dense → Output.

Input representation is crucial. Instead of binary values, try using different numbers for the snake’s head, body, and tail. This gives the AI more detailed information. Also, consider adding a layer for the snake’s current direction.

For the output, you might want to use a policy network approach, where the AI predicts the probability of each move being the best. This can be combined with reinforcement learning techniques for more effective training.

Remember, the key is experimentation. Keep adjusting your network architecture and training process until you see improvement in the AI’s performance.