coline.s
coline.s

Reputation: 1

Neural network input shape error

I am a beginner in keras and I am trying to classify data with a neural network.

   x_train = x_train.reshape(1,x_train.shape[0],window,5)
   x_val = x_val.reshape(1,x_val.shape[0],window,5)

   x_train = x_train.astype('float32')
   x_val = x_val.astype('float32')

   model = Sequential()

   model.add(Dense(64,activation='relu',input_shape= (data_dim,window,5)))
   model.add(Dropout(0.5))

   model.add(Dense(64,activation='relu'))
   model.add(Dropout(0.5))
   model.add(Dense(2,activation='softmax'))

   model.compile(loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

   weights = model.get_weights()


   model_info = model.fit(x_train, y_train,batch_size=batchsize, nb_epoch=15,verbose=1,validation_data=(x_val, y_val))

  print x_train.shape
  #(1,1600,45,5)

  print y_train.shape
  #(1600,2)

I always have this error with this script and I don't understand why:

 ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (16000, 2)

Upvotes: 0

Views: 158

Answers (1)

Red Dwarf
Red Dwarf

Reputation: 728

Your model's output (dense_3, so named because it is the third Dense layer) has four dimensions. However, the labels you are attempting to compare it to (y_train) is only two dimensions. You will need to alter your network's architecture so that your model reshapes the data to match the labels.

Keeping track of tensor shapes is difficult when you're just starting out, so I recommend calling plot_model(model, to_file='model.png', show_shapes=True) before calling model.fit. You can look at the resulting PNG to understand what effect layers are having on the shape of your data.

Upvotes: 2

Related Questions