Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to most effectively visualize 1D convolutions?

Tags:

python

keras

I am currently using a 1D convolutional neural network to classify multivariate time series in Keras. In particular, each instance is represented by 9, equal-length time series (300 points each).

As I have read in the literature, when using 2D convolutions over images, it is possible to get a hint on where the network is looking in order to get to a classification: you may use, for example, the so called Class Activation Map, for example:

https://rajpurkar.github.io/mlx/visualizing-cnns/class_activation_maps.png

Is there anything similar that I could use to visualize the most "meaningful" slices in a given multivariate time series?

This is my current network architecture:

Input shape: 300 9
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_1 (Conv1D)            (None, 292, 128)          10496     
_________________________________________________________________
batch_normalization_1 (Batch (None, 292, 128)          512       
_________________________________________________________________
activation_1 (Activation)    (None, 292, 128)          0         
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 58, 128)           0         
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 50, 128)           147584    
_________________________________________________________________
batch_normalization_2 (Batch (None, 50, 128)           512       
_________________________________________________________________
activation_2 (Activation)    (None, 50, 128)           0         
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 10, 128)           0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 1280)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 300)               384300    
=================================================================
Total params: 543,404
Trainable params: 542,892
Non-trainable params: 512
_________________________________________________________________

As for now, I have succeeded just in visualizing the activation functions in the network. For instance, the following code snippet prints the result of the first activation function (1st over 128) in the first activation layer, given an input instance:

from keras import models

layer_outputs = [layer.output for layer in model.layers[:2]]
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(X_train_windows[0:1])
first_layer_activation = activations[0]
print(first_layer_activation.shape)

plt.plot(first_layer_activation[0, :, 0])

The result is the following time series, of length 292:

https://i.ibb.co/TqK6g9D/Schermata-2019-01-15-alle-10-24-39-2.png

However, I find it very difficult to intuitively interpret the graph.

How can I give a meaning to such a time series? Is there a way instead to highlight the input as it is done in CAMs?

Thank you!

like image 403
Andrea Avatar asked Oct 17 '22 08:10

Andrea


1 Answers

You could use something like the Keras-Vis Library

Basically you try to find the input that maximises a certain class, this results in some 1D sequence (in your case a sequence of 300 vectors of 9 elements).

You can then plot each of the 9 channels separately or on top of each other to get a sense of how this thing looks. You then need to have domain knowledge to try and understand what this means (if it means anything). This is useful if you want to understand how inputs from a certain class look like.

You could do the same with Activation Maps (Saliency Maps) (that you can compute using the same library linked). This is useful if you want to understand where (in space) the information resides.

Now without knowing the nature, domain or context of your data, it is hard to say anything more...

Edit:

Ok I now understand your problem. It might also be worth trying to treat this problem as a Multiple Instance Learning

You could also try and use an LSTM with an Attention Mechanism.

like image 193
Luca Angioloni Avatar answered Oct 21 '22 09:10

Luca Angioloni