def hinton(W, bg='grey', facecolors=('w', 'k')):
"""Draw a hinton diagram of the matrix W on the current pylab axis
Hinton diagrams are a way of visualizing numerical values in a matrix/vector,
popular in the neural networks and machine learning literature. The area
occupied by a square is proportional to a value's magnitude, and the colour
indicates its sign (positive/negative).
Example usage:
R = np.random.normal(0, 1, (2,1000))
h, ex, ey = np.histogram2d(R[0], R[1], bins=15)
hh = h - h.T
hinton.hinton(hh)
"""
M, N = W.shape
square_x = np.array([-.5, .5, .5, -.5])
square_y = np.array([-.5, -.5, .5, .5])
ioff = False
if plt.isinteractive():
plt.ioff()
ioff = True
plt.fill([-.5, N - .5, N - .5, - .5], [-.5, -.5, M - .5, M - .5], bg)
Wmax = np.abs(W).max()
for m, Wrow in enumerate(W):
for n, w in enumerate(Wrow):
c = plt.signbit(w) and facecolors[1] or facecolors[0]
plt.fill(square_x * w / Wmax + n, square_y * w / Wmax + m, c, edgecolor=c)
plt.ylim(-0.5, M - 0.5)
plt.xlim(-0.5, M - 0.5)
if ioff is True:
plt.ion()
plt.draw_if_interactive()
评论列表
文章目录