【发布时间】:2019-05-17 16:33:28
【问题描述】:
我正在生成一维卷积,但我的数据的输入形状有些问题。我看了一些帖子,似乎错误是数据必须是 3D,但我的数据已经是 3D。
# shape
# x_train shape: (1228, 1452, 20)
# y_train shape: (1228, 1452, 8)
# x_val shape: (223, 680, 20)
# x_val shape: (223, 680, 8)
###
n_outputs = 8
n_timesteps = 1452
n_features = 20
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(x_train.shape[1:]))) # ie 1452, 20
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=9,
batch_size=64,
shuffle=True)
但我不断收到此错误消息:
ValueError: A target array with shape (1228, 1452, 8) was passed for an output of shape (None, 8) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
我从中收集到的是 3 维的目标形状与 2 维输出不同,因此无法计算出损失,但我只需要找到一种方法来重塑它们平等的。
编辑
model.summary()如下图
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d (Conv1D) (None, 1450, 64) 3904
_________________________________________________________________
conv1d_1 (Conv1D) (None, 1448, 64) 12352
_________________________________________________________________
dropout (Dropout) (None, 1448, 64) 0
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 724, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 46336) 0
_________________________________________________________________
dense (Dense) (None, 100) 4633700
_________________________________________________________________
dense_1 (Dense) (None, 8) 808
=================================================================
Total params: 4,650,764
Traceback (most recent call last):
Trainable params: 4,650,764
Non-trainable params: 0
【问题讨论】:
-
唯一让我大吃一惊的可能是你的
input_shape可能需要另一个逗号来使批量大小不明确。也许input_shape=(1452,20,) -
恐怕这不走运
-
:( 那是因为我没有注意到。就像错误说的那样:您的
y值具有意外的维度。请注意y_train shape: 1228,1452,8但在您的最后一个 Dense 层下n_outputs=8. 真正影响你的输出的是(batch,1452,8),但它期待(batch,8) -
:) 确切地说,那么:您的
y值应该(仅)是长度为 8 的一次性编码向量。网络将只欢迎形状 (batch,1452,20) 的输入并且只输出形状 (batch,8)。y_train是 (batch,1452,8) 但必须是 (batch,8)。此外,您的训练和验证形状不一致:(batch,1452,20) 用于训练和 (batch,680,20) 用于验证,但是 (1452 != 680)。 -
谢谢,现在这更有意义了!
标签: python tensorflow machine-learning keras conv-neural-network