1

I am trying to pass my neural network an array with shape with shape (1169909, 10, 10).

But no matter what I do...

input_shape=(None,10,10)
input_shape=x_train.shape[1:]
input_shape=x_train.shape
input_shape=(1169909, 1)
input_shape=(10,10)
input_shape=(1169909,10)
input_shape=(1169909,10,10)
input_shape=(1,10,10)

I still get an error. The error changes:

but got array with shape (1169909, 10, 10)
but got array with shape (1169909, 1)

Depending on which way I give the input, which only adds to my confusion.

The actual input looks like this, it's an array of these smaller 10x10 arrays:

array([[ 2, -1, -1, -1, -1, -1, -1, -1, -1,  0],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [11, -1, 11,  6,  3,  2, -1, -1, -1, 11],
       [-1, -1, -1, -1,  5,  7, -1, -1,  2,  7],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, 22, 25, -1, -1, -1, 22],
       [22, -1, -1, -1, 26, 29, -1, -1, 26, 25],
       [27, -1, -1, -1, -1, -1, -1, -1, -1, 23],
       [31, 24, 31, -1, -1, -1, -1, -1, -1, 31]])

I've tried to understand this issue by looking on stack overflow and elsewhere, but I can't figure out the problem and those solutions don't work for me.

Here's the model as of now:

x_train, x_test, y_train, y_test = train_test_split(xving, ywing, test_size=0.2)

x_train = numpy.array(x_train)
y_train = numpy.array(y_train)


model = Sequential()
model.add(keras.layers.Dense(250,activation='tanh', input_shape=(None,10,10)))
model.add(keras.layers.Dense(150,activation='relu'))
model.add(keras.layers.Dense(25,activation='sigmoid'))
model.add(keras.layers.Dense(2,activation='softmax'))
optimizerr = keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
model.compile(optimizer=optimizerr, loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train,epochs = 25, batch_size = 32, verbose=1)

EDIT: So when I use:

input_shape=x_train.shape[1:]

The error becomes with the target:

Error when checking target: expected dense_32 to have 3 dimensions, but got array with shape (1169909, 1)

But the target is an array. When I keep it as a list, I instead get the error:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 1169909 arrays: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

So now I guess my question concerns why the target is an issue.

y_train.shape

yields:

(1169909,)

So I'm confused still?

CapnShanty
  • 529
  • 8
  • 20
  • 1
    Could you show the rest of the code? at least the first layer. – vlizana Oct 30 '18 at 19:07
  • I added the model itself and the bits preceding it – CapnShanty Oct 30 '18 at 19:09
  • As for the target error: [Dense layer is applied on the last axis](https://stackoverflow.com/a/52092176/2099607), hence the shape mismatch. – today Oct 30 '18 at 19:22
  • when I changed the last layer to be model.add(keras.layers.Dense(1,activation='softmax')) (changed 2 to 1) I still get the same error? I want it to give probabilities for both classes still so I went with 2. Even when I use np_utils.to_categorical(y_train) it fails. – CapnShanty Oct 30 '18 at 19:25
  • Yeah, you must change the number of units in the last layer to 1. However, as I mentioned in my previous comment, the other problem is that the output of model has a shape of `(None, 10, 1)` and your labels has a shape of `(1169909,1)` and therefore they don't match. One way to resolve this is to add a Flatten layer somewhere in the model (for example as the first layer). Alternatively, you can reshape your training data to `(num_samples, 10*10)` instead and change the `input_shape` of the first layer of the model accordingly. – today Oct 30 '18 at 19:29

1 Answers1

1

Assuming you have 1169909 pictures (or 2D data items) as a dataset you should use a 2D convolutional layer at the front if you want to pass them to your model as matrices, otherwise it would be no different than flattening each picture before passing them all to your model. I suggest using a convolutional schema, but if that's not the case you can flatten the array like this:

x = x.reshape((1169909, 100))
model.add(keras.layers.Dense(250, activation='tanh', input_dim=100))

Again, passing 2D data to a dense layer will not take advantage of the structural properties of the data.

vlizana
  • 2,962
  • 1
  • 16
  • 26
  • Hmm I hadn't thought about using a convolutional scheme since I'm actually trying to make it predict if a Stratego board is likely to lead to a win or not, I added some conv layers but I'm getting a different kind of error. However you get the correct answer marker since my original problem no longer exists which is the same as solving it hahaha – CapnShanty Oct 30 '18 at 20:17