33 CSVMOcas::CSVMOcas(E_SVM_TYPE type)
57 bool CSVMOcas::train_machine(
CFeatures* data)
59 SG_INFO(
"C=%f, epsilon=%f, bufsize=%d\n", get_C1(), get_epsilon(), bufsize)
60 SG_DEBUG("use_bias = %i\n", get_bias_enabled())
66 if (!data->has_property(
FP_DOT))
67 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
72 int32_t num_vec=features->get_num_vectors();
74 for (int32_t i=0; i<num_vec; i++)
80 if (num_vec!=lab.vlen || num_vec<=0)
81 SG_ERROR("num_vec=%d num_train_labels=%d\n", num_vec, lab.vlen)
88 tmp_a_buf=SG_CALLOC(float64_t, w.vlen);
89 cp_value=SG_CALLOC(float64_t*, bufsize);
90 cp_index=SG_CALLOC(uint32_t*, bufsize);
91 cp_nz_dims=SG_CALLOC(uint32_t, bufsize);
92 cp_bias=SG_CALLOC(float64_t, bufsize);
97 if (method == SVM_OCAS)
99 ocas_return_value_T result = svm_ocas_solver( get_C1(), num_vec, get_epsilon(),
100 TolAbs, QPBound, get_max_train_time(), bufsize, Method,
101 &CSVMOcas::compute_W,
103 &CSVMOcas::add_new_cut,
104 &CSVMOcas::compute_output,
109 SG_INFO("Ocas Converged after %d iterations\n"
110 "==================================\n"
111 "timing statistics:\n"
112 "output_time: %f s\n"
117 "ocas_time %f s\n\n", result.nIter, result.output_time, result.sort_time,
118 result.add_time, result.w_time, result.qp_solver_time, result.ocas_time);
122 primal_objective = result.Q_P;
124 uint32_t num_cut_planes = result.nCutPlanes;
126 SG_DEBUG("num_cut_planes=%d\n", num_cut_planes)
127 for (uint32_t i=0; i<num_cut_planes; i++)
129 SG_DEBUG(
"cp_value[%d]=%p\n", i, cp_value)
130 SG_FREE(cp_value[i]);
131 SG_DEBUG("cp_index[%d]=%p\n", i, cp_index)
132 SG_FREE(cp_index[i]);
157 float64_t CSVMOcas::update_W( float64_t t,
void* ptr )
159 float64_t sq_norm_W = 0;
160 CSVMOcas* o = (CSVMOcas*) ptr;
161 uint32_t nDim = (uint32_t) o->w.vlen;
162 float64_t* W=o->w.vector;
163 float64_t* oldW=o->old_w;
165 for(uint32_t j=0; j <nDim; j++)
167 W[j] = oldW[j]*(1-t) + t*W[j];
168 sq_norm_W += W[j]*W[j];
170 o->bias=o->old_bias*(1-t) + t*o->bias;
184 int CSVMOcas::add_new_cut(
185 float64_t *new_col_H, uint32_t *new_cut, uint32_t cut_length,
186 uint32_t nSel,
void* ptr)
188 CSVMOcas* o = (CSVMOcas*) ptr;
190 uint32_t nDim=(uint32_t) o->w.vlen;
191 float64_t* y = o->lab.vector;
193 float64_t** c_val = o->cp_value;
194 uint32_t** c_idx = o->cp_index;
195 uint32_t* c_nzd = o->cp_nz_dims;
196 float64_t* c_bias = o->cp_bias;
199 uint32_t i, j, nz_dims;
202 float64_t* new_a = o->tmp_a_buf;
203 memset(new_a, 0,
sizeof(float64_t)*nDim);
205 for(i=0; i < cut_length; i++)
210 c_bias[nSel]+=y[new_cut[i]];
216 for(j=0; j < nDim; j++ ) {
219 sq_norm_a += new_a[j]*new_a[j];
224 c_nzd[nSel] = nz_dims;
230 c_idx[nSel]=SG_MALLOC(uint32_t, nz_dims);
231 c_val[nSel]=SG_MALLOC(float64_t, nz_dims);
234 for(j=0; j < nDim; j++ )
238 c_idx[nSel][idx] = j;
239 c_val[nSel][idx++] = new_a[j];
244 new_col_H[nSel] = sq_norm_a;
246 for(i=0; i < nSel; i++)
248 float64_t tmp = c_bias[nSel]*c_bias[i];
249 for(j=0; j < c_nzd[i]; j++)
250 tmp += new_a[c_idx[i][j]]*c_val[i][j];
260 int CSVMOcas::sort(float64_t* vals, float64_t* data, uint32_t size)
271 int CSVMOcas::compute_output(float64_t *output,
void* ptr)
273 CSVMOcas* o = (CSVMOcas*) ptr;
277 float64_t* y = o->lab.vector;
281 for (int32_t i=0; i<nData; i++)
282 output[i]+=y[i]*o->bias;
297 void CSVMOcas::compute_W(
298 float64_t *sq_norm_W, float64_t *dp_WoldW, float64_t *alpha, uint32_t nSel,
301 CSVMOcas* o = (CSVMOcas*) ptr;
302 uint32_t nDim= (uint32_t) o->w.vlen;
304 float64_t* W=o->w.vector;
305 float64_t* oldW=o->old_w;
306 memset(W, 0,
sizeof(float64_t)*nDim);
307 float64_t old_bias=o->bias;
310 float64_t** c_val = o->cp_value;
311 uint32_t** c_idx = o->cp_index;
312 uint32_t* c_nzd = o->cp_nz_dims;
313 float64_t* c_bias = o->cp_bias;
315 for(uint32_t i=0; i<nSel; i++)
317 uint32_t nz_dims = c_nzd[i];
319 if(nz_dims > 0 && alpha[i] > 0)
321 for(uint32_t j=0; j < nz_dims; j++)
322 W[c_idx[i][j]] += alpha[i]*c_val[i][j];
324 bias += c_bias[i]*alpha[i];
328 *dp_WoldW =
CMath::dot(W,oldW, nDim) + bias*old_bias;
332 o->old_bias = old_bias;
335 void CSVMOcas::init()
351 primal_objective = 0.0;
353 m_parameters->add(&C1,
"C1",
"Cost constant 1.");
354 m_parameters->add(&C2,
"C2",
"Cost constant 2.");
355 m_parameters->add(&use_bias,
"use_bias",
356 "Indicates if bias is used.");
357 m_parameters->add(&epsilon,
"epsilon",
"Convergence precision.");
358 m_parameters->add(&bufsize,
"bufsize",
"Maximum number of cutting planes.");
360 "SVMOcas solver type.");
363 float64_t CSVMOcas::compute_primal_objective()
const
365 return primal_objective;
368 #endif //USE_GPL_SHOGUN
virtual void dense_dot_range(float64_t *output, int32_t start, int32_t stop, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
The class Labels models labels, i.e. class assignments of objects.
static void qsort_index(T1 *output, T2 *index, uint32_t size)
virtual int32_t get_num_vectors() const =0
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)=0
Features that support dot products among other operations.
void print(CJLCoverTreePoint &p)
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
Binary Labels for binary classification.
static void swap(T &a, T &b)
virtual void set_labels(CLabels *lab)