Help with first neural network

Asked

Viewed 157 times

6

I am beginner in dealing with Nns, and need help and explanation in certain things with my first project.

Basicamento, to making an NN for checkers, my input are the black parts of the board (8x4) filled with the state: 0 empty, 1 normal piece, 3 queen. Positive or negative symbolizes whether you are an ally or an enemy:

[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [-1. -1. -1. -1.]
 [-1. -1. -1. -1.]
 [-1. -1. -1. -1.]]

and the output are the possible movements, 4 for each black part (32x4) or (128x1), the index where '1' is shows the part and the movement:

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0.]

My database is 1000 professional games in the American model of checkers, and I’ve turned those matches into 200k of Boards and moves

The model I’m using is a very inspired by the tensorflow guide, I just increased the number of layers and neurons and changed Loss to 'categorical_crossentropy', because 'sparse_categorical_crossentropy' does not work for my problem:

model = keras.Sequential([
        keras.layers.Flatten(input_shape=(8, 4)),
        keras.layers.Dense(1000, activation=tf.nn.relu),
        keras.layers.Dense(1000, activation=tf.nn.relu),
        keras.layers.Dense(1000, activation=tf.nn.relu),
        keras.layers.Dense(1000, activation=tf.nn.relu),
        keras.layers.Dense(1000, activation=tf.nn.relu),
        keras.layers.Dense(128, activation=tf.nn.softmax)
    ])

model.compile(optimizer='adam', 
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

With this model my NN achieves around 50 seasons this:

loss: 1.1787 - acc: 0.5935 - val_loss: 7.1449 - val_acc: 0.0840

And more times change almost nothing, another detail is that some times start with 0.8 acc but in the end fall to 0.6, so I really need help in what to do and why, tips on what to change in the model, etc.

1 answer

2


Input Data

0 empty, 1 normal piece, 3 queen. Positive or negative symbolizes whether it is allied or enemy

Is called categorical variable, the variable that holds data that assumes a limited number of possible values, and no numerical relation to each other. The input matrix of your problem is composed of categorical variables, because there is no quantitative relationship between having, for example, a white piece or a black piece in one position.

Unfortunately, neural networks do not work well on categorical data. This is because each neuron in the network will decide whether or not to activate, from the application of an activation function in a linear combination of input values, and so, by using categorical variables as neuron inputs, the user embeds in the classifier a hierarchy between the chances of activating a neuron, information such that it is not always true.[1]

Suggestion

The simplest way to correct this situation is to apply one-hot coding, which consists of replacing categorical variables by N simulated numerical variables[2], capable of assuming the values 1 or 0, indicating whether a particular category occurs in fact or not, being N the number of categories the original variable was able to assume (in its case, N=5). It is simple to program this processing manually, but if you prefer it is also present in the class Onehotencoding scikit-Learn.

Output Data

If I understand correctly, when well trained, your program will show suggestions of moves, from any moment of a game. But the classifier knows, just with the input data you’re using, which player can move? If you have not yet added this information to the classifier, the classifier will provide guesses for both players at any given time, although you know that only one of the players is allowed to play. This extra information can be added to the classifier to improve your performance.

Suggestion

You can add the restriction, without modifying the network, with the following preprocessing:

  1. Referee a color, which will be the player’s color of the time, across the dice set
  2. When an instance occurs where the board refers to a time of the match when it is the player you did not choose, reverse the colors of all the pieces on the board

This would be enough for the classifier to just "think" the right move for the player who is allowed to play.

Finally, you can reduce the output dimension because the pieces in the corners cannot move out of the board.

Other considerations

  • Note that, although unlikely, nothing prevents the classifier suggests a move that cannot be made according to the rules. If you want to make a bot, or something like that, remember to validate the result given by the neural network with the rules.

[1] Adapted from here, original in English.

[2] I used the term variável simulada as translation of Dummy Variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.