import os

import chainer
import chainer.links as L
from net import SiameseNetwork

import numpy as np
import matplotlib.pyplot as plt

# 訓練済みモデルをロード
model = SiameseNetwork()
chainer.serializers.load_npz(os.path.join('result', 'model.npz'), model)

# テストデータをロード
_, test = chainer.datasets.get_mnist(ndim=3)
test_data, test_label = test._datasets

# テストデータを学習した低次元空間（2次元）に写像
y = model.forward_once(test_data)
feat = y.data

# ラベルごとに描画
c = ['#ff0000', '#ffff00', '#00ff00', '#00ffff', '#0000ff',
     '#ff00ff', '#990000', '#999900', '#009900', '#009999']

# 各ラベルごとに異なる色でプロット
# 同じクラス内のインスタンスが近くに集まり、
# 異なるクラスのインスタンスが離れていれば成功
for i in range(10):
    f = feat[np.where(test_label == i)]
    plt.plot(f[:, 0], f[:, 1], '.', c=c[i])
plt.legend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
plt.savefig(os.path.join('result', 'result.png'))

[((11, 8, 11, 24), 'net.SiameseNetwork', 'SiameseNetwork', ({}, {}), '()', False, 'from net import SiameseNetwork\n'), ((15, 10, 15, 44), 'chainer.datasets.get_mnist', 'chainer.datasets.get_mnist', (), '', False, 'import chainer\n'), ((32, 0, 32, 62), 'matplotlib.pyplot.legend', 'plt.legend', ({(32, 11, 32, 61): "['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"}, {}), "(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])", True, 'import matplotlib.pyplot as plt\n'), ((12, 29, 12, 64), 'os.path.join', 'os.path.join', ({(12, 42, 12, 50): '"""result"""', (12, 52, 12, 63): '"""model.npz"""'}, {}), "('result', 'model.npz')", False, 'import os\n'), ((31, 4, 31, 43), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n'), ((33, 12, 33, 48), 'os.path.join', 'os.path.join', ({(33, 25, 33, 33): '"""result"""', (33, 35, 33, 47): '"""result.png"""'}, {}), "('result', 'result.png')", False, 'import os\n'), ((30, 13, 30, 38), 'numpy.where', 'np.where', ({(30, 22, 30, 37): '(test_label == i)'}, {}), '(test_label == i)', True, 'import numpy as np\n')]