Draw Hinton diagram for visualizing a weight matrix.
(matrix, max_weight=None, ax=None)
| 15 | |
| 16 | |
| 17 | def hinton(matrix, max_weight=None, ax=None): |
| 18 | """Draw Hinton diagram for visualizing a weight matrix.""" |
| 19 | ax = ax if ax is not None else plt.gca() |
| 20 | |
| 21 | if not max_weight: |
| 22 | max_weight = 2 ** np.ceil(np.log2(np.abs(matrix).max())) |
| 23 | |
| 24 | ax.patch.set_facecolor('gray') |
| 25 | ax.set_aspect('equal', 'box') |
| 26 | ax.xaxis.set_major_locator(plt.NullLocator()) |
| 27 | ax.yaxis.set_major_locator(plt.NullLocator()) |
| 28 | |
| 29 | for (x, y), w in np.ndenumerate(matrix): |
| 30 | color = 'white' if w > 0 else 'black' |
| 31 | size = np.sqrt(abs(w) / max_weight) |
| 32 | rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, |
| 33 | facecolor=color, edgecolor=color) |
| 34 | ax.add_patch(rect) |
| 35 | |
| 36 | ax.autoscale_view() |
| 37 | ax.invert_yaxis() |
| 38 | |
| 39 | |
| 40 | if __name__ == '__main__': |
no test coverage detected
searching dependent graphs…