呉恩達マシン学習練習:神経ネットワーク(逆伝搬)


1 Neural Networks神経ネットワーク
1.1 Visual lizing the data可視化データ
この部分はランダムに100個のサンプルを選び,可視化した。トレーニングセットは全部で5000個のトレーニングサンプルがあります。各サンプルは20×20ピクセルのデジタルグレースケール画像です。各ピクセルは浮動小数点を表し、その位置の階調強度を表します。20×20の画素グリッドは400次元のベクトルに展開される。私たちのデータマトリックスXでは、各サンプルが一行になりました。これは私たちに5000個をくれました。×400マトリクスXは、すべての行が手書きのデジタル画像のトレーニングサンプルです。

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
import scipy.optimize as opt
from sklearn.metrics import classification_report  #         

def load_mat(path):
    '''    '''
    data = loadmat('ex4data1.mat')  # return a dict
    X = data['X']
    y = data['y'].flatten()    
    return X, y  

def plot_100_images(X):
    """   100   """
    index = np.random.choice(range(5000), 100)
    images = X[index]
    fig, ax_array = plt.subplots(10, 10, sharey=True, sharex=True, figsize=(8, 8))
    for r in range(10):
        for c in range(10):
            ax_array[r, c].matshow(images[r*10 + c].reshape(20,20), cmap='gray_r')
    plt.xticks([])
    plt.yticks([])
    plt.show()

X,y = load_mat('ex4data1.mat')
plot_100_images(X)
这里写图片描述
1.2 Model representationモデル表示
私たちのネットワークは3階建て、入力層、隠蔽層、出力層があります。私たちの入力はデジタル画像の画素値であり、各デジタルの画像サイズは20×20であるため、入力層は400個のユニットがある(ここでは常に出力にバイアスを加えるユニットを含まない)。
image.png
1.2.1 load train data set読出しデータ
まず、ラベル値(1,2,3,4,…,10)を非線形関連のベクトルに変換します。ベクトル対応位置(y[i−1])上の値は1に等しく、例えばy[0]=6はy[0]=[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]に変換します。

from sklearn.preprocessing import OneHotEncoder
def expand_y(y):
    result = []
    #  y            ,   lable           1
    for i in y:
        y_array = np.zeros(10)
        y_array[i-1] = 1
        result.append(y_array)
    '''
    #    sklearn OneHotEncoder  
    encoder =  OneHotEncoder(sparse=False)  # return a array instead of matrix
    y_onehot = encoder.fit_transform(y.reshape(-1,1))
    return y_onehot
    ''' 
    return np.array(result)
トレーニングデータセットを取得し、トレーニングセットに対応する処理を行い、我々のinput X、lables yを得る。

raw_X, raw_y = load_mat('ex4data1.mat')
X = np.insert(raw_X, 0, 1, axis=1)
y = expand_y(raw_y)
X.shape, y.shape
'''
((5000, 401), (5000, 10))
'''
.csdn.net/Cowry5/article/details/80399350
1.2.2 load weight読み出し重み
ここではトレーニング済みのパラメータを提供します。θ1,θ2,ex 4 weight.matファイルに格納します。これらのパラメータの次元は神経ネットワークの大きさによって決まり、第二層は25のユニットがあり、出力層は10のユニット(10の数字クラスに対応)がある。

def load_weight(path):
    data = loadmat(path)
    return data['Theta1'], data['Theta2'] 

t1, t2 = load_weight('ex4weights.mat')
t1.shape, t2.shape
# ((25, 401), (10, 26))
1.2.3展開パラメータ
高度な最適化法を用いて神経ネットワークを最適化するには,複数のパラメータ行列を展開する必要があり,最適化関数に入って来てから形状を回復することができる。

def serialize(a, b):
    '''    '''
    return np.r_[a.flatten(),b.flatten()]

theta = serialize(t1, t2)  #      ,25*401+10*26=10285
theta.shape  # (10285,)

def deserialize(seq):
    '''    '''
    return seq[:25*401].reshape(25, 401), seq[25*401:].reshape(10, 26)
1.3 Feedforward and cost functionフィードフォワードと代価関数1.3.1 Feedforward
各レイヤーのユニット数を確保し、出力時に1つのバイアスユニットを追加し、s(1)=400+1、s(2)=25+1、s(3)=10を追加します。
image.png

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def feed_forward(theta, X,):
    '''          '''
    t1, t2 = deserialize(theta)
    #            ,        
    a1 = X
    z2 = a1 @ t1.T
    a2 = np.insert(sigmoid(z2), 0, 1, axis=1)
    z3 = a2 @ t2.T
    a3 = sigmoid(z3)
    return a1, z2, a2, z3, a3

