In [1]:
import chainer
import chainer.links as L
import chainer.functions as F
class MLP(chainer.Chain):
def __init__(self, n_mid_units=100, n_out=10):
super(MLP, self).__init__()
# パラメータを持つ層の登録
with self.init_scope():
self.l1 = L.Linear(None, n_mid_units)
self.l2 = L.Linear(n_mid_units, n_mid_units)
self.l3 = L.Linear(n_mid_units, n_out)
def forward(self, x):
# データを受け取った際のforward計算を書く
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
return self.l3(h2)
In [2]:
from chainer import serializers
my_net = MLP()
serializers.load_npz('my_mnist.model', my_net)
In [3]:
from chainer.datasets import mnist
from chainer.dataset import concat_examples
from chainer import iterators
train_val, test = mnist.get_mnist(withlabel=True, ndim=1)
x, t = concat_examples(test, -1)
with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
y = my_net(x)
accuracy = F.accuracy(y, t)
print("精度:", accuracy.array, "N:", len(y))
In [33]:
import numpy as np
import matplotlib.pyplot as plt
label_pred = y.array.argmax(axis=1)
label_true = t
i_wrong = np.where(label_pred != label_true)[0] # HACK: 謎のtupleで返る為、[0]必要
In [42]:
fig = None
i_ax = 0
for i_x in i_wrong:
if fig is None or i_ax >= 100:
fig = plt.figure(figsize=(12, 12))
plt.subplots_adjust(hspace=0, wspace=0)
i_ax = 0
i_ax += 1
ax = fig.add_subplot(10, 10, i_ax)
ax.imshow(x[i_x].reshape(28, 28), cmap='gray', interpolation='none')
ax.axis('off')
plt.show()
コメント
コメントを投稿