R neuralNet: "non-conformable arguments"
Asked Answered
E

5

5

Argh! I keep getting the following error when attempting to compute with my neural network:

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

  1. 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
    
  2. 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
    

Note: see the following thread for more details on why I'm using model.matrix: "Working with neuralnet in R for the first time: get “requires numeric/complex matrix/vector arguments” but don't know how to correct".

Evelineevelinn answered 9/7, 2013 at 14:41 Comment(2)
What package are you using?Aetolia
I'm using the neuralnet package. In case it matters, the IDE I'm using is RStudio.Evelineevelinn
A
9

It looks like you need to remove the predictor variable. Try this:

nn_pred<-compute(nn,test[,3:11])
Anabas answered 10/2, 2016 at 14:53 Comment(0)
V
5

I tried this with the neuralnet package as well. I think if you instead of

net.results <- compute(
  net, matrix.train2

do

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

it should work. The names of the variables needs 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.

Vineyard answered 21/6, 2016 at 20:7 Comment(0)
A
1

I haven't used the neuralnet ackage, but unless it's doing something weird you shouldn't be calling model.matrix like that. neuralnet has a formula interface, so it will call model.matrix for you. You just have to give it the training data frame train1.

This also applies for predicting on test data. Don't create a model matrix; just pass it the data frame train2.

Aetolia answered 9/7, 2013 at 18:0 Comment(4)
I wasn't calling model.matrix originally but when I passed in the data frame directly I got a requires numeric/complex matrix/vector arguments error. See #17457528 for more details regarding why I'm using model.matrixEvelineevelinn
Good lord, is that neuralnet code hideous. This doesn't answer your question, but maybe you should try using nnet in package nnet (part of the base R install). It only does single-hidden-layer MLPs and uses quasi-Newton rather than flavours of backprop, but at least it knows how to handle factors.Aetolia
And by "handle factors" I mean everything that the standard model.frame/model.matrix approach gives you: ability to deal with missing values, subset your data, do predictions easily on new data (without having to worry about the ordering of columns), and so on.Aetolia
Even though this didn't directly answer my question, it did turn me onto nnet which in turn got me to look at the caret package, which is awesome. I'd heard about caret before but hadn't yet looked into it. It looks awesome. Thanks!Evelineevelinn
E
0

Try the answer used for this questions,predicting class for new data using neuralnet

Elegy answered 9/7, 2013 at 16:13 Comment(3)
Still no luck. > net.compute <- compute(net, matrix.train2[,1:10]) Error in neurons[[i]] %*% weights[[i]] : non-conformable argumentsEvelineevelinn
when you use model matrix for train2: matrix.train2 <- model.matrix( ~ pclass + sex + age + sibsp + parch + fare + embarked, data=train2) you dont specify embarkedC, embarkedQ, embarkedS. Maybe this is why you keep getting the error because the apparent different data it needs to compare. Why don't you try to run your actual code with +embarkedC + embarkedQ + embarkedS modificationElegy
I wish it were that simple! The "embarked" vector has three levels: C, Q, and S. When I run the model.matrix function, the function automatically slices each level into its own variable so that they follow the binary format. So instead of "embarked = C, Q, or S" we get embarkedC = 1/0, embarkedQ = 1/0, embarkedS = 1/0. This is an automatic part of the function so I don't need to specify it when running model.matrix, but when I use the matrix for training I have to account for the new variables.Evelineevelinn
H
0

change the neuralnet train to this

t <- neuralnet(
survived ~ pclass + sexmale + age + sibsp + parch + fare + embarkedC + 
embarkedQ + embarkedS)

change the predictor variable to

NN_pred<-compute(t,test[,1:9])

it should have the same order of the data taken in a model

Humbert answered 5/5, 2018 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.