I am attempting to use optim to solve a neural network problem.  I would like to optimize coefficients that are currently stored in a matrix

Y=270 x 1
X= 27- x 14
b1= 10x14
b2= 11x1
V= 10 x 14 set of prior variances.

I have the following function:

posterior.mode1=function(y,X,b_0,b2,V) {

  log.like=function(b1) {
    a_g=compute(b1)
    z_g=tanh(a_g);
    z_g=cbind(1,z_g)
    p=softmax(z_g%*%b2);
    a=sum(y*log(p)+(1-y)*log(1-p));
    return(a);
  }

  compute=function(b1) {
    a_g=NULL;
    for(i in 1:nrow(b1)){
      a_g=cbind(a_g,X%*%b1[i,])
    }
    return(a_g);
  }

  log.posterior=function(b1) {
    -log.like(b1)+1/2*t(as.vector(b1))%*%diag(as.vector(V))%*%as.vector(b1)
  }

  a=optim(b_0,log.posterior,method="CG",hessian=TRUE)
  return(a);
}

When I run

posterior.mode1(y,X,b1,b2,b1)



I get the following error



Error in 1:nrow(b1) : argument of length 0

Any comments would be very helpful

Thanks



	[[alternative HTML version deleted]]

