Note
Go to the endto download the full example code.
Hinton diagrams#
Hinton diagrams are useful for visualizing the values of a 2D array (e.g.a weight matrix): Positive and negative values are represented by white andblack squares, respectively, and the size of each square represents themagnitude of each value.
Initial idea from David Warde-Farley on the SciPy Cookbook

importmatplotlib.pyplotaspltimportnumpyasnpdefhinton(matrix,max_weight=None,ax=None):"""Draw Hinton diagram for visualizing a weight matrix."""ax=axifaxisnotNoneelseplt.gca()ifnotmax_weight:max_weight=2**np.ceil(np.log2(np.abs(matrix).max()))ax.patch.set_facecolor('gray')ax.set_aspect('equal','box')ax.xaxis.set_major_locator(plt.NullLocator())ax.yaxis.set_major_locator(plt.NullLocator())for(x,y),winnp.ndenumerate(matrix):color='white'ifw>0else'black'size=np.sqrt(abs(w)/max_weight)rect=plt.Rectangle([x-size/2,y-size/2],size,size,facecolor=color,edgecolor=color)ax.add_patch(rect)ax.autoscale_view()ax.invert_yaxis()if__name__=='__main__':# Fixing random state for reproducibilitynp.random.seed(19680801)hinton(np.random.rand(20,20)-0.5)plt.show()