簡體   English   中英

R:nrow [w] * ncol [w]中的錯誤:使用Neuronet程序包時二進制運算符的非數字參數

[英]R: Error in nrow[w] * ncol[w] : non-numeric argument to binary operator, while using neuralnet package

我正在使用Neuronet軟件包來訓練分類器。 訓練數據如下所示:

> 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'

為什么會出現此錯誤,我該如何解決?

我只是遇到了同樣的問題。 查看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代碼。

編輯

看來您可以通過注釋calculate.neuralnet函數的第6566行來避免此重置

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

然后一切都變成了魅力:)

嘗試將threshold調整為高於0.01的值,或者將stepmax為大於1e06的threshold ,或者使用0.1的threshold ,然后從此處減小。 您還可以添加lifesign = "full"參數來觀察模型創建的性能,以1000個步長為增量,以實際輸入閾值。 這“解決”了我的非二進制誤差,但是模型的准確性,均方誤差和其他結果均不足以滿足直接結果。

執行str(cv_data)並確保它們都是數字。

因為您從未在函數startweights neuralnet()設置startweights
根據文檔

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,並且不會隨機初始化。 嘗試在此放置一些東西,看看是否可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM