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?
while(runNN(0.02)>0.1){} # This works but it seems sloppy.
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)
print("start")
runNN = function(decayParam) {
data = data.frame(h=1:24);
data$qty <- sin(data$h/48*2*pi)*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 = sqrt(mean((data$qty-pred$qty)^2))/mean(data$qty)
rsds = paste("rsd=",rsd)
print(rsds)
alldata <- rbind(data,pred)
print(ggplot(data=alldata, aes(x=h, y=qty, group=v, color=v)) +
geom_line() +
geom_point()+theme_classic()+ggtitle(rsds));
return(rsd)
}
print(runNN(0)) # with no decay
print(runNN(0.02)) # with decay
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.