LoginSignup
0
1

More than 5 years have passed since last update.

Hinton Diagram

Last updated at Posted at 2019-03-04

Hinton Diagram

結合行列の視覚化を行います。ここでは、neuronの結合を想定しています。シミュレーションでは、1~Mがexcitatory neuronで、M~Nがinhibitory neuronとすると、左のほうにexcitatoryの結合が来て、右のほうにinhibitoryの結合が来るので見やすいです。
横軸がpreで縦軸がpostです。

メイン

以下の関数ではexcitatoryの結合を赤で、inhibitoryの結合を青で書いています。

python
def hinton(matrix, max_weight=None, ax=None):
    """結合行列を描画"""
    ax = ax if ax is not None else plt.gca()

    if not max_weight:
        max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
    N = len(matrix)
    ax.patch.set_facecolor('gray')
    ax.set_aspect('equal', 'box')
    ax.set_xticks(np.arange(1, N+1))
    ax.set_yticks(np.arange(1, N+1))
    ax.set_xlim([0.5, N+0.5])
    ax.set_ylim([0.5, N+0.5])

    for (x, y), w in np.ndenumerate(matrix):
        color = 'red' if w > 0 else 'blue'
        size = np.sqrt(np.abs(w) / max_weight)
        rect = plt.Rectangle([x+1 - size / 2, y+1 - size / 2], size, size,
                             facecolor=color, edgecolor=color)
        ax.add_patch(rect)

    ax.autoscale_view()
    ax.invert_yaxis()

実際に描画するときはplt.style.use("ggplot")でいい感じになるかと思います。
以下のpspはnd.arrayでpsp[i][j]j -> iの結合に対応します。

plot
plt.style.use("ggplot")
plt.figure(figsize=(6,6))
hinton(psp.T)
plt.xlabel("pre")
plt.ylabel("post")
plt.savefig("hinton_diagram.pdf", bbox_inches="tight", pad_inches=0)

適当に乱数で結合行列を生成してhinton diagramで描画した結果です。結合の強さが四角の大きさを見るだけでわかります。
hinton_diagram.png

参考

以下のコードを元に作成しました。
https://matplotlib.org/gallery/specialty_plots/hinton_demo.html

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1