python中的神经网络感知场可视化

时间:2015-05-17 18:25:29

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

我有一个神经网络,有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. 我必须使用什么色彩图?

2 个答案:

答案 0 :(得分:3)

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

我发现spectral色彩图是最适合这种任务的 我还添加了您可以指定的边框。

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

以下是一些使用示例:

训练不佳的网络: enter image description here

训练有素的网络: enter image description here

正如您所看到的那样,边框可以很容易地将一个过滤器(重量)与另一个过滤器(重量)区分开来。

答案 1 :(得分: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]

这样你就不必处理子图。