在R中使用neuralnet包时如何实现自己的错误函数? [英] how to implement own error function while using neuralnet package in R?

查看:693
本文介绍了在R中使用neuralnet包时如何实现自己的错误函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



正常情况下,'sse'和'ce'代表错误的平方和交叉熵用于计算错误。任何人都可以提供有关如何实现自己的错误函数的详细信息。尽管该包说我们可以使用定制的错误功能,但用户Manuel对此没有任何帮助。

解决方案

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

请参考以下示例。

  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 <--network(AND + OR_Val1 + 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次重复计算。

#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 < - function(x,y){1/2 *(yx)^ 2}
打印(net <--network(AND + OR_Val1 + Var2 + Var3,binary.data,hidden = 0,rep = 10,linear.output = FALSE,err.fct = custom))

#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.

这篇关于在R中使用neuralnet包时如何实现自己的错误函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