SEXP predictions_nn(SEXP X, SEXP n, SEXP p, SEXP theta, SEXP neurons,SEXP yhat, SEXP reqCores) { int i,j,k; double sum,z; int rows, columns, nneurons; double *pX, *ptheta, *pyhat; SEXP list; rows=INTEGER_VALUE(n); columns=INTEGER_VALUE(p); nneurons=INTEGER_VALUE(neurons); PROTECT(X=AS_NUMERIC(X)); pX=NUMERIC_POINTER(X); PROTECT(theta=AS_NUMERIC(theta)); ptheta=NUMERIC_POINTER(theta); PROTECT(yhat=AS_NUMERIC(yhat)); pyhat=NUMERIC_POINTER(yhat); for(i=0;i<rows;i++) { sum=0; for(k=0;k<nneurons;k++) { z=0; for(j=0;j<columns;j++) { z+=pX[i+(j*rows)]*ptheta[(columns+2)*k+j+2]; } z+=ptheta[(columns+2)*k+1]; sum+=ptheta[(columns+2)*k]*tansig(z); } pyhat[i]=sum; } PROTECT(list=allocVector(VECSXP,1)); SET_VECTOR_ELT(list,0,yhat); UNPROTECT(4); return(list); }
//*************************************************** NeuroNet *************************************************** //Нейронная сеть 3-10-1 //3 входа, 10 нейронов в скрытом (первом) слое, один нейрон в выходном (втором) слое float NeuroNet( float Nst, float Ps, float Pd) { float IN[3], sum, LW_1[10], LW_2; int i,j; IN[0]= Nst; //Ограничения по оборотам if (IN[0] <13500) IN[0]=13500; if (IN[0] >22500) IN[0]=22500; //Переводим из кгс/см2 в атм IN[1]= Ps / 0.010197 / 101.325; //Ограничения по входному давлению if (IN[1] <30) IN[1]=30; if (IN[1] >55) IN[1]=55; //Переводим из кгс/см2 в атм IN[2]= Pd / 0.010197 / 101.325; //Ограничения по выходному давлению if (IN[2] <40) IN[2]=40; if (IN[2] >160) IN[2]=160; //Первый слой for (j= 0; j<10; j++) { sum= 0; for (i= 0; i < 3; i++) sum= sum + IN[i] * W1[j][i]; sum= sum + off_1[j]; //выход i-го нейрона первого слоя LW_1[j]= tansig(sum); } //for j:= 1 to 10 do sum= 0; //Второй слой for (i= 0; i<10; i++) sum= sum + LW_1[i] * W2[i]; //выход нейрона второго слоя LW_2= sum + off_2; return LW_2; }
//This function will calculate the Jocobian for the errors SEXP jacobian_(SEXP X, SEXP n, SEXP p, SEXP theta, SEXP neurons,SEXP J, SEXP reqCores) { int i,j,k; double z,dtansig; int useCores, haveCores; double *pX; double *ptheta; double *pJ; int rows, columns, nneurons; SEXP list; rows=INTEGER_VALUE(n); columns=INTEGER_VALUE(p); nneurons=INTEGER_VALUE(neurons); PROTECT(X=AS_NUMERIC(X)); pX=NUMERIC_POINTER(X); PROTECT(theta=AS_NUMERIC(theta)); ptheta=NUMERIC_POINTER(theta); PROTECT(J=AS_NUMERIC(J)); pJ=NUMERIC_POINTER(J); /* Set the number of threads */ #ifdef _OPENMP //R_CStackLimit=(uintptr_t)-1; useCores=INTEGER_VALUE(reqCores); haveCores=omp_get_num_procs(); if(useCores<=0 || useCores>haveCores) useCores=haveCores; omp_set_num_threads(useCores); #endif #pragma omp parallel private(j,k,z,dtansig) { #pragma omp for schedule(static) for(i=0; i<rows; i++) { //Rprintf("i=%d\n",i); for(k=0; k<nneurons; k++) { z=0; for(j=0;j<columns;j++) { z+=pX[i+(j*rows)]*ptheta[(columns+2)*k+j+2]; } z+=ptheta[(columns+2)*k+1]; dtansig=pow(sech(z),2.0); /* Derivative with respect to the weight */ pJ[i+(((columns+2)*k)*rows)]=-tansig(z); /* Derivative with respect to the bias */ pJ[i+(((columns+2)*k+1)*rows)]=-ptheta[(columns+2)*k]*dtansig; /* Derivate with respect to the betas */ for(j=0; j<columns;j++) { pJ[i+(((columns+2)*k+j+2)*rows)]=-ptheta[(columns+2)*k]*dtansig*pX[i+(j*rows)]; } } } } PROTECT(list=allocVector(VECSXP,1)); SET_VECTOR_ELT(list,0,J); UNPROTECT(4); return(list); }
SEXP predictions_nn(SEXP X, SEXP n, SEXP p, SEXP theta, SEXP neurons,SEXP yhat, SEXP reqCores) { int i,j,k; double sum,z; int rows, columns, nneurons; int useCores, haveCores; double *pX, *ptheta, *pyhat; SEXP list; rows=INTEGER_VALUE(n); columns=INTEGER_VALUE(p); nneurons=INTEGER_VALUE(neurons); PROTECT(X=AS_NUMERIC(X)); pX=NUMERIC_POINTER(X); PROTECT(theta=AS_NUMERIC(theta)); ptheta=NUMERIC_POINTER(theta); PROTECT(yhat=AS_NUMERIC(yhat)); pyhat=NUMERIC_POINTER(yhat); /* Set the number of threads */ #ifdef _OPENMP //R_CStackLimit=(uintptr_t)-1; useCores=INTEGER_VALUE(reqCores); haveCores=omp_get_num_procs(); if(useCores<=0 || useCores>haveCores) useCores=haveCores; omp_set_num_threads(useCores); #endif #pragma omp parallel private(j,k,z,sum) { #pragma omp for schedule(static) for(i=0;i<rows;i++) { sum=0; for(k=0;k<nneurons;k++) { z=0; for(j=0;j<columns;j++) { z+=pX[i+(j*rows)]*ptheta[(columns+2)*k+j+2]; } z+=ptheta[(columns+2)*k+1]; sum+=ptheta[(columns+2)*k]*tansig(z); } pyhat[i]=sum; } } PROTECT(list=allocVector(VECSXP,1)); SET_VECTOR_ELT(list,0,yhat); UNPROTECT(4); return(list); }
//This function will calculate the Jocobian for the errors SEXP jacobian_(SEXP X, SEXP n, SEXP p, SEXP theta, SEXP neurons,SEXP J, SEXP reqCores) { int i,j,k; double z,dtansig; double *pX; double *ptheta; double *pJ; int rows, columns, nneurons; SEXP list; rows=INTEGER_VALUE(n); columns=INTEGER_VALUE(p); nneurons=INTEGER_VALUE(neurons); PROTECT(X=AS_NUMERIC(X)); pX=NUMERIC_POINTER(X); PROTECT(theta=AS_NUMERIC(theta)); ptheta=NUMERIC_POINTER(theta); PROTECT(J=AS_NUMERIC(J)); pJ=NUMERIC_POINTER(J); for(i=0; i<rows; i++) { //Rprintf("i=%d\n",i); for(k=0; k<nneurons; k++) { z=0; for(j=0;j<columns;j++) { z+=pX[i+(j*rows)]*ptheta[(columns+2)*k+j+2]; } z+=ptheta[(columns+2)*k+1]; dtansig=pow(sech(z),2.0); /* Derivative with respect to the weight */ pJ[i+(((columns+2)*k)*rows)]=-tansig(z); /* Derivative with respect to the bias */ pJ[i+(((columns+2)*k+1)*rows)]=-ptheta[(columns+2)*k]*dtansig; /* Derivate with respect to the betas */ for(j=0; j<columns;j++) { pJ[i+(((columns+2)*k+j+2)*rows)]=-ptheta[(columns+2)*k]*dtansig*pX[i+(j*rows)]; } } } PROTECT(list=allocVector(VECSXP,1)); SET_VECTOR_ELT(list,0,J); UNPROTECT(4); return(list); }