深度學習入門(1)

1.感知機是神經網絡的起源。
2.偏置決定了神經元被激活的容易程度。具體表現我爲 y = wx + b b爲偏置項 w爲權重

3.常用激活函數及實現
1)階越函數

y = x>0?1:0

實現:

def step_function(x):
    return np.array(x > 0, dtype=np.int)

2)sigmoid函數

y =  1/(1+exp(-x))

實現:

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

3)ReLu函數

y = x>0?x:0

實現:

def relu(x):
    return np.maximum(0, x)

4.輸出層設計(softmax,)

Yk = exp(Ak)/sum(exp(Ai))

通常會大數溢出,
因此做個改動,都減去最大值,歸1化處理。
實現:

def softmax(x):
    x = x - np.max(x) 
    return np.exp(x) / np.sum(np.exp(x))

5 . 感知機與神經網絡的區別
主要是激活函數不一樣,感知機用的不連續的階越函數,而神經網絡一般用連續的RELU或者sigmoid函數。

6.批處理學習一下

7.損失函數
表示神經網絡的惡劣程度,性能有多好。常用的損失函數有:
<1>均方誤差
實現:

def mean_squared_error(y, t):
    return 0.5 * np.sum((y-t)**2)

<2>交差商誤差

def cross_entropy_error(y, t):
   delta = 1e-7
   return -np.sum(t * np.log(y + delta))

誤差的值越小,表示與真實值越相近。
8.mini-batch學習
onehot 編碼

def cross_entropy_error(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
    batch_size = y.shape[0]
    return -np.sum(t*np.log(y + 1e-7))/batch_size

包含非onehot編碼

def cross_entropy_error1(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
        
    if t.size == y.size:
        t = t.argmax(axis=1)
             
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size

損失函數:
在進行神經網絡的學習時,不能將識別精度作爲指標。因爲如果以識別精度爲指標,則參數的導數在絕大多數地方都會變爲0.

9.誤差反向傳播
激活層的實現:
1)RELU

class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0) #萃取小於等於0的數的座標
        out = x.copy()
        out[self.mask] = 0  #將小於等於0的數置0

        return out

    def backward(self, dout):
        dout[self.mask] = 0 #反向傳播小於等於0 爲0  大於零,原樣
        dx = dout

        return dx

2)Sigmoid

class Sigmoid:
    def __init__(self):
        self.out = None

    def forward(self, x):
        out = sigmoid(x)
        self.out = out
        return out

    def backward(self, dout):
        dx = dout * (1.0 - self.out) * self.out
        return dx

3)Affine層

class Affine:
    def __init__(self, W, b):
        self.W =W
        self.b = b
        
        self.x = None
        self.dW = None
        self.db = None

    def forward(self, x):
        self.x = x
        out = np.dot(self.x, self.W) + self.b

        return out

    def backward(self, dout):
        dx = np.dot(dout, self.W.T)
        self.dW = np.dot(self.x.T, dout)
        self.db = np.sum(dout, axis=0)
        
        return dx

4)softmax-with-loss

class SoftmaxWithLoss:
    def __init__(self):
        self.loss = None
        self.y = None # 
        self.t = None # 

    def forward(self, x, t):
        self.t = t
        self.y = softmax(x)
        self.loss = cross_entropy_error(self.y, self.t)
        
        return self.loss

    def backward(self, dout=1):
        batch_size = self.t.shape[0]
        if self.t.size == self.y.size: # 
            dx = (self.y - self.t) / batch_size
        else:
            dx = self.y.copy()
            dx[np.arange(batch_size), self.t] -= 1
            dx = dx / batch_size
        
        return dx

10.學習算法步驟
神經網絡存在合適的權重和偏轉,調整權重和偏轉以便擬合訓練數據的過程稱爲學習,神經網絡的學習主要分成如下4個步驟:

步驟1  mini-batch
從訓練數據中挑選一部分數據,稱爲mini-batch
步驟2  計算梯度
爲了減小mini-batch的損失函數的值,需要求出各個權重參數的梯度。
步驟3  更新參數
將權重參數沿梯度減小的方向進行微小更新。
步驟4  重複
重複步驟1,步驟2,步驟3,直至訓練完成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章