【问题标题】:Keras ValueError: Error when checking target: expected dense_15 to have 3 dimensions, but got array with shape (301390, 8)Keras ValueError:检查目标时出错:预期 dense_15 具有 3 个维度,但得到的数组具有形状 (301390, 8)
【发布时间】:2018-11-23 09:02:07
【问题描述】:

我想从输入文本中预测 8 个类。这是我预处理数据的代码:

num_max = 1000
tok = Tokenizer(num_words=num_max)
tok.fit_on_texts(x_train)
mat_texts = tok.texts_to_matrix(x_train,mode='count')
num_max = 1000
tok = Tokenizer(num_words=num_max)
tok.fit_on_texts(x_train)
max_len = 100
cnn_texts_seq = tok.texts_to_sequences(x_train)
print(cnn_texts_seq[0])

[12, 4, 303]

# padding the sequences
cnn_texts_mat = sequence.pad_sequences(cnn_texts_seq,maxlen=max_len)
print(cnn_texts_mat[0])
print(cnn_texts_mat.shape)

[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0  12   4 303]

(301390, 100)

下面是我的模型的结构,它包含一个嵌入层:

max_features = 20000
max_features = cnn_texts_mat.shape[1]
maxlen = 100
embedding_size = 128


model = Sequential()
model.add(Embedding(max_features, embedding_size, input_length=maxlen))
model.add(Dropout(0.2))

model.add(Dense(5000, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(600, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(units=y_train.shape[1], activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy',
              optimizer=sgd)

以下是模型摘要:

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_5 (Embedding)      (None, 100, 128)          12800     
_________________________________________________________________
dropout_13 (Dropout)         (None, 100, 128)          0         
_________________________________________________________________
dense_13 (Dense)             (None, 100, 5000)         645000    
_________________________________________________________________
dropout_14 (Dropout)         (None, 100, 5000)         0         
_________________________________________________________________
dense_14 (Dense)             (None, 100, 600)          3000600   
_________________________________________________________________
dropout_15 (Dropout)         (None, 100, 600)          0         
_________________________________________________________________
dense_15 (Dense)             (None, 100, 8)            4808      
=================================================================
Total params: 3,663,208
Trainable params: 3,663,208
Non-trainable params: 0

在此之后,当我尝试运行模型时出现以下错误:

model.fit(x=cnn_texts_mat, y=y_train, epochs=2, batch_size=100)

ValueError                                Traceback (most recent call last)
<ipython-input-41-4b9da9914e7e> in <module>
----> 1 model.fit(x=cnn_texts_mat, y=y_train, epochs=2, batch_size=100)

~/.local/lib/python3.5/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

~/.local/lib/python3.5/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    787                 feed_output_shapes,
    788                 check_batch_axis=False,  # Don't enforce the batch size.
--> 789                 exception_prefix='target')
    790 
    791             # Generate sample-wise weight values given the `sample_weight` and

~/.local/lib/python3.5/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    126                         ': expected ' + names[i] + ' to have ' +
    127                         str(len(shape)) + ' dimensions, but got array '
--> 128                         'with shape ' + str(data_shape))
    129                 if not check_batch_axis:
    130                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_15 to have 3 dimensions, but got array with shape (301390, 8)

【问题讨论】:

    标签: python machine-learning keras deep-learning nlp


    【解决方案1】:

    查看模型摘要中最后一层的输出形状:它是(None, 100, 8)。这不是你要找的。每个样本的标签形状为(8,) 而不是(100,8)。为什么会这样?那是因为the Dense layer is applied on the last axis of its input,因此Embedding层的输出是3D的,所以后面所有Dense层的输出也是3维的。

    如何解决这个问题?一种方法是在模型的某处使用Flatten 层(可能就在嵌入层之后)。这样,您将获得形状为 (None, 8) 的 2D 输出,这是您想要的并且与标签的形状一致。

    但是,请注意,您最终可能会得到一个非常大的模型(即太多参数),这很容易过度拟合。要么减少密集层中的单元数量,要么使用Conv1DMaxPooling1D 层甚至 RNN 层来处理嵌入并降低结果张量的维数(这也可能使用它们也可以提高模型)。

    【讨论】:

    • 谢谢!我选择了 Conv1D 模型,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2020-07-16
    • 2020-02-05
    相关资源
    最近更新 更多