R 机器学习算法训练快速深度学习进行回归预测
深度学习 近年来,该算法由于能够区分卷积神经网络的特征而再次流行。其实它在很多年前就已经出现了,但是由于深度学习的计算复杂性,它并没有得到广泛的应用。
一般情况下,卷积层的计算形式为:
,其中x对应当前卷积层中的j特征和♶i。上一层。特征; k表示第j当前层与第i层之间的卷积核; M表示要卷积的前一层。特征集,b是当前卷积层j的卷积核对应的偏差。 f 是激活函数。
卷积层的权重和阈值通过随机梯度下降法得到:
式中,a为学习率。
损失函数对于卷积层参数的梯度可以通过链导数得到如下:
式中,表示上一层的梯度。
卷积神经网络的激活函数有多种形式:
其中a 是固定参数。
式中,每批次教学样本均值分布中随机抽取,测试中取
。
从上面的卷积神经网络可以看出,学习过程中需要梯度迭代。在实现工业表达等实际应用时,时间复杂度非常高。因此学术界对其进行了优化,对单层神经网络进行了优化。网络的极限学习机解决了这个问题,并且在过去得到了广泛的应用。
为了解决上述问题,极限学习机出现了。
通过最小二乘法求解的特定结果对应于矩阵求逆形式
,即Mooren-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)
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。