本文介绍了R:nrow[w] * ncol[w] 中的错误:二元运算符的非数字参数,同时使用神经网络包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用神经网络包来训练分类器.训练数据如下所示:

I am using neuralnet package for training a classifier.The training data looks like this:

> head(train_data)
   mvar_12      mvar_40 v10       mvar_1   mvar_2  Labels
1 136.51551310       6   0   656.78784220      0      0
2 145.10739860      87   0    14.21413596      0      0
3 194.74940330       4   0   196.62888080      0      0
4 202.38663480       2   0   702.27307720      0      1
5  60.14319809       9   0    -1.00000000     -1      0
6  95.46539380       6   0   539.09479640      0      0

代码如下:

n <- names(train_data)
f <- as.formula(paste("Labels ~", paste(n[!n %in% "Labels"], collapse = " + ")))
library(neuralnet)
nn <- neuralnet(f, tr_nn, hidden = 4, threshold = 0.01,
                stepmax = 1e+05, rep = 1,
                lifesign.step = 1000,
                algorithm = "rprop+")

当我尝试对测试集进行预测时出现问题:

The problem arises when I try to make a prediction for a test set:

pred <- compute(nn, cv_data)

其中 cv_data 看起来像:

Where cv_data looks like:

> head(cv_data)
   mvar_12      mvar_40 v10      mvar_1    mvar_2
1 213.84248210       1   9  -1.000000000     -1
2 110.73985680       0   0  -1.000000000     -1
3 152.74463010      14   0 189.521812800     -1
4  64.91646778       7   0  47.854257730     -1
5 141.28878280      12   0 248.557857500      5
6  55.36992840       2   0   4.785425773     -1

对此,我收到一条错误消息:

To this I get an error saying:

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'

为什么会出现此错误,我该如何解决?

Why do I get this error and how can I fix it?

推荐答案

我刚刚遇到了同样的问题.检查 compute 函数的源代码,我们可以看到它假定只有在网络完美完成训练时才定义的结果属性之一(即 weights).

I just came up against the very same problem. Checking the source code of the compute function we can see that it assumes one of the resulting attributes (i.e. weights) only defined when the network finishes the training flawless.

> trace("compute",edit=TRUE)
function (x, covariate, rep = 1) {
    nn <- x
    linear.output <- nn$linear.output
    weights <- nn$weights[[rep]]
    [...]
}

我认为真正的问题在于,一旦达到 stepmax 值,neuralnet 不会保存当前网络,导致稍后在 中出现此错误计算代码.

I think the real problem lies on the fact that neuralnet doesn't save the current network once reached the stepmax value, causing this error later in the compute code.

编辑

看来您可以通过注释行 65 来避免这种重置 &calculate.neuralnet函数的66

It seems you can avoid this reset by commenting lines 65 & 66 of the calculate.neuralnet function

> fixInNamespace("calculate.neuralnet", pos="package:neuralnet")
[...]
#if (reached.threshold > threshold)
#    return(result = list(output.vector = NULL, weights = NULL))
[...]

然后一切都像一个魅力:)

Then everything works as a charm :)

这篇关于R:nrow[w] * ncol[w] 中的错误:二元运算符的非数字参数,同时使用神经网络包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:33