python - 将 CNN 过滤器权重可视化为 tensorflow 中的 numpy 数组

标签 python numpy tensorflow conv-neural-network

我已经训练了一个 2 层卷积神经网络层 CNN 来对 64 channel EEG 数据集进行分类。该模型具有良好的训练、验证和测试精度。现在我想查看模型为每个 channel (特征)学习的权重,我设法将过滤器权重导出并保存为 numpy 数组。 当我读取并显示我的过滤器权重时,每个过滤器内部都有两个数组,一个有 10 个值,另一个有 11 个值。我在第 1 层有 32 个过滤器,在第 2 层有 64 个过滤器。所有过滤器看起来像这样。

(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]))

我无法根据我的 64 个 channel 来解释这一点,因为我很想知道每个 channel 学到的权重。我的减肥方法是否正确?如果正确,我该如何匹配 64 个特征?同一过滤器的第一个数组中有 10 个值,第二个数组中有 11 个值。

我的第一个和第二个卷积层是这样的

#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)

在训练完我的模型后,这是我如何保存卷积层 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)

为了保存权重,我遵循了 here 中的一个问题

最佳答案

我提取了特征映射权重而不是过滤器,并且能够提取每个 channel 学习的权重。第一个卷积层的feature maps有pooling之前的所有 channel ,所以我使用了第一个卷积层的feature maps。我还尝试使用权重最高的较少 channel 重新训练我的模型,但它仍然获得了很高的准确性。

关于python - 将 CNN 过滤器权重可视化为 tensorflow 中的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47535940/

相关文章:

python - 在 Windows 上使用 Pip Python 安装 TensorFlow

python - tf.gradients 是线程安全的吗?

tensorflow - 自定义池化层 - minmax pooling - Keras - Tensorflow

python - 如何解决python 3中的 'numpy.float64'对象没有属性 'encode'

python - 镜像包含锚定数据的 CNN 训练图像有哪些缺点?

python - `numpy.random.multivariate_normal` 的向量化实现

python - 在 Visual Studio 中使用 NumPy

python - 谁能推荐一个支持 OpenType (.OTF) 的 Python PDF 生成器?

python - 完整更新 pip 包时出错

python - CV2 : numpy. core.multiarray 导入失败的问题