Keras模型与Maxpooling1D和channel_first

7
我在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 中说存在一个关键字 data_format 用于 MaxPooling2D,但对于 MaxPooling1D 却没有这样的关键字。
我使用了 channels_last 数据格式测试了整个设置,并且它按照我的期望工作。但由于从 channels_first 转换到 channels_last 对我来说需要相当长的时间,所以我真的更喜欢它能够使用 channels_first。我感觉我可能只是错过了什么。
如果您需要更多信息,请让我知道。

阅读源代码(https://github.com/keras-team/keras/blob/master/keras/layers/pooling.py#L57),看起来你只需要传递 data_format='channels_first' - fractals
@HSK 当我尝试这样做时 (model.add(MaxPooling1D(pool_size=8, data_format='channels_first')),我收到一个错误 TypeError: ('Keyword argument not understood:', 'dataformat')。我有什么遗漏吗? - Gretel_f
抱歉,我误读了代码;在class Layer被定义的地方之前,它并不接受data_format作为关键字参数之一。我以为它接受data_format,因为它调用了K.pool2d(inputs, pool_size, strides, padding, data_format, pool_mode='max'),但是源代码的其余部分假定所有内容都在channels_last中。不幸的是,如果我没有漏掉什么东西,看起来你需要自己实现这个功能,如果你想使用channels_first - fractals
没关系。我之前试过了,因为这是自然的第一件事。但很好知道,我不是唯一被这个困惑的人。 - Gretel_f
1
我已经提交了一个关于这个问题的PR,并且现在已经合并了。https://github.com/keras-team/keras/pull/10966 - fractals
1个回答

4
更新: 正如评论中@HSK所提到的, 由于这个PR, 现在在MaxPooling层中支持data_format参数。

好的,一种替代方案是使用Permute层(并移除第二个卷积层的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
_________________________________________________________________

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接