[R] Neuralnet Error
arun
smartpink111 at yahoo.com
Thu Aug 2 07:39:46 CEST 2012
Hi,
I am glad it is solved.
#ir3 <- data.frame(rbind(ir1[1:4],ir2))
should be "cbind" in your previous code.
Even if that was corrected,
n<- neuralnet(Output~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=ir3,err.fct="sse",hidden=c(3),linear.output=FALSE)
Error in neurons[[i]] %*% weights[[i]] :
requires numeric/complex matrix/vector arguments
#Probably due to:
str(ir3)
'data.frame': 150 obs. of 6 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Output : Factor w/ 3 levels "","0","1": 3 3 3 3 3 3 3 3 3 3 ...
# my dataset
str(iris1)
----------------
$ Output : num 1 1 1 1 1 1 1 1 1 1 ...
Your new str()
str(ir4)
num [1:150, 1:5] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "Sepal.Length" "Sepal.Width" "Petal.Width" "Petal.Length" ...
#Matrix/numeric/vector arguments are allowed.
#The result.matrix of your new code is different from that I got. May be because you left out "virginica".
n$result.matrix
1
error 125.007861173323
reached.threshold 0.007860966751
steps 29.000000000000
Intercept.to.1layhid1 0.522166529273
Sepal.Length.to.1layhid1 1.491041058746
Sepal.Width.to.1layhid1 -0.544345459033
Petal.Length.to.1layhid1 -0.615294328176
Petal.Width.to.1layhid1 3.734627433294
Intercept.to.1layhid2 3.633493428721
Sepal.Length.to.1layhid2 2.124216647102
Sepal.Width.to.1layhid2 2.029136986941
Petal.Length.to.1layhid2 2.181854757855
Petal.Width.to.1layhid2 3.410291043541
Intercept.to.1layhid3 0.603689174589
Sepal.Length.to.1layhid3 2.665300953113
Sepal.Width.to.1layhid3 1.322847578383
Petal.Length.to.1layhid3 1.897467378865
Petal.Width.to.1layhid3 3.275017257909
Intercept.to.Output 2.345020538556
1layhid.1.to.Output 1.642408665201
1layhid.2.to.Output 3.552359709094
1layhid.3.to.Output 2.319432740042
I am not sure which one is correct. May be its with the vectorization.
So, I tried that,
Sepal.Length<-c(iris1$Sepal.Length)
Sepal.Width<-c(iris1$Sepal.Width)
Petal.Width<-c(iris1$Petal.Width)
Petal.Length<-c(iris1$Petal.Length)
Output<-c(iris1$Output)
iris2<-cbind(Sepal.Length,Sepal.Width,Petal.Width,Petal.Length,Output)> n3<-neuralnet(Output~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=iris2,err.fct="sse",hidden=c(3),linear.output=FALSE)
n3$result.matrix
1
error 0.014412178415
reached.threshold 0.009527746351
steps 45.000000000000
Intercept.to.1layhid1 4.563970987648
Sepal.Length.to.1layhid1 4.070751169463
Sepal.Width.to.1layhid1 2.994119258844
Petal.Length.to.1layhid1 4.926769597100
Petal.Width.to.1layhid1 2.823718327794
Intercept.to.1layhid2 -1.280594422741
Sepal.Length.to.1layhid2 0.635447411640
Sepal.Width.to.1layhid2 -3.621227214142
Petal.Length.to.1layhid2 2.426457019767
Petal.Width.to.1layhid2 2.238558775899
Intercept.to.1layhid3 -1.808205462130
Sepal.Length.to.1layhid3 -0.645677082354
Sepal.Width.to.1layhid3 -1.463897594407
Petal.Length.to.1layhid3 2.257667858752
Petal.Width.to.1layhid3 4.055657058740
Intercept.to.Output 1.426771691174
1layhid.1.to.Output 2.525255850764
1layhid.2.to.Output -4.115814022250
1layhid.3.to.Output -4.492878425478
Slightly different than the one I got before.
A.K.
________________________________
From: Rahul Bhalla <rahulbhalla_3521 at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Thursday, August 2, 2012 12:54 AM
Subject: Re: [R] Neuralnet Error
Thank you for your help. The problem was with the arguments which were not vector arguments so i modified my code to
library(neuralnet)
ir<-read.table(file="iris_data.txt",header=TRUE,row.names=NULL)
ir1 <- data.frame(ir[1:100,2:6])
ir2 <- data.frame(ifelse(ir1$Species=="setosa",1,ifelse(ir1$Species=="versicolor",0,"")))
colnames(ir2)<-c("Output")
ir3 <- (cbind(ir1[1:4],ir2))
#ir4 <- dput(head(ir3,4))
#rownames(ir3)<-c("SL","SW","PL","PW","Output")
print(ir3)
#n<- neuralnet(Output~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=ir,err.fct="sse",hidden=c(3),linear.output=FALSE)
Sepal.Length <- c(ir3$Sepal.Length)
Sepal.Width <- c(ir3$Sepal.Width)
Petal.Width <- c(ir3$Petal.Width)
Petal.Length <- c(ir3$Petal.Length)
Output <- c(ir3$Output)
ir4 <- cbind(Sepal.Length,Sepal.Width,Petal.Width,Petal.Length,Output)
#print(ir4)
n<- neuralnet(Output~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=ir4,err.fct="sse",hidden=c(3),linear.output=FALSE)
#plot(n)
#prediction(n)
S.Length <-c(3.5)
S.width<-c(2.0)
P.Length <- c(1)
P.Width<-c(0.1)
________________________________
From: arun <smartpink111 at yahoo.com>
To: Rahul Bhalla <rahulbhalla_3521 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, August 2, 2012 9:40 AM
Subject: Re: [R] Neuralnet Error
Hello,
I guess the error might be due to the struture of your dataset.
Try this:
data(iris)
iris1<-iris
iris1[iris1$Species=="setosa","Output"]<- 1
iris1[iris1$Species=="versicolor","Output"]<- 0
#Here, if you set "virginica" to 2, still it runs, with a warning message "-- response is not binary". I also run it without setting values for viriginica ,i.e. NA, it didn't gave any error or warnings.
iris1[iris1$Species=="virginica","Output"]<- 0
nn1<-neuralnet(Output~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=iris1,hidden=2,err.fct="ce",linear.output=FALSE)
plot(nn1)
nn1$result.matrix
1
error 0.018911271401
reached.threshold 0.009459417063
steps 92.000000000000
Intercept.to.1layhid1 -4.353018230633
Sepal.Length.to.1layhid1 1.720240396708
Sepal.Width.to.1layhid1 -6.428901709134
Petal.Length.to.1layhid1 3.273018763978
Petal.Width.to.1layhid1 7.110403675648
Intercept.to.1layhid2 4.815489181151
Sepal.Length.to.1layhid2 -0.123278321659
Sepal.Width.to.1layhid2 6.348589101327
Petal.Length.to.1layhid2 -6.376680199897
Petal.Width.to.1layhid2 -8.065600706230
Intercept.to.Output
-0.224816723130
1layhid.1.to.Output -9.042646714781
1layhid.2.to.Output 8.799061406981
str(iris1)
'data.frame': 150 obs. of 6 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Output : num 1 1 1 1 1 1 1 1 1 1 ...
Hope it helps you.
A.K.
----- Original Message -----
From: Rahul Bhalla <rahulbhalla_3521 at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, August 1, 2012 1:51 PM
Subject: [R] Neuralnet Error
I require some help in debugging this code
library(neuralnet)
ir<-read.table(file="iris_data.txt",header=TRUE,row.names=NULL)
ir1 <- data.frame(ir[1:100,2:6])
ir2 <- data.frame(ifelse(ir1$Species=="setosa",1,ifelse(ir1$Species=="versicolor",0,"")))
colnames(ir2)<-("Output")
ir3 <- data.frame(rbind(ir1[1:4],ir2))
#rownames(ir3)<-c("SL","SW","PL","PW","Output")
print(ir3)
n<- neuralnet(Output~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=ir3,err.fct="sse",hidden=2,linear.output=FALSE)
Output:
Error
in neurons[[i]] %*% weights[[i]] :
requires numeric/complex matrix/vector arguments
Any assisstance is appreciated
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list