📄 ginikernel.cpp
字号:
for ( GINI_u32 i = 0; i < dim; i++) { currval += a[i]*b[i]; } } else { // In sparse format the first dimension stores the length // of the vector. while (( count1+1 <= (GINI_u32)(2*a[0]) ) && ( count2+1 <= (GINI_u32)(2*b[0]))) { while (( a[count1] < b[count2] ) && ( count1+1 <= (GINI_u32)(2*a[0]))) { count1 += 2; } while (( b[count2] < a[count1] ) && ( count2+1 <= (GINI_u32)(2*b[0]))) { count2 += 2; } if ( b[count2] == a[count1] ) { currval += a[count1+1]*b[count2+1]; count2+=2; count1+=2; } } } return pow(offset + scale*currval,power);}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_bool GINI_PolyKernel::Write( FILE* output ){ GINISVMKernelType ktype = GetId(); if ( fwrite(&ktype,sizeof(GINISVMKernelType),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&scale,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&offset,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&power,sizeof(GINI_u32),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&sparseflag,sizeof(GINI_bool),1,output) == 0 ) { return GINI_FALSE; } return GINI_TRUE;}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_bool GINI_PolyKernel::Read( FILE* output ){ GINISVMKernelType ktype; if ( fread(&ktype,sizeof(GINISVMKernelType),1,output) == 0 ) { return GINI_FALSE; } if ( ktype != GetId()) { return GINI_FALSE; } if ( fread(&scale,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fread(&offset,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fread(&power,sizeof(GINI_u32),1,output) == 0 ) { return GINI_FALSE; } GINI_bool inpflag; if ( fread(&inpflag,sizeof(GINI_bool),1,output) == 0 ) { return GINI_FALSE; } if ( inpflag != sparseflag ) { printf("Data format mismatch !! Both SV and Test data should be sparse/non-sparse\n"); return GINI_FALSE; } printf("Poly Kernel with parameters = (%4.5f,%4.5f,%5d)\n", scale,offset,power); return GINI_TRUE;}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_DTKKernel::GINI_DTKKernel( GINI_double inpscale, GINI_double inppenaltyid, GINI_double inppenaltysame, GINI_double inppenaltysb ){ scale = inpscale; penaltyid= inppenaltyid; penaltysb = inppenaltysb; penaltysame = inppenaltysame;}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_double GINI_DTKKernel::Value( GINI_double* a, GINI_double* b, GINI_u32 dim){ // For this kernel dim = 0 and the first element of the // vectors a and b store the dimensionality. GINI_u32 dim1 = (GINI_u32)a[0]; GINI_u32 dim2 = (GINI_u32)b[0]; GINI_u32 i,j; GINI_double Sdown,Sside,Sdiag; // To remove compiler warnings i = dim; // Play a dangerous game where we change // the vectors and then replace later on. a[0] = 0; b[0] = 0; GINI_double **Sarray = new (GINI_double*)[dim1+1]; for (i = 0; i < dim1+1; i++ ) { Sarray[i] = new GINI_double[dim2+1]; } // Initialize corner penalties Sarray[0][0] = 0.0; // Now initialize the row and the column // elements. for ( i =1; i < dim2+1; i++ ) { if ( fabs(b[i]-b[i-1]) < 0.001 ) { Sarray[0][i] = Sarray[0][i-1] + penaltysame; } else { Sarray[0][i] = Sarray[0][i-1] + penaltyid; } } for ( i =1; i < dim1+1; i++ ) { if ( fabs(a[i]-a[i-1]) < 0.001 ) { Sarray[i][0] = Sarray[i-1][0] + penaltysame; } else { Sarray[i][0] = Sarray[i-1][0] + penaltyid; } } for ( i = 1; i< dim1+1; i++) { for ( j=1; j< dim2+1; j++ ) { if ( fabs(a[i] - a[i-1]) < 0.001 ) { Sdown = Sarray[i-1][j] + penaltysame; } else { Sdown = Sarray[i-1][j] + penaltyid; } if ( fabs(b[j] - b[j-1]) < 0.001 ) { Sside = Sarray[i][j-1] + penaltysame; } else { Sside = Sarray[i][j-1] + penaltyid; } if (fabs(a[i]-b[j]) < 0.001) { Sdiag = Sarray[i-1][j-1]; } else { Sdiag = Sarray[i-1][j-1]+penaltysb; } // Find the minimum of all the three // scores. if ((Sdown < Sdiag) && (Sdown < Sside)) { Sarray[i][j] = Sdown; } else { if ( Sdiag < Sside) { Sarray[i][j] = Sdiag; } else { Sarray[i][j] = Sside; } } } } GINI_double result = Sarray[dim1][dim2]; // Now delete all the memory for ( i =0; i < dim1+1; i++ ) { delete [] Sarray[i]; } delete [] Sarray; // Replace the damage we did before a[0] = dim1; b[0] = dim2; return exp(-scale*result);}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_bool GINI_DTKKernel::Write( FILE* output ){ GINISVMKernelType ktype = GetId(); if ( fwrite(&ktype,sizeof(GINISVMKernelType),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&scale,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&penaltyid,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&penaltysame,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&penaltysb,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } return GINI_TRUE;}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_bool GINI_DTKKernel::Read( FILE* output ){ GINISVMKernelType ktype; if ( fread(&ktype,sizeof(GINISVMKernelType),1,output) == 0 ) { return GINI_FALSE; } if ( ktype != GetId()) { return GINI_FALSE; } if ( fread(&scale,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fread(&penaltyid,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fread(&penaltysame,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fread(&penaltysb,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } printf("DTK Kernel with parameters = (%4.5f,%4.5f,%4.5f,%4.5f)\n", scale,penaltyid,penaltysame,penaltysb); return GINI_TRUE;}/*****************************************************************************/// FUNCTION :// // DESCRIPTION : //// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_TanhKernel::GINI_TanhKernel( GINI_double inpscale, GINI_bool inpflag ){ scale = inpscale; sparseflag = inpflag; // cout << "Using a Tanh kernel with"<<endl; }/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_double GINI_TanhKernel::Value( GINI_double* a, GINI_double* b, GINI_u32 dim){ GINI_double currval = 0.0; GINI_u32 count1 = 1; GINI_u32 count2 = 1; if ( sparseflag == GINI_FALSE ) { for ( GINI_u32 i = 0; i < dim; i++) { currval += a[i]*b[i]; } } else { // In sparse format the first dimension stores the length // of the vector. while (( count1+1 <= (GINI_u32)(2*a[0]) ) && ( count2+1 <= (GINI_u32)(2*b[0]))) { while (( a[count1] < b[count2] ) && ( count1+1 <= (GINI_u32)(2*a[0]))) { count1 += 2; } while (( b[count2] < a[count1] ) && ( count2+1 <= (GINI_u32)(2*b[0]))) { count2 += 2; } if ( b[count2] == a[count1] ) { currval += a[count1+1]*b[count2+1]; count2+=2; count1+=2; } } } return tanh(scale*currval);}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_bool GINI_TanhKernel::Write( FILE* output ){ GINISVMKernelType ktype = GetId(); if ( fwrite(&ktype,sizeof(GINISVMKernelType),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&scale,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } if ( fwrite(&sparseflag,sizeof(GINI_bool),1,output) == 0 ) { return GINI_FALSE; } return GINI_TRUE;}/*****************************************************************************/// FUNCTION :// // DESCRIPTION ://// INPUT ://// OUTPUT ://///*****************************************************************************/GINI_bool GINI_TanhKernel::Read( FILE* output ){ GINISVMKernelType ktype; if ( fread(&ktype,sizeof(GINISVMKernelType),1,output) == 0 ) { return GINI_FALSE; } if ( ktype != GetId()) { printf("Configuration file contains kernel with id = %d\n",ktype); return GINI_FALSE; } if ( fread(&scale,sizeof(GINI_double),1,output) == 0 ) { return GINI_FALSE; } GINI_bool inpflag; if ( fread(&inpflag,sizeof(GINI_bool),1,output) == 0 ) { return GINI_FALSE; } if ( inpflag != sparseflag ) { printf("Data format mismatch !! Both SV and Test data should be sparse/non-sparse\n"); return GINI_FALSE; } printf("Tanh Kernel with parameters = (%4.5f)\n",scale); return GINI_TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -