0

I met the problem with "Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (867, 44, 44)".

In my opinion, I think I need to transfer the dimension during preprocessing part or change the loss function.

I have already searched the related questions on stackoverflow but fail to solve it. Can somebody help me please?

The input are some colorful pictures with height:46 and width:120, so I set the input with(46,120,3).

The shape of the X_train is :(1084, 46, 120, 3) The shape of the Y_train(which is the label) after transfer to one hot encoding is :(1084, 44, 44)

And details of the preprocessing part and the model are as below:

model = Sequential()

X_train = X_train/255  Y_train = to_categorical(Y_train,num_classes = 44)

random_seed = 2 X_train, X_val, Y_train, Y_val = train_test_split(X_train, 
                Y_train, test_size = 0.2, random_state=random_seed)



model.add(Conv2D(filters=16,kernel_size=(5,5),padding='same',input_shape=(46,120,3),activation='relu',data_format
= 'channels_last')) 
model.add(Conv2D(filters=16,kernel_size=(5,5),padding='same',activation='relu'))

model.add(MaxPool2D(pool_size=(2,2),strides = (1,1))) 
model.add(Dropout(0.25))

model.add(Conv2D(filters=32,kernel_size=(5,5),padding='same',activation='relu')) 
model.add(Conv2D(filters=32,kernel_size=(5,5),padding='same',activation='relu'))

model.add(MaxPool2D(pool_size=(2,2),strides = (1,1))) model.add(Dropout(0.25))

model.add(Conv2D(filters=32,kernel_size=(5,5),padding='same',activation='relu')) 
model.add(Conv2D(filters=32,kernel_size=(5,5),padding='same',activation='relu'))

model.add(MaxPool2D(pool_size=(2,2),strides = (1,1))) 
model.add(Dropout(0.25))

model.add(Flatten()) 
model.add(Dense(256,activation='relu')) 
model.add(Dropout(0.3))

model.add(Dense(44,activations='softmax'))

model.compile(loss='binary_crossentropy',
               optimizer='rmsprop',metrics=['accuracy']) 
model.summary()

# Set the learning rate annealer learning_rate_reduction = 
ReduceLROnPlateau(monitor='val_acc', 
                                            patience = 3,
                                            verbose = 1, 
                                            factor = 0.5, 
                                            min_lr = 0.00001) epochs = 100 batch_size = 86

model.fit(X_train,Y_train)  

ValueError:Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (867, 44, 44)

By the way, the dense_2 is the last layer of my model.

2
  • 1
    is there something wrong with your Y_train? Why Y_train shape is (1084, 44, 44). Should it be (1084, 44)? Commented May 21, 2019 at 10:55
  • @IoannisNasios Yes,that's the reason, but I can't fix it... I think it's because I convert the Y_train to the one hot encoding, I tried to add Y_train.squeeze() to lower the dimension after the to_categorical() method.. but it didn't work... Commented May 21, 2019 at 15:56

2 Answers 2

1

I fix the problem with delete the to_categorical() method and use the binary_crossentropy as the loss. Because the every single Y_train is a binary vector with the shape of (1,44), and If you add another to_categorical() method ,it will make the every single Y_train's shape become (1,44,44) , I don't know why and I'm still trying to figure it out. But that's where the problem is. Thanks for everyone's help!

Sign up to request clarification or add additional context in comments.

1 Comment

replace binary_crossentropy with categorical_crossentropy
0

You can't expect a labeling in 2D while having a Dense layer as final output. So either you want a 2D labeling and you need a layer that will output a 2D tensor or you need to modify your Y_train to something a Dense layer can predict.

1 Comment

I tried to use the Y_train.squeeze() after the to_categorical() method to lower the dimension, but it still can't fix it... do you know any other method to lower the label's dimension? Thanks for your reply~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.