📄 nnmisc.c
字号:
else {
*PI_vectorpp = mmake(iteration,1);
subvec(*PI_vectorpp,PI_vector,0,iteration-1);
}
*iter = iteration;
*lam = lambda;
mfree(L_hidden); mfree(H_hidden); mfree(L_output); mfree(H_output);
mfree(h1); mfree(h2); mfree(y1); mfree(y2);
mfree(E); mfree(E_new); mfree(E_vector); mfree(E_new_vector);
mfree(W1_new); mfree(W2_new); mfree(D);mfree(Dtmp); mfree(PI_vector);mfree(Htmp);
mfree(theta); mfree(thtmp); mfree(theta_index); mfree(theta_red); mfree(theta_red_new);
mfree(PSI); mfree(G); mfree(H); mfree(R); mfree(h);
mfree(all); mfree(index0); mfree(index7); mfree(onesvec); mfree(tmp0); mfree(tmp2);
mfree(tmp3); mfree(index); mfree(index2); mfree(PHI);
lt = time(NULL);
c = localtime(<);
printf("\n\nNetwork training ended at %.8s\n\n\n",asctime(c)+11);
}
/*
--------------------------------------------------------------------------------
---------------- END OF NETWORK TRAINING --------------
--------------------------------------------------------------------------------
*/
/********************************************************************************
* *
* SOME ADDITIONAL MATRIX FUNCTIONS *
* -------------------------------- *
* *
********************************************************************************/
/*
* MAT2SM
* ------
*
* Conversion of a (real) Matlab matrix into the SteffenMagnus format
*
* INPUT : MatlabMatrix - Matrix in Matlab form
* OUTPUT: SMmatrix - Matrix in SM form
*
*/
matrix *mat2sm(const mxArray *MatlabMatrix)
{
int rows, cols, i, j;
double *M;
matrix *SMmatrix;
rows = mxGetM(MatlabMatrix);
cols = mxGetN(MatlabMatrix);
M = mxGetPr(MatlabMatrix);
SMmatrix = mmake(rows,cols);
for(i=0;i<cols;i++)
{
for(j=0;j<rows;j++)
{
put_val(SMmatrix,j,i,M[j+i*rows]);
}
}
return SMmatrix;
}
matrix *matstring2sm(const mxArray *MatlabMatrix)
{
int rows, cols, i, j;
unsigned short *M;
matrix *SMmatrix;
rows = mxGetM(MatlabMatrix);
cols = mxGetN(MatlabMatrix);
M = (unsigned short*)mxGetPr(MatlabMatrix);
SMmatrix = mmake(rows,cols);
for(i=0;i<cols;i++)
{
for(j=0;j<rows;j++)
{
put_val(SMmatrix,j,i,(double)M[j+i*rows]);
}
}
return SMmatrix;
}
/*
* SM2MAT
* ------
*
* Conversion of a SteffenMagnus matrix to a (real) Matlab matrix format
*
* INPUTS: MatlabMatrix - Pointer to Matlab matrix
* SMmatrix - Pointer to SM matrix
*
*/
void sm2mat(mxArray *MatlabMatrix, matrix *SMmatrix)
{
int rows, cols, i, j;
double *M;
rows = mxGetM(MatlabMatrix);
cols = mxGetN(MatlabMatrix);
M = mxGetPr(MatlabMatrix);
for(i=0;i<cols;i++)
{
for(j=0;j<rows;j++)
{
M[j+i*rows]=get_val(SMmatrix,j,i);
}
}
mfree(SMmatrix);
}
/*
* MCOPYI
* ------
* Copy the elements in matrix B pointed out by the index vectors
* bri (rows) and bci (columns) to matrix A (to the locations
* specified by bri and bci)
*
*/
void mcopyi(matrix *A, matrix *ari, matrix *aci, matrix *B, matrix *bri,\
matrix *bci)
{
register int i,j, rows, cols;
rows = ari->row;
cols = aci->row;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
A->mat[(int)cvget(ari,i)][(int)cvget(aci,j)] = \
B->mat[(int)cvget(bri,i)][(int)cvget(bci,j)];
}
}
}
/*
* MTANH
* -----
* Computes tanh to the elements in matrix B pointed out by the
* index vectors bri (rows) and bci (columns), and place the result
* in matrix A (in the locations specified by ari and aci).
*
* INPUTS: A, B - Pointers to matrices
* ari, aci, bri, bci - Index vectors
*
*/
void mtanh(matrix *A, matrix *ari, matrix *aci, matrix *B, matrix *bri,\
matrix *bci)
{
register int i,j, rows, cols;
rows = ari->row;
cols = aci->row;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
A->mat[(int)cvget(ari,i)][(int)cvget(aci,j)] = \
tanh(B->mat[(int)cvget(bri,i)][(int)cvget(bci,j)]);
}
}
}
/*
* NEUVECTOR:
* ----------
* This function finds the locations of a given type
* of neurons in a row of the NetDef matrix.
*
* INPUTS: NetDef - Network definition string matrix
* layer - Layer of interest in NetDef
* neu - Neuron type to search for
*
*/
matrix *neuvector(matrix *NetDef,int layer, char neu)
{
matrix *tmp, *retvec, *NDef;
int i;
NDef = mmake(1,getcols(NetDef));
submat(NDef,NetDef,layer-1,layer-1,0,getcols(NDef)-1);
tmp = mfind(NDef,(double)neu);
if(getrows(tmp)==0)
{
retvec = mmake(1,1);
retvec->row=0;
retvec->col=0;
}
else
{
retvec = mmake(getrows(tmp),1);
for(i=0;i<getrows(tmp);i++) cvput(retvec,i,mget(tmp,i,1));
}
return retvec;
}
/*
* SSE
* ---
* Given an error matrix, the function finds the sum of
* squared error.
*
* INPUT: E - Error matrix [ny | N]
* OUTPUT: SSE - Sum of squared error
*/
double sse(matrix *E)
{
double SSE;
register int i,j;
SSE=0.0;
for(j=0;j<E->col;j++)
{
for(i=0;i<E->row;i++)
{
SSE+=E->mat[i][j] * E->mat[i][j];
}
}
return SSE;
}
/*
* DELTATANH
* ---------
*
* Calculates the deltas for tanh units
*
* INPUTS: delta - The return argument
* y - The output of the units
* E - The back propagated error
* H_vec - Indices to the layers tanh units
*/
void deltatanh(matrix *delta, matrix *y, matrix *E, matrix *H_vec)
{
register int i, j, N, veclen;
N = getcols(y);
veclen=getrows(H_vec);
for(i=0;i<veclen;i++)
{
for(j=0;j<N;j++)
{
put_val(delta, (int)cvget(H_vec,i), j,\
(1 - get_val(y,(int)cvget(H_vec,i),j) * get_val(y,(int)cvget(H_vec,i),j))\
*get_val(E,(int)cvget(H_vec,i),j));
}
}
}
/*
* MMULTR1
* -------
*
* Matrix multiplication ptm1 = ptm2'*ptm3
*
* Input: *ptm1 - Pointer to result matrix (Not equal to *ptm1 or *ptm2)
* *ptm2 - Pointer to first argument matrix (the one to be
* transposed)
* *ptm3 - Pointer to second argument matrix
*
*/
void mmultr1( matrix *ptm1, matrix *ptm2, matrix *ptm3 )
{
register int i,j,k;
#if RUNCHK
if ((ptm1==ptm2) || (ptm1==ptm3)) \
merror("Memory conflict error in mmultr1!");
if ( !( (ptm2->row == ptm3->row) && \
(ptm2->col == ptm1->row) && (ptm3->col == ptm1->col) ) ) \
merror("Dimension mismatch error in mmultr1!");
#endif
for ( i=0; i < ptm2->col; i++ )
{
for ( j=0; j < ptm3->col; j++ )
{
ptm1->mat[i][j] = 0.0;
for ( k=0; k < ptm2->col; k++ )
{
ptm1->mat[i][j] += ptm2->mat[k][i] * ptm3->mat[k][j];
}
}
}
}
/*
* MMULTR2
* -------
*
* Matrix multiplication ptm1 = ptm2*ptm3'
*
* Input: *ptm1 - Pointer to result matrix (Not equal to *ptm1 or *ptm2)
* *ptm2 - Pointer to first argument matrix
* *ptm3 - Pointer to second argument matrix (the one to
* be transposed)
*
*/
void mmultr2( matrix *ptm1, matrix *ptm2, matrix *ptm3 )
{
register int i,j,k;
#if RUNCHK
if ((ptm1==ptm2) || (ptm1==ptm3)) \
merror("Memory conflict error in mmultr1!");
if ( !( (ptm2->col == ptm3->col) && \
(ptm2->row == ptm1->row) && (ptm3->row == ptm1->col) ) ) \
merror("Dimension mismatch error in mmultr2!");
#endif
for ( i=0; i < ptm2->row; i++ )
{
for ( j=0; j < ptm3->row; j++ )
{
ptm1->mat[i][j] = 0.0;
for ( k=0; k < ptm2->col; k++ )
{
ptm1->mat[i][j] += ptm2->mat[i][k] * ptm3->mat[j][k];
}
}
}
}
/* SPROD3 :
* --------
* This function calculates the scalar-product of two COLUMN vectors of equal
* length.
* Inputs : ptv1 - Pointer to first factor vector.
* ptv2 - Pointer to second factor vector.
* Output : prod - Scalar product of the two vectors.
*/
double sprod3( matrix* ptv1, matrix* ptv2 )
{
register double prod = 0.0;
register int i, elements;
#if RUNCHK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -