Reputation: 3
I'm trying to do simple neural network modelling, but the NNet result gives me poor result. It is simply ' output = 0.5 x input ' model that I want nnet model to learn, but the prediction shows all '1' as a result. What is wrong?
library(neuralnet)
traininginput <- as.data.frame(runif(50,min=1,max=100))
trainingoutput <- traininginput/2
trainingdata<-cbind(traininginput,trainingoutput)
colnames(trainingdata)<-c("Input","Output")
net.sqrt2 <- nnet(trainingdata$Output~trainingdata$Input,size=0,skip=T, linout=T)
Testdata<-as.data.frame(1:50)
net.result2<-predict(net.sqrt2, Testdata)
cleanoutput2 <- cbind(Testdata,Testdata/2,as.data.frame(net.result2))
colnames(cleanoutput2)<-c("Input2","Expected Output2","Neural Net Output2")
print(cleanoutput2)
Upvotes: 0
Views: 442
Reputation: 4030
library(nnet)
traininginput <- as.data.frame(runif(50,min=1,max=100))
trainingoutput <- traininginput/2
trainingdata<-cbind(traininginput,trainingoutput)
colnames(trainingdata)<-c("Input","Output")
net.sqrt2 <- nnet(Output~Input, data=trainingdata, size=0,skip=T, linout=T)
Testdata<-data.frame(Input=1:50)
net.result2<-predict(net.sqrt2, newdata = Testdata, type="raw")
cleanoutput2 <- cbind(Testdata,Testdata/2,as.data.frame(net.result2))
colnames(cleanoutput2)<-c("Input2","Expected Output2","Neural Net Output2")
print(cleanoutput2)
You're using predict
and the formula
in nnet
wrong. Predict expects newdata
which needs to be a data.frame
with a column of the inputs to your model (i.e. in this case a column called Input
). The formula
in nnet
is not to be built by literal calls on the data. It's symbolic, so it should be the names of the columns in your data. Additionally, the package you are using is not neuralnet
but nnet
.
Upvotes: 1