2016-07-16 66 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