Skip to main content
1 of 4
Chris
  • 1.3k
  • 10
  • 31

R's nnet needs decay to perform with sin() like function, why variant reproducibility

I've noticed that for sin() like data, I need to use "decay" which is available in nnet to get the ANN to perform. Why would that be in theory? Also when I run runNN(0.02) over and over, sometimes the model performs beautifully (relative rmse of 0.04) and other times poorly (relative rmse of 0.42). How can one make the training of the nnet not so dependent on the random initialization of the weights?

I attempted a while loop to run the nnet() function until the rmse happened to be low amount, but strangely the nnet didn't seem to generate different weights. Only when I ran the once again on the command-line did it do so. I suspect it is using the same random seed in the loop?

Is there a way to set the training parameters better in order to always converge on the "best" result?

For the training data: Imagine a restaurant has the most customers at noon and the qty of the tips is highest at noon and we want to make a neural net predict tips gained in an hour.

library(quantmod)
library(nnet)
runNN=function(decayParam) {
data=data.frame(h=1:24); data$qty<-sin(data$h/482pi)*1000 data$v=c(paste("actual",decayParam)) mynn<-nnet(qty~h,data,size=2,decay = decayParam,linout = TRUE,maxit=2000) pred=data.frame(h=1:24); ps<-predict(mynn,pred); pred$qty=ps[,1]; pred$v=c(paste("pred",decayParam)) rsd=paste("rsd=",sqrt(mean((data$qty-pred$qty)^2))/mean(data$qty)) print(rsd) alldata <- rbind(data,pred) print(ggplot(data=alldata, aes(x=h, y=qty, group=v, color=v)) + geom_line() + geom_point()+theme_classic()+ggtitle(rsd)); }
runNN(0) #with no decay - tends to converge to the population mean

runNN(0.02) #with decay - works better, but if you run this multiple times, sometimes it converges badly.

Chris
  • 1.3k
  • 10
  • 31