0

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')
Loanb222
  • 841
  • 1
  • 11
  • 29

1 Answers1

3

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)

satyajith
  • 64
  • 4
  • When I change that I got a different feed error: ValueError: Cannot feed value of shape (1,) for Tensor u'targets/Y:0', which has shape '(?, 9)' – Loanb222 Apr 27 '17 at 14:15
  • You are getting the new error because you have the same problem with the targets too. You have already resolved the previous error. To resolve this new one I suggest you to include y = np.reshape(y,(-1,9)) before the model.fit – satyajith Apr 27 '17 at 15:01
  • We can't convert an array of 30 elements to the size of 9. I didn't get that point how you are converting 30 elements array to (-1, 9). Please provide details I am facing error ValueError: cannot reshape array of size 30 into shape (9) – Parvez Khan May 14 '18 at 14:04
  • @ParvezKhan You aren't converting a list with 30 elements to size 9. If you see my answer, it is listed that you convert the features to (-1, 30, 9) and labels to (-1, 9). Note that labels do not have 30 elements but only have one. It is the features that have 30 elements. – satyajith May 17 '18 at 09:28
  • @satyajith, I have figured out where the problem is and you're absolutely wrong for y = np.reshape(y,(-1,9)) instead we have to y = np.reshape(y,(-1,1)) and the final fully connected layer should be network = fully_connected(network, 1, activation='linear') – Parvez Khan May 17 '18 at 10:39
  • @satyajith, Thanks for knowledge sharing and keep it up :). – Parvez Khan May 18 '18 at 04:22