pythontensorflowneural-networktflearngame-ai

TFlearn error feeding a shape as an input


I am creating a neural network to play Tic Tac Toe. I am using tflearn for the neural network. This is the training data that I am using

[[[1, 1, 1, 0, -1, -1, -1, 0, 0], 6], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 3], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 5],
                     [[1, 1, 1, 0, -1, -1, -1, 0, 0], 2], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 7], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 1],
                     [[0, 0, 1, -1, 1, 0, 1, -1, -1], 4], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 3], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 8],
                     [[0, 0, 1, -1, 1, 0, 1, -1, -1], 5], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 9], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 7],
                     [[0, -1, 1, 0, 1, 0, 1, -1, -1], 9], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 3], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 2],
                     [[0, -1, 1, 0, 1, 0, 1, -1, -1], 5], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 8], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 7],
                     [[1, -1, -1, 0, 1, 0, -1, 0, 1], 2], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 1], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 3],
                     [[1, -1, -1, 0, 1, 0, -1, 0, 1], 5], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 7], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 9],
                     [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 1], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 5], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 3],
                     [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 2], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 7], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 8]]

It contains the current board status a list of 9 numbers and the placement of the piece 1 number. I seperated out the board and the placement into the data and labels. When I feed the data into the nerual network I get this error

ValueError: Cannot feed value of shape (30, 9) for Tensor u'input/X:0', which has shape '(?, 30, 9)'

This is the code that I am using to create and training the model

def create_model():
network = input_data(shape=(None, 30, 9), name='input')

network = fully_connected(network, 128, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 128, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 9, activation='linear')
network = regression(network, optimizer='adam', learning_rate=0.01, loss='mean_square', name='targets')

model = tflearn.DNN(network, tensorboard_dir='log')

return model

def train_model():
training_data = [[[1, 1, 1, 0, -1, -1, -1, 0, 0], 6], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 3], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 5],
                 [[1, 1, 1, 0, -1, -1, -1, 0, 0], 2], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 7], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 1],
                 [[0, 0, 1, -1, 1, 0, 1, -1, -1], 4], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 3], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 8],
                 [[0, 0, 1, -1, 1, 0, 1, -1, -1], 5], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 9], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 7],
                 [[0, -1, 1, 0, 1, 0, 1, -1, -1], 9], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 3], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 2],
                 [[0, -1, 1, 0, 1, 0, 1, -1, -1], 5], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 8], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 7],
                 [[1, -1, -1, 0, 1, 0, -1, 0, 1], 2], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 1], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 3],
                 [[1, -1, -1, 0, 1, 0, -1, 0, 1], 5], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 7], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 9],
                 [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 1], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 5], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 3],
                 [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 2], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 7], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 8]]
x = []
y = []

for i in training_data:
    x.append(i[0])
    y.append(i[1])

model = create_model()
model.fit({'input': x}, {'targets': y}, n_epoch=10, snapshot_step=500, show_metric=True, run_id='openai_learning')

Solution

  • In your line 2 you have written

    network = input_data(shape=(None, 30, 9), name='input')
    

    This creates a TensorFlow placeholder with the specified shape which is (None, 30, 9) where None represents batch size.

    However when you supply your input in this line

     model.fit({'input': x}, {'targets': y}, n_epoch=10, snapshot_step=500, show_metric=True, run_id='openai_learning')
    

    you are supplying a shape of (30, 9) which does not match the shape of the placeholder created by the input_data function. So I suggest you to import numpy and add this line before model.fit

    x = np.reshape(x, (-1, 30, 9))
    

    This reshapes your array into the shape expected by the placeholder. which is (batch_size, 30, 9)