Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Model with Maxpooling1D and channel_first

I have a problem with my current attempt to build a sequential model for time series classification in Keras. I want to work with channels_first data, because it is more convenient from a perprocessing perspective (I only work with one channel, though). This works fine for the Convolution1D layers I'm using, as I can specify data_sample='channels_first', but somehow this won't work for Maxpooling1D, which doesn't have this option as it seems.

The model I want to build is structured as follows:

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

With window_length = 5000 I get the following summary after all three layers are added:

_________________________________________________________________
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

Now, I wonder if this is correct, as I would expect the third dimension (i.e. the number of neurons in a feature map) and not the second (i.e. the number of filters) to be reduced by the pooling layer? As I see it, MaxPooling1D does not recognize the channels_first ordering and while the Keras documentation says there exists a keyword data_format for MaxPooling2D, there's no such keyword for MaxPooling1D.

I tested the whole setup with a channels_last data format, and it worked as I expected. But since the conversion from channels_first to channels_last takes quite some time for me, I'd really rather have this work with channels_first. And I have the feeling that I'm simply missing something.

If you need any more information, let me know.

like image 779
Gretel_f Avatar asked Aug 22 '18 07:08

Gretel_f


1 Answers

Update: as mentioned by @HSK in the comments, the data_format argument is now supported in MaxPooling layers as a result of this PR.


Well, one alternative is to use the Permute layer (and remove the channels_first for the second conv layer):

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()

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
_________________________________________________________________
like image 69
today Avatar answered Oct 05 '22 12:10

today