2016-07-16 86 views
2

我在波士顿可用的数据集的R上尝试过神经网络。使用神经网络函数时出现错误

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

只保留我们希望使用那些可变:

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

在此情况下式被存储在所谓的F R对象。 响应变量medv将被“回归”其余九个属性。我已经做了如下:

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

要设置506行的数据,无需更换的列车样品400使用示例方法收集:R t的

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

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) 

但是警告消息显示为算法不收敛。

警告消息: 算法没有在1个重复(S)的1 stepmax内收敛

试图预测使用的计算,则显示

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

跟随误差MSG

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' 

为什么错误即将到来以及如何从中恢复以进行预测。我想在该数据集上使用神经网络函数。

+2

您是否考虑在训练之前缩放数据集? – sebastianmm

+0

我还没有缩放它。会导致更快的收敛吗?截至目前,问题似乎是不衔接。 – shan

+0

是的。看到我的编辑如下。 – sebastianmm

回答

2

neuralnet不收敛,所得到的神经网络是不完整的。你可以通过拨打attributes(fit)$names来判断。当训练收敛,它看起来就像这样:

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

当它没有,有些属性不会被定义为:

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

这就解释了为什么compute不起作用。

当训练不收敛时,第一种可能的解决方案可能是增加stepmax(默认100000)。您还可以添加lifesign = "full",以更好地了解培训过程。另外,看着你的代码,我会说有10,12和20个神经元的三层太多了。在我的情况下,我将从一个图层开始,具有与输入数量相同的神经元数量。

编辑:

随着缩放(记得要同时调整训练和测试数据,并以“德标尺” compute结果),它收敛速度更快。还请注意,我减少了层数和神经元数量,并且仍然降低了错误阈值。

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) 
0

该问题似乎在您的论点linear.output = TRUE

与您的数据,但改变一个位(不限定式并添加某些说明性注释)的代码:

library(neuralnet)    
fit <- neuralnet(formula = medv ~ crim + indus + nox + rm + age + dis + tax + ptratio + lstat, 
       data = data[train,], 
       hidden=c(10, 12, 20), # number of vertices (neurons) in each hidden layer 
       algorithm = "rprop+", # resilient backprop with weight backtracking, 
       err.fct = "sse",  # calculates error based on the sum of squared errors 
       act.fct = "logistic", # smoothing the cross product of neurons and weights with logistic function 
       threshold = 0.1,  # of the partial derivatives for error function, stopping 
       linear.output=FALSE)  # act.fct applied to output neurons 

print(net) 

Call: neuralnet(formula = medv ~ crim + indus + nox + rm + age + dis +  tax + ptratio + lstat, data = data[train, ], hidden = c(10,  12, 20), threshold = 0.1, rep = 10, algorithm = "rprop+",  err.fct = "sse", act.fct = "logistic", linear.output = FALSE) 

10 repetitions were calculated. 

     Error Reached Threshold Steps 
1 108955.0318  0.03436116236  4 
5 108955.0339  0.01391790099  8 
3 108955.0341  0.02193379592  3 
9 108955.0371  0.01705056758  6 
8 108955.0398  0.01983134293  8 
4 108955.0450  0.02500006437  5 
6 108955.0569  0.03689097762  5 
7 108955.0677  0.04765829189  5 
2 108955.0705  0.05052776877  5 
10 108955.1103  0.09031966778  7 
10 108955.1103  0.09031966778  7 

# now compute will work 
pred <- compute(fit, data[-train, 1:9]) 
+0

'linear.output = TRUE'用于回归,在这种情况下是需要的。您正在更改所需的输出,这不是一个解决方案。 – sebastianmm

+0

是的.. @ sebastianmm改变linear.output参数在这里是不需要的。 #如果我计算平方相关系数,均方根误差(mse)和#根均方差(rmse),假FALSE参数值为 round(cor(pred $ net.result,data [-train,10])^ 2 ,6) 的值要小得多。它应该在0.809458 mse附近(数据[-train,10],pred $ net.result)很高 #应该接近0.2607601849 rmse(data [-train,10],pred $ net。结果)很高 #应该靠近0.5106468299 – shan