expected dense to have shape but got array with shape
Asked Answered
P

2

12

I am getting the following error while calling the model.predict function when running a text classification model in keras. I searched the everywhere but it isn't working for me.

ValueError: Error when checking input: expected dense_1_input to have shape (100,) but got array with shape (1,)

My data has 5 classes and has a total of 15 examples only. Below is the dataset

             query        tags
0               hi       intro
1      how are you       wellb
2            hello       intro
3        what's up       wellb
4       how's life       wellb
5              bye          gb
6    see you later          gb
7         good bye          gb
8           thanks   gratitude
9        thank you   gratitude
10  that's helpful   gratitude
11      I am great  revertfine
12            fine  revertfine
13       I am fine  revertfine
14            good  revertfine

This is the code of my model

from keras.preprocessing.text import Tokenizer
from sklearn.preprocessing import LabelBinarizer
from keras.models import Sequential
import pandas as pd
from keras.layers import Dense, Activation

data = pd.read_csv('text_class.csv')
train_text = data['query']
train_labels = data['tags']

tokenize = Tokenizer(num_words=100)
tokenize.fit_on_texts(train_text)

x_data = tokenize.texts_to_matrix(train_text)

encoder = LabelBinarizer()
encoder.fit(train_labels)
y_data = encoder.transform(train_labels)

model = Sequential()
model.add(Dense(512, input_shape=(100,)))
model.add(Activation('relu'))
model.add(Dense(5))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
model.fit(x_data, y_data, batch_size=8, epochs=10)

predictions = model.predict(x_data[0])
tag_labels = encoder.classes_
predicted_tags = tag_labels[np.argmax(predictions)]
print (predicted_tags)

I am not able to figure out where the problem lies and how to fix it.

Phyllys answered 19/7, 2018 at 11:46 Comment(2)
What is the shape of x_data?Haggi
@Haggi 15,100Phyllys
R
15

x_data is 2-dimensional array with shape (15, 100)

  print(x_data.shape) 

but x_data[0] is 1-dimensional array with shape (100, )

  print(x_data[0].shape) 

and it makes problem.

Use slicing x_data[0:1] to get it as 2-dimensional array with shape (1, 100)

 print(x_data[0:1].shape) 

and it will work

 predictions = model.predict(x_data[0:1])
Radiotelephone answered 19/7, 2018 at 22:24 Comment(3)
completely unrelated question. How did you convert his text data into csv or dataframe?Millicent
@krishna I used pandas.read_table(filename, sep="\s{2,}") - and even io.StringIO(text) to create file in memory, not on disk.Radiotelephone
Thanks a lot ... 😀Phyllys
B
3

Change predictions = model.predict(x_data) to predictions = model.predict(x_data[0:1])

Your input layer in your NN had 100 neurons but it seems that your input has a shape of only (1,), therefore you need change the input shape

Browning answered 19/7, 2018 at 12:6 Comment(2)
didbt work it says Error when checking input: expected dense_19_input to have shape (1,) but got array with shape (100,)Phyllys
@BhaveshLaddagiri I updated my answer. I hope it helps.Browning

© 2022 - 2024 — McMap. All rights reserved.