21 #include <shogun/classifier/svm/qpbsvmlib.h>
25 using namespace shogun;
27 #define DEBUG_SUBGRADIENTLPM
30 : CLinearClassifier(), C1(1), C2(1), epsilon(1e-5), qpsize(42),
31 qpsize_max(2000), use_bias(false), delta_active(0), delta_bound(0)
37 : CLinearClassifier(), C1(C), C2(C), epsilon(1e-5), qpsize(42),
38 qpsize_max(2000), use_bias(false), delta_active(0), delta_bound(0)
40 CLinearClassifier::features=traindat;
41 CClassifier::labels=trainlab;
51 int32_t num_feat, int32_t num_vec, int32_t& num_active, int32_t& num_bound)
84 for (int32_t i=0; i<num_vec; i++)
124 for (int32_t i=0; i<num_vec; i++)
134 #ifdef DEBUG_SUBGRADIENTSVM
168 for (int32_t i=0; i<num_vec; i++)
197 for (int32_t i=0; i<num_feat; i++)
223 for (int32_t i=0; i<num_vec; i++)
253 for (int32_t i=0; i<num_feat+num_vec; i++)
270 D[i]=
C1*(1-
proj[i-num_feat]);
273 if (A[i]==C[i] && B[i]>D[i])
275 else if (A[i]==C[i] && B[i]==D[i])
304 while (i < num_hinge-1 && sgrad < 0)
311 sgrad += C[hinge_idx[i]] - A[hinge_idx[i]];
326 int32_t num_feat, int32_t num_vec, int32_t num_active, int32_t num_bound)
358 for (int32_t i=0; i<num_bound; i++)
364 for (int32_t i=0; i<num_feat; i++)
415 for (int32_t i=0; i<num_vec; i++)
418 result +=
C1 * (1.0-
proj[i]);
426 for (int32_t i=0; i<num_vec; i++)
427 proj[i]=get_label(i)*(features->dense_dot(i, w, num_feat) + bias);
441 for (int32_t i=0; i<num_feat; i++)
449 memset(
w_pos,0,
sizeof(int32_t)*num_feat);
452 memset(
w_zero,0,
sizeof(int32_t)*num_feat);
455 memset(
w_neg,0,
sizeof(int32_t)*num_feat);
481 memset(
hinge_idx,0,
sizeof(int32_t)*(num_vec+num_feat));
484 memset(
active,0,
sizeof(uint8_t)*num_vec);
490 memset(
idx_bound,0,
sizeof(int32_t)*num_vec);
496 memset(
beta,0,
sizeof(
float64_t)*num_feat+1+num_feat+num_vec);
549 SG_ERROR(
"Specified features are not of type CDotFeatures\n");
554 int32_t num_iterations=0;
555 int32_t num_train_labels=labels->get_num_labels();
556 int32_t num_feat=features->get_dim_feature_space();
557 int32_t num_vec=features->get_num_vectors();
559 ASSERT(num_vec==num_train_labels);
561 init(num_vec, num_feat);
563 int32_t num_active=0;
585 #ifdef DEBUG_SUBGRADIENTLPM
586 SG_PRINT(
"==================================================\niteration: %d ", num_iterations);
588 SG_PRINT(
"objective:%.10f alpha: %.10f dir_deriv: %f num_bound: %d num_active: %d work_eps: %10.10f eps: %10.10f auto_eps: %10.10f time:%f\n",
614 SG_PRINT(
"CHECKING OPTIMALITY CONDITIONS: "
615 "work_epsilon: %10.10f delta_active:%d alpha: %10.10f norm_grad: %10.10f a*norm_grad:%10.16f\n",
647 if (get_max_train_time()>0 && time.
cur_time_diff()>get_max_train_time())
651 SG_INFO(
"converged after %d iterations\n", num_iterations);
654 SG_INFO(
"objective: %f alpha: %f dir_deriv: %f num_bound: %d num_active: %d\n",
655 obj, alpha, dir_deriv, num_bound, num_active);
657 #ifdef DEBUG_SUBGRADIENTLPM
Class Time that implements a stopwatch based on either cpu time or wall clock time.
bool has_property(EFeatureProperty p)
void update_active(int32_t num_feat, int32_t num_vec)
virtual ~CSubGradientLPM()
Class CCplex to encapsulate access to the commercial cplex general purpose optimizer.
void compute_projection(int32_t num_feat, int32_t num_vec)
compute projection
bool init(E_PROB_TYPE t, int32_t timeout=60)
init cplex with problem type t and retry timeout 60 seconds
float64_t autoselected_epsilon
float64_t compute_min_subgradient(int32_t num_feat, int32_t num_vec, int32_t num_active, int32_t num_bound)
bool setup_subgradientlpm_QP(float64_t C, CLabels *labels, CSparseFeatures< float64_t > *features, int32_t *idx_bound, int32_t num_bound, int32_t *w_zero, int32_t num_zero, float64_t *vee, int32_t num_dim, bool use_bias)
The class Labels models labels, i.e. class assignments of objects.
static void qsort_index(T1 *output, T2 *index, uint32_t size)
float64_t stop(bool verbose=false)
static float64_t log10(float64_t v)
static void add(T *target, T alpha, const T *v1, T beta, const T *v2, int32_t len)
target=alpha*vec1 + beta*vec2
bool optimize(float64_t *sol, float64_t *lambda=NULL)
int32_t last_it_noimprovement
virtual bool train_machine(CFeatures *data=NULL)
static void vec1_plus_scalar_times_vec2(T *vec1, T scalar, const T *vec2, int32_t n)
x=x+alpha*y
void cleanup()
de-alloc helper arrays
Features that support dot products among other operations.
int32_t find_active(int32_t num_feat, int32_t num_vec, int32_t &num_active, int32_t &num_bound)
void init(int32_t num_vec, int32_t num_feat)
alloc helper arrays
float64_t time_diff_sec(bool verbose=false)
float64_t cur_time_diff(bool verbose=false)
float64_t * sum_CXy_active
static T sum_abs(T *vec, int32_t len)
return sum(abs(vec))
float64_t line_search(int32_t num_feat, int32_t num_vec)
performs a line search to determine step size
static void display_vector(const T *vector, int32_t n, const char *name="vector")
display vector (useful for debugging)
static T max(T a, T b)
return the maximum of two integers
float64_t compute_objective(int32_t num_feat, int32_t num_vec)
compute svm objective
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
static bool cancel_computations()
#define SG_ABS_PROGRESS(...)
int32_t num_it_noimprovement
void update_projection(float64_t alpha, int32_t num_vec)
only computes updates on the projection
The class Features is the base class of all feature objects.
static T min(T a, T b)
return the minimum of two integers
static void swap(T &a, T &b)
swap e.g. floats a and b
#define SG_MALLOC(type, len)
static T abs(T a)
return the absolute value of a number