Python – Use LSTM for multi-label classification of variable-length signals using Keras

Use LSTM for multi-label classification of variable-length signals using Keras… here is a solution to the problem.

Use LSTM for multi-label classification of variable-length signals using Keras

I recently started looking at various classes of ECG signal classification. It is basically a multi-label classification task (4 classes in total). I’m new to deep learning, LSTM, and Keras, so I’m confused about a few things.

  1. I’m thinking about normalizing the raw signal as input to the network, is this a good approach?

  2. I also need to understand the training input shape of the LSTM, because the length of the ECG signal is variable (9000 to 18000 samples), and usually the classifier requires fixed variable inputs. How do I handle this type of input in the case of LSTM.

  3. Finally, for such a long input, what should be the structure of a deep LSTM network and how many layers should I use.

Thank you for your time.
Greetings

Solution

I am thinking about giving normalized original signal as input to the network, is this a good approach?

Yes, this is a good way to do it. Normalizing or rescaling your input is actually pretty standard for deep learning algorithms.

This usually helps your model converge faster because now you are in a smaller range (i.e., [-1, 1]) than the larger, non-normalized range of the original input (e.g. [0, 1000]). It can also help you achieve better and more precise results because it helps solve problems such as vanishing gradients and better accommodate modern activation and optimizer functions.


I also need to understand training input shape for LSTM as ECG signals are of variable length (9000 to 18000 samples) and usually classifier need fixed variable input. How can i handle such type of input in case of LSTM.

This part is very important. You’re right, LSTM expects to receive input with a fixed shape, which you know beforehand (in fact, any deep learning layer expects input with a fixed shape). This is also explained in keras docs on the loop layer, they say:

Input shape

3D tensor with shape (batch_size, timesteps, input_dim).

As we can see, it expects your data to have multiple timesteps and dimensions for each time step (the batch size is typically 1). For example, suppose your input data contains the following elements: [[1,4],[2,3],[3,2],[4,1]]. Then, using a batch_size of 1, the shape of the data will be (1,4,2). Since you have 4 time steps, each time step has 2 characteristics.

So most importantly, you have to make sure that the data is preprocessed so that it has a fixed shape, and then you can pass it to the LSTM layer. You have to go through yourself because you know your data and problems better than we do.

Maybe you can fix the samples taken from the signal, discard

some and keep the others so that each signal is the same length (if you say that the signal is between 9k and 18k, it might be logical to choose 9000, discard from everyone else you get). You can even do some additional transformations on the data by mapping inputs of 9000-18000 to a fixed size.


Finally what should be structure of deep LSTM network for such lengthy input and how many layers should i use.

This question is really broad and there is no single answer. This will depend on the nature of your problem, it is not so simple to determine these parameters a priori.

What I recommend doing is start with a simple model and gradually add layers and blocks until you are happy with the result.

Start by trying just one hidden layer, train and test your model and check your performance. You can then add more blocks and see if your performance improves. You can also add more layers and check if they are the same until you are satisfied.

This is a great way to create deep learning models because you can get the results you want while keeping your network as lean as possible, which in turn helps reduce execution time and complexity. Good luck coding, I hope you find this useful.

Related Problems and Solutions