differences between Convolution1D/2D and MaxPooling1D/2D

4,013 views
Skip to first unread message

Kyle McDonald

unread,
Jan 30, 2016, 12:31:08 AM1/30/16
to Keras-users
hi,

i'm interested in working with 1d data in keras (waveforms and frequency spectra) similar to studies i've done with lasagne[1].

when i use the convolution and pooling layers following the example of mnist_cnn, but in 1d instead of 2d, i get unexpected layer output sizes.[2]

with both, the input shape looks correct:

2D: Initial input shape: (None, 1, 28, 28)
1D: Initial input shape: (None, 1, 28)

but after the same convolution, they look very different:

2D: Convolution2D (convolution2d) (None, 32, 28, 28)
1D: Convolution1D (convolution1d) (None, 1, 32)

while the 2D convolution has resulted in (batch_size, nb_filters, nb_conv, nb_conv), the 1D convolution results in (batch_size, ?, nb_filters).

then after a max pooling operation it gets even stranger:

2D: MaxPooling2D (maxpooling2d)   (None, 32, 14, 14)
1D: MaxPooling1D (maxpooling1d)   (None, 0, 32)

since it seems like the 2d implementation is correct, should i stick with 2d convolutions for now and just use 1 for the row size? or would this be inefficient?

thanks!

kyle

François Chollet

unread,
Jan 30, 2016, 9:26:56 PM1/30/16
to Kyle McDonald, Keras-users
Convolution1D is doing temporal convolution over the time dimension (which is dimension 1). If that's not what you need, then you should probably use Convolution2D instead. For a typical use case of Convolution1D with time series, you can check out our example "CNN LSTM" for IMDB.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/CAO9UE66ZccrU4XwqUaARTsTErwE2MiNBRTETWu9UuL0Eg_njnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Kyle McDonald

unread,
Jan 31, 2016, 3:04:17 PM1/31/16
to François Chollet, Keras-users
I see. So Convolution2D operates on (batch size, channels, rows, columns) and Covolution1D operates on (batch size, length, channels). I was expecting Convolution1D to be (batch size, channels, length) keeping consistent with Convolution2D, but I can imagine most applications of Convolution1D have a large number of "channels" (e.g., one hot token vectors) so it's more efficient to make it the final dimension.

François Chollet

unread,
Jan 31, 2016, 3:29:38 PM1/31/16
to Kyle McDonald, Keras-users
This is due to dimension ordering conventions in Keras, which are inherited from Theano. Note that you can also run Convolution2D on (samples, rows, columns, channels) if you specify dim_ordering="tf".
Reply all
Reply to author
Forward
0 new messages