期望密集具有形状,但具有形状的阵列

Bha*_*iri 10 python shape text-classification keras

在keras中运行文本分类模型时,调用model.predict函数时出现以下错误。我到处搜索,但对我来说不起作用。

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

我的数据有5个类,总共只有15个示例。以下是数据集

             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
Run Code Online (Sandbox Code Playgroud)

这是我模型的代码

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)
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚问题出在哪里以及如何解决。

fur*_*ras 7

x_data 是具有形状的二维数组 (15, 100)

  print(x_data.shape) 
Run Code Online (Sandbox Code Playgroud)

但是x_data[0]形状是一维数组(100, )

  print(x_data[0].shape) 
Run Code Online (Sandbox Code Playgroud)

这会带来问题。

使用切片x_data[0:1]将其作为具有形状的二维数组(1, 100)

 print(x_data[0:1].shape) 
Run Code Online (Sandbox Code Playgroud)

它会工作

 predictions = model.predict(x_data[0:1])
Run Code Online (Sandbox Code Playgroud)