22 #undef DEBUG_SUBGRADIENTSVM
24 using namespace shogun;
28 qpsize_max(2000), use_bias(false), delta_active(0), delta_bound(0)
35 qpsize_max(2000), use_bias(false), delta_active(0), delta_bound(0)
80 int32_t num_feat, int32_t num_vec, int32_t& num_active, int32_t& num_bound)
87 for (int32_t i=0; i<num_vec; i++)
127 for (int32_t i=0; i<num_vec; i++)
137 #ifdef DEBUG_SUBGRADIENTSVM
171 for (int32_t i=0; i<num_vec; i++)
203 for (int32_t i=0; i<num_vec; i++)
230 for (int32_t i=0; i<num_vec; i++)
250 float64_t grad_val = 2*A_zero*alpha + B_zero + sum_B;
264 for (int32_t i=1; i < num_hinge && grad_val < 0; i++)
267 grad_val = 2*A_zero*alpha + B_zero + sum_B;
271 ASSERT(old_grad_val-grad_val != 0);
272 float64_t gamma = -grad_val/(old_grad_val-grad_val);
273 alpha = old_alpha*gamma + (1-gamma)*alpha;
277 old_grad_val = grad_val;
281 grad_val = 2*A_zero*alpha + B_zero + sum_B;
289 int32_t num_feat, int32_t num_vec, int32_t num_active, int32_t num_bound)
302 for (int32_t i=0; i<num_bound; i++)
314 for (int32_t i=0; i<num_bound; i++)
316 for (int32_t j=i; j<num_bound; j++)
321 Z[j*num_bound+i]=Z[i*num_bound+j];
332 #ifdef DEBUG_SUBGRADIENTSVM
349 #ifdef DEBUG_SUBGRADIENTSVM
367 for (int32_t i=0; i<num_bound; i++)
375 for (int32_t i=0; i<num_bound; i++)
396 for (int32_t i=0; i<num_vec; i++)
399 result +=
C1 * (1.0-
proj[i]);
407 for (int32_t i=0; i<num_vec; i++)
416 void CSubGradientSVM::init(int32_t num_vec, int32_t num_feat)
459 memset(
hinge_idx,0,
sizeof(int32_t)*num_vec);
462 memset(
active,0,
sizeof(uint8_t)*num_vec);
468 memset(
idx_bound,0,
sizeof(int32_t)*num_vec);
474 memset(
Z,0,
sizeof(
float64_t)*qpsize_limit*qpsize_limit);
539 SG_ERROR(
"Specified features are not of type CDotFeatures\n");
544 int32_t num_iterations=0;
549 ASSERT(num_vec==num_train_labels);
551 init(num_vec, num_feat);
553 int32_t num_active=0;
567 #ifdef DEBUG_SUBGRADIENTSVM
577 #ifdef DEBUG_SUBGRADIENTSVM
578 SG_PRINT(
"==================================================\niteration: %d ", num_iterations);
580 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",
606 #ifdef DEBUG_SUBGRADIENTSVM
607 SG_PRINT(
"CHECKING OPTIMALITY CONDITIONS: "
608 "work_epsilon: %10.10f delta_active:%d alpha: %10.10f norm_grad: %10.10f a*norm_grad:%10.16f\n",
643 #ifdef DEBUG_SUBGRADIENTSVM
653 SG_INFO(
"converged after %d iterations\n", num_iterations);
656 SG_INFO(
"objective: %f alpha: %f dir_deriv: %f num_bound: %d num_active: %d\n",
657 obj, alpha, dir_deriv, num_bound, num_active);
659 #ifdef DEBUG_SUBGRADIENTSVM
Class Time that implements a stopwatch based on either cpu time or wall clock time.
bool has_property(EFeatureProperty p)
float64_t compute_objective(int32_t num_feat, int32_t num_vec)
compute svm objective
void compute_projection(int32_t num_feat, int32_t num_vec)
compute projection
virtual float64_t get_label(int32_t i)
void update_projection(float64_t alpha, int32_t num_vec)
only computes updates on the projection
The class Labels models labels, i.e. class assignments of objects.
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
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
virtual bool train_machine(CFeatures *data=NULL)
void set_solver(E_QPB_SOLVER solver)
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)=0
float64_t line_search(int32_t num_feat, int32_t num_vec)
performs a line search to determine step 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
virtual CDotFeatures * get_features()
static void vec1_plus_scalar_times_vec2(T *vec1, T scalar, const T *vec2, int32_t n)
x=x+alpha*y
int32_t num_it_noimprovement
Features that support dot products among other operations.
virtual ~CSubGradientSVM()
virtual int32_t get_dim_feature_space() const =0
float64_t time_diff_sec(bool verbose=false)
float64_t cur_time_diff(bool verbose=false)
void cleanup()
de-alloc helper arrays
float64_t * sum_CXy_active
float64_t autoselected_epsilon
int32_t find_active(int32_t num_feat, int32_t num_vec, int32_t &num_active, int32_t &num_bound)
static void display_vector(const T *vector, int32_t n, const char *name="vector")
display vector (useful for debugging)
float64_t get_max_train_time()
static T max(T a, T b)
return the maximum of two integers
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)
static bool cancel_computations()
#define SG_ABS_PROGRESS(...)
The class Features is the base class of all feature objects.
void update_active(int32_t num_feat, int32_t num_vec)
static T min(T a, T b)
return the minimum of two integers
virtual void set_features(CDotFeatures *feat)
void init(int32_t num_vec, int32_t num_feat)
alloc helper arrays
static void swap(T &a, T &b)
swap e.g. floats a and b
int32_t solve_qp(float64_t *result, int32_t len)
result has to be allocated & zeroed
virtual void set_labels(CLabels *lab)
float64_t compute_min_subgradient(int32_t num_feat, int32_t num_vec, int32_t num_active, int32_t num_bound)
#define SG_MALLOC(type, len)
int32_t last_it_noimprovement
static T abs(T a)
return the absolute value of a number