【问题标题】:Getting errors while using neuralnet function使用神经网络函数时出错
【发布时间】:2016-11-19 11:27:17
【问题描述】:

我在可用的波士顿数据集上尝试了 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行无放回使用sample方法采集:

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

拟合了 R 的神经网络函数。

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)

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

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

尝试使用计算进行预测,

 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'

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

【问题讨论】:

  • 您是否考虑过在训练之前扩展您的数据集?
  • 我没有缩放它。是否会导致更快的收敛。截至目前,问题似乎是不收敛。
  • 是的。请参阅下面的编辑。
  • 我已经尝试过使用 deepnet ,相同的数据集,虽然它没有显示任何错误,但结果似乎不正确。你能看透吗?
  • 我不明白你在说什么。您是否尝试过我发布的解决方案?

标签: r machine-learning statistics neural-network


【解决方案1】:

问题似乎在于你的论点linear.output = TRUE

使用您的数据,但稍微更改代码(不定义公式并添加一些解释性 cmets):

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

【讨论】:

  • linear.output = TRUE 用于回归,在这种情况下是需要的。您正在更改所需的输出,这不是解决方案。
  • 是的.. @sebastianmm 在这里不需要更改 linear.output 参数。 # 如果我用 FALSE 参数值计算平方相关系数、均方误差 (mse) 和#均方根误差 (rmse) round(cor(pred$net.result,data[-train,10])^2,6 ) 值要小得多。它应该接近 0.809458 mse(data [-train ,10] , pred$net.result ) 非常高 # 应该接近 0.2607601849 rmse (data [-train ,10] , pred$net.result ) 很多高#应该接近0.5106468299
【解决方案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 个神经元太多了。我将从具有与输入数量相同数量的神经元的一层开始,在您的情况下为 9。

编辑:

通过缩放(请记住缩放训练和测试数据,并“去缩放”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)

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2014-01-18
    • 2017-12-08
    相关资源
    最近更新 更多