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

text_mining
deep_learning
python

#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.
    aspectTerm=pd.get_dummies(aspect[“Aspect.Terms”])
    print(aspectTerm)
    from sklearn.model_selection import train_test_split
    from sklearn.svm import SVC
    aspect=pd.concat([aspect,aspectTerm],axis=1)
  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:

classifier=Sequential()
#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
classifier.add(Dropout(rate=0.1))
#input dim is mandatory in the beginning
classifier.add(Dense(units=8,kernel_initializer=‘uniform’,activation=‘sigmoid’))
#Going to apply stochastic gradient descent to the ANN
classifier.compile(optimizer=‘adam’,loss=‘binary_crossentropy’,metrics=[‘accuracy’])

classifier.fit(X_train,y_train,batch_size=1000,epochs=3)

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


#2

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.


#3

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 ?