我正在使用Neuralnet包来训练分类器。 训练数据如下所示:
> 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+")
当我尝试对测试集进行预测时出现问题:
pred <- compute(nn, cv_data)
cv_data的样子:
> 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
对此我得到一个错误说:
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'
为什么我会收到此错误以及如何解决?
答案 0 :(得分:4)
我刚刚遇到了同样的问题。检查compute
函数的源代码,我们可以看到它假设一个结果属性(即weights
)仅在网络完成培训时定义完美。
> trace("compute",edit=TRUE)
function (x, covariate, rep = 1) {
nn <- x
linear.output <- nn$linear.output
weights <- nn$weights[[rep]]
[...]
}
我认为真正的问题在于,neuralnet
一旦达到stepmax
值就不会保存当前网络,导致compute
代码中出现此错误。
修改强>
似乎你可以通过注释 65 &amp;行来避免这种重置。 calculate.neuralnet
函数的 66
> fixInNamespace("calculate.neuralnet", pos="package:neuralnet")
[...]
#if (reached.threshold > threshold)
# return(result = list(output.vector = NULL, weights = NULL))
[...]
然后一切都像魅力一样:)
答案 1 :(得分:3)
尝试将threshold
调整为高于0.01的值,或将stepmax
调整为超过1e06,或使用0.1的threshold
,然后从那里减少它。您还可以添加lifesign = "full"
参数,以1000步的增量观察模型创建性能,以真正拨入阈值。这“解决了”我的非二元错误,但模型的准确性,均方误差和其他结果不如直接结果那么令人满意。
答案 2 :(得分:0)
执行str(cv_data)并确保它们都是数字。
答案 3 :(得分:-2)
因为你从未在函数startweights
中设置neuralnet()
根据文件
neuralnet(formula, data, hidden = 1, threshold = 0.01,
stepmax = 1e+05, rep = 1, startweights = NULL,
learningrate.limit = NULL,
learningrate.factor = list(minus = 0.5, plus = 1.2),
learningrate=NULL, lifesign = "none",
lifesign.step = 1000, algorithm = "rprop+",
err.fct = "sse", act.fct = "logistic",
linear.output = TRUE, exclude = NULL,
constant.weights = NULL, likelihood = FALSE)
startweights a vector containing starting values for the weights. The weights will not be randomly initialized.
请注意,默认值为NULL,不会随机初始化。尝试在那里放一些东西,看看是否有效。