【问题标题】:Neural network receptive field visualization in pythonpython中的神经网络感受野可视化
【发布时间】:2015-05-17 18:25:28
【问题描述】:

我有一个包含 300 个隐藏层的神经网络,我想将其可视化(全部一起)。

在python中最好的方法是什么?

我已经尝试过使用subplot,但是感受野之间的距离太远,我几乎看不到它们。

编辑:

所以在输出上我只有 28*28 我想要可视化的权重(图像)。

这是我当前的代码:

# Plot receptive fields
f, axarr = pyplot.subplots(30, 10)


for weight_numb in xrange(300):
    currnt_sub_handler = axarr[weight_numb / 10, weight_numb % 10]
    weight = main.model_params[:, weight_numb].reshape(28, 28)
    currnt_sub_handler.axis('off')
    currnt_sub_handler.imshow(weight)

pyplot.show()

所以,换个说法:

  1. 如何使图像彼此尽可能接近?
  2. 我必须使用什么颜色图?

【问题讨论】:

  • 你能详细说明你想要什么样的可视化吗?通常最好的方法是在你用来进行训练/特征提取的任何框架内工作。
  • @mprat 我有 28*28 的图像,数量为 300。我只是想要一种将它们放在一个图像中的好方法,以便人们可以清楚地看到它们。
  • 那么你的问题真的是如何将 300 张图像彼此相邻绘制?
  • @mprat,基本上是的。我想让它们尽可能靠近。但我也猜想有一些颜色图非常适合这项任务。

标签: python matplotlib neural-network visualization data-visualization


【解决方案1】:

这是我想出的解决方案。感谢@mprat 的帮助。

我发现spectral colormap 最适合此类任务,并且 我还添加了您可以指定的边框。

from matplotlib import pyplot
import numpy as np

border = 2
images_amount = 300
row_amount = 10
col_amount = 30
image_height = 28
image_width = 28


all_filter_image = np.zeros((row_amount*image_height + border*row_amount,
                             col_amount*image_width + border*col_amount))


for filter_num in range(images_amount):
    start_row = image_height*(filter_num / col_amount) +\
                (filter_num / col_amount + 1)*border

    end_row = start_row + image_height

    start_col = image_width*(filter_num % col_amount) +\
                (filter_num % col_amount + 1)*border

    end_col = start_col + image_width

    all_filter_image[start_row:end_row, start_col:end_col] = \
        all_filters[filter_num]

    print start_row, end_row, start_col, end_col


pyplot.imshow(all_filter_image)
pyplot.axis('off')
pyplot.set_cmap('spectral')
pyplot.colorbar()
pyplot.savefig('repflds1.png')

以下是一些用法示例:

训练得不太好的网络:

训练有素的网络:

如您所见,边框使区分一个过滤器(权重)与另一个过滤器(重量)变得非常容易。

【讨论】:

    【解决方案2】:

    为什么不制作一个大图像(矩阵),例如 (10x28)x(30x28),然后将每个 28x28 过滤器放入该矩阵的一部分,然后一次绘制整个图像。有点像这样:

    # assuming your filters are stored in a list called all_filters
    all_filter_image = zeros(10*28, 30*28)
    for filter_num in range(300):
        # calculate start_x and start_y based on the size of your "large filter" 
        # and the filter index
        all_filter_image[start_x:start_x + 28, start_y: start_y + 28] = all_filters[filter_num]
    

    这样你就不必处理子图了。

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 2020-03-27
      • 2015-07-05
      • 2019-01-13
      • 2020-03-19
      • 1970-01-01
      • 2017-05-20
      • 2019-01-18
      相关资源
      最近更新 更多