【问题标题】:Keras: ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)Keras:ValueError:检查目标时出错:预期dense_2具有形状(10,)但得到形状为(1,)的数组
【发布时间】:2018-09-27 00:55:27
【问题描述】:

我是深度学习和 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)

【问题讨论】:

  • 我怀疑你还是需要to_categorical;你能展示一下你的尝试吗?
  • @Ares 刚刚更新!

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


【解决方案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_splitdocs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多