Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does 1D Convolutional layer support variable sequence lengths?

I have a series of processed audio files I am using as input into a CNN using Keras. Does the Keras 1D Convolutional layer support variable sequence lengths? The Keras documentation makes this unclear.

https://keras.io/layers/convolutional/

At the top of the documentation it mentions you can use (None, 128) for variable-length sequences of 128-dimensional vectors. Yet at the bottom it declares that the input shape must be a

3D tensor with shape: (batch_size, steps, input_dim)

Given the following example how should I input sequences of variable length into the network

Lets say I have two examples (a and b) containing X 1 dimensional vectors of length 100 that I want to feed into the 1DConv layer as input

a.shape = (100, 100)
b.shape = (200, 100)

Can I use an input shape of (2, None, 100)? Do I need to concatenate these tensors into c where

c.shape = (300, 100)

Then reshape it to be something

c_reshape.shape = (3, 100, 100)

Where 3 is the batch size, 100, is the number of steps, and the second 100 is the input size? The documentation on the input vector is not very clear.

like image 999
JME Avatar asked Nov 30 '17 15:11

JME


People also ask

What does a 1D convolutional layer do?

1D CNN can perform activity recognition task from accelerometer data, such as if the person is standing, walking, jumping etc. This data has 2 dimensions. The first dimension is time-steps and other is the values of the acceleration in 3 axes.

Can CNN be used for 1D data?

You can certainly use a CNN to classify a 1D signal. Since you are interested in sleep stage classification see this paper. Its a deep neural network called the DeepSleepNet, and uses a combination of 1D convolutional and LSTM layers to classify EEG signals into sleep stages.

Can LSTM take variable length?

An important thing to note is that the wrapper should not be applied to temporal layers, such as GRU or LSTM. This type of layer can already handle variable lengths by default.

How do you change CNN 1D to CNN 2D?

Instead of Sequential API, you can try Functional API by providing input shape to both 1D-CNN and 2D-CNN at the first layer only. Then by adding flatten before Dense layer in both 1D-CNN and 2D-CNN will solve your issue.


1 Answers

Keras supports variable lengths by using None in the respective dimension when defining the model.

Notice that often input_shape refers to the shape without the batch size.

So, the 3D tensor with shape (batch_size, steps, input_dim) suits perfectly a model with input_shape=(steps, input_dim).

All you need to make this model accept variable lengths is use None in the steps dimension:

input_shape=(None, input_dim)

Numpy limitation

Now, there is a numpy limitation about variable lengths. You cannot create a numpy array with a shape that suits variable lengths.

A few solutions are available:

  • Pad your sequences with dummy values until they all reach the same size so you can put them into a numpy array of shape (batch_size, length, input_dim). Use Masking layers to disconsider the dummy values.
  • Train with separate numpy arrays of shape (1, length, input_dim), each array having its own length.
  • Group your images by sizes into smaller arrays.

Be careful with layers that don't support variable sizes

In convolutional models using variable sizes, you can't for instance, use Flatten, the result of the flatten would have a variable size if this were possible. And the following Dense layers would not be able to have a constant number of weights. This is impossible.

So, instead of Flatten, you should start using GlobalMaxPooling1D or GlobalAveragePooling1D layers.

like image 51
Daniel Möller Avatar answered Sep 29 '22 03:09

Daniel Möller