a1, z2, a2, z3, h = feed_forward(theta, X)
1.3.2 Cost function
神経ネットワークの対価関数を回顧する(正規化項を持たない)
image.png
出力層はサンプルに対する予測であり、5000個のデータを含み、各データは10個の要素を含むベクトルに対応しており、結果は10種類あることを示している。数式では、各要素がロゴアイテムに対応して乗算されます。
最後に訓練を提供するパラメータを使用します。θ,算出したコストは0.2887629であるべきです。

def cost(theta, X, y):
    a1, z2, a2, z3, h = feed_forward(theta, X)
    J = 0
    for i in range(len(X)):
        first = - y[i] * np.log(h[i])
        second = (1 - y[i]) * np.log(1 - h[i])
        J = J + np.sum(first - second)
    J = J / len(X)
    return J
'''
     # or just use verctorization
     J = - y * np.log(h) - (1 - y) * np.log(1 - h)
     return J.sum() / len(X)
'''

cost(theta, X, y) # 0.2876291651613189
1.4 Reglarized cost function正規化代価関数
image.png
各層のバイアス項を正規化しないことに注意してください。
最後にYou shoud see that the cost is about 0.83770

def regularized_cost(theta, X, y, l=1):
    '''            ,           '''
    t1, t2 = deserialize(theta)
    reg = np.sum(t1[:,1:] ** 2) + np.sum(t2[:,1:] ** 2)  # or use np.power(a, 2)
    return l / (2 * len(X)) * reg + cost(theta, X, y)

regularized_cost(theta, X, y, 1) # 0.38376985909092354
2 Backpropagation逆伝搬 
2.1 Sigmoid gradient S関数導関数
image.png
ここでは手動で導出できます。難しくないです。

def sigmoid_gradient(z):
    return sigmoid(z) * (1 - sigmoid(z))
2.2 Random initializationランダム初期化
神経ネットワークを訓練するとき,ランダム初期化パラメータは重要であり,データの対称性を破ることができる。有効な戦略は均一分布(−e,e)においてランダムに値を選択することであり、e=0.12の範囲の値を選択してパラメータが十分に小さいことを確保でき、トレーニングがより効率的になる。

def random_init(size):
    '''                size    '''
    return np.random.uniform(-0.12, 0.12, size)
2.3 Backpropagationの逆伝搬
image.png
ターゲット:ネットワーク全体の対価関数の勾配を取得します。最適化アルゴリズムで解くために。
この中では必ず順方向伝播と逆方向伝搬の過程を理解してこそ,ネットワークにおける各種パラメータの次元を明らかにし,メモしておくことができる。例えば手書きで毎回伝わる式。

print('a1', a1.shape,'t1', t1.shape)
print('z2', z2.shape)
print('a2', a2.shape, 't2', t2.shape)
print('z3', z3.shape)
print('a3', h.shape)
'''
a1 (5000, 401) t1 (25, 401)
z2 (5000, 25)
a2 (5000, 26) t2 (10, 26)
z3 (5000, 10)
a3 (5000, 10)
'''

def gradient(theta, X, y):
    '''
    unregularized gradient, notice no d1 since the input layer has no error 
    return     theta   ,   D(i)   theta(i) shape,  。
    '''
    t1, t2 = deserialize(theta)
    a1, z2, a2, z3, h = feed_forward(theta, X)
    d3 = h - y # (5000, 10)
    d2 = d3 @ t2[:,1:] * sigmoid_gradient(z2)  # (5000, 25)
    D2 = d3.T @ a2  # (10, 26)
    D1 = d2.T @ a1 # (25, 401)
    D = (1 / len(X)) * serialize(D1, D2)  # (10285,)
    return D
2.4 Graadient chocing勾配検出
あなたの神経ネットワークでは、対価関数を最小化します。Θ)。勾配チェックを実行するとパラメータが想像できます。Θ(1)Θ(2)長いベクトルになるθ。このようにすることで、以下の勾配を使って過程を調べることができます。
image.png

