R neuralNet: “non-conformable arguments”

772    Asked by DanielCameron in Data Science , Asked on Jun 10, 2021

When attempting to compute with my neural network, getting error “error in neurons[[i]] %*% weights[[i]] : non-conformable arguments”as shown below:

> net.compute <- compute(net, matrix.train2)
Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

I can't figure out what the problem is. Below I'll provide you with an example data and formatting from my matrices and then I'll show you the code I'm attempting to run.

matrix.train1 is used for training the network

> matrix.train1
    (Intercept) survived pclass sexmale    age sibsp parch fare embarkedC embarkedQ embarkedS
1             1 0   3 1 22.00     1 0 7.2500     0 0 1
2             1 1   1 0 38.00     1 0 71.2833     1 0 0
3             1 1   3 0 26.00     0 0 7.9250     0 0 1
4             1 1   1 0 35.00     1 0 53.1000     0 0 1
5             1 0   3 1 35.00     0 0 8.0500     0 0 1
6             1 0   3 1 999.00     0 0 8.4583     0 1 0
7             1 0   1 1 54.00     0 0 51.8625     0 0 1
8             1 0   3 1 2.00     3 1 21.0750     0 0 1
9             1 1   3 0 27.00     0 2 11.1333     0 0 1
10            1 1   2 0 14.00     1 0 30.0708     1 0 0
11            1 1   3 0 4.00     1 1 16.7000     0 0 1
matrix.train2 is a slice of the training data used for testing the model
> matrix.train2
    (Intercept) pclass sexmale    age sibsp parch fare embarkedC embarkedQ embarkedS
1             1 1 1  49.00 1 1 110.8833         1 0 0
2             1 3 1  42.00 0 0   7.6500 0     0 1
3             1 1 0  18.00 1 0 227.5250         1 0 0
4             1 1 1  35.00 0 0 26.2875         0 0 1
5             1 3 0  18.00 0 1 14.4542         1 0 0
6             1 3 1  25.00 0 0   7.7417 0     1 0
7             1 3 1  26.00 1 0   7.8542 0     0 1
8             1 2 1  39.00 0 0 26.0000         0 0 1
9             1 2 0  45.00 0 0 13.5000         0 0 1
10            1 1 1  42.00 0 0 26.2875         0 0 1
11            1 1 0  22.00 0 0 151.5500         0 0 1

The only real difference between the two matrices is that matrix.train2 doesn't contain the survived column.

Here's the R code I'm attempting to run:

#Build a matrix from the training data matrix.train1 <- model.matrix(
  ~ survived + pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train1
)
library(neuralnet)
#Train the neural net
net <- neuralnet(
  survived ~ pclass + sexmale + age + sibsp + parch + fare + embarkedC + 
  embarkedQ + embarkedS, data=matrix.train1, hidden=10, threshold=0.01
)
#Build a matrix from test data
matrix.train2 <- model.matrix(
  ~ pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train2
)
#Apply neural net to test matrix 
net.results <- compute(
  net, matrix.train2
)
Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

Can anyone tell me what I'm doing wrong here?

Thanks!

Updates based on comments so far:

Using the solution from "Predicting class for new data using neuralnet" doesn't seem to work.

> net.compute <- compute(net, matrix.train2[,1:10])
Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

I'm manually putting my train1 and train2 data frames into matrices via model. matrix because if I don't I get the following error:

> Error in neurons[[i]] %*% weights[[i]] : 

requires numeric/complex matrix/vector arguments


Answered by Carl Paige

If you instead of doing this:

net.results <- compute(
  net, matrix.train2
PERFORM THIS:
net.result <- compute(
     net, matrix.train2[,c("pclass",
     "sexmale", "age", "sibsp", "parch",
     "fare","embarkedC","embarkedQ","embaredS")])

it should perform well. The names of the variables need to be in the exact order of the model.list$variables, so you can also type

net.result <- compute(  
     net, matrix.train2[, net.result$model.list$variables])

I hope this helps. The reason is - I think - that neuralnet has a problem finding out which variables are in your net and which in the matrix... so you match them explicitly instead.



Your Answer

Interviews

Parent Categories