bips-hb / neuralnet

Training of Neural Networks
31 stars 12 forks source link

It does not work when I normalize both x and y #46

Open yang2021aa opened 10 months ago

yang2021aa commented 10 months ago

I just did a simple simulation and find It does not work when I normalize both x and y

I will appreciate if anyone can help me.

I present the code below.

library(neuralnet) library(MASS)

########plots ########plots ########plots ########plots

N=200

model=2 d=5 N0=50

N_train=N-N0

x=matrix(rnorm(Nd),N,d) u=rnorm(N) if(model==1){ y=0.4x[,1]+0.4x[,2]+u } if(model==2){ y=x[,1]^2+x[,1]x[,1]+u }

x_train=x[1:N_train,] x_test=x[(N_train+1):T,] y_train=y[1:N_train]

if(model==1){ theta0=0.4x_test[,1]+0.4x_test[,2] } ###theta0 is the true value if(model==2){ theta0=x_test[,1]^2+x_test[,1]*x_test[,1] }

##################only normalize y

max_y=max(y_train) min_y=min(y_train)

y_train_NL=(y_train-min_y)/(max_y-min_y)

Xy_train=cbind(x_train, y_train_NL)

Xy_train=as.data.frame(Xy_train)

colnames(Xy_train)[d+1]<-'y_tr'

nn_phi=neuralnet(y_tr~., data=Xy_train, hidden=3, threshold=0.05, linear.output = F )

 theta_vec1=predict(nn_phi, x_test  )*(max_y-min_y)+min_y

################## normalize both x and y

minx <- apply(x_train, 2, min) maxx <- apply(x_train, 2, max)

x_train_NL <- scale(x_train, center=minx,scale=maxx-minx)

x_test_NL<- scale(x_test, center=minx,scale=maxx-minx)

max_y=max(y_train) min_y=min(y_train)

y_train_NL=(y_train-min_y)/(max_y-min_y)

Xy_train=cbind(x_train_NL, y_train_NL)

Xy_train=as.data.frame(Xy_train)

colnames(Xy_train)[d+1]<-'y_tr'

nn_phi=neuralnet(y_tr~., data=Xy_train, hidden=3, threshold=0.05, linear.output = F )

 theta_vec2=predict(nn_phi, x_test_NL  )*(max_y-min_y)+min_y

par(mfrow=c(1,2)) plot(theta0, theta_vec1) abline(0,1) plot(theta0, theta_vec2) abline(0,1)

mnwright commented 9 months ago

Isn't this a regression problem and you should set linear.output = TRUE?