Keras:ValueError:检查目标时出错:预期density_2的形状为(10,),但数组的形状为(1,)

时间:2018-09-25 19:45:45

标签: python python-3.x keras deep-learning

我是深度学习和Keras的新手。当我使用Keras拟合LSTM模型时,出现以下错误消息:ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)

这是我用于构建LSTM的代码:

def build(self, embedding_matrix, dim, num_class, vocab_size, maxlen):
    model = Sequential()
    model.add(Embedding(vocab_size, dim, weights = [embedding_matrix],
                        input_length = maxlen, trainable = False)) ## pre-trained model
    model.add(LSTM(dim))
    model.add(Dense(dim, activation = "relu"))
    model.add(Dense(num_class, activation = "softmax"))
    self.model = model

在发布本文之前,我尝试了其他SO文章中提及的几种解决方案。例如,使用to_categorical转换标签,在最后一层之前使用Flatten。遗憾的是,他们都没有工作。

这是我用于运行脚本的日志文件:

Start to fit GLOVE with reported data
Applying GLOVE pre-trained model
Number of unique tokens: 308758
Pre-trained model finished

Applying keras text pre-processing
Finish to apply keras text pre-processing

Start to fit the model...

Build LSTM model...
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding_1 (Embedding)      (None, 36303, 300)        92627400
_________________________________________________________________
lstm_1 (LSTM)                (None, 300)               721200
_________________________________________________________________
dense_1 (Dense)              (None, 300)               90300
_________________________________________________________________
dense_2 (Dense)              (None, 10)                3010
=================================================================
Total params: 93,441,910
Trainable params: 814,510
Non-trainable params: 92,627,400
_________________________________________________________________
None
Finish model building

一切顺利,直到history = self.model.fit(train_padded, y_train, epochs = 10, batch_size = 128, validation_split = 0.2),然后出现了以上错误。

我用光了解决方案。任何帮助都将是有用的!

编辑:

关于y_train,这是我用来构建y_train的代码:

labels = dt["category"].values
num_class = len(np.unique(labels))
classes = np.unique(labels)
le = LabelEncoder()
y = le.fit_transform(labels)
y = to_categorical(y, num_class)
## split to training and test set
x_train, y_train, x_test, y_test = train_test_split(text, y, test_size = 0.33, 
                     random_state = 42, 
                     stratify = dt["category"].astype("str"))

另一个更新:这是形状。

The shape of y_train:  (48334,)
The shape of x_train:  (98132,)
The shape of y_test:  (48334, 10)
The shape of x_test:  (98132, 10)

1 个答案:

答案 0 :(得分:1)

问题是您输入的x_train, y_train, x_test, y_test顺序错误,因此分配错误。 train_test_split返回以下内容:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,random_state=42)

您有x_train, y_train, x_test, y_test

train_test_split docs