• Introduction to 1D Convolutional Neural Networks in Keras for Time Sequences
  • By Nils Ackermann
  • Translation from: The Gold Project
  • This article is permalink: github.com/xitu/gold-m…
  • Translator: haiyang – tju
  • Proofreader: Lsvih Leviding

An overview of the

Many technical articles have focused on the use of 2D convolutional neural networks (2D CNN), especially in image recognition. One-dimensional convolutional neural networks (1D CNNs) are only involved to a certain extent, such as in natural language processing (NLP) applications. There are very few articles on how to construct one-dimensional convolutional neural networks to solve some of the machine learning problems you might be facing. This article attempts to fill in such a short board.

When to use 1D CNN?

CNN is good at identifying simple patterns in the data and then using those simple patterns to generate more complex patterns at higher levels. 1D CNN is very effective when you want to get a feature of interest from a shorter (fixed-length) segment of the overall data set, and the location of the feature in that segment is not highly correlated.

1D CNN can be well applied to time series analysis of sensor data (such as gyroscope or accelerometer data); It is also useful for analyzing signal data with fixed periods (such as audio signals). In addition, it can be applied to tasks in natural language processing (since word proximity may not always be a good indicator of trainable patterns, LSTM networks are more promising for NLP applications).

What’s the difference between 1D CNN and 2D CNN?

No matter one-dimensional, two-dimensional or three-dimensional, convolutional neural networks (CNNs) have the same characteristics and the same processing methods. The key difference is the dimension of the input data and how the feature detector (or filter) slides across the data:

“One and two dimensional convolutional Neural Networks” is licensed BY Nils Ackermann under the Creative Commons license CC BY-ND 4.0.

Problem description

In this article, we will focus on the processing of time-slice based acceleration sensor data from the user’s belt-like smartphone device. Based on accelerometer data on the X, Y, and Z axes, 1D CNN is used to predict the type of activity the user is doing (such as “walking,” “jogging,” or “standing”). You can find more information in my other two articles here and here. For various activities, the data looks similar at every time interval.

Sample time series from accelerometer data

How to construct a 1D CNN in Python?

There are already many standard CNN models available. I chose a model described on the Keras website and fine-tuned it to accommodate the issues described earlier. The following images provide a high-level overview of the built model. Each of these layers will be further explained.

“One-dimensional Convolutional Neural Networks Example” is licensed BY Nils Ackermann under the Creative Commons license CC BY-ND 4.0.

Let’s first look at the corresponding Python code to build this model:

