python 中的神经网络感受野可视化
Neural network receptive field visualization in python
我有一个包含 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()
因此,重新表述问题:
- 如何使图像彼此尽可能接近?
- 我必须使用什么颜色图?
为什么不制作一个大图像(矩阵),也就是说,(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]
这样你就不必处理子图了。
这是我想出的解决方案。感谢@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')
这些是一些用法示例:
训练有素的网络:
真正训练有素的网络:
如您所见,边框使区分一个过滤器(权重)与另一个变得非常容易。
我有一个包含 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()
因此,重新表述问题:
- 如何使图像彼此尽可能接近?
- 我必须使用什么颜色图?
为什么不制作一个大图像(矩阵),也就是说,(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]
这样你就不必处理子图了。
这是我想出的解决方案。感谢@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')
这些是一些用法示例:
训练有素的网络:
真正训练有素的网络:
如您所见,边框使区分一个过滤器(权重)与另一个变得非常容易。