R语言极限学习机算法训练 快速深度学习进行回归预测

2018-10-2310:11:13人工智能与大数据Comments3,425 views字数 3899阅读模式

深度学习在过去几年,由于卷积神经网络的特征提取能力让这个算法又火了一下,其实在很多年以前早就有所出现,但是由于深度学习的计算复杂度问题,一直没有被广泛应用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

一般的,卷积层的计算形式为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

其中、x分别表示当前卷积层中第j个特征、前一层的第i个特征;k表示当前层的第j个特征与前一层的第i个特征之间的卷积核;M表示需要卷积的前一层的特征的集合,b表示当前卷积层中第j个卷积核对应的偏置。f为激活函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

卷积层中的权值与阈值通过随机梯度下降法得到:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

式中,a为学习率。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

损失函数对卷积层参数的梯度可通过链式求导来得到,如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

式中,R语言极限学习机算法训练 快速深度学习进行回归预测表示前一层的梯度。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

卷积神经网络中的激活函数有多种形式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

式中a为固定的参数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

式中R语言极限学习机算法训练 快速深度学习进行回归预测,每个batch训练样本中的都随机采样自均值分布,在测试中取R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

从上述卷积神经网络看出,学习过程中需要进行梯度迭代,真正在实现工业检测等实际应用时时间复杂度极高,因此学术界进行了优化,优化后的一种单层神经网络极限学习机解决了此问题,在过去应用十分广泛。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

为解决上述问题,出现了极限学习机。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

用最小二乘法解决的一种特殊结果为,等价为一种矩阵求逆的形式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

R语言极限学习机算法训练 快速深度学习进行回归预测为的Moore-Penrose广义逆。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

1)由于极限学习机求取权值的时候只是计算一个广义逆,因此训练速度比基于梯度的学习算法快很多;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

2)基于梯度的学习算法存在很多问题,比如学习速率难以确定、局部网络最小化等,极限学习机有效的改善了此类问题,在分类过程中取得了更好的效果;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

3)与其他神经网络算法不同,极限学习机在训练过程中,选择激活函数过程中可以选择不可微函数。;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

4)极限学习机算法训练过程并不复杂。极限学习机只需要三步就可以完成整个的学习过程。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

以下用R代码讲解一下极限学习机文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

###训练过程如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

训练过程4步即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

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步即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

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自带的示例:通过极限学习机预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/6948.html

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://www.cainiaoxueyuan.com/ai/6948.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ai/6948.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定