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.
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.
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.
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.
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.
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)
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:
(batch_size, length, input_dim)
. Use Masking
layers to disconsider the dummy values. (1, length, input_dim)
, each array having its own length. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With