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
The code is as follows:
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)
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?
Becasue you never set
startweights
in the functionneuralnet()
According to the documentation
Note that the default value is NULL, and it will NOT be randomly initialized. Try to put something there and see if that works.
Try adjusting the
threshold
to a higher than 0.01 value or thestepmax
to more than 1e06, or using athreshold
of 0.1 and then decreasing it from there. You can also add in thelifesign = "full"
argument to observe the model creation performance in increments of 1000 steps to really dial in the threshold. This "resolved" the non-binary error I had, but the accuracy of the model, the mean squared error, and other results were less than satisfying as a direct result.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.I think the real problem lies on the fact that
neuralnet
doesn't save the current network once reached thestepmax
value, causing this error later in thecompute
code.Edit
It seems you can avoid this reset by commenting lines 65 & 66 of the
calculate.neuralnet
functionThen everything works as a charm :)
Do a str(cv_data) and make sure they are all numeric.