具有 Maxpooling1D 和 channel_first 的 Keras 模型
Keras Model with Maxpooling1D and channel_first
我目前尝试在 Keras 中为时间序列分类构建顺序模型时遇到问题。我想使用 channels_first
数据,因为从每个处理的角度来看它更方便(不过我只使用一个通道)。这适用于我正在使用的 Convolution1D
层,因为我可以指定 data_sample='channels_first'
,但不知何故这不适用于 Maxpooling1D
,它似乎没有此选项.
我要构建的模型结构如下:
model = Sequential()
model.add(Convolution1D(filters=16, kernel_size=35, activation='relu', input_shape=(1, window_length), data_format='channels_first'))
model.add(MaxPooling1D(pool_size=5)
model.add(Convolution1D(filters=16, kernel_size=10, activation='relu', data_format='channels_first'))
[...] #several other layers here
使用 window_length = 5000
添加所有三层后,我得到以下摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_1 (Conv1D) (None, 32, 4966) 1152
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 4, 4966) 0
_________________________________________________________________
conv1d_2 (Conv1D) (None, 16, 4957) 656
=================================================================
Total params: 1,808
Trainable params: 1,808
Non-trainable params: 0
现在,我想知道这是否正确,因为我预计池化层会减少第三维(即特征图中的神经元数量)而不是第二维(即过滤器数量) ?正如我所见,MaxPooling1D
不识别 channels_first
排序,而 Keras documentation 表示 MaxPooling2D
存在关键字 data_format
,但没有这样的关键字MaxPooling1D
.
我用 channels_last
数据格式测试了整个设置,结果如我所料。但由于从 channels_first
到 channels_last
的转换对我来说需要相当长的时间,所以我真的更愿意使用 channels_first
进行这项工作。而且我觉得我只是错过了一些东西。
如果您需要更多信息,请告诉我。
更新:, the data_format
argument is now supported in MaxPooling
layers as a result of this PR.
好吧,一种替代方法是使用 Permute
层(并删除第二个 conv 层的 channels_first
):
model = Sequential()
model.add(Convolution1D(filters=16, kernel_size=35, activation='relu', input_shape=(1, 100), data_format='channels_first'))
model.add(Permute((2, 1)))
model.add(MaxPooling1D(pool_size=5))
model.add(Convolution1D(filters=16, kernel_size=10, activation='relu'))
model.summary()
模型摘要:
Layer (type) Output Shape Param #
=================================================================
conv1d_7 (Conv1D) (None, 16, 66) 576
_________________________________________________________________
permute_1 (Permute) (None, 66, 16) 0
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 13, 16) 0
_________________________________________________________________
conv1d_8 (Conv1D) (None, 4, 16) 2096
=================================================================
Total params: 2,672
Trainable params: 2,672
Non-trainable params: 0
_________________________________________________________________
我目前尝试在 Keras 中为时间序列分类构建顺序模型时遇到问题。我想使用 channels_first
数据,因为从每个处理的角度来看它更方便(不过我只使用一个通道)。这适用于我正在使用的 Convolution1D
层,因为我可以指定 data_sample='channels_first'
,但不知何故这不适用于 Maxpooling1D
,它似乎没有此选项.
我要构建的模型结构如下:
model = Sequential()
model.add(Convolution1D(filters=16, kernel_size=35, activation='relu', input_shape=(1, window_length), data_format='channels_first'))
model.add(MaxPooling1D(pool_size=5)
model.add(Convolution1D(filters=16, kernel_size=10, activation='relu', data_format='channels_first'))
[...] #several other layers here
使用 window_length = 5000
添加所有三层后,我得到以下摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_1 (Conv1D) (None, 32, 4966) 1152
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 4, 4966) 0
_________________________________________________________________
conv1d_2 (Conv1D) (None, 16, 4957) 656
=================================================================
Total params: 1,808
Trainable params: 1,808
Non-trainable params: 0
现在,我想知道这是否正确,因为我预计池化层会减少第三维(即特征图中的神经元数量)而不是第二维(即过滤器数量) ?正如我所见,MaxPooling1D
不识别 channels_first
排序,而 Keras documentation 表示 MaxPooling2D
存在关键字 data_format
,但没有这样的关键字MaxPooling1D
.
我用 channels_last
数据格式测试了整个设置,结果如我所料。但由于从 channels_first
到 channels_last
的转换对我来说需要相当长的时间,所以我真的更愿意使用 channels_first
进行这项工作。而且我觉得我只是错过了一些东西。
如果您需要更多信息,请告诉我。
更新:data_format
argument is now supported in MaxPooling
layers as a result of this PR.
好吧,一种替代方法是使用 Permute
层(并删除第二个 conv 层的 channels_first
):
model = Sequential()
model.add(Convolution1D(filters=16, kernel_size=35, activation='relu', input_shape=(1, 100), data_format='channels_first'))
model.add(Permute((2, 1)))
model.add(MaxPooling1D(pool_size=5))
model.add(Convolution1D(filters=16, kernel_size=10, activation='relu'))
model.summary()
模型摘要:
Layer (type) Output Shape Param #
=================================================================
conv1d_7 (Conv1D) (None, 16, 66) 576
_________________________________________________________________
permute_1 (Permute) (None, 66, 16) 0
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 13, 16) 0
_________________________________________________________________
conv1d_8 (Conv1D) (None, 4, 16) 2096
=================================================================
Total params: 2,672
Trainable params: 2,672
Non-trainable params: 0
_________________________________________________________________