# ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (2,)

I am trying to build a CNN model on Keras. The data has a dimension of 921 rows × 10000 columns.

Here is the code:

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.optimizers import SGD

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(merge.iloc[:,166:10166], merge[['Result_cat','Result_cat1']].values, test_size=0.2)

model = Sequential()

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.SGD(lr=0.01),
metrics=['accuracy'])

model.fit(X_train, y_train, batch_size=100, epochs=10)
score = model.evaluate(X_test, y_test, batch_size=32)


Then I encountered error

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

I am new to Keras and CNN. Can someone please explain to me what this means and how I can fix this? Thanks.

• Welcome to SE:AI! Our focus is the theoretical aspects of the field, as opposed to troubleshooting. I'm leaving open (pending community closure) per the accepted answer, but, in general, this type of question is a better fit for Stack Overflow. – DukeZhou Nov 19 '19 at 22:06
• @DukeZhou Thanks for the reminder I'll do it next time – nilsinelabore Nov 19 '19 at 22:35
• Hope to see more of you over here too! :) – DukeZhou Nov 20 '19 at 0:14

• thank you! The data has a temperature time series data in each row for each observation, and classification labelling Y/N in the last two columns. It worked after changing the number of neurones in the final layer to 2. However, after making the change to batch_size=1000, epochs=1000, I experienced a repeated loss/ accuracy level for each Epoch:736/736 [==============================] - 0s 563us/step - loss: 1.9146 - acc: 0.5584. Is there a way I can fix this? – nilsinelabore Nov 18 '19 at 12:31