33 CSVMOcas::CSVMOcas(E_SVM_TYPE type)
57 bool CSVMOcas::train_machine(
CFeatures* data)
66 if (!data->has_property(
FP_DOT))
67 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
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)
84 old_w=SG_CALLOC(
float64_t, current_w.vlen);
88 tmp_a_buf=SG_CALLOC(float64_t, current_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)
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]);
162 CSVMOcas* o = (CSVMOcas*) ptr;
163 uint32_t nDim = (uint32_t) o->current_w.vlen;
167 for(uint32_t j=0; j <nDim; j++)
169 W[j] = oldW[j]*(1-t) + t*W[j];
170 sq_norm_W += W[j]*W[j];
172 o->bias=o->old_bias*(1-t) + t*o->bias;
186 int CSVMOcas::add_new_cut(
187 float64_t *new_col_H, uint32_t *new_cut, uint32_t cut_length,
188 uint32_t nSel,
void* ptr)
190 CSVMOcas* o = (CSVMOcas*) ptr;
192 uint32_t nDim=(uint32_t) o->current_w.vlen;
196 uint32_t** c_idx = o->cp_index;
197 uint32_t* c_nzd = o->cp_nz_dims;
201 uint32_t i, j, nz_dims;
205 memset(new_a, 0,
sizeof(
float64_t)*nDim);
207 for(i=0; i < cut_length; i++)
212 c_bias[nSel]+=y[new_cut[i]];
218 for(j=0; j < nDim; j++ ) {
221 sq_norm_a += new_a[j]*new_a[j];
226 c_nzd[nSel] = nz_dims;
232 c_idx[nSel]=SG_MALLOC(uint32_t, nz_dims);
233 c_val[nSel]=SG_MALLOC(
float64_t, nz_dims);
236 for(j=0; j < nDim; j++ )
240 c_idx[nSel][idx] = j;
241 c_val[nSel][idx++] = new_a[j];
246 new_col_H[nSel] = sq_norm_a;
248 for(i=0; i < nSel; i++)
251 for(j=0; j < c_nzd[i]; j++)
252 tmp += new_a[c_idx[i][j]]*c_val[i][j];
273 int CSVMOcas::compute_output(
float64_t *output,
void* ptr)
275 CSVMOcas* o = (CSVMOcas*) ptr;
281 f->
dense_dot_range(output, 0, nData, y, o->current_w.vector, o->current_w.vlen, 0.0);
283 for (int32_t i=0; i<nData; i++)
284 output[i]+=y[i]*o->bias;
297 void CSVMOcas::compute_W(
301 CSVMOcas* o = (CSVMOcas*) ptr;
302 uint32_t nDim= (uint32_t) o->current_w.vlen;
311 uint32_t** c_idx = o->cp_index;
312 uint32_t* c_nzd = o->cp_nz_dims;
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;
356 "Indicates if bias is used.");
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)
virtual void set_w(const SGVector< float64_t > src_w)
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.
virtual int32_t get_dim_feature_space() const =0
void add(bool *param, const char *name, const char *description="")
void print(CJLCoverTreePoint &p)
float64_t get_max_train_time()
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)