CFLOAT kernel(KERNEL_PARM *kernel_parm, DOC *a, DOC *b) /* calculate the kernel function */ { // double k1; kernel_cache_statistic++; switch(kernel_parm->kernel_type) { case 0: /* linear */ return(CFLOAT)sprod(a, b); case 1: /* polynomial */ return ((CFLOAT)pow(kernel_parm->coef_lin*sprod(a, b) +kernel_parm->coef_const,(double)kernel_parm->poly_degree)); case 2: /* radial basis function */ return((CFLOAT)exp(-kernel_parm->rbf_gamma*(a->twonorm_sq-2*sprod(a,b)+b->twonorm_sq))); case 3: /* sigmoid neural net */ return((CFLOAT)tanh(kernel_parm->coef_lin*sprod(a,b)+kernel_parm->coef_const)); case 4: /* custom-kernel supplied in file kernel.h */ return((CFLOAT)custom_kernel(kernel_parm,a,b)); case 5: /* combine kernels */ return ((CFLOAT)advanced_kernels(kernel_parm,a,b)); case 11: /* Re-ranking of predicate argument structures only trees, i.e. re-ranking using trees */ return((CFLOAT)SRL_re_ranking_CoNLL2006(kernel_parm,a,b)); case 12: /* Re-ranking of predicate argument structures only trees + vectors */ return((CFLOAT)SRL_re_ranking_CoNLL2006(kernel_parm,a,b)); case 13: /* Re-ranking of predicate argument structures only vectors */ return((CFLOAT)SRL_re_ranking_CoNLL2006(kernel_parm,a,b)); case 14: /* Classification of Entailment pairs */ return((CFLOAT)ACL2006_Entailment_kernel(kernel_parm,a,b)); default: /* Advanced vectorial kernels*/ printf("\nNo kernel corresponding to -t = %ld option \n",kernel_parm->kernel_type); exit(-1); /*if (b->num_of_trees > 0 && a->num_of_trees>0) { printf("\n tree 1: <"); writeTreeString(a->forest_vec[0]->root); printf(">\n tree 2: <"); writeTreeString(b->forest_vec[0]->root);printf(">\n"); printf("Kernel :%1.20lf \n",k1); }*/ } }
CFLOAT kernel(KERNEL_PARM *kernel_parm, DOC *a, DOC *b) /* calculate the kernel function */ { // double k1; kernel_cache_statistic++; /* Start - added by Faisal on 26-Apr-2011 */ double tkd = 0; /* End - added by Faisal on 26-Apr-2011 */ switch(kernel_parm->kernel_type) { case 0: /* linear */ tkd = sprod(a, b); //printf("\nAfter computing 1 vector kernel, tkd = %f option \n", tkd); return(CFLOAT) tkd; case 1: /* polynomial */ return ((CFLOAT)pow(kernel_parm->coef_lin*sprod(a, b) +kernel_parm->coef_const,(double)kernel_parm->poly_degree)); case 2: /* radial basis function */ return((CFLOAT)exp(-kernel_parm->rbf_gamma*(a->twonorm_sq-2*sprod(a,b)+b->twonorm_sq))); case 3: /* sigmoid neural net */ return((CFLOAT)tanh(kernel_parm->coef_lin*sprod(a,b)+kernel_parm->coef_const)); case 4: /* custom-kernel supplied in file kernel.h */ return((CFLOAT)custom_kernel(kernel_parm,a,b)); case 5: /* combine kernels */ tkd = advanced_kernels(kernel_parm,a,b); //printf("\nAfter merging 1 vector and 1 tree kernels, tkd = %f option \n", tkd); return ((CFLOAT) tkd); case 6: /* Tree kernels on u*/ return tree_kernel(kernel_parm, a, b, atoi(kernel_parm->custom), atoi(kernel_parm->custom)); case 11: /* Re-ranking of predicate argument structures only trees, i.e. re-ranking using trees */ return((CFLOAT)SRL_re_ranking_CoNLL2006(kernel_parm,a,b)); case 12: /* Re-ranking of predicate argument structures only trees + vectors */ return((CFLOAT)SRL_re_ranking_CoNLL2006(kernel_parm,a,b)); case 13: /* Re-ranking of predicate argument structures only vectors */ return((CFLOAT)SRL_re_ranking_CoNLL2006(kernel_parm,a,b)); case 14: /* Classification of Entailment pairs */ return((CFLOAT)ACL2006_Entailment_kernel(kernel_parm,a,b)); case 15: /* Classification of Entailment pairs */ return (CFLOAT)JHU_KERNELS(kernel_parm, a, b); case 16: /* Classification of Entailment pairs */ return((CFLOAT)ACL2008_Entailment_kernel(kernel_parm,a,b)); case 17: return (CFLOAT)ACL2008(kernel_parm, a, b); case 30: /* Classification of Entailment pairs */ return Question_Answer_Classification(kernel_parm, a, b); case 51: /* Re-ranking of predicate argument structures only trees, i.e. re-ranking using trees */ return((CFLOAT) SRL2008(kernel_parm, a, b)); case 52: /* Re-ranking of predicate argument structures only trees + vectors */ return((CFLOAT) SRL2008(kernel_parm, a, b)); case 53: /* Re-ranking of predicate argument structures only vectors */ return((CFLOAT) SRL2008(kernel_parm, a, b)); case 100: /* Use only the tree whose index is specified in the custom parameter */ return((CFLOAT) tree_kernel(kernel_parm, a, b, atoi(kernel_parm->custom),atoi(kernel_parm->custom))); /* Start - added by Faisal on 26-Apr-2011 */ case 54: /* - custom-kernel supplied in file kernel.h - combine kernels */ tkd = custom_kernel(kernel_parm,a,b); kernel_parm->combination_type = 'V'; return ((CFLOAT)( advanced_kernels(kernel_parm,a,b) + kernel_parm->tree_constant*tkd )); /* End - added by Faisal on 26-Apr-2011 */ /* Start - added by Faisal on 27-Apr-2011 This is virtually same as the 54. The only difference is that, computation of vector kernel is called first instead of tree kernel. However, the results should be same. */ case 45: /* - combine kernels - custom-kernel supplied in file kernel.h */ kernel_parm->combination_type = 'V'; tkd = advanced_kernels(kernel_parm,a,b); kernel_parm->combination_type = 'T'; return ((CFLOAT)( tkd + kernel_parm->tree_constant*custom_kernel(kernel_parm,a,b) )); /* End - added by Faisal on 27-Apr-2011 */ /* Start - added by Faisal on 24-Aug-2011 */ case 50: /* - combine two feature vector kernels */ // kernel_parm->second_kernel=0; return (CFLOAT)( basic_kernel(kernel_parm, a, b, 0, 0) + basic_kernel(kernel_parm, a, b, 1, 1) ); /* End - added by Faisal on 24-Aug-2011 */ /* Start - added by Faisal on 24-Aug-2011 * / case 501: /* - combine 1 vector and 1 tree kernels * / kernel_parm->combination_type = 'T'; tkd = ((CFLOAT)advanced_kernels(kernel_parm,a,b)); kernel_parm->second_kernel=0; kernel_parm->combination_type = 'V'; int totalVect = 1, v=0; // Faisal: hard coded float kernel_val = 0; for ( v=0; v<totalVect; v++ ) kernel_val+=(CFLOAT) basic_kernel(kernel_parm, a, b, v, v); return (CFLOAT)( kernel_val + kernel_parm->tree_constant*tkd ); /* End - added by Faisal on 24-Aug-2011 */ /* Start - added by Faisal on 24-Aug-2011 */ case 502: /* - combine 2 vectors and 1 tree kernels */ // step 1 - merge 1 vector and 1 tree kernel /* tkd = ((CFLOAT)advanced_kernels(kernel_parm,a,b)); //printf("\nAfter merging 1 vector and 1 tree kernels, tkd = %f option \n", tkd); kernel_parm->second_kernel=0; kernel_parm->combination_type = 'V'; // step 2 - compute 2nd vector kernel and merge it with previously computed outcome float kernel_val = (CFLOAT) basic_kernel(kernel_parm, a, b, 1, 1); //printf("\nAfter computing 2nd vector kernel, kernel_val = %f option \n", kernel_val); kernel_val = (CFLOAT)( kernel_val + tkd ); // printf("\ntkd + kernel_val = %f option \n", kernel_val); return (CFLOAT)( kernel_val ); */ tkd = basic_kernel(kernel_parm, a, b, 0, 0) + basic_kernel(kernel_parm, a, b, 1, 1) + kernel_parm->tree_constant*tree_kernel(kernel_parm, a, b, 0, 0); return (CFLOAT)( tkd ); /* End - added by Faisal on 24-Aug-2011 */ default: /* Advanced vectorial kernels*/ printf("\nNo kernel corresponding to -t = %ld option \n",kernel_parm->kernel_type); exit(-1); /*if (b->num_of_trees > 0 && a->num_of_trees>0) { printf("\n tree 1: <"); writeTreeString(a->forest_vec[0]->root); printf(">\n tree 2: <"); writeTreeString(b->forest_vec[0]->root);printf(">\n"); printf("Kernel :%1.20lf \n",k1); }*/ } }