R利用卷积神经网络做预测

深度学习在过去几年,由于卷积神经网络的特征提取能力让这个算法又火了一下,其实在很多年以前早就有所出现,但是由于深度学习的计算复杂度问题,一直没有被广泛应用。

一般的,卷积层的计算形式为:


其中、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)
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值