且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在R中使用neuralnet包时如何实现自己的错误函数?

更新时间:2022-12-16 20:23:07

我有同样的问题。这是我收到的解决方案/帮助。
您可以使用R函数的常用定义(function(x,y){...})。因此,错误函数必须是函数类型(x,y),其中x是拟合值,y是真实值。

请参考以下示例。

  library(neuralnet)

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

#10次重复计算。

#Error达到阈值步骤
#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 打印(net
#Call:neuralnet(公式= AND + OR〜Var1 + Var2 + Var3,data = binary.data,hidden = 0,rep = 10,err.fct = custom,linear.output = FALSE)

#10次重复计算。

#Error达到阈值步骤
#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

您可以使用基本上每个可区分的错误函数。 b $ b

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.

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.