how to implement own error function while using neuralnet package in R?

99封情书 提交于 2019-12-10 17:33:27

问题


I am trying to implement a customized error function in package neuralnet in R.

Normally ’sse’ and ’ce’ which stand for the sum of squared errors and the cross-entropy are used to calculate error.Can anyone provide me details about how to implement own error function. Though the package says we can use customized error function,there is no help in the user Manuel about this.


回答1:


I had the same Problem. This is the solution/help I received. You can use the usual definition of R functions (function(x,y){...}). Hence, the error function must be of the type function(x,y) where x is the fitted value and y is the true value.

Please refer to the following example.

library(neuralnet)

AND <- c(rep(0,7),1)
OR <- c(0,rep(1,7))
binary.data <- data.frame(expand.grid(c(0,1), c(0,1), c(0,1)), AND, OR)
set.seed(3)
print(net <- neuralnet(AND+OR~Var1+Var2+Var3,  binary.data, hidden=0, rep=10, err.fct="sse", linear.output=FALSE))

#Call: neuralnet(formula = AND + OR ~ Var1 + Var2 + Var3, data = binary.data,     hidden = 0, rep = 10, err.fct = "sse", linear.output = FALSE)
#
#10 repetitions were calculated.
#
#Error Reached Threshold Steps
#7  0.04043122185    0.008248439644   116
#5  0.04426319054    0.009619409680   124
#8  0.04698485282    0.007947430014   117
#2  0.04931335384    0.008792873261    88
#1  0.04965332555    0.009631079320    89
#4  0.05396400022    0.009092193542    96
#6  0.05488395412    0.009990028287   124
#3  0.06383087672    0.009964206587    94
#10 0.51657348285    0.008602371325    51
#9  0.52514202592    0.007890927099    40


set.seed(3)
custom <- function(x,y){1/2*(y-x)^2}
print(net <- neuralnet(AND+OR~Var1+Var2+Var3,  binary.data, hidden=0, rep=10, linear.output=FALSE, err.fct=custom))

#Call: neuralnet(formula = AND + OR ~ Var1 + Var2 + Var3, data = binary.data,     hidden = 0, rep = 10, err.fct = custom, linear.output = FALSE)
#
#10 repetitions were calculated.
#
#Error Reached Threshold Steps
#7  0.04043122185    0.008248439644   116
#5  0.04426319054    0.009619409680   124
#8  0.04698485282    0.007947430014   117
#2  0.04931335384    0.008792873261    88
#1  0.04965332555    0.009631079320    89
#4  0.05396400022    0.009092193542    96
#6  0.05488395412    0.009990028287   124
#3  0.06383087672    0.009964206587    94
#10 0.51657348285    0.008602371325    51
#9  0.52514202592    0.007890927099    40

You can use basically every error function that can be differentiated.



来源:https://stackoverflow.com/questions/25510960/how-to-implement-own-error-function-while-using-neuralnet-package-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!