Reputation: 31
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.
Upvotes: 0
Views: 1035
Reputation: 31
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!
Upvotes: 1
Reputation: 2331
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.
Upvotes: 0