32 void CMulticlassLibSVM::register_params()
41 struct svm_model* model =
nullptr;
43 struct svm_node* x_space;
45 problem = svm_problem();
51 SG_INFO(
"%d trainlabels, %d classes\n", problem.l, num_classes)
58 SG_ERROR(
"Number of training vectors does not match number of " 64 problem.y=SG_MALLOC(
float64_t, problem.l);
65 problem.x=SG_MALLOC(
struct svm_node*, problem.l);
66 problem.pv=SG_MALLOC(
float64_t, problem.l);
67 problem.C=SG_MALLOC(
float64_t, problem.l);
69 x_space=SG_MALLOC(
struct svm_node, 2*problem.l);
71 for (int32_t i=0; i<problem.l; i++)
75 problem.x[i]=&x_space[2*i];
77 x_space[2*i+1].index=-1;
83 param.kernel_type = LINEAR;
96 param.weight_label = NULL;
100 const char* error_msg = svm_check_parameter(&problem,¶m);
105 model = svm_train(&problem, ¶m);
109 if (model->nr_class!=num_classes)
111 SG_ERROR(
"LibSVM model->nr_class=%d while num_classes=%d\n",
112 model->nr_class, num_classes);
114 ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef))
117 int32_t* offsets=SG_MALLOC(int32_t, num_classes);
120 for (int32_t i=1; i<num_classes; i++)
121 offsets[i] = offsets[i-1]+model->nSV[i-1];
124 for (int32_t i=0; i<num_classes; i++)
126 for (int32_t j=i+1; j<num_classes; j++)
131 if (model->label[i]>model->label[j])
134 int32_t num_sv=model->nSV[i]+model->nSV[j];
138 ASSERT(model->sv_coef[i] && model->sv_coef[j-1])
145 for (k=0; k<model->nSV[i]; k++)
147 SG_DEBUG(
"setting SV[%d] to %d\n", sv_idx,
148 model->SV[offsets[i]+k]->index);
150 svm->
set_alpha(sv_idx, sgn*model->sv_coef[j-1][offsets[i]+k]);
154 for (k=0; k<model->nSV[j]; k++)
156 SG_DEBUG(
"setting SV[%d] to %d\n", sv_idx,
157 model->SV[offsets[i]+k]->index);
159 svm->
set_alpha(sv_idx, sgn*model->sv_coef[i][offsets[j]+k]);
169 for (k=0; k<model->label[i]; k++)
170 idx+=num_classes-k-1;
172 for (l=model->label[i]+1; l<model->label[j]; l++)
177 for (k=0; k<model->label[j]; k++)
178 idx+=num_classes-k-1;
180 for (l=model->label[j]+1; l<model->label[i]; l++)
184 else if (num_classes == 3)
186 idx = model->label[j]+model->label[i] - 1;
188 else if (num_classes == 2)
198 SG_DEBUG(
"svm[%d] has %d sv (total: %d), b=%f " 199 "label:(%d,%d) -> svm[%d]\n",
200 s, num_sv, model->l, bias, model->label[i],
201 model->label[j], idx);
217 svm_destroy_model(model);
virtual bool init(CFeatures *lhs, CFeatures *rhs)
int32_t get_num_classes() const
virtual ELabelType get_label_type() const =0
multiclass one vs one strategy used to train generic multiclass machines for K-class problems with bu...
The class Labels models labels, i.e. class assignments of objects.
virtual int32_t get_num_labels() const =0
multi-class labels 0,1,...
virtual ~CMulticlassLibSVM()
virtual int32_t get_num_vectors() const =0
float64_t m_max_train_time
int32_t cache_size
cache_size in MB
LIBSVM_SOLVER_TYPE solver_type
Multiclass Labels for multi-class classification.
void set_objective(float64_t v)
void set_bias(float64_t bias)
CMulticlassStrategy * m_multiclass_strategy
bool set_alpha(int32_t idx, float64_t val)
bool set_support_vector(int32_t idx, int32_t val)
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
bool create_multiclass_svm(int32_t num_classes)
A generic Support Vector Machine Interface.
virtual bool train_machine(CFeatures *data=NULL)
bool set_svm(int32_t num, CSVM *svm)
CMulticlassLibSVM(LIBSVM_SOLVER_TYPE st=LIBSVM_C_SVC)