简体   繁体   English

R 中的神经网络 package 大错误

[英]Neuralnet package in R big error

I am trying to figure out how to make the neuralnet package to work.我试图弄清楚如何使神经网络 package 工作。 I did some tests with data I created and with their outcomes (about 50 rows of data and three columns with the fourth being the outcome I wanted and it was made from simple mathematical executions like summing the other three columns) and so far so good.我对我创建的数据及其结果进行了一些测试(大约 50 行数据和三列,第四列是我想要的结果,它是由简单的数学执行制成的,比如对其他三列求和),到目前为止一切都很好。 Then I decided to apply the package on real data.然后我决定将 package 应用于真实数据。 I downloaded the mpg dataset from here http://vincentarelbundock.github.io/Rdatasets/datasets.html我从这里下载了 mpg 数据集http://vincentarelbundock.github.io/Rdatasets/datasets.html

I was running the code bellow:我正在运行以下代码:

net<- neuralnet(cty~displ+year+cyl+hwy,
                datain, hidden=3)

Even if I have 3 hidden layers, or 8 or 18 the error is the same and the time that the package processes the data is relatively small from this amount of data (234 lines):即使我有 3 个隐藏层,或者 8 个或 18 个,错误是相同的,并且 package 处理数据的时间相对于这个数据量(234 行)相对较小:

        Error Reached Threshold Steps
1 2110.173077    0.006277805853    54

Any good advice for this?对此有什么好的建议吗?

It's a scale problem i guess, you can normalize or scale it. 这是一个规模问题我猜,你可以规范化或扩展它。 There are differences between scaling and normalizing , it will affect your results and worths a separate question on SO: scalingnormalizing之间存在差异,它会影响您的结果,值得在SO上单独提问:

normalize inputs 规范化输入

norm.fun = function(x){ 
  (x - min(x))/(max(x) - min(x)) 
}

require(ggplot2) # load mpg dataset
require(neuralnet)

data = mpg[, c('cty', 'displ', 'year', 'cyl', 'hwy')]
data.norm = apply(data, 2, norm.fun)

net = neuralnet(cty ~ displ + year + cyl + hwy, data.norm, hidden = 2)

Then you can denormalize the data 然后,您可以对数据进行非规范化

# restore data 
y.net = min(data[, 'cty']) + net$net.result[[1]] * range(data[, 'cty'])
plot(data[, 'cty'], col = 'red')
points(y.net)

在此输入图像描述

scale inputs 规模投入

data.scaled = scale(data)
net = neuralnet(cty ~ displ + year + cyl + hwy, data.scaled, hidden = 2)

# restore data 
y.sd = sd(data[, 'cty'])
y.mean = mean(data[, 'cty'])

y.net = net$net.result[[1]] * y.sd + y.mean
plot(data[, 'cty'], col = 'red')
points(y.net)

在此输入图像描述

You can also try the nnet package, it's very fast: 你也可以试试nnet包,速度非常快:

require(nnet)

data2 = mpg
data2$year = scale(data2$year)
fit = nnet(cty ~ displ + year + cyl + hwy, size = 10, data = data2, linout = TRUE)
plot(mpg$cty)
points(fit$fitted.values, col = 'red')

Have you tried enabling linear output in your neural net model through linear.output=TRUE label?您是否尝试通过 linear.output=TRUE label 在您的神经网络 model 中启用线性 output?

neuralnet(formula, data, hidden = 1, threshold = 0.01,
  stepmax = 1e+05, rep = 1, startweights = NULL,
  learningrate.limit = NULL, learningrate.factor = list(minus = 0.5,
  plus = 1.2), learningrate = NULL, lifesign = "none",
  lifesign.step = 1000, algorithm = "rprop+", err.fct = "sse",
  act.fct = "logistic", ****linear.output = TRUE****, exclude = NULL,
  constant.weights = NULL, likelihood = FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM