| ExamplesOfnn {lazy.nntools} | R Documentation |
Examples of nntools package: nonlinear regression and supervised classification
## Not run:
#
# The following packages are required:
library(neuralnet), library(NeuralNetTools), library(nlsr)
#######################################################################
#######################################################################
# Bivariate Nonlinear Regression Example
#######################################################################
#######################################################################
if(0){
library(lazy.mat)
library(neuralnet)
library(NeuralNetTools)
library(nlsr)
library(lazy.nntools)
rm(list=ls())
}
# get data from lazy.nntools::data_1_2
X=matrix(data_1_2$x1,,1)
Y=as.matrix( data_1_2[,c("y1","y2")] )
Y0=data_1_2[,c("y01","y02")]
# plot of data
matplot(X,Y0, type="l", pch=1:2, main="data", ylim=c(-11,11))
points(X,Y[,1])
points(X,Y[,2], pch=2, col=2)
# # of neurons in the hidden layers as a vector
hidden=7
hidden=c(3,3) # GN fails
hidden=c(2,9) # OK neuralnet fails
hidden=c(2,2,2,2)
hidden=c(4,7,4) # OK GN fails
hidden=c(9) # OK
hidden=c(7,7,7) # OK
hidden=7 # OK
# # of neurons including first and the last layer
n=nrow(Y); np=ncol(Y); nq=ncol(X)
nn=c(nq,hidden,np)
Layer=paste(nn,collapse=":")
nlayer=length(nn)
nparam=0
for( i in 2:nlayer ) nparam=nparam+(nn[i-1]+1)*nn[i]
# Solution by the neuralnet package
# requires neuralnet package
title=paste("Neural Network by neuralnet::neuralnet: layer =", Layer)
dd <- data.frame(x1=X, Y )
set.seed(1701)
resnn <- neuralnet( y1+y2~x1, dd, hidden=hidden, stepmax=1000000
, threshold=0.05, rep=1)
# pick up the best result
locbest=which.min(resnn$result.matrix[1,])
# pick up the weight as a list from the result
W=getW_from_neuralnet( resnn$weights[[locbest]] )
rss0=resnn$result.matrix[1,locbest]*2
Yhat=resnn$net.result[[locbest]]
# calculate rss again from W list
temp=summary_nn( W, X, Y, linear=1, softmax=0 )
rss=temp$rss; rmse=temp$rmse; rmse1=temp$rmse1
Yhat=temp$Yhat
maxag=temp$maxag
Print(rss0, rss, rmse, rmse1)
Print(maxag)
title2=paste(title, " rss =", round(rss,4))
# plot of neural net
plot_nn(W, type=1, title=title2)
plot_nn(W, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse1,3),collapse=", "), collapse="" )
matplot(X, Yhat, type="l", pch=1:2, main=title2, sub=title3, ylim=c(-11,11))
points(X,Y[,1])
points(X,Y[,2], pch=2, col=2)
# get initial weight of neuralnet
W0=getW_from_neuralnet( resnn$startweights[[locbest]] )
#
#
# Solution by the Gauss-Nowton Method
#
#
# Solution by nlsr::nlfb
title=paste("Neural Network by nlsr::nlfb: layer =", Layer)
# initialize the storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W1=init$W
# initial value of W
winit=unlist(W0)
if(0){
# when W0 is not good enough.
set.seed(1701)
winit=runif(nparam)
}
# use nlfb with yhatmy_nn
if(0){
# by nlfb w/o jacobian
resnlsr=nlfb( winit, yhatmy_nn, W=W1, O=O, Y=Y, control=list(femax=1000
, japprox="jacentral") )
}
resnlsr=nlfb( winit, yhatmy_nn, W=W1, O=O, Y=Y, control=list(femax=1000)
, jacfn=Jac_nn_a2 )
# pick up the weight as a list from the result
W1=reshapeW( resnlsr$coefficients, nn )
W1=mapply( function(x,y){dimnames(x)=y; x}, W1, wname)
rssnlfb=resnlsr$ssquares
title2=paste(title, " rss =", round(rssnlfb,4))
# calculate rss again from W list
temp=summary_nn( W1, X, Y, linear=1, softmax=0 )
rss1=temp$rss; rmse1=temp$rmse; rmse11=temp$rmse1
Yhat1=temp$Yhat
maxag1=temp$maxag
Print(rssnlfb, rss1, rmse1, rmse11)
Print(maxag1)
title2=paste(title, " rss =", round(rss1,4))
# plot of neural net
plot_nn(W1, type=1, title=title2)
plot_nn(W1, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse1,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse11,3),collapse=", "), collapse="" )
matplot(X, Yhat1, type="l", pch=1:2, main=title2, sub=title3, ylim=c(-11,11))
points(X,Y[,1])
points(X,Y[,2], pch=2, col=2)
# Solution by lazy.mat::GN
title=paste("Neural Network by lazy.mat::GN: layer =", Layer)
# initialize the storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W2=init$W
# initial value of W
winit=unlist(W0)
if(0){
# when W0 is not good enough.
set.seed(1701)
winit=runif(nparam)
}
# resGN=GN( winit, ymyhat_nn, W=W, O=O, k=0, maxiter=1000 )
resGN=GN( winit, ymyhat_nn, W=W2, O=O, Y=Y, linear=1, softmax=0
, maxiter=1000, jacobian=Jac_nn_a )
W2=reshapeW( resGN$par, nn )
W2=mapply( function(x,y){dimnames(x)=y; x}, W2, wname)
rssGN=resGN$objective
# calculate rss again from W list
temp=summary_nn( W2, X, Y, linear=1, softmax=0 )
rss2=temp$rss; rmse2=temp$rmse; rmse21=temp$rmse1
Yhat2=temp$Yhat
maxag2=temp$maxag
Print(rssGN, rss2, rmse2, rmse21)
Print(maxag2)
title2=paste(title, " rss =", round(rss2,4))
# plot of neural net
plot_nn(W2, type=1, title=title2)
plot_nn(W2, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse2,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse21,3),collapse=", "), collapse="" )
matplot(X, Yhat2, type="l", pch=1:2, main=title2, sub=title3, ylim=c(-11,11))
points(X,Y[,1])
points(X,Y[,2], pch=2, col=2)
#
#
# Solution by the Newton-Raphson Method
#
#
# Solution by nlminb
title=paste("Neural Network by nlminb: layer =", Layer)
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W1=init$W
# initial value
winit=unlist(W0)
if(0){
# when W0 is not good enough.
set.seed(1701)
winit=runif(nparam)
}
# nlminb
resnlminb=nlminb( winit, rss_nn, W=W1, O=O, Y=Y, control=list(iter.max=1000)
, gradient=drss_nn_aJ )
# pick up the weight as a list from the result
W1=reshapeW( resnlminb$par, nn )
W1=mapply( function(x,y){dimnames(x)=y; x}, W1, wname)
rssnlminb=resnlminb$objective
title2=paste(title, " rss =", round(rssnlminb,4))
# calculate rss again from W list
temp=summary_nn( W1, X, Y, linear=1, softmax=0 )
rss1=temp$rss; rmse1=temp$rmse; rmse11=temp$rmse1
Yhat1=temp$Yhat
maxag1=temp$maxag
Print(rssnlminb, rss1, rmse1, rmse11)
Print(maxag1)
title2=paste(title, " rss =", round(rss1,4))
# plot of neural net
plot_nn(W1, type=1, title=title2)
plot_nn(W1, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse1,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse11,3),collapse=", "), collapse="" )
matplot(X, Yhat1, type="l", pch=1:2, main=title2, sub=title3, ylim=c(-11,11))
points(X,Y[,1])
points(X,Y[,2], pch=2, col=2)
# Solution by lazy.mat::NR
title=paste("Neural Network by lazy.mat::NR: layer =", Layer)
# initialize the storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W2=init$W
# initial value
winit=unlist(W0)
if(0){
# when W0 is not good enough.
set.seed(1701)
winit=runif(nparam)
}
# resNR=NR( winit, ymyhat_nn, W=W, O=O, k=0, maxiter=1000 )
resNR=NR( winit, rss_nn, W=W2, O=O, Y=Y, linear=1, softmax=0
, maxiter=1000, gradient=drss_nn_a, flipd=1 )
W2=reshapeW( resNR$par, nn )
W2=mapply( function(x,y){dimnames(x)=y; x}, W2, wname)
rssNR=resNR$objective
# calculate rss again from W list
temp=summary_nn( W2, X, Y, linear=1, softmax=0 )
rss2=temp$rss; rmse2=temp$rmse; rmse21=temp$rmse1
Yhat2=temp$Yhat
maxag2=temp$maxag
Print(rssNR, rss2, rmse2, rmse21)
Print(maxag2)
title2=paste(title, " rss =", round(rss2,4))
# plot of neural net
plot_nn(W2, type=1, title=title2)
plot_nn(W2, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse2,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse21,3),collapse=", "), collapse="" )
matplot(X, Yhat2, type="l", pch=1:2, main=title2, sub=title3, ylim=c(-11,11))
points(X,Y[,1])
points(X,Y[,2], pch=2, col=2)
#######################################################################
#######################################################################
# Supervised Classification Example
#######################################################################
#######################################################################
if(0){
library(lazy.mat)
library(neuralnet)
library(NeuralNetTools)
library(nlsr)
library(lazy.nntools)
rm(list=ls())
}
###
### Discriminant analysis or Supervised Classification
### Multivariate X, Multivariate Y (three classes)
###
# get data from lazy.nntools::data_2_3
X=as.matrix(data_2_3[,c("x1","x2")])
Y=as.matrix( data_2_3[,c("y1","y2","y3")] )
class_true=data_2_3$class
# for plot by image
x1=unique(X[,1])
x2=unique(X[,2])
image(x1, x2, matrix(class_true,length(x1))
, main="data")
# # of neurons in the hidden layers as a vector
hidden=c(3,3) # NG
hidden=c(3,5,3) # OK, use random for nlfb and GN
hidden=c(4,4) # OK
# # of neurons including first and the last layer
n=nrow(Y); np=ncol(Y); nq=ncol(X)
nn=c(nq,hidden,np)
Layer=paste(nn,collapse=":")
nlayer=length(nn)
nparam=0
for( i in 2:nlayer ) nparam=nparam+(nn[i-1]+1)*nn[i]
# Solution by the neuralnet package
# requires neuralnet package
title=paste("Neural Network by neuralnet::neuralnet: layer =", Layer)
# neural network by neuralnet package
# require neuralnet package
dd <- data.frame(X, Y )
set.seed(1701)
resnn <- neuralnet( y1+y2+y3~x1+x2, dd, hidden=hidden, stepmax=1000000
, linear.output=FALSE
, threshold=0.005, rep=3)
# pick up the best result
locbest=which.min(resnn$result.matrix[1,])
# pick up the weight as a list from the result
W=getW_from_neuralnet( resnn$weights[[locbest]] )
rss0=resnn$result.matrix[1,locbest]*2
Yhat=resnn$net.result[[locbest]]
# calculate rss again from W list
temp=summary_nn( W, X, Y, linear=0, softmax=0 )
rss=temp$rss; rmse=temp$rmse; rmse1=temp$rmse1
Yhat=temp$Yhat
class=apply(Yhat,1,which.max)
maxag=temp$maxag
Print(rss0, rss, rmse, rmse1)
Print(maxag)
title2=paste(title, " rss =", round(rss,4))
# plot of neural net
plot_nn(W, type=1, title=title2)
plot_nn(W, type=2, title=title2)
# plot the result
title3=paste( paste("rmse = ", round(rmse,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse1,3),collapse=", "), collapse="" )
image(x1, x2, matrix(class,length(x1)), main=title2, sub=title3)
# get initial weight of neuralnet
W0=getW_from_neuralnet( resnn$startweights[[locbest]] )
#
#
# Solution by the Gauss-Newton Method
#
#
# Solution by nlsr::nlfb
title=paste("Neural Network by nlsr::nlfb: layer =", Layer)
# initialize the storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W1=init$W
# initial value of W
winit=unlist(W0)
if(0){
# when W0 is not good enough.
set.seed(1701)
winit=runif(nparam)
}
if(0){
resnlsr=nlfb( winit, yhatmy_nn, W=W1, O=O, Y=Y, linear=0
, control=list(femax=1000, japprox="jacentral") )
}
resnlsr=nlfb( winit, yhatmy_nn, W=W1, O=O, Y=Y, linear=0
, control=list(femax=1000)
, jacfn=Jac_nn_a2 )
# pick up the weight as a list from the result
W1=reshapeW( resnlsr$coefficients, nn )
W1=mapply( function(x,y){dimnames(x)=y; x}, W1, wname)
rssnlfb=resnlsr$ssquares
# calculate rss again from W list
temp=summary_nn( W1, X, Y, linear=0, softmax=0 )
rss1=temp$rss; rmse1=temp$rmse; rmse11=temp$rmse1
Yhat1=temp$Yhat
class1=apply(Yhat1,1,which.max)
maxag1=temp$maxag
Print(rssnlfb, rss1, rmse1, rmse11)
Print(maxag1)
title2=paste(title, " rss =", round(rss1,4))
# plot of neural net
plot_nn(W1, type=1, title=title2)
plot_nn(W1, type=2, title=title2)
title3=paste( paste("rmse = ", round(rmse1,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse11,3),collapse=", "), collapse="" )
image(x1, x2, matrix(class1,length(x1)), main=title2, sub=title3)
# Solution by lazy.mat::GN
title=paste("Neural Network by lazy.mat::GN: layer =", Layer)
# initialize the storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W2=init$W
# initial value of W
winit=unlist(W0)
if(0){
# when W0 is not good enough.
set.seed(1701)
winit=runif(nparam)
}
# resGN=GN( winit, ymyhat_nn, W=W, O=O, k=0, maxiter=1000 )
resGN=GN( winit, ymyhat_nn, W=W2, O=O, Y=Y, linear=0, softmax=0
, maxiter=1000, jacobian=Jac_nn_a )
W2=reshapeW( resGN$par, nn )
W2=mapply( function(x,y){dimnames(x)=y; x}, W2, wname)
rssGN=resGN$objective
# calculate rss again from W list
temp=summary_nn( W2, X, Y, linear=0, softmax=0 )
rss2=temp$rss; rmse2=temp$rmse; rmse21=temp$rmse1
Yhat2=temp$Yhat
class2=apply(Yhat2,1,which.max)
maxag2=temp$maxag
Print(rssGN, rss2, rmse2, rmse21)
Print(maxag2)
title2=paste(title, " rss =", round(rss2,4))
# plot of neural net
plot_nn(W2, type=1, title=title2)
plot_nn(W2, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse2,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse21,3),collapse=", "), collapse="" )
image(x1, x2, matrix(class2,length(x1)), main=title2)
#
#
# Newton Raphson Method
#
#
# neural network by nlminb
title=paste("Neural Network by nlminb: layer =", Layer)
# initialization of storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W1=init$W
# initial value of W
winit=unlist(W0)
if(0){
# replace W0 by random numbers
set.seed(1701)
winit=runif(nparam)
}
# Solution by nlminb
if(0){
# by nlminb w/o jacobian
resnlminb=nlminb( winit, rss_nn, W=W1, O=O, Y=Y, control=list(iter.max=1000) )
}
resnlminb=nlminb( winit, rss_nn, W=W1, O=O, Y=Y, control=list(iter.max=1000)
, gradient=drss_nn_aJ )
# pick up the weight as a list from the result
W1=reshapeW( resnlminb$par, nn )
W1=mapply( function(x,y){dimnames(x)=y; x}, W1, wname)
rssnlminb=resnlminb$objective
title2=paste(title, " rss =", round(rssnlminb,4))
# calculate rss again from W list
temp=summary_nn( W1, X, Y, linear=1, softmax=0 )
rss1=temp$rss; rmse1=temp$rmse; rmse11=temp$rmse1
Yhat1=temp$Yhat
maxag1=temp$maxag
Print(rssnlminb, rss1, rmse1, rmse11)
Print(maxag1)
title2=paste(title, " rss =", round(rss1,4))
# plot of neural net
plot_nn(W1, type=1, title=title2)
plot_nn(W1, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse1,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse11,3),collapse=", "), collapse="" )
image(x1, x2, matrix(class1,length(x1)), main=title2, sub=title3)
# Solution by lazy.mat::NR
title=paste("Neural Network by lazy.mat::NR: layer =", Layer)
# initialize storage
init=initialize_nn( X, nn)
# needs empty O and W
O=init$O
wname=init$wname
W2=init$W
# initial value of W
winit=unlist(W0)
if(0){
# when W0 is not good enough
set.seed(1701)
winit=runif(nparam)
}
# resNR=NR( winit, ymyhat_nn, W=W, O=O, k=0, maxiter=1000 )
resNR=NR( winit, rss_nn, W=W2, O=O, Y=Y, linear=1, softmax=0
, maxiter=1000, gradient=drss_nn_a, flipd=1 )
W2=reshapeW( resNR$par, nn )
W2=mapply( function(x,y){dimnames(x)=y; x}, W2, wname)
rssNR=resNR$objective
# calculate rss again from W list
temp=summary_nn( W2, X, Y, linear=1, softmax=0 )
rss2=temp$rss; rmse2=temp$rmse; rmse21=temp$rmse1
Yhat2=temp$Yhat
maxag2=temp$maxag
Print(rssNR, rss2, rmse2, rmse21)
Print(maxag2)
title2=paste(title, " rss =", round(rss2,4))
# plot of neural net
plot_nn(W2, type=1, title=title2)
plot_nn(W2, type=2, title=title2)
# plot of X vs Yhat
title3=paste( paste("rmse = ", round(rmse1,3),": ", sep="")
, paste( paste("rmse-",1:ncol(Y)," = ",sep="")
, round(rmse11,3),collapse=", "), collapse="" )
image(x1, x2, matrix(class1,length(x1)), main=title2, sub=title3)
## End(Not run)