python - 如何在 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)
我写错了什么,为什么错了?
解决方案
Conv1D
并MaxPool1D
期望输入形状像(n_batches, n_steps, n_features)
. 所以,输入形状应该是这样的input_shape=(n_steps, n_features)
。如果您想将 6 视为步骤,那么它可能是input_shape=(6,1)
.
- 要添加最后一个维度,请尝试以下操作:
train_X = np.expand_dims(train_x, axis=-1)
validate_x = np.expand_dims(validate_x, axis=-1)
- 由于每个卷积层,默认填充有效,减去
n_steps
2,那么你的第二维形状变化就像:
- 输入-> 6
- 在第一个
Conv1D
-> 4之后 - 秒后
Conv1D
-> 2
而且您不能应用MaxPool1D
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
_________________________________________________________________
推荐阅读
- highcharts - Highcharts 组织结构图 - 节点显示不正确
- c# - 在 Azure DevOps 上显示 NUnit 测试代码覆盖率
- azure - 长期运行的 Azure SQL 数据库维护
- java - 超类访问检查失败
- python - 有什么方法可以检测 Excel 工作簿是否在 Python 或 Robot 框架中受密码保护?
- python - 属性 Nonetype 没有方法 .count()
- performance - Is there a more efficient function for finding []byte similarities?
- javascript - 使用电子无法正常工作的网页
- c++ - OpenGL 中的多线程功能
- angular - 如果 1 秒内没有响应,如何添加到数组中?