Changeset 3531:026914bc603e in orange
 Timestamp:
 04/13/07 12:22:56 (7 years ago)
 Branch:
 default
 Convert:
 8d15a4816396bf73576f950d4faf7c691d77f9f9
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

source/orange/svm.cpp
r3451 r3531 37 37 #endif 38 38 39 /* CHANGES TO LIBSVM2.8 140 *#include "svm.h" to #include"svm. hpp"39 /* CHANGES TO LIBSVM2.83 40 *#include "svm.h" to #include"svm.ppp" 41 41 *commented the swap function definition due to conflict with std::swap 42 42 *added #ifdef around min max definitions … … 57 57 #include <string.h> 58 58 #include <stdarg.h> 59 #include <errno.h>60 59 #include "svm.ppp" 61 60 typedef float Qfloat; 62 61 typedef signed char schar; 63 #if _MSC_VER!=0 && _MSC_VER<130064 62 #ifndef min 65 63 template <class T> inline T min(T x,T y) { return (x<y)?x:y; } … … 69 67 #endif 70 68 //template <class T> inline void swap(T& x, T& y) { T t=x; x=y; y=t; } 71 #endif72 69 template <class S, class T> inline void clone(T*& dst, S* src, int n) 73 70 { 74 71 dst = new T[n]; 75 72 memcpy((void *)dst,(void *)src,sizeof(T)*n); 73 } 74 inline double powi(double base, int times) 75 { 76 double tmp = base, ret = 1.0; 77 78 for(int t=times; t>0; t/=2) 79 { 80 if(t%2==1) ret*=tmp; 81 tmp = tmp * tmp; 82 } 83 return ret; 76 84 } 77 85 #define INF HUGE_VAL … … 260 268 // svm_parameter 261 269 const int kernel_type; 262 const doubledegree;270 const int degree; 263 271 const double gamma; 264 272 const double coef0; … … 272 280 double kernel_poly(int i, int j) const 273 281 { 274 return pow (gamma*dot(x[i],x[j])+coef0,degree);282 return powi(gamma*dot(x[i],x[j])+coef0,degree); 275 283 } 276 284 double kernel_rbf(int i, int j) const … … 281 289 { 282 290 return tanh(gamma*dot(x[i],x[j])+coef0); 291 } 292 double kernel_precomputed(int i, int j) const 293 { 294 return x[i][(int)(x[j][0].value)].value; 283 295 } 284 296 double kernel_custom(int i, int j) const … … 308 320 kernel_function = &Kernel::kernel_sigmoid; 309 321 break; 322 case PRECOMPUTED: 323 kernel_function = &Kernel::kernel_precomputed; 324 break; 310 325 case CUSTOM: 311 326 kernel_function = &Kernel::kernel_custom; … … 360 375 return dot(x,y); 361 376 case POLY: 362 return pow (param.gamma*dot(x,y)+param.coef0,param.degree);377 return powi(param.gamma*dot(x,y)+param.coef0,param.degree); 363 378 case RBF: 364 379 { … … 404 419 case SIGMOID: 405 420 return tanh(param.gamma*dot(x,y)+param.coef0); 421 case PRECOMPUTED: //x: test (validation), y: SV 422 return x[(int)(y>value)].value; 406 423 case CUSTOM: 407 424 { 408 while(y>index!=1) 425 while(y>index!=1) // the value in the last svm_node is the index of the training example 409 426 ++y; 410 427 while(x>index!=1) … … 2073 2090 svm_destroy_model(submodel); 2074 2091 svm_destroy_param(&subparam); 2075 free(subprob.x);2076 free(subprob.y);2077 }2092 } 2093 free(subprob.x); 2094 free(subprob.y); 2078 2095 } 2079 2096 sigmoid_train(prob>l,dec_values,prob>y,probA,probB); … … 2588 2605 2589 2606 int p=0; 2590 int pos=0;2591 2607 for(i=0;i<nr_class;i++) 2592 2608 for(int j=i+1;j<nr_class;j++) … … 2605 2621 for(k=0;k<cj;k++) 2606 2622 sum += coef2[sj+k] * kvalue[sj+k]; 2607 sum = model>rho[p++]; 2608 dec_values[pos++] = sum; 2623 sum = model>rho[p]; 2624 dec_values[p] = sum; 2625 p++; 2609 2626 } 2610 2627 … … 2704 2721 const char *kernel_type_table[]= 2705 2722 { 2706 "linear","polynomial","rbf","sigmoid", NULL2723 "linear","polynomial","rbf","sigmoid","precomputed",NULL 2707 2724 }; 2708 2725 … … 2718 2735 2719 2736 if(param.kernel_type == POLY) 2720 fprintf(fp,"degree % g\n", param.degree);2737 fprintf(fp,"degree %d\n", param.degree); 2721 2738 2722 2739 if(param.kernel_type == POLY  param.kernel_type == RBF  param.kernel_type == SIGMOID) … … 2779 2796 2780 2797 const svm_node *p = SV[i]; 2781 while(p>index != 1) 2782 { 2783 fprintf(fp,"%d:%.8g ",p>index,p>value); 2784 p++; 2785 } 2798 2799 if(param.kernel_type == PRECOMPUTED) 2800 fprintf(fp,"0:%d ",(int)(p>value)); 2801 else 2802 while(p>index != 1) 2803 { 2804 fprintf(fp,"%d:%.8g ",p>index,p>value); 2805 p++; 2806 } 2786 2807 fprintf(fp, "\n"); 2787 2808 } 2788 2789 fclose(fp); 2790 return 0; 2809 if (ferror(fp) != 0  fclose(fp) != 0) return 1; 2810 else return 0; 2791 2811 } 2792 2812 … … 2856 2876 } 2857 2877 else if(strcmp(cmd,"degree")==0) 2858 fscanf(fp,"% lf",¶m.degree);2878 fscanf(fp,"%d",¶m.degree); 2859 2879 else if(strcmp(cmd,"gamma")==0) 2860 2880 fscanf(fp,"%lf",¶m.gamma); … … 2911 2931 else 2912 2932 { 2913 fprintf(stderr,"unknown text in model file \n");2933 fprintf(stderr,"unknown text in model file: [%s]\n",cmd); 2914 2934 free(model>rho); 2915 2935 free(model>label); … … 2974 2994 x_space[j++].index = 1; 2975 2995 } 2976 2977 fclose(fp); 2996 if (ferror(fp) != 0  fclose(fp) != 0) return NULL; 2978 2997 2979 2998 model>free_sv = 1; // XXX … … 3015 3034 return "unknown svm type"; 3016 3035 3017 // kernel_type 3036 // kernel_type, degree 3018 3037 3019 3038 int kernel_type = param>kernel_type; … … 3022 3041 kernel_type != RBF && 3023 3042 kernel_type != SIGMOID && 3043 kernel_type != PRECOMPUTED && 3024 3044 kernel_type != CUSTOM) 3025 3045 return "unknown kernel type"; 3046 3047 if(param>degree < 0) 3048 return "degree of polynomial kernel < 0"; 3026 3049 3027 3050 // cache_size,eps,C,nu,p,shrinking … … 3300 3323 for(i=0;i<nr_class;i++) 3301 3324 nSV>at(i)=model>nSV[i]; 3302 //printf("nSV");3303 3325 3304 3326 coef=mlnew TFloatListList(nr_class1);
Note: See TracChangeset
for help on using the changeset viewer.