深度學習在過去幾年,由於卷積神經網絡的特征提取能力讓這個算法又火了一下,其實在很多年以前早就有所出現,但是由於深度學習的計算復雜度問題,一直沒有被廣泛應用。
一般的,卷積層的計算形式為:
其中、x分別表示當前卷積層中第j個特征、前一層的第i個特征;k表示當前層的第j個特征與前一層的第i個特征之間的卷積核;M表示需要卷積的前一層的特征的集合,b表示當前卷積層中第j個卷積核對應的偏置。f為激活函數。
卷積層中的權值與閾值通過隨機梯度下降法得到:
式中,a為學習率。
損失函數對卷積層參數的梯度可通過鏈式求導來得到,如下:
式中,表示前一層的梯度。
卷積神經網絡中的激活函數有多種形式:
式中a為固定的參數。
式中,每個batch訓練樣本中的都隨機采樣自均值分布,在測試中取
。
從上述卷積神經網絡看出,學習過程中需要進行梯度迭代,真正在實現工業檢測等實際應用時時間復雜度極高,因此學術界進行了優化,優化后的一種單層神經網絡極限學習機解決了此問題,在過去應用十分廣泛。
為解決上述問題,出現了極限學習機。
用最小二乘法解決的一種特殊結果為,等價為一種矩陣求逆的形式
為的Moore-Penrose廣義逆。
1)由於極限學習機求取權值的時候只是計算一個廣義逆,因此訓練速度比基於梯度的學習算法快很多;
2)基於梯度的學習算法存在很多問題,比如學習速率難以確定、局部網絡最小化等,極限學習機有效的改善了此類問題,在分類過程中取得了更好的效果;
3)與其他神經網絡算法不同,極限學習機在訓練過程中,選擇激活函數過程中可以選擇不可微函數。;
4)極限學習機算法訓練過程並不復雜。極限學習機只需要三步就可以完成整個的學習過程。
以下用R代碼講解一下極限學習機
###訓練過程如下:
訓練過程4步即可。
elmtrain.default <-
function(x,y,nhid,actfun,...) {
require(MASS)
if(nhid < 1) stop("ERROR: number of hidden neurons must be >= 1")
########1.選擇數據,X與Y
T <- t(y)
P <- t(x)
########2.隨機產生權值,目的在於將X值進行變化
inpweight <- randomMatrix(nrow(P),nhid,-1,1)
tempH <- inpweight %*% P
biashid <- runif(nhid,min=-1,max=1)
biasMatrix <- matrix(rep(biashid, ncol(P)), nrow=nhid, ncol=ncol(P), byrow = F)
tempH = tempH + biasMatrix
########3.將變化后的X值進行高維映射,最常用是sig函數
if(actfun == "sig") H = 1 / (1 + exp(-1*tempH))
else {
if(actfun == "sin") H = sin(tempH)
else {
if(actfun == "radbas") H = exp(-1*(tempH^2))
else {
if(actfun == "hardlim") H = hardlim(tempH)
else {
if(actfun == "hardlims") H = hardlims(tempH)
else {
if(actfun == "satlins") H = satlins(tempH)
else {
if(actfun == "tansig") H = 2/(1+exp(-2*tempH))-1
else {
if(actfun == "tribas") H = tribas(tempH)
else {
if(actfun == "poslin") H = poslin(tempH)
else {
if(actfun == "purelin") H = tempH
else stop(paste("ERROR: ",actfun," is not a valid activation function.",sep=""))
}
}
}
}
}
}
}
}
}
########4.擬合出模型系數,即Y=AX中的A
outweight <- ginv(t(H), tol = sqrt(.Machine$double.eps)) %*% t(T)
Y <- t(t(H) %*% outweight)
model = list(inpweight=inpweight,biashid=biashid,outweight=outweight,actfun=actfun,nhid=nhid,predictions=t(Y))
model$fitted.values <- t(Y)
model$residuals <- y - model$fitted.values
model$call <- match.call()
class(model) <- "elmNN"
model
}
測試過程,過程4步即可。
function (object, newdata = NULL, ...)
{
if (is.null(newdata))
predictions <- fitted(object)
else {
if (!is.null(object$formula)) {
x <- model.matrix(object$formula, newdata)
}
else {
x <- newdata
}
########1.獲取訓練模型中的參數
inpweight <- object$inpweight
biashid <- object$biashid
outweight <- object$outweight
actfun <- object$actfun
nhid <- object$nhid
TV.P <- t(x)
########2.通過參數將X值進行變化
tmpHTest = inpweight %*% TV.P
biasMatrixTE <- matrix(rep(biashid, ncol(TV.P)), nrow = nhid,
ncol = ncol(TV.P), byrow = F)
tmpHTest = tmpHTest + biasMatrixTE
########3.高維度映射,通常選擇sig函數
if (actfun == "sig")
HTest = 1/(1 + exp(-1 * tmpHTest))
else {
if (actfun == "sin")
HTest = sin(tmpHTest)
else {
if (actfun == "radbas")
HTest = exp(-1 * (tmpHTest^2))
else {
if (actfun == "hardlim")
HTest = hardlim(tmpHTest)
else {
if (actfun == "hardlims")
HTest = hardlims(tmpHTest)
else {
if (actfun == "satlins")
HTest = satlins(tmpHTest)
else {
if (actfun == "tansig")
HTest = 2/(1 + exp(-2 * tmpHTest)) -
1
else {
if (actfun == "tribas")
HTest = tribas(tmpHTest)
else {
if (actfun == "poslin")
HTest = poslin(tmpHTest)
else {
if (actfun == "purelin")
HTest = tmpHTest
else stop(paste("ERROR: ", actfun,
" is not a valid activation function.",
sep = ""))
}
}
}
}
}
}
}
}
}
########4.進行預測的值計算,即Y(預測)=AX
TY = t(t(HTest) %*% outweight)
predictions <- t(TY)
}
predictions
}
通過R講述了極限學習機的內部構造,以下是R自帶的示例:通過極限學習機預測
library(elmNN)
set.seed(1234)
Var1 <- runif(50, 0, 100)
sqrt.data <- data.frame(Var1, Sqrt=sqrt(Var1))
model <- elmtrain.formula(Sqrt~Var1, data=sqrt.data, nhid=10, actfun="sig")
new <- data.frame(Sqrt=0,Var1 = runif(50,0,100))
p <- predict(model,newdata=new)
轉自:https://ask.hellobi.com/blog/Zason/4543