How to solve "ValueError: Error when checking target: expected dense_14 to have shape (8,) but got array with shape (1,)"



Hi Everyone,
I am new to Deep Learning and I tried some code with Deep Learning techniques to predict the categories of the text. Please find the specifications of dataframe below.

  1. Stopwords, punctuations and tolower are done to all the text and it is now in bag of words format
  2. Many aspect Terms are one hot encoded.
    from sklearn.model_selection import train_test_split
    from sklearn.svm import SVC
  3. There are totally 9 aspect categories (Target)

Input dataframe shape:(2801, 2431)
Target dataframe unique categories: 9
I tried using very simple ANN in Keras as below:

#We will start adding layers Adding the input layer and one hidden layer
classifier.add(Dense(units=1220,kernel_initializer=‘uniform’,activation=‘relu’,input_dim=2431)) #output_dim is chosen (11 input+1 output)/2
#input dim is mandatory in the beginning
#Going to apply stochastic gradient descent to the ANN

But I am getting the following error:
ValueError: Error when checking target: expected dense_14 to have shape (8,) but got array with shape (1,)

Could you please advice on the above ?

Thank you in advance


Hi @mosrihari,

There are two issues which I can mentioned right now -

  1. You have mentioned that there are 9 unique categories in your target. Therefore, your target should be a One Hot encoded vector of shape -> (2801, 9). You will use this vector to train your model.
  2. Also, you last layer would have 9 units instead of 8.

If you do these two things which I mentioned, your model would hopefully run successfullly.


Hi @jalFaizy Thank you so much and it worked fine. But the problem is there are no categories that are predicted more than 0.5 . Could you please help me how I can replace the maximum value in a dataframe to 1 and all the other value as 0 ?