def gradient_checking(theta, X, y, e):
    def a_numeric_grad(plus, minus):
        """
             theta_i      ,     。
        """
        return (regularized_cost(plus, X, y) - regularized_cost(minus, X, y)) / (e * 2)
    numeric_grad = [] 
    for i in range(len(theta)):
        plus = theta.copy()  # deep copy otherwise you will change the raw theta
        minus = theta.copy()
        plus[i] = plus[i] + e
        minus[i] = minus[i] - e
        grad_i = a_numeric_grad(plus, minus)
        numeric_grad.append(grad_i)
    numeric_grad = np.array(numeric_grad)
    analytic_grad = regularized_gradient(theta, X, y)
    diff = np.linalg.norm(numeric_grad - analytic_grad) / np.linalg.norm(numeric_grad + analytic_grad)
    print('If your backpropagation implementation is correct,
the relative difference will be smaller than 10e-9 (assume epsilon=0.0001).
Relative Difference: {}
'.format(diff))

gradient_checking(theta, X, y, epsilon= 0.0001)#      ,    
2.5 Reglarized Neural Networks正規化神経ネットワーク
image.png

def regularized_gradient(theta, X, y, l=1):
    """          """
    a1, z2, a2, z3, h = feed_forward(theta, X)
    D1, D2 = deserialize(gradient(theta, X, y))
    t1[:,0] = 0
    t2[:,0] = 0
    reg_D1 = D1 + (l / len(X)) * t1
    reg_D2 = D2 + (l / len(X)) * t2
    return serialize(reg_D1, reg_D2)
2.6 Learning parameters using fmincg最適化パラメータ

def nn_training(X, y):
    init_theta = random_init(10285)  # 25*401 + 10*26
    res = opt.minimize(fun=regularized_cost,
                       x0=init_theta,
                       args=(X, y, 1),
                       method='TNC',
                       jac=regularized_gradient,
                       options={'maxiter': 400})
    return res

res = nn_training(X, y)# 
res
'''
     fun: 0.5156784004838036
     jac: array([-2.51032294e-04, -2.11248326e-12,  4.38829369e-13, ...,
        9.88299811e-05, -2.59923586e-03, -8.52351187e-04])
 message: 'Converged (|f_n-f_(n-1)| ~= 0)'
    nfev: 271
     nit: 17
  status: 1
 success: True
       x: array([ 0.58440213, -0.02013683,  0.1118854 , ..., -2.8959637 ,
        1.85893941, -2.78756836])
'''

def accuracy(theta, X, y):
    _, _, _, _, h = feed_forward(res.x, X)
    y_pred = np.argmax(h, axis=1) + 1
    print(classification_report(y, y_pred))

accuracy(res.x, X, raw_y)
'''
             precision    recall  f1-score   support
          1       0.97      0.99      0.98       500
          2       0.98      0.97      0.98       500
          3       0.98      0.95      0.96       500
          4       0.98      0.97      0.97       500
          5       0.97      0.98      0.97       500
          6       0.99      0.98      0.98       500
          7       0.99      0.97      0.98       500
          8       0.96      0.98      0.97       500
          9       0.97      0.98      0.97       500
         10       0.99      0.99      0.99       500
avg / total       0.98      0.98      0.98      5000
'''
3 Visual lizing the hidden layer可視化隠蔽層
神経ネットワークがどのように学習されているかを理解する良い方法として、隠し層ユニットによって捕捉されたコンテンツを可視化することが挙げられる。一般的には、所与の隠蔽層ユニットが、計算された内容を可視化する方法は入力xを見つけることであり、xは、このユニットを活性化することができる(すなわち、活性化値は1に近い)。私たちが訓練しているネットについて、気づきました。θ1の各行は401次元のベクトルであり、各隠蔽層ユニットのパラメータを表しています。バイアス項目を無視すると、400次元のベクトルが得られます。このベクトルは、各サンプルが各隠蔽層ユニットに入力されるピクセルの重みを表します。従って可視化の一つの方法は、この400次元のベクトルが(20,20)の画像を次に出力することである。
注:
It turns out that is equivalent to finding the input that gives the highest activation for the hidden unit,given a norm constrant on the input.
これは、入力の標準的な制限を与えられた陰層ユニットに最も高い活性化値を与えた入力に相当する。例えば(124 x 124 2≦1)
(この部分はまだよく分かりません。)

def plot_hidden(theta):
    t1, _ = deserialize(theta)
    t1 = t1[:, 1:]
    fig,ax_array = plt.subplots(5, 5, sharex=True, sharey=True, figsize=(6,6))
    for r in range(5):
        for c in range(5):
            ax_array[r, c].matshow(t1[r * 5 + c].reshape(20, 20), cmap='gray_r')
            plt.xticks([])
            plt.yticks([])
    plt.show()

plot_hidden(res.x)
这里写图片描述
この練習では、どのように逆伝搬アルゴリズムを使って神経ネットワークのパラメータを学習しますか?より多くの関連機器を学習します。神経ネットの内容は以前の文章を検索してください。または次の関連記事を引き続き閲覧してください。これからもよろしくお願いします。