2

我有一個有300個隱藏層的神經網絡,我想要將它們(可以一起)可視化。python中的神經網絡感受野可視化

什麼是在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. 我必須使用什麼顏色表?
+0

你能詳細說明你想要什麼樣的可視化?通常最好的方法是在你用來進行訓練/特徵提取的任何框架內工作。 – mprat

+0

@mprat我有28 * 28的圖像數量爲300.我只是想要一個很好的方式將它們放在一個圖像中,以便人們可以清楚地看到它們中的每一個。 – warmspringwinds

+0

所以你的問題真的是如何畫300張圖像相鄰? – mprat

回答

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] 

這樣你就不必處理子圖了。

3

這是我想出的解決方案。感謝@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') 

這些都是一些用法示例:

並非如此訓練有素的網絡: enter image description here

真的很好訓練的網絡: enter image description here

正如你可以看到邊框使它真正容易區分一個過濾器(重量)與另一個。