AI 事始め17:CS231n:Module 1: Neural Networksの例題 おまけ メモ1
Putting it together: Minimal Neural Network Case Study¶
minimal 2D toy data example : see https://cs231n.github.io/neural-networks-case-study/¶
おまけ numpyの動作理解と式の解釈メモ¶
import numpy as np
import matplotlib.pyplot as plt
np.ones((2, 3)) # 2行 3列
np.ones((6,2)) # 6行 2列
W = np.array([[1., 2., 3.],[2., 3., 4.]]) # 2行 3列
X = np.ones((6,2)) # 6行 2列
X[0:6,1]= (1,2,3,4,5,6)
np.dot(X, W) # 順番に注意
N = num_examples つまり、N = 300 として記述する
バイアスベクトル \( \quad b = (b_1,b_2,b_3)\quad \) は省略して、カタチだけ記述してみる。
$$\quad\quad データ配列 \quad\quad\quad\quad 重み行列 \quad\quad\quad\quad\quad\quad スコア行列 \quad\quad\quad\quad 解答配列$$ $$\longrightarrow d\quad\quad\quad\quad\quad\longrightarrow j\quad\quad\quad\quad\quad\quad\quad\quad\longrightarrow j \quad\quad\quad\quad\quad$$
$$ \Bigg\downarrow i \pmatrix{ x_{11} & x_{12} \cr x_{21} & x_{22} \cr x_{31} & x_{32} \cr \vdots & \vdots \cr x_{N1} & x_{N2} \cr }\pmatrix{ W_{11} & W_{12} & W_{13} \cr W_{21} & W_{22} & W_{23} \cr }=\pmatrix{ f_{11} & f_{12} & f_{13} \cr f_{21} & f_{22} & f_{23} \cr f_{31} & f_{32} & f_{33}\cr \vdots & \vdots & \vdots \cr f_{N1} & f_{N2} & f_{N3}\cr}\quad,,,\quad\pmatrix{y_{1}\cr y_{2}\cr y_{3}\cr \vdots \cr y_{N}\cr}$$
$$L_i=-\log\frac{\mathrm{e}^{f_{y_i}}}{\sum_{j} \mathrm{e}^{f_j}}$$
\( \mathrm{e}^{f_{y_i}} \) ⇒ exp_scores = np.exp(scores)
i番目のデータに対応する解答配列yの要素を \( j=y_i\) とすると、\( \mathrm{e}^{f_{y_i}} \)はスカラー量になる。 しかし、\( \sum_{j} \mathrm{e}^{f_j} \)を計算しなければいけないので、\( f_j \) 全部を計算する。
\( \frac{1}{2}\lambda\sum_{k}\sum_{l}W_{k,l}^2 \quad\quad \) ⇒ \( \quad\quad \) reg_loss = 0.5*reg*np.sum(W*W)
\( k,l \)の説明がなく、理解が進まないが、右記のコードで計算する結果と理解した。
W = np.array([[ 1.03370118, 1.10345865, -2.13524878],
[-2.32943088, 2.71627529, -0.39072902]])
b = np.array([[-0.01569868, -0.044992 , 0.06069068]])
# plot the resulting classifier
h = 0.02 # この値を変えると、メッシュの数が変化する。
# numpy.ndarray.min(axis = None, out = None)
# (最小値を求めたい配列).min()の形で使います。
# X[:, 0]は、[(先頭から最後まで), 0]
x_min, x_max = -0.5 - 1, 0.5 + 1
y_min, y_max = -0.5 - 1, 0.5 + 1
# numpy.arange([start, ]stop, [step, ]dtype=None)
# Return evenly spaced values within a given interval.
# np.arange(x_min, x_max, h)→.....-1.5,-1.48,-1.46......1.46,1.48,1.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# Return coordinate matrices from coordinate vectors.
#
Z = np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b # xxもyyも描画面をすべて含む巨大な配列なので
# 一次元のメッシュ配列に並びかえて、
# メッシュの要素をデータとしてスコアを計算する
Z = np.argmax(Z, axis=1) #計算したスコアで最大のものを、そのメッシュのクラスにする。
Z = Z.reshape(xx.shape) # 配列を描画面に合わせて並び替える
fig = plt.figure()
sc=plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) # 等高線と塗りつぶし輪郭を 描きます。
# contour([X, Y,] Z, [levels], **kwargs)
#plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)# 散布図 (Scatter plot) を描く
plt.xlim(xx.min(), xx.max()) # グラフの横軸、縦軸を調整する。
plt.ylim(yy.min(), yy.max()) # グラフの横軸、縦軸を調整する。
plt.colorbar(sc) #カラーマップを表示してみた。
#fig.savefig('spiral_linear.png')
# plot the resulting classifier
h = 0.5 # この値を変えると、メッシュの数が変化する。
# numpy.ndarray.min(axis = None, out = None)
# (最小値を求めたい配列).min()の形で使います。
# X[:, 0]は、[(先頭から最後まで), 0]
x_min, x_max = -1.5 , 1.5
y_min, y_max = -1.5 , 1.5
xnn = (x_max - x_min) / h
ynn = (y_max - y_min) / h
pnn = int(xnn * ynn)
# numpy.arange([start, ]stop, [step, ]dtype=None)
# Return evenly spaced values within a given interval.
# np.arange(x_min, x_max, h)→.....-1.5,-1.48,-1.46......1.46,1.48,1.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# Return coordinate matrices from coordinate vectors.
#
Z = np.array(range(pnn))
Z = Z.reshape(xx.shape) # 配列を描画面に合わせて並び替える
#plt.rcParams['image.interpolation'] = 'spline16'
fig = plt.figure()
sc=plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) # 等高線と塗りつぶし輪郭を 描きます。
# contour([X, Y,] Z, [levels], **kwargs)
#plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)# 散布図 (Scatter plot) を描く
#plt.xlim(xx.min(), xx.max()) # グラフの横軸、縦軸を調整する。
#plt.ylim(yy.min(), yy.max()) # グラフの横軸、縦軸を調整する。
plt.colorbar(sc) #カラーマップを表示してみた。
#fig.savefig('spiral_linear.png')
sc2=plt.pcolor(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.colorbar(sc2)
#ある点をプロットする時は、その要素とその次の要素で囲まれた領域を塗り潰します。
#例えば、5*5の行列をプロットしたいときは、x, yは6個の要素を持っている必要があります。
# plot the resulting classifier
h = 0.5 # この値を変えると、メッシュの数が変化する。
x_min, x_max = -1.5 , 2.0
y_min, y_max = -1.5 , 2.0
xnn = (x_max - x_min) / h
ynn = (y_max - y_min) / h
pnn = int(xnn * ynn)
# numpy.arange([start, ]stop, [step, ]dtype=None)
# Return evenly spaced values within a given interval.
# np.arange(x_min, x_max, h)→.....-1.5,-1.48,-1.46......1.46,1.48,1.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# Return coordinate matrices from coordinate vectors.
#
Z = np.array(range(pnn))
Z = Z.reshape(xx.shape) # 配列を描画面に合わせて並び替える
fig = plt.figure()
sc=plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) # 等高線と塗りつぶし輪郭を 描きます。
# contour([X, Y,] Z, [levels], **kwargs)
#plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)# 散布図 (Scatter plot) を描く
#plt.xlim(xx.min(), xx.max()) # グラフの横軸、縦軸を調整する。
#plt.ylim(yy.min(), yy.max()) # グラフの横軸、縦軸を調整する。
plt.colorbar(sc) #カラーマップを表示してみた。
sc2=plt.pcolor(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.colorbar(sc2)
# https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html?highlight=interpolation
plt.rcParams['image.interpolation'] = 'spline16'
sc3=plt.pcolor(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.colorbar(sc3)