I'm trying to teach an AI different pattern of tic tac toe to recognize wether a given pattern represents a win or not.
Unfortunately it's not learning to recognize them correctly and I think may way of representing/encoding the game into vectors is wrong.
I choose a way that is easy for an human (me, in particular!) to make sense of:
training_data = np.array([[0,0,0,
0,0,0,
0,0,0],
[0,0,1,
0,1,0,
0,0,1],
[0,0,1,
0,1,0,
1,0,0],
[0,1,0,
0,1,0,
0,1,0]], "float32")
target_data = np.array([[0],[0],[1],[1]], "float32")
This basically just use an array of length 9 to represent a 3 x 3 board. The first three items represent the first row, the next three the second row and so on. The line breaks should make it obvious I guess.
The target data then maps the first two game states to "no wins" and the last two game states to "wins".
Then I wanted to create some validation data that is slightly different to see if it generalizes.
validation_data = np.array([[0,0,0,
0,0,0,
0,0,0],
[1,0,0,
0,1,0,
1,0,0],
[1,0,0,
0,1,0,
0,0,1],
[0,0,1,
0,0,1,
0,0,1]], "float32")
Obviously, again the last two game states should be "wins" whereas the first two should not.
I tried to play with the number of neurons and learning rate but no matter what I try, my output looks pretty of. E.g.
[[ 0.01207292]
[ 0.98913926]
[ 0.00925775]
[ 0.00577191]]
I tend to think it's the way how I represent the game state that may be wrong but actually I have no idea :D
Can anyone help me out here?
This is the entire code that I use
import numpy as np
from keras.models import Sequential
from keras.layers.core import Activation, Dense
from keras.optimizers import SGD
training_data = np.array([[0,0,0,
0,0,0,
0,0,0],
[0,0,1,
0,1,0,
0,0,1],
[0,0,1,
0,1,0,
1,0,0],
[0,1,0,
0,1,0,
0,1,0]], "float32")
target_data = np.array([[0],[0],[1],[1]], "float32")
validation_data = np.array([[0,0,0,
0,0,0,
0,0,0],
[1,0,0,
0,1,0,
1,0,0],
[1,0,0,
0,1,0,
0,0,1],
[0,0,1,
0,0,1,
0,0,1]], "float32")
model = Sequential()
model.add(Dense(2, input_dim=9, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)
history = model.fit(training_data, target_data, nb_epoch=10000, batch_size=4, verbose=0)
print(model.predict(validation_data))