model_m = Sequential()
model_m.add(Reshape((TIME_PERIODS, num_sensors), input_shape=(input_shape,)))
model_m.add(Conv1D(100, 10, activation='relu', input_shape=(TIME_PERIODS, num_sensors)))
model_m.add(Conv1D(100, 10, activation='relu'))
model_m.add(MaxPooling1D(3))
model_m.add(Conv1D(160, 10, activation='relu'))
model_m.add(Conv1D(160, 10, activation='relu') Model_M. ADD (GlobalAveragePooling1D()) Model_M. ADD (Dense(NUM_classes, activation='softmax'))
print(model_m.summary())
Copy the code

Running this code yields the following deep neural network:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   ================================================================= reshape_45 (Reshape) (None, 80, 3) 0 _________________________________________________________________ conv1d_145 (Conv1D) (None, 71, 100) 3100 _________________________________________________________________ conv1d_146 (Conv1D) (None, 62, 100) 100100 _________________________________________________________________ max_pooling1d_39 (MaxPooling (None, 20, 100) 0 _________________________________________________________________ conv1d_147 (Conv1D) (None, 11, 160) 160160 _________________________________________________________________ conv1d_148 (Conv1D) (None, 2, 160) 256160 _________________________________________________________________ global_average_pooling1d_29 (None, 160) 0 _________________________________________________________________ dropout_29 (Dropout) (None, 160) 0 _________________________________________________________________ dense_29 (Dense) (None, 6) 966 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 520486 Trainable params: 520486 Non - trainable params: 0 _________________________________________________________________ NoneCopy the code

Let’s dive into each layer and see what’s really going on:

  • Input data: After the data is preprocessed, each data record contains 80 time slices (the data is recorded at the sampling frequency of 20Hz, so each time interval contains 4 seconds of accelerometer data). At each time interval, the x, y, and Z axes of the accelerometer are stored. So you get an 80 x 3 matrix. Since I usually use neural networks in iOS, the data has to be spread out into a vector of 240 degrees into the neural network. The first layer of the network must be redeformed to its original 80 x 3 shape.
  • First 1D CNN layer: The first layer defines a filter (also called a feature detector) of height 10 (also known as the convolution kernel size). Only by defining a filter can the neural network learn a single feature in the first layer. That might not be enough, so we’re going to define 100 filters. So we have 100 different features trained in the first layer of the network. The output of the first neural network layer is a 71 x 100 matrix. Each column of the output matrix contains the weight of a filter. With the kernel size defined and the length of the input matrix taken into account, each filter will contain 71 weight values.
  • Second 1D CNN layer: The output of the first CNN will be input into the second CNN layer. We will again define 100 different filters on this network layer for training. Following the same logic as the first layer, the output matrix is of size 62 x 100.
  • Maximum pooling layer: To reduce output complexity and prevent data overfitting, pooling layer is often used after the CNN layer. In our example, we chose a pool layer of size 3. This means that the output matrix of this layer is only one third the size of the input matrix.
  • Third and fourth 1D CNN layers: Two more 1D CNN layers are used here in order to learn higher level features. The output matrix after these two levels is a 2 x 160 matrix.
  • Average pooling layer: An additional pooling layer is added to further avoid overfitting. Instead of taking the maximum, I’m taking the average of the two weights in the neural network. The output matrix has size 1 x 160. Each feature detector is left with only one weight in this layer of the neural network.
  • Dropout Layer: The Dropout layer randomly assigns zero weights to neurons in the network. Since we chose a ratio of 0.5, 50% of the neurons will be zero weighted. By doing so, the network becomes less responsive to small changes in data. Therefore, it can further improve the accuracy of invisible data processing. The output of this layer is still a 1 x 160 matrix.
  • Full connection layer activated with Softmax: The last layer will reduce vectors of length 160 to vectors of length 6 because we have 6 categories to predict (i.e. “jog”, “sit”, “walk”, “stand”, “go upstairs”, “go downstairs”). The dimensionalization here is accomplished by another matrix multiplication. Softmax is used as the activation function. It forces the sum of all six outputs of the neural network to be one. Therefore, the output value will represent the probability of occurrence of each of the six categories.

Train and test the neural network

Here is a Python code for the training model with a batch size of 400, where the split between the training set and the validation set is 80/20.

callbacks_list = [
    keras.callbacks.ModelCheckpoint(
        filepath='best_model.{epoch:02d}-{val_loss:.2f}.h5',
        monitor='val_loss', save_best_only=True),
    keras.callbacks.EarlyStopping(monitor='acc', patience=1)
]

model_m.compile(loss='categorical_crossentropy',
                optimizer='adam', metrics=['accuracy'])

BATCH_SIZE = 400
EPOCHS = 50

historyX_train, y_train, batch_size= batch_size, epochs= epochs, callbacks=callbacks_list, validation_split=0.2, verbose=1)Copy the code

The accuracy of the model can reach 97% on the training data.

. Epoch 9/50 16694/16694 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 16 s 973 us/step - loss: 0.0975 acc: 0.9683 - val_loss: 0.7468 - val_acc: 0.8031 Epoch 10/50 16694/16694 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 17 s 989 us/step - loss: 0.0917-ACC: 0.9715-val_loss: 0.7215-val_acc: 0.8064 Epoch 11/50 16694/16694 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 17 s 1 ms/step - loss: 0.0877 acc: 0.9716 - val_loss: 0.7233 - val_acc: 0.8040 Epoch 12/50 16694/16694 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 17 s 1 ms/step - loss: 0.0659 acc: 0.9802-val_loss: 0.7064 -val_acc: 0.8347 Epoch 13/50 16694/16694 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 17 s 1 ms/step - loss: 0.0626 acc: 0.9799 - val_loss: 0.7219 - val_acc: 0.8107Copy the code

Based on the test set data, the accuracy rate is 92%.

Accuracy on testData: 0.92 Loss ontestData: 0.39Copy the code

Considering that we are using the standard 1D CNN model, this is a good result. Our model also scored high on precision, recall and F1-score.

Precision Recall F1-Score support 0 0.76 0.78 0.77 650 1 0.98 0.96 0.97 1990 2 0.91 0.94 0.92 452 3 0.99 0.84 0.91 370 4 0.82 0.77 0.79 725 5 0.93 0.98 0.95 2397 AVg/total 0.92 0.92 0.92 0.92 6584Copy the code

Here’s a brief review of what these scores mean:

Prediction and Outcome Matrix is licensed BY Nils Ackermann under the Creative Commons CC BY-ND 4.0 license.

  • Accuracy: Ratio of the result of a correct prediction to the sum of all predictions. ((TP + TN)/(TP + TN + FP + FN))
  • Precision: Is the model correct when it predicts a positive sample? The positive sample of all the correct predictions divided by all the positive sample predictions. (TP/(TP + FP))
  • Recall: What percentage of all positive samples identified for the model are positive samples that are correctly predicted? The positive sample of correct predictions divided by all the positive sample predictions. TP over TP plus FN.
  • F1 value (F1-Score) : is the weighted average of accuracy and recall rate. (2 x recall x precision/(recall + precision))

The corresponding confusion matrix on the test data is shown below.

conclusion

Taking smartphone accelerometer data to predict user behavior as an example, this paper discusses how to use 1D CNN to train the network. The full Python code is available on Github.

Links and references

  • Part of the Keras documentation on one-dimensional convolutional neural networks
  • Part of Keras use case about one-dimensional convolutional neural networks
  • A good article on natural language processing using one-dimensional convolutional neural networks

disclaimer

The posts are their own and do not represent the employer’s articles, strategies or opinions.

Contact the Raven team on Telegram

If you find any errors in the translation or other areas that need improvement, you are welcome to revise and PR the translation in the Gold Translation program, and you can also get corresponding bonus points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


Diggings translation project is a community for translating quality Internet technical articles from diggings English sharing articles. The content covers the fields of Android, iOS, front end, back end, blockchain, products, design, artificial intelligence and so on. For more high-quality translations, please keep paying attention to The Translation Project, official weibo and zhihu column.