Interpolate between functions a (i) and b (j) in a neural network

I am trying to use the nnet function in R to approximate the function of two variables y (a, b). Both variables, “a” and “b, are known at fixed points:

a={a(1),a(2),...,a(i),...a(n)} and b={b(1),b(2),...,b(i),...b(n)}. 

The value of the function "y" is known for each pair a (i), b (i), i.e.

 y(a,b)={y(a(1),b(1)),y(a(2),b(2),...y(a(i),b(i),...y(a(n),b(n)}. 

I do

 res=nnet(y~a+b,size=X,maxit=M,linout=TRUE), 

where X and M. are defined

How to get the result value between points, for example, with some a-value that is between the values ​​(i) and a (i + 1) and b, which is between b (j) and b (j + 1)?

+4
source share
1 answer

This seems like the correct naming problem. I suggest using data.frame to avoid confusion.

 library(nnet) X <- 20 # Number of neurons in hidden layer M <- 1e4 # Maximum number of iterations N <- 50 # Length of data vectors set.seed(1) ## Train set df.train <- data.frame(a=rnorm(N,5,1), b=rnorm(N,10,2)) df.train$y <- with(df.train, 5*a-2*b+rnorm(N)) ## Test set for interpolation a1 <- sapply(2:N, function(i) with(df.train, (a[i]+a[i-1])/2)) b1 <- sapply(2:N, function(i) with(df.train, (b[i]+b[i-1])/2)) df.test <- data.frame(a=a1, b=b1) 

The above code creates two data.frame s:

  • df.train with columns a , b and y .
  • df.test with columns a and b .

Note that the column names for the variables must match as in data.frame s.

Training:

 res <- nnet(y~a+b, data=df.train, size=X, maxit=M, linout=TRUE) p1 <- predict(res, df.train) 

Here p1 is the forecast for the training data.

Interpolation:

 p2 <- predict(res, df.test) 

Visually, the interpolation looks quite natural:

 library(ggplot2) ggplot() + geom_line(aes(x,y), data=data.frame(x=1:N, y=df.train$y)) + # original y geom_point(aes(x,y), data=data.frame(x=1.5:N, y=p2), colour="red") # interpolations 

enter image description here

0
source

All Articles