21 using namespace shogun;
25 model(NULL), norm_wc(NULL), norm_wcw(NULL), rho(0), m_num_classes(0)
32 norm_wc(NULL), norm_wcw(NULL), rho(0), m_num_classes(0)
38 norm_wc(NULL), norm_wcw(NULL), rho(0), m_num_classes(0)
59 SG_ERROR(
"Number of training vectors does not match number of labels\n")
66 for (int32_t i=0; i<num_vectors; i++)
70 int32_t Nmin=num_vectors;
87 result=train_no_bias_libsvm();
92 result=train_no_bias_svmlight();
100 SG_INFO(
"valid nu interval [%f ... %f]\n", nu_min, nu_max)
103 SG_ERROR(
"nu out of valid range [%f ... %f]\n", nu_min, nu_max)
105 result=train_testrule12();
113 bool CScatterSVM::train_no_bias_libsvm()
115 struct svm_node* x_space;
122 x_space=SG_MALLOC(struct svm_node, 2*
problem.l);
124 for (int32_t i=0; i<
problem.l; i++)
128 x_space[2*i].index=i;
129 x_space[2*i+1].index=-1;
132 int32_t weights_label[2]={-1,+1};
138 param.svm_type=C_SVC;
139 param.kernel_type = LINEAR;
154 param.weight_label = weights_label;
155 param.weight = weights;
159 const char* error_msg = svm_check_parameter(&
problem,&
param);
165 m_kernel->set_normalizer(prev_normalizer);
170 ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef))
180 for (int32_t i=0; i<m_num_classes; i++)
182 int32_t num_sv=model->nSV[i];
186 norm_wcw[i]=model->normwcw[i];
189 for (int32_t j=0; j<num_sv; j++)
203 SG_FREE(model->SV[i]);
206 svm_destroy_model(model);
219 bool CScatterSVM::train_no_bias_svmlight()
223 m_num_classes-1, -1,
m_labels, prev_normalizer);
232 norm_wcw = SG_MALLOC(float64_t, m_num_classes);
237 for (int32_t i=0; i<num_sv; i++)
246 #endif //USE_SVMLIGHT
248 bool CScatterSVM::train_testrule12()
250 struct svm_node* x_space;
252 SG_INFO(
"%d trainlabels\n", problem.l)
254 problem.y=SG_MALLOC(float64_t, problem.l);
255 problem.x=SG_MALLOC(struct svm_node*, problem.l);
256 x_space=SG_MALLOC(struct svm_node, 2*problem.l);
258 for (int32_t i=0; i<problem.l; i++)
261 problem.x[i]=&x_space[2*i];
262 x_space[2*i].index=i;
263 x_space[2*i+1].index=-1;
266 int32_t weights_label[2]={-1,+1};
272 param.svm_type=NU_MULTICLASS_SVC;
273 param.kernel_type = LINEAR;
285 param.weight_label = weights_label;
286 param.weight = weights;
290 const char* error_msg = svm_check_parameter(&problem,¶m);
295 model = svm_train(&problem, ¶m);
299 ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef))
301 ASSERT(model->nr_class==m_num_classes)
307 norm_wcw = SG_MALLOC(float64_t,
m_machines->get_num_elements());
309 for (int32_t i=0; i<m_num_classes; i++)
311 int32_t num_sv=model->nSV[i];
315 norm_wcw[i]=model->normwcw[i];
318 for (int32_t j=0; j<num_sv; j++)
332 SG_FREE(model->SV[i]);
335 svm_destroy_model(model);
347 void CScatterSVM::compute_norm_wc()
360 for (int32_t i=0; i<num_sv; i++)
363 for (int32_t j=0; j<num_sv; j++)
382 SG_ERROR(
"SVM can not proceed without kernel!\n")
397 for (int32_t i=0; i<num_vectors; i++)
403 float64_t* outputs=SG_MALLOC(float64_t, num_vectors*m_num_classes);
406 for (int32_t i=0; i<num_vectors; i++)
414 float64_t s= (label==c) ? (m_num_classes-1) : (-1);
420 for (int32_t i=0; i<num_vectors; i++)
423 float64_t max_out=outputs[i*m_num_classes+0];
427 float64_t out=outputs[i*m_num_classes+j];
441 #endif //USE_SVMLIGHT
455 outputs[i]=svm->
apply();
459 for (int32_t i=0; i<num_vectors; i++)
515 outputs[j]/=norm_wcw[j];
517 float64_t max_out=outputs[0];
520 if (outputs[j]>max_out)
532 #endif //USE_SVMLIGHT
540 if (outputs[i]>max_out)