0

I am very green behind the ears when it comes to creating a NN. Right now I am receiving the following error:

ValueError: Error when checking : expected dense_1_input to have 3 dimensions, but got array with shape (8, 8)

Background: I am using an 8x8 board this is how I am initializing it:

self.state = np.zeros((LENGTH, LENGTH))

Here is the code where I build my Model:

def build_model(self):
    #builds the NN for Deep-Q Model
    model = Sequential() 
    model.add(Dense(24,input_shape = (LENGTH, LENGTH), activation='relu'))
    model.add(Flatten())
    model.add(Dense(24, activation='relu'))
    model.add(Dense(self.action_size, activation = 'linear'))
    model.compile(loss='mse', optimizer='Adam')

    return model

I figured since the shape of the board is (8,8) that the input_size should be the same. Not sure what I am doing wrong?

Just in case this is helpful:

The game I have made is super simple that involves 5 pieces on the board:

  • player1 has 1 piece and can move forward and backward diagonally only 1 step
  • player2 has 4 pieces and can only move forward from their position diagonally 1 step

The objective for player1 is to get to the other side of the board The objective for player2 is to trap player 1 so he cannot move

Any help would be greatly appreciated!

  • The first axis corresponds to samples (i.e. batch axis). Therefore, change your code to this: `np.zeros((1, LENGTH, LENGTH))` this means one sample of shape `(LENGTH, LENGTH)`. – today Sep 18 '18 at 17:52
  • Thank you so much for your response! I am going to try this out shortly :) – pi-r-squared Sep 18 '18 at 19:06
  • So I gave it a try and sort of fell on my face, Your explanation was solid though and it helped me understand what the 3rd dimension was for. I managed to get it to run... but this is how: I changed input_shape to this `input_shape = (LENGTH, )` the problem is I have no idea why it accepts this? and if I am even doing it right? Sorry for being such a noob. – pi-r-squared Sep 18 '18 at 19:51

1 Answers1

0

I managed to get it to run... but this is how: I changed input_shape to this input_shape = (LENGTH, ) the problem is I have no idea why it accepts this? and if I am even doing it right?

No, the first input shape you have used (i.e. (LENGTH,LENGTH)) was the correct one. Note that the input_shape argument specifies the shape of one and only one single training sample, and not all the training data you have. For example, if you have 1000 boards of 8x8 then the training data has a shape of (1000, 8, 8) but the input_shape argument must be assigned (8,8), i.e. the shape of one training sample.

Further, as you may or may not know the Dense layer is applied on the last axis and since you have defined the input shape of Dense layer as (LENGTH,LENGTH), the Dense layer would be applied not on all of the input (i.e. board) but on the second axis (i.e. rows of the board). I guess this is not what you are looking for, so you have two options here: 1) you can move the Flatten layer to the top and put it as the first layer of the model:

model = Sequential()
model.add(Flatten(input_shape=(LENGTH, LENGTH)))
model.add(Dense(24, activation='relu'))
# the rest of the model

or 2) you can reshape the training data to make it have a shape of (num_boards, LENGTH*LENGTH) and adjust the input_shape argument accordingly (in this case there is no need for the Flatten layer you have in your model, you can remove it):

training_data = np.reshape(training_data, (num_boards, LENGTH*LENGTH))

model = Sequential() 
model.add(Dense(24,input_shape=(LENGTH*LENGTH,), activation='relu'))

As a side note, if you have only one training/test sample (which is odd!) or whatever number of training/test samples, the first axis of the training/test data array must corresponds to samples, i.e. the shape of all the training/test data array must be (num_sample, ...). Otherwise, you would get errors complaining about shape, as you have gotten, when calling fit/predict/evaluate methods. The same thing applies to the array that contains the training/test labels.

today
  • 32,602
  • 8
  • 95
  • 115
  • Wow thank you for such a detailed response! I am trying to implement a DQN for game based on some articles I found. I do have some of the basics down...but I am with some of things you listed I am still struggling (batches, inputs, outputs). I will give this a try in an hour or so.. and keep you posted on how it works out. Any resources that you used to learn this would be greatly appreciated as well thank you again so much! – pi-r-squared Sep 18 '18 at 20:49
  • Thank you so much again for your answer! I spent some time and got it working based on your input and got it working!!! If you have any resources you could share that helped you understand inputs and outputs for NN's I would super appreciate it as this is something I am still struggling with but this for sure helped! – pi-r-squared Sep 19 '18 at 17:49
  • @pi-r-squared If you want to know more about input_shape, batch size, etc. I recommend [this](https://stackoverflow.com/q/44747343/2099607) and [this](https://stats.stackexchange.com/q/153531/114422) questions and their answers. If you want to learn more about Keras, I recommend its official guides ([sequential](https://keras.io/getting-started/sequential-model-guide/) and [functional](https://keras.io/getting-started/functional-api-guide/)), its [official blog](https://blog.keras.io/) and a [book](https://github.com/fchollet/deep-learning-with-python-notebooks) written by creator of Keras. – today Sep 19 '18 at 18:01