首页 > 解决方案 > 如何在 Conv1D() 函数中使用“input_shape”?

问题描述

我有一百万行数据,包含六个特征和三个类。

6.442  6.338  7.027  8.789 10.009 12.566  A
6.338  7.027  5.338 10.009  8.122 11.217  A
7.027  5.338  5.335  8.122  5.537  6.408  B
5.338  5.335  5.659  5.537  5.241  7.043  B
5.659  6.954  5.954  8.470  9.266  9.334  C
6.954  5.954  6.117  9.266  9.243 12.200  C
5.954  6.117  6.180  9.243  8.688 11.842  A
6.117  6.180  5.393  8.688  5.073  7.722  A
... ... ... ... ... ... ... ... ... ... ...

我想将此数据集输入 CNN。

因此,我编写了以下 Keras 代码:

model = Sequential()
model.add(Conv1D(filters=n_hidden_1, kernel_size=3, activation='sigmoid', 
        input_shape=(1, num_features)))
model.add(Conv1D(filters=n_hidden_2, kernel_size=3, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

这段代码给了我以下错误:

ValueError: Negative dimension size caused by subtracting 3 from 1 for 
'{{node conv1d/conv1d}} 
= Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1]
, explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true]
(conv1d/conv1d/ExpandDims, conv1d/conv1d/ExpandDims_1)' with input shapes
: [?,1,1,6], [1,3,6,64].

编辑:然后,我将模型修改如下:

model = Sequential()
model.add(Conv1D(filters=n_hidden_1, kernel_size=3, activation='sigmoid', 
    input_shape=(n_hidden_1, num_features, 1)))
model.add(Conv1D(filters=n_hidden_2, kernel_size=3, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(3))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

现在我收到以下错误消息:

ValueError: Input 0 of layer max_pooling1d is incompatible with the 
layer: expected ndim=3, found ndim=4. Full shape received: 
    (None, 64, 2, 64)

我写错了什么,为什么错了?

标签: pythonkerasconv-neural-network

解决方案


Conv1DMaxPool1D期望输入形状像(n_batches, n_steps, n_features). 所以,输入形状应该是这样的input_shape=(n_steps, n_features)。如果您想将 6 视为步骤,那么它可能是input_shape=(6,1).

  1. 要添加最后一个维度,请尝试以下操作:
train_X = np.expand_dims(train_x, axis=-1)
validate_x = np.expand_dims(validate_x, axis=-1)
  1. 由于每个卷积层,默认填充有效,减去n_steps2,那么你的第二维形状变化就像:
  • 输入-> 6
  • 在第一个Conv1D-> 4之后
  • 秒后Conv1D-> 2

而且您不能应用MaxPool1D3 倍的池大小。您可以将池大小更改为 2,或者添加padding="same"到您的卷积层之一:

model = tf.keras.Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='sigmoid', input_shape=(6, 1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='sigmoid')) 
model.add(Dropout(0.5))
model.add(MaxPooling1D(2)) # change to 2 or add `padding="same"` to the conv layers
model.add(Flatten())
model.add(Dense(3, activation='softmax'))

model.summary()

概括:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_2 (Conv1D)            (None, 4, 64)             256       
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 2, 64)             12352     
_________________________________________________________________
dropout_1 (Dropout)          (None, 2, 64)             0         
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 1, 64)             0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 3)                195       
=================================================================
Total params: 13,258
Trainable params: 13,258
Non-trainable params: 0
_________________________________________________________________

推荐阅读