神经网络:即使在设定种子后,ANN结果也不可重现

时间:2016-09-26 05:30:29

标签: r

简而言之,将解释代码; 我试图通过在一天内创建24小时模型并将结果整理到数据框中进行预测。基本问题即使在设置种子后也无法重现#the输出。请任何人帮助我。一些自定义函数#和我有的对象他们没有随机化。(仅供参考)。

f <- as.formula("actual~ lag.1 + last3.avg+monsoon+mon.thurs+wdaySaturday+wdaySunday+holiday
      ")    #Defining the formula for neural network
    require(dplyr);require(neuralnet)
        set.seed(123456)
        nnet.hour=data.frame()#Initializing a dataframe
        #k=0
        #x=list()
        for(i in 1:24){#Running it for 24 hours in a day
          sub<-new.day.ahead[new.day.ahead$hour==i,]
          sub$lag.1<-lag(sub$actual,1)
          for(i in 1:nrow(sub)){
            sub$last3.avg[i]=sum(lag(sub$actual,1)[i],lag(sub$actual,2)[i],lag(sub$actual,3)[i],na.rm=TRUE)/3
          }

          ind=which(sub$mod.date==ymd(t[1]));ind#t[1] is basically a date #initialisation,getting the index
          monsoon=as.factor(sub$Monsoon.Dummy)
          wday=as.factor(sub$wday.dummy)
          holiday=as.factor(sub$holiday)
          sub=as.data.frame(cbind(sub[,c(4,16,17)],cbind(
            monsoon=model.matrix(~monsoon)[,-1],
            wday=model.matrix(~wday)[,-1],
            holiday=model.matrix(~holiday)[,-1]
          )))
          names(sub)[5]<-"mon.thurs"
          ##Normalising the data for training in a neural net 
          sub[,2][1]=0
          maxs <- apply(sub, 2, max) 
          mins <- apply(sub, 2, min)

          scaled <- as.data.frame(scale(sub, center = mins, scale = maxs - mins))
          train<- scaled[1:I(ind-1),]
          test<- scaled[ind,]

          set.seed(123456)
          nn <- neuralnet(f,data=train,hidden =7,linear.output = TRUE)
          pr.nn<-neuralnet::compute(nn,test[,-1])
          #Normalising back
          pr.nn.<- pr.nn$net.result*(max(sub$actual)-min(sub$actual))+min(sub$actual)
          test.r <- (test$actual)*(max(sub$actual)-min(sub$actual))+min(sub$actual)


          u=mape(as.numeric(test.r),as.numeric(pr.nn.));u#Calculating Mean Absolute Percentage Error
          if(i==1){

            nnet.hour=data.frame(actual=as.numeric(test.r),forecast1=as.numeric(pr.nn.),mape=u)
          }else{

            nnet.hour=rbind(nnet.hour,data.frame(data.frame(actual=as.numeric(test.r),forecast1=as.numeric(pr.nn.),mape=u)))
            nnet.hour=data.frame(nnet.hour)
          }
        }

1 个答案:

答案 0 :(得分:0)

是的。这已经解决。实际上,对于一些迭代,我没有调用&#d; dplyr&#39;包,所以我使用滞后创建的滞后变量(函数&#39;滞后&#39;同时在基数和dplyr包中)函数返回的变量与我试图预测的变量相同的系列错误是〜可以忽略不计。 一旦我调用dplyr包,结果就是可重现的。

感谢。