Neural Network to play a snake game – Towards Data Science part 2steemCreated with Sketch.

in #neural-network7 years ago

Input data

The neural network need some data to learn on. Input data is very important part of machine learning. If you have a huge amount of data, you can achieve great results even if an architecture of your network is not good. That’s why companies like Google are trying to gain all information they can get from their users (of course not because they have bad architectures but because really big data is precious).

So we need to generate some data. You can sit and play as many games as you can, but it is always good when you can generate data automatically (from scratch or modifying data that you have). In our case it is easy to create data just randomly choosing direction and observing if the snake is still alive after the turn.

After 100 games I’ve got 5504 training examples. It is enough for training to survive

Architecture of neural network

Choosing the right architecture or your neural network is always hard. You can choose number of neurons in layers, number of layers and types of neurons. It always depends on task that you trying to solve. It’s better to try different variations and choose the one that fits more than others.

Our task is very simple therefore we will use only input and output layers. No hidden layers are needed.

In TensorFlow it will look like(I’m using TFLearn):

network = input_data(shape=[None, 4, 1], name='input')
network = fully_connected(network, 1, activation='linear')
network = regression(network, optimizer='adam', learning_rate=1e-2, loss='mean_square', name='target')
model = tflearn.DNN(network)

You can find the full code here

Results

Each turn we give to the network three arrays with possible actions and choose one with better output. After training the snake chose the easiest way to survive:

website: https://towardsdatascience.com/today-im-going-to-talk-about-a-small-practical-example-of-using-neural-networks-training-one-to-6b2cbd6efdb3

Sort:  

Congratulations @jobsua2018! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - A better rich list comparator
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @jobsua2018! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!