使用神经网络功能时出现错误 [英] Getting errors while using neuralnet function

查看:750
本文介绍了使用神经网络功能时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在可用的波士顿数据集上尝试了R中的神经网络.

I tried neural net in R on Boston data set available.

data("Boston",package="MASS") 
data <- Boston

仅保留我们要使用的变量:

Retaining only those variable we want to use:

keeps <- c("crim", "indus", "nox", "rm" , "age", "dis", "tax" ,"ptratio", "lstat" ,"medv" ) 
data <- data[keeps]

在这种情况下,公式存储在名为f的R对象中. 响应变量medv将针对其余9个属性进行回归".我做到了,如下所示:

In this case the formula is stored in an R object called f. The response variable medv is to be "regressed" against the remaining nine attributes. I have done it as below:

f <- medv ~ crim + indus + nox + rm + age + dis + tax + ptratio + lstat

要使用样本方法收集506行数据中的火车样本400,而无需进行替换:

To set up train sample 400 of the 506 rows of data without replacement is collected using the sample method:

set.seed(2016) 
n = nrow(data) 
train <- sample(1:n, 400, FALSE)

符合R的神经网络功能.

neuralnet function of R is fitted.

library(neuralnet)
fit<- neuralnet(f, data = data[train ,], hidden=c(10 ,12 ,20), 
                 algorithm = "rprop+", err.fct = "sse", act.fct = "logistic", 
                 threshold =0.1, linear.output=TRUE)

但是警告消息显示为算法未收敛.

But warning message is displayed as algorithm not converging.

警告信息: 算法未在stepmax内的1次重复中收敛1次

Warning message: algorithm did not converge in 1 of 1 repetition(s) within the stepmax

使用计算尝试预测

 pred <- compute(fit,data[-train, 1:9])

显示以下错误消息

Error in nrow[w] * ncol[w] : non-numeric argument to binary operator
In addition: Warning message:
In is.na(weights) : is.na() applied to non-(list or vector) of type 'NULL'

为什么会出现错误以及如何从错误中恢复进行预测.我想在该数据集上使用Neuronet函数.

Why the error is coming up and how to recover from it for prediction. I want to use the neuralnet function on that data set.

推荐答案

neuralnet不收敛时,所得的神经网络将不完整.您可以通过调用attributes(fit)$names来判断.训练收敛时,它将如下所示:

When neuralnet doesn't converge, the resulting neural network is not complete. You can tell by calling attributes(fit)$names. When training converges, it will look like this:

 [1] "call"                "response"            "covariate"           "model.list"          "err.fct"  
 [6] "act.fct"             "linear.output"       "data"                "net.result"          "weights"  
[11] "startweights"        "generalized.weights" "result.matrix"

如果未定义,则不会定义某些属性:

When it doesn't, some attributes will not be defined:

[1] "call"          "response"      "covariate"     "model.list"    "err.fct"       "act.fct"       "linear.output"
[8] "data"   

这说明了为什么compute不起作用.

That explains why compute doesn't work.

当训练不收敛时,第一个可能的解决方案是增加stepmax(默认值为100000).您还可以添加lifesign = "full",以更好地了解培训过程.

When training doesn't converge, the first possible solution could be to increase stepmax (default 100000). You can also add lifesign = "full", to get better insight into the training process.

另外,看看您的代码,我想说具有10、12和20个神经元的三层实在太多了.在您的情况下,我将从神经元数量与输入数量相同的一层开始.

Also, looking at your code, I would say three layers with 10, 12 and 20 neurons is too much. I would start with one layer with the same number of neurons as the number of inputs, in your case 9.

通过缩放(记住缩放训练和测试数据,并缩放" compute结果),其收敛速度更快.另外请注意,我减少了层和神经元的数量,但仍然降低了错误阈值.

With scaling (remember to scale both training and test data, and to 'de-scale' compute results), it converges much faster. Also note that I reduced the number of layers and neurons, and still lowered the error threshold.

data("Boston",package="MASS") 
data <- Boston

keeps <- c("crim", "indus", "nox", "rm" , "age", "dis", "tax" ,"ptratio", "lstat" ,"medv" ) 
data <- data[keeps]

f <- medv ~ crim + indus + nox + rm + age + dis + tax + ptratio + lstat

set.seed(2016) 
n = nrow(data) 
train <- sample(1:n, 400, FALSE)

# Scale data. Scaling parameters are stored in this matrix for later.
scaledData <- scale(data)

fit<- neuralnet::neuralnet(f, data = scaledData[train ,], hidden=9, 
                algorithm = "rprop+", err.fct = "sse", act.fct = "logistic", 
                threshold = 0.01, linear.output=TRUE, lifesign = "full")

pred <- neuralnet::compute(fit,scaledData[-train, 1:9])

scaledResults <- pred$net.result * attr(scaledData, "scaled:scale")["medv"] 
                                 + attr(scaledData, "scaled:center")["medv"]

cleanOutput <- data.frame(Actual = data$medv[-train], 
                          Prediction = scaledResults, 
                          diff = abs(scaledResults - data$medv[-train]))

# Show some results
summary(cleanOutput)

这篇关于使用神经网络功能时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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