Hinton图

Hinton图对于可视化2D阵列的值(例如,权重矩阵)是有用的:正值和负值分别由白色和黑色方块表示,并且每个方块的大小表示每个值的大小。

David Warde-Farley在SciPy Cookbook上的初步想法

Hinton图示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. def hinton(matrix, max_weight=None, ax=None):
  4. """Draw Hinton diagram for visualizing a weight matrix."""
  5. ax = ax if ax is not None else plt.gca()
  6. if not max_weight:
  7. max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
  8. ax.patch.set_facecolor('gray')
  9. ax.set_aspect('equal', 'box')
  10. ax.xaxis.set_major_locator(plt.NullLocator())
  11. ax.yaxis.set_major_locator(plt.NullLocator())
  12. for (x, y), w in np.ndenumerate(matrix):
  13. color = 'white' if w > 0 else 'black'
  14. size = np.sqrt(np.abs(w) / max_weight)
  15. rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
  16. facecolor=color, edgecolor=color)
  17. ax.add_patch(rect)
  18. ax.autoscale_view()
  19. ax.invert_yaxis()
  20. if __name__ == '__main__':
  21. # Fixing random state for reproducibility
  22. np.random.seed(19680801)
  23. hinton(np.random.rand(20, 20) - 0.5)
  24. plt.show()

下载这个示例