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");