简体   繁体   中英

Trouble with Neuralnet package in R

I'm trying to compute a neural network with the package neuralnet, to solve a regression problem. I'm trying to approximate the function: f(x1,x2) = sqrt(x1) + sin(x2) + x1*x2.

here is my code:

library(neuralnet)
library(scatterplot3d)
X1 <- as.data.frame(runif(1000, min = 0 , max = 100))
X2 <- as.data.frame(runif(1000, min = 0 , max = 100))
input <- cbind(X1,X2)
sortie <- sqrt(X1) + sin(X2) + X1*X2
donnee <- cbind(sortie,input)
colnames(donnee) <- c("sortie","entree1","entree2")

f <- as.formula(sortie ~ entree1 + entree2)
net.f <- neuralnet(f , donnee, hidden = c(10,10,10) ,linear.output = FALSE)

here is the code to look at the scatterplot of the outputs of the neural networks:

  abscisse1 <- 0:100
  abscisse2 <- 0:100
  net.abscisseformule <- compute(net.f , cbind(abscisse1,abscisse2))
  neuralsortie <-  c(net.abscisseformule$net.result)
  scatterplot3d(abscisse1,abscisse2,neuralsortie)

I'm pretty sure that the result is wrong because the scatterplot doesn't looks like the scatterplot of the function f. I thonk that the problem comes from the line

f <-as.formula(sortie ~ entree1 + entree2)

here is the code to look at the scatterplot of the function

x <- seq(0, 100, 1)
y <- seq(0, 100, 1)
z <- sqrt(x) + sin(y) +x*y
scatterplot3d(x,y,z)

this is the graph of f https://i.stack.imgur.com/HkpbG.png

this is the graph of the outputs of the neuralnet https://i.stack.imgur.com/N38dd.png

Can somebody give me a piece of advice ? Many Thanks !

I find the answer to my question. According to the book The Elements of Statistical Learning (by Friedman,Tibshirani and Hastie), when solving a regression problem it is required to use the identity function in the last layer of the neural network. Which mean that the output is a linear combination of the previous layer. In order to do so with R, it is required to set "linear.output" to TRUE and not FALSE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM