一维卷积神经网络输入形状'ValueError'

时间:2019-05-17 16:33:29

标签: python tensorflow machine-learning keras conv-neural-network

我正在生成一维卷积,但是我的数据输入形状有些麻烦。我看了一些帖子,似乎错误是数据必须是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

1 个答案:

答案 0 :(得分:1)

在我的案例中,问题在于目标向量是3D,而输出向量是2D,因此存在明显的不匹配。要解决此问题,请将y_train的形状更改为(batch, 8)或使用return_sequences=True从上一个LSTM层返回相同的形状。