【问题标题】:Keras Model with Maxpooling1D and channel_first具有 Maxpooling1D 和 channel_first 的 Keras 模型
【发布时间】:2019-01-28 10:25:48
【问题描述】:

我当前尝试在 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_formatMaxPooling1D 没有这样的关键字。

我使用channels_last 数据格式测试了整个设置,它按我的预期工作。但是由于从channels_firstchannels_last 的转换对我来说需要相当长的时间,所以我真的更希望与channels_first 一起工作。我有一种感觉,我只是错过了一些东西。

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 阅读source code,看来你可以通过data_format='channels_first'
  • @HSK 当我尝试这样做时 (model.add(MaxPooling1D(pool_size=8, data_format='channels_first')) 我收到错误 TypeError: ('Keyword argument not understood:', 'dataformat')。我错过了什么吗?
  • 对不起,我看错了代码;阅读到实际定义 class Layer 的位置,它不接受 data_format 作为关键字参数之一。我认为它接受data_format,因为它调用K.pool2d(inputs, pool_size, strides, padding, data_format, pool_mode='max'),但源代码的其余部分假定所有内容都在channels_last 中。不幸的是,如果你想使用channels_first,如果我没有遗漏什么,你似乎必须自己实现这个。
  • 没有伤害。我之前尝试过,因为先尝试是很自然的事情。但很高兴知道,我不是唯一一个对此感到困惑的人。
  • 我对此进行了 PR,现已合并。 github.com/keras-team/keras/pull/10966

标签: python machine-learning keras conv-neural-network max-pooling


【解决方案1】:

更新as mentioned by @HSK in the commentsMaxPooling 层现在支持data_format 参数,这是this PR 的结果。


好吧,另一种选择是使用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
_________________________________________________________________

【讨论】:

    猜你喜欢
    • 2018-09-20
    • 2017-09-29
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2021-06-04
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多