19 using namespace shogun;
23 model(NULL), norm_wc(NULL), norm_wcw(NULL), rho(0), m_num_classes(0)
30 norm_wc(NULL), norm_wcw(NULL), rho(0), m_num_classes(0)
36 norm_wc(NULL), norm_wcw(NULL), rho(0), m_num_classes(0)
57 SG_ERROR(
"Number of training vectors does not match number of labels\n")
64 for (int32_t i=0; i<num_vectors; i++)
68 int32_t Nmin=num_vectors;
85 result=train_no_bias_libsvm();
93 SG_INFO(
"valid nu interval [%f ... %f]\n", nu_min, nu_max)
96 SG_ERROR(
"nu out of valid range [%f ... %f]\n", nu_min, nu_max)
98 result=train_testrule12();
106 bool CScatterSVM::train_no_bias_libsvm()
108 struct svm_node* x_space;
115 x_space=SG_MALLOC(struct svm_node, 2*
problem.l);
117 for (int32_t i=0; i<
problem.l; i++)
121 x_space[2*i].index=i;
122 x_space[2*i+1].index=-1;
125 int32_t weights_label[2]={-1,+1};
131 param.svm_type=C_SVC;
132 param.kernel_type = LINEAR;
147 param.weight_label = weights_label;
148 param.weight = weights;
152 const char* error_msg = svm_check_parameter(&
problem,&
param);
158 m_kernel->set_normalizer(prev_normalizer);
163 ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef))
173 for (int32_t i=0; i<m_num_classes; i++)
175 int32_t num_sv=model->nSV[i];
179 norm_wcw[i]=model->normwcw[i];
182 for (int32_t j=0; j<num_sv; j++)
196 SG_FREE(model->SV[i]);
199 svm_destroy_model(model);
213 bool CScatterSVM::train_testrule12()
215 struct svm_node* x_space;
217 SG_INFO(
"%d trainlabels\n", problem.l)
219 problem.y=SG_MALLOC(float64_t, problem.l);
220 problem.x=SG_MALLOC(struct svm_node*, problem.l);
221 x_space=SG_MALLOC(struct svm_node, 2*problem.l);
223 for (int32_t i=0; i<problem.l; i++)
226 problem.x[i]=&x_space[2*i];
227 x_space[2*i].index=i;
228 x_space[2*i+1].index=-1;
231 int32_t weights_label[2]={-1,+1};
237 param.svm_type=NU_MULTICLASS_SVC;
238 param.kernel_type = LINEAR;
250 param.weight_label = weights_label;
251 param.weight = weights;
255 const char* error_msg = svm_check_parameter(&problem,¶m);
260 model = svm_train(&problem, ¶m);
264 ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef))
266 ASSERT(model->nr_class==m_num_classes)
272 norm_wcw = SG_MALLOC(float64_t,
m_machines->get_num_elements());
274 for (int32_t i=0; i<m_num_classes; i++)
276 int32_t num_sv=model->nSV[i];
280 norm_wcw[i]=model->normwcw[i];
283 for (int32_t j=0; j<num_sv; j++)
297 SG_FREE(model->SV[i]);
300 svm_destroy_model(model);
312 void CScatterSVM::compute_norm_wc()
325 for (int32_t i=0; i<num_sv; i++)
328 for (int32_t j=0; j<num_sv; j++)
347 SG_ERROR(
"SVM can not proceed without kernel!\n")
362 for (int32_t i=0; i<num_vectors; i++)
379 outputs[i]=svm->
apply();
383 for (int32_t i=0; i<num_vectors; i++)
439 outputs[j]/=norm_wcw[j];
441 float64_t max_out=outputs[0];
444 if (outputs[j]>max_out)
459 if (outputs[i]>max_out)
virtual float64_t apply_one(int32_t num)
virtual bool init(CFeatures *lhs, CFeatures *rhs)
int32_t get_num_support_vectors()
virtual bool train_machine(CFeatures *data=NULL)
virtual ELabelType get_label_type() const =0
Real Labels are real-valued labels.
virtual float64_t apply_one(int32_t num)
virtual int32_t get_num_labels() const
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 bool set_normalizer(CKernelNormalizer *normalizer)
CDynamicObjectArray * m_machines
virtual int32_t get_num_vectors() const =0
float64_t kernel(int32_t idx_a, int32_t idx_b)
int32_t get_num_elements() const
virtual int32_t get_num_vec_lhs()
int32_t cache_size
cache_size in MB
bool set_label(int32_t idx, float64_t label)
Multiclass Labels for multi-class classification.
virtual CKernelNormalizer * get_normalizer()
void set_bias(float64_t bias)
CMulticlassStrategy * m_multiclass_strategy
bool set_alpha(int32_t idx, float64_t val)
SCATTER_TYPE scatter_type
float64_t get_alpha(int32_t idx)
the scatter kernel normalizer
bool set_support_vector(int32_t idx, int32_t val)
static void fill_vector(T *vec, int32_t len, T value)
The class Kernel Normalizer defines a function to post-process kernel values.
int32_t get_support_vector(int32_t idx)
virtual int32_t get_num_vec_rhs()
int32_t get_num_classes() const
training with bias using test rule 2
The class Features is the base class of all feature objects.
training with bias using test rule 1
bool create_multiclass_svm(int32_t num_classes)
static T min(T a, T b)
return the minimum of two integers
A generic Support Vector Machine Interface.
void set_kernel(CKernel *k)
multiclass one vs rest strategy used to train generic multiclass machines for K-class problems with b...
bool set_svm(int32_t num, CSVM *svm)
void display_vector(const char *name="vector", const char *prefix="") const
static float32_t sqrt(float32_t x)
x^0.5
virtual CLabels * classify_one_vs_rest()
virtual bool has_features()
virtual void set_labels(CLabels *lab)
#define SG_UNSTABLE(func,...)
CSVM * get_svm(int32_t num)
virtual CLabels * apply(CFeatures *data=NULL)