我是深度学习和 Keras 的新手。当我使用 Keras 拟合 LSTM 模型时,收到以下错误消息:ValueError: Error when check target: Expected dend_2 to have shape (10,) but getting 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)
最佳答案
问题是您的 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
关于python - Keras:ValueError:检查目标时出错:期望dense_2具有形状(10,)但得到形状为(1,)的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52505683/