神經(jīng)網(wǎng)絡(luò)的學(xué)習(xí)
- 學(xué)習(xí):從訓(xùn)練數(shù)據(jù)中自動(dòng)獲取最優(yōu)權(quán)重參數(shù)的過(guò)程
- 指標(biāo):損失函數(shù)
- 目的:以損失函數(shù)為基準(zhǔn)伦忠,找到能使損失函數(shù)的值達(dá)到最小的權(quán)重參數(shù)
- 機(jī)器學(xué)習(xí)的方案
- 從圖像中提取特征量(可以從輸入數(shù)據(jù)中準(zhǔn)確提取本質(zhì)數(shù)據(jù)的轉(zhuǎn)換器)
- 用機(jī)器學(xué)習(xí)技術(shù)學(xué)習(xí)特征量的模式
- CV領(lǐng)域常用的特征量包括SIFT东涡,SURF和HOG
- 深度學(xué)習(xí)有時(shí)也成為端到端機(jī)器學(xué)習(xí)(end-to-end machine learning),從原始數(shù)據(jù)中獲得目標(biāo)結(jié)果
- 評(píng)價(jià)模型
- 泛化能力:處理未被觀察過(guò)的數(shù)據(jù)的能力萍肆。獲得泛化能力是幾期學(xué)習(xí)的最終目標(biāo)
損失函數(shù)
- 在神經(jīng)網(wǎng)絡(luò)的學(xué)習(xí)中射亏,用損失函數(shù)作為線索尋找最優(yōu)權(quán)重參數(shù)
- 損失函數(shù)是表示神經(jīng)網(wǎng)絡(luò)性能的“惡劣程度”的指標(biāo)琢唾,即當(dāng)前的神經(jīng)網(wǎng)絡(luò)對(duì)監(jiān)督數(shù)據(jù)在多大程度上不擬合寡壮,在多大程度上不一致生闲。
- one-hot表示:將正確解標(biāo)簽表示為1,其他標(biāo)簽表示為0的表示方法
均方誤差
表示神經(jīng)網(wǎng)絡(luò)的輸出,
表示監(jiān)督數(shù)據(jù),
表示數(shù)據(jù)的維數(shù)
import numpy as np
#均方誤差的實(shí)現(xiàn)
def mean_squared_error(y,t):
return 0.5*np.sum((y-t)**2)
t = [0,0,1,0,0,0,0,0,0,0]
y = [0.1,0.05,0.6,0.0,0.05,0.1,0.0,0.1,0.0,0.0]
mean_squared_error(np.array(y),np.array(t))
0.09750000000000003
交叉熵誤差
-
是神經(jīng)網(wǎng)絡(luò)的輸出,
是正確解標(biāo)簽(用one-hot表示).
- 該式只計(jì)算對(duì)應(yīng)正確解標(biāo)簽的輸出的自然對(duì)數(shù)
- 如果正確解標(biāo)簽對(duì)應(yīng)的輸出較小,則函數(shù)值較大
#交叉熵誤差實(shí)現(xiàn)
#y:1*n,t:1*n
def cross_entropy_error(y,t):
delta = 1e-7
return -np.sum(t*np.log(y+delta))
- 在計(jì)算np.log是加上了一個(gè)微小值delta,當(dāng)出現(xiàn)np.log(0),會(huì)變?yōu)樨?fù)無(wú)限大的-inf.
mini-batch學(xué)習(xí)
- 對(duì)于所有訓(xùn)練數(shù)據(jù)求交叉熵誤差
- 假設(shè)訓(xùn)練數(shù)據(jù)有N個(gè),
表示第
個(gè)數(shù)據(jù)的第
個(gè)元素的值.
-
是對(duì)和進(jìn)行正規(guī)化(normalization),獲得和訓(xùn)練數(shù)據(jù)的數(shù)量無(wú)關(guān)的統(tǒng)一指標(biāo)
- 從全部數(shù)據(jù)中選擇一部分?jǐn)?shù)據(jù),作為全部數(shù)據(jù)的"近似"(稱(chēng)為mini-batch,小批量),然后對(duì)每個(gè)mini-batch進(jìn)行學(xué)習(xí)
#mini-batch版交叉熵誤差的實(shí)現(xiàn)
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
為何要設(shè)定損失函數(shù)
- 用識(shí)別精度作為指標(biāo),微調(diào)參數(shù)引起的識(shí)別精度的變化是離散的.
- 用損失函數(shù)作為指標(biāo),微調(diào)參數(shù)引起的函數(shù)值變化是連續(xù)的
數(shù)值微分
導(dǎo)數(shù)
偏導(dǎo)數(shù)
梯度
()由全部變量的偏導(dǎo)數(shù)匯總而成的向量稱(chēng)為梯度
#梯度計(jì)算(可以計(jì)算多維)
def numerical_gradient(f, x):
h = 1e-4 # 0.0001
grad = np.zeros_like(x)
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
idx = it.multi_index
tmp_val = x[idx]
x[idx] = float(tmp_val) + h
fxh1 = f(x) # f(x+h)
x[idx] = tmp_val - h
fxh2 = f(x) # f(x-h)
grad[idx] = (fxh1 - fxh2) / (2*h)
x[idx] = tmp_val # 還原值
it.iternext()
return grad
梯度法
稱(chēng)為學(xué)習(xí)率(learning rate),決定在一次學(xué)習(xí)中,應(yīng)該學(xué)習(xí)多少,以及在多大程度上更新參數(shù).
#梯度下降法的實(shí)現(xiàn)
def gradient_descent(f,init_x,lr=0.01,step_num=100):
x = init_x
for i in range(step_num):
grad = numerical_gradient(f,x)
x -= lr*grad
return x
- 參數(shù)f是要進(jìn)行最優(yōu)化的函數(shù),init_x是初始值,lr是學(xué)習(xí)率learning rate,step_num是梯度法的重復(fù)次數(shù).
- 超參數(shù):需要人工設(shè)定,嘗試多個(gè)值以便可以使學(xué)習(xí)順利進(jìn)行的設(shè)定
神經(jīng)網(wǎng)絡(luò)的梯度
損失函數(shù)關(guān)于權(quán)重參數(shù)的梯度
from sourcecode.common.functions import softmax,cross_entropy_error
from sourcecode.common.gradient import numerical_gradient
class simpleNet:
def __init__(self):
self.W = np.random.randn(2,3)#用高斯分布進(jìn)行初始化
def predict(self,x):
return np.dot(x,self.W)
def loss(self,x,t):
z =self.predict(x)
y = softmax(z)
loss = cross_entropy_error(y,t)
return loss
net=simpleNet()
print(net.W)
x = np.array([0.6,0.9])
p = net.predict(x)
print(p)
print(np.argmax(p))
t = np.array([0,0,1])#正確解標(biāo)簽
print(net.loss(x,t))
[[ 0.96028135 -1.10055385 -1.26426151]
[ 0.4756395 1.3477234 0.45475418]]
[ 1.00424436 0.55261875 -0.34927815]
0
1.992699002936635
#求梯度
def f(W):
return net.loss(x,t)
dW = numerical_gradient(f,net.W)
print(dW)
[[ 0.31663563 0.20156786 -0.51820349]
[ 0.47495345 0.30235179 -0.77730524]]
學(xué)習(xí)算法的實(shí)現(xiàn)
- 前提
神經(jīng)網(wǎng)絡(luò)存在合適的權(quán)重和偏置. - mini-batch
從訓(xùn)練數(shù)據(jù)中隨機(jī)選出一部分?jǐn)?shù)據(jù). - 計(jì)算梯度
求出各個(gè)權(quán)重參數(shù)的梯度 - 更新參數(shù)
將權(quán)重參數(shù)沿梯度方向進(jìn)行微小更新 - 重復(fù)2.3.4
- 隨機(jī)梯度下降法(stochastic gradient descent,SGD)
- epoch是一個(gè)單位,一個(gè)epoch表示學(xué)習(xí)中所有訓(xùn)練數(shù)據(jù)均被使用過(guò)一次時(shí)的更新次數(shù)