10

I'm trying to predict the class (0 or 1) for a test dataset using a neural network trained using the neuralnet package in R.

The data I have looks as follows:

For train:

x1          x2          x3          x4          y
0.557       0.6217009   0.4839      0.5606936   0
0.6549      0.6826347   0.4424      0.4117647   1
0.529       0.5744681   0.5017      0.4148148   1
0.6016771   0.5737052   0.3526971   0.3369565   1
0.6353945   0.6445013   0.5404255   0.464       1
0.5735294   0.6440678   0.4385965   0.5698925   1
0.5252      0.5900621   0.4412      0.448       0
0.7258687   0.7022059   0.5347222   0.4498645   1

and more.

The test set looks the exact same as the training data, just with different values (if need be I will post some samples).

The code I use looks as follows:

> library(neuralnet)
> nn <- neuralnet(y ~ x1+x2+x3+x4, data=train, hidden=2, err.fct="ce", linear.output=FALSE)
> plot(nn)
> compute(nn, test)

The network trains and I can successfully plot the network, but compute doesn't work. When I run compute it gives me the following error:

Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

So basically I'm trying to train a neural network to successfully classify the new test data.

Any help is appreciated.

Edit:

A sampling of the test object is:

x1          x2  x3          x4          y
0.5822  0.6591  0.6445013   0.464       1
0.4082  0.5388  0.5384616   0.4615385   0
0.4481  0.5438  0.6072289   0.5400844   1
0.4416  0.5034  0.5576923   0.3757576   1
0.5038  0.6878  0.7380952   0.5784314   1
0.4678  0.5219  0.5609756   0.3636364   1
0.5089  0.5775  0.6183844   0.5462555   1
0.4844  0.7117  0.6875      0.4823529   1
0.4098  0.711   0.6801471   0.4722222   1

I've also tried it with the y column empty of any values.

user1074057
  • 1,772
  • 5
  • 20
  • 30
  • 5
    You need to try it with no y column at all. Whether or not they are empty will not affect the test for conformable arrays. – IRTFM Jun 07 '12 at 21:45

3 Answers3

19

Hard to say in the absence of a good description of the 'test'-object, but can you see if this gives better results:

compute(nn, test[, 1:4])
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 2
    That did it, as did deleting the y column from the test set. Thank you very much! – user1074057 Jun 07 '12 at 22:06
  • Piggy-backing on this: if you used a formula that did not involve ALL the columns in your data frame, you will want to do something like: `mf <- model.frame(fmla, data=DF); compute(nn, mf[,2:ncol(mf)])` (depending on which column is your target) – rhombidodecahedron Sep 14 '15 at 14:58
  • @42 Is it possible that I train neuralnet object (`nn` in this example) again and again with a random subset of my huge trainData? Will that object store all the successive trainings? – Newbie Sep 19 '16 at 15:57
7

I had the same problem. I put debugonce(neuralnet) and I discovered neuralnet was multiplying matrix from different sizes.

I solved the problem removing the y column from test with this function

columns <- c("x1","x2","x3","x4")
covariate <- subset(test, select = columns)
Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
Aquiles
  • 857
  • 1
  • 10
  • 19
0

I know this is an old post, but I came across a unique piece that may help someone in the future. Thought this post was most applicable as it throws the same error.

Scaling of a dataset must be converted back into a data.frame for use in compute

#scaled data
scaledData=scale(data)
nn=neuralnet(y~x,data=scaledData[train,])

#this repeatedly failed for me
compute(nn,scaledData[test,])

#this worked 
compute(nn,as.data.frame(scaledData)[test,])
jtclaypool
  • 131
  • 1
  • 5