简体   繁体   中英

Visualising CNN filter weights as numpy array in tensorflow

I have trained a 2 layer convolution neural network layer CNN to perform classification on 64 channels EEG dataset. The model has good both training, validation and testing accuracies. Now I want to see the weights learned by model for each channel(feature), I managed to export and save the filter weights as numpy array. When I read and display my filters weights, each filter has two arrays inside, one with 10 values and another with 11 values. I have 32 filters in layer 1 and 64 in layer 2. All the filters look like this.

(array([2, 3, 1, 2, 8, 2, 2, 1, 1, 3]), array([-0.17596437, -0.14086468, -0.10576499, -0.0706653 , -0.03556561, -0.00046591, 0.03463378, 0.06973347, 0.10483316, 0.13993285, 0.17503254]))

I fail to interpret this in relation to my 64 channels as I am interested to know the weight learned by each channel.Is my weight saving method right?,If right how do I match with the 64 features?,as I have 10 values in the first array and 11 values in the second array of the same filter.

My first and second convolution layers look like this

#weight and bias the first convolution layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])

#first convolution and pooling 
h_conv1 = tf.nn.relu(conv2d(x_iput, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

#weight and bias of the second convolution layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

#second convolution and pooling 
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

After training my model, this is how I save my weights of convolution layer 1

with tf.variable_scope('conv1', reuse=True) as scope_conv:
    Conv1_weights = W_conv1.eval()

    with open("conv1.weights.npz", "wb") as outfile:
       np.save(outfile, Conv1_weights)

To save the weights I followed a question from here

I extracted the feature maps weight instead of the filters and was able to extract the weights learned by each channel. The feature maps in the first convolution layer has all the channels before pooling,so I used the feature maps of the first convolution layer. I also tried to retrain my model with the fewer channels which has the highest weight and it still got high accuracy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM