# [R] R: optim problems and neural networks

Clark Allan Allan at STATS.uct.ac.za
Mon Dec 13 17:16:57 CET 2004

```hi all

i have two questions:

1. i have been having problems with using the "optim" function. I have
tried to define the matrix containing the parameters as a matrix, ie not
simply p*1. The function does not allow this. Am i correct?

2. in the code below i have tried to calculate the weights of a basic
feed forward neural network (I know that i could use the nnet package.).
the aim is to learn how to code and become familiar with the language.
note that the code still has to be generalised and does not allow for
skip layers. No error checking on the parameter estimates are included
yet. when i run the above code, i get the following error, "Error in
fn(par, ...) : recursive default argument reference ". why does this
happen? As a simple case, use x<-c(1,2,3) and y<-2+3*x. I know that
there is no errors. I simply want the code to work at this stage.

NNET<-function(p=2,size=1,skip=T,x,y)
{

p<-p

x<-x
x<-as.matrix(x)
x<-cbind(rep(1,nrow(x)),x)

y<-y

size<-size
skip<-skip
betas<-matrix(1:((p+1)*size),nrow=1+p,ncol=size)

f3<-function (betas,x,y,p=p)
{

#b1<-matrix(betas,nrow=p,ncol=size)

#the input to hidden layer section
hl<-x%*%betas[1:p,]

hl_act<-(exp(hl)-1)/(1+exp(hl))

#b2<-matrix(betas[(1+p*size):((1+p)*size)],nrow=size)

#the hidden layer to output section
ol<-hl_act%*%betas[p+1,]

# the square of the residuals
e<-(y-ol)

fit_nn<-(ol)

# the sums of squares / 2
E<-sum(e*e)/2
}

optim(c(rnorm((p+1)*size)),f3,x=x,y=y,hessian=T,method=c("BFGS"))
}

NNET(p=2,x=x,y=y)
```