如何在 Conv1D() 函数中使用 'input_shape'?

How to use 'input_shape' in Conv1D() function?

我有 100 万行数据,其中包含六个特征和三个 类。

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)

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

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_steps 减去 2,那么你的第二维形状变化如下:
  • 输入 -> 6
  • Conv1D -> 4
  • 秒后 Conv1D -> 2

并且您不能应用 MaxPool1D by 3 池大小。您可以将池大小更改为 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
_________________________________________________________________