6
$\begingroup$

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))
$\endgroup$

closed as off-topic by Robert Cartaino Aug 11 '16 at 22:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about artificial intelligence, within the scope defined in the help center." – Robert Cartaino
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ I believe programming questions are off-topic, since on-topic on Stack Overflow. If you'd like to confirm it, I've asked that at meta. Otherwise we'll have to have bunch of new programming tags, such as Python, etc. It was already suggested here as well. $\endgroup$ – kenorb Aug 11 '16 at 21:20
  • $\begingroup$ I just noticed that. Can the question be moved to SO by someone or should I simply create a new one there? $\endgroup$ – Christoph Aug 11 '16 at 21:23
  • $\begingroup$ Ok, voted for closing and created a new one here. stackoverflow.com/questions/38906409/… $\endgroup$ – Christoph Aug 11 '16 at 21:26
  • $\begingroup$ I've updated post on meta with extra arguments where it was suggested that TensorFlow-like questions are not really programming questions, but framework implementations and they may be a chance to be on-topic, because they're a bit of out-of-place at SO. You may share you thoughts on that. $\endgroup$ – kenorb Aug 11 '16 at 21:43
  • 1
    $\begingroup$ @Christoph Do you see a way to make the question more "abstract"? There can be code to illustrate what you are trying to achieve, but the core of the question should be independent from a particular technology. It does not seem the problem is proper to Keras or ML. Perhaps focus more on the game modeling? In the process of working out the question, it may become clearer "where it belongs". For now, I would move it to SO. $\endgroup$ – Eric Platon Aug 12 '16 at 1:18

Browse other questions tagged or ask your own question.