15 using namespace shogun;
18 :
CSVM(0), multiclass_type(ONE_VS_REST), m_num_svms(0), m_svms(NULL)
24 :
CSVM(0), multiclass_type(type), m_num_svms(0), m_svms(NULL)
31 :
CSVM(C, k, lab), multiclass_type(type), m_num_svms(0), m_svms(NULL)
41 void CMultiClassSVM::init()
44 "multiclass_type",
"Type of MultiClassSVM.");
46 "Number of classes.");
74 SG_ERROR(
"unknown multiclass type\n");
104 SG_ERROR(
"unknown multiclass type\n");
117 SG_ERROR(
"SVM can not proceed without kernel!\n");
125 result=
new CLabels(num_vectors);
133 SG_INFO(
"num_svms:%d svm[%d]=0x%0X\n", m_num_svms, i,
m_svms[i]);
140 for (int32_t v=0; v<num_vectors; v++)
157 int32_t max_votes=votes[0];
161 if (votes[i]>max_votes)
188 SG_ERROR(
"SVM can not proceed without kernel!\n");
196 result=
new CLabels(num_vectors);
209 for (int32_t i=0; i<num_vectors; i++)
244 SG_ERROR(
"unknown multiclass type\n");
259 if (outputs[i]>max_out)
290 int32_t max_votes=votes[0];
294 if (votes[i]>max_votes)
309 char char_buffer[1024];
312 int32_t line_number=1;
317 if (fscanf(modelfl,
"%15s\n", char_buffer)==EOF)
318 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
321 char_buffer[15]=
'\0';
322 if (strcmp(
"%MultiClassSVM", char_buffer)!=0)
323 SG_ERROR(
"error in multiclass svm file, line nr:%d\n", line_number);
329 if (fscanf(modelfl,
" multiclass_type=%d; \n", &int_buffer) != 1)
330 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
339 if (fscanf(modelfl,
" num_classes=%d; \n", &int_buffer) != 1)
340 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
346 SG_ERROR(
"less than 2 classes - how is this multiclass?\n");
351 if (fscanf(modelfl,
" num_svms=%d; \n", &int_buffer) != 1)
352 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
358 SG_ERROR(
"Mismatch in number of svms: m_num_svms=%d vs m_num_svms(file)=%d\n",
m_num_svms, int_buffer);
360 if (fscanf(modelfl,
" kernel='%s'; \n", char_buffer) != 1)
361 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
369 if (fscanf(modelfl,
"\n%4s %d of %d\n", char_buffer, &svm_idx, &int_buffer)==EOF)
372 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
377 if (strncmp(
"%SVM", char_buffer, 4)!=0)
380 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
384 SG_ERROR(
"svm index mismatch n=%d, n(file)=%d\n", n, svm_idx);
390 if (fscanf(modelfl,
"numsv%d=%d;\n", &svm_idx, &int_buffer) != 2)
391 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
394 SG_ERROR(
"svm index mismatch n=%d, n(file)=%d\n", n, svm_idx);
399 SG_INFO(
"loading %ld support vectors for svm %d\n",int_buffer, svm_idx);
404 if (fscanf(modelfl,
" b%d=%lf; \n", &svm_idx, &double_buffer) != 2)
405 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
408 SG_ERROR(
"svm index mismatch n=%d, n(file)=%d\n", n, svm_idx);
415 if (fscanf(modelfl,
"alphas%d=[\n", &svm_idx) != 1)
416 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
419 SG_ERROR(
"svm index mismatch n=%d, n(file)=%d\n", n, svm_idx);
429 if (fscanf(modelfl,
"\t[%lf,%d]; \n", &double_buffer, &int_buffer) != 2)
430 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
439 if (fscanf(modelfl,
"%2s", char_buffer) == EOF)
442 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
447 if (strcmp(
"];", char_buffer)!=0)
450 SG_ERROR(
"error in svm file, line nr:%d\n", line_number);
472 SG_ERROR(
"Multiclass SVM not trained!\n");
474 SG_INFO(
"Writing model file...");
475 fprintf(modelfl,
"%%MultiClassSVM\n");
478 fprintf(modelfl,
"num_svms=%d;\n",
m_num_svms);
485 fprintf(modelfl,
"\n%%SVM %d of %d\n", i, m_num_svms-1);
487 fprintf(modelfl,
"b%d=%+10.16e;\n",i,svm->
get_bias());
489 fprintf(modelfl,
"alphas%d=[\n", i);
493 fprintf(modelfl,
"\t[%+10.16e,%d];\n",
497 fprintf(modelfl,
"];\n");
virtual const char * get_name() const =0
int32_t get_num_support_vectors()
CLabels * classify_one_vs_one()
virtual float64_t get_label(int32_t i)
bool set_label(int32_t idx, float64_t label)
bool save(FILE *svm_file)
The class Labels models labels, i.e. class assignments of objects.
virtual CLabels * classify_one_vs_rest()
virtual float64_t classify_example_one_vs_rest(int32_t num)
virtual int32_t get_num_vec_lhs()
bool create_multiclass_svm(int32_t num_classes)
float64_t classify_example_one_vs_one(int32_t num)
void add(bool *param, const char *name, const char *description="")
bool set_svm(int32_t num, CSVM *svm)
Class SGObject is the base class of all shogun objects.
void set_bias(float64_t bias)
bool set_alpha(int32_t idx, float64_t val)
EMultiClassSVM multiclass_type
float64_t get_label(int32_t idx)
float64_t get_alpha(int32_t idx)
bool load(FILE *svm_file)
bool set_support_vector(int32_t idx, int32_t val)
int32_t get_support_vector(int32_t idx)
virtual int32_t get_num_vec_rhs()
virtual CLabels * apply()
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
virtual ~CMultiClassSVM()
A generic Support Vector Machine Interface.
virtual CLabels * apply()
void set_kernel(CKernel *k)
#define SG_MALLOC(type, len)