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.add(Dense(50, input_shape=(10000,))) 
model.add(Dense(50, activation='softmax')) 
model.add(Dense(1, activation='linear')) 


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.

  • $\begingroup$ 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. $\endgroup$
    – DukeZhou
    Nov 19 '19 at 22:06
  • $\begingroup$ @DukeZhou Thanks for the reminder I'll do it next time $\endgroup$ Nov 19 '19 at 22:35
  • $\begingroup$ Hope to see more of you over here too! :) $\endgroup$
    – DukeZhou
    Nov 20 '19 at 0:14

Problem is in the output layer and you are using categorical_crossentropy for a loss function. Quoting Keras documentation:

Note: when using the categorical_crossentropy loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros except for a 1 at the index corresponding to the class of the sample). In order to convert integer targets into categorical targets, you can use the Keras utility to_categorical:

I would check if the target values are defined consistently. They seem to be defined for sparse_categorical_crossentropy, which requires a dense layer where number of neurons = number of classes of the problem. If they are defined as integers - as required for sparse_categorical_crossentropy, you can convert them to categorical formatting using to_categorical function, also defined in the documentation page linked above; or you can keep them as integers, and change the number of neurons in the final layer to 2, and loss function to sparse_categorical_crossentropy.

  • $\begingroup$ 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? $\endgroup$ Nov 18 '19 at 12:31
  • $\begingroup$ That seems to be unrelated to this question. It would be better if you create a new question for that issue. $\endgroup$
    – serali
    Nov 18 '19 at 12:36
  • $\begingroup$ Sure. I will check some reference before asking the new question. Thank you! $\endgroup$ Nov 18 '19 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.