76 SG_ERROR(
"Kernel still initialized on destruction.\n")
81 SG_INFO(
"Kernel deleted (%p).\n",
this)
105 REQUIRE(l,
"CKernel::init(%p, %p): Left hand side features required!\n", l, r)
106 REQUIRE(r,
"CKernel::init(%p, %p): Right hand side features required!\n", l, r)
112 "Right hand side of features (%s) must be compatible with left hand side features (%s)\n",
118 "Right hand side of features (%s) must be compatible with left hand side features (%s)\n",
146 SG_DEBUG(
"leaving CKernel::init(%p, %p)\n", l, r)
186 SG_ERROR(
"kernel has zero rows: num_lhs=%d num_rhs=%d\n",
189 uint64_t buffer_size=0;
197 if (buffer_size>((uint64_t) totdoc)*totdoc)
198 buffer_size=((uint64_t) totdoc)*totdoc;
209 kernel_cache.active2totdoc = SG_MALLOC(int32_t, totdoc);
210 kernel_cache.totdoc2active = SG_MALLOC(int32_t, totdoc);
220 for(i=0;i<totdoc;i++) {
224 for(i=0;i<totdoc;i++) {
230 for(i=0;i<totdoc;i++) {
239 int32_t docnum, int32_t *active2dnum,
float64_t *buffer,
bool full_line)
245 if (docnum>=num_vectors)
246 docnum=2*num_vectors-1-docnum;
266 for(i=0;(j=active2dnum[i])>=0;i++)
289 for(i=0;(j=active2dnum[i])>=0;i++)
304 register int32_t j,k,l;
314 cache = kernel_cache_clean_and_malloc(m);
322 if((
kernel_cache.index[k] != -1) && (l != -1) && (k != m)) {
336 perror(
"Error: Kernel cache full! => increase cache size");
341 void* CKernel::cache_multiple_kernel_row_helper(
void* p)
344 S_KTHREAD_PARAM* params = (S_KTHREAD_PARAM*) p;
346 for (int32_t i=params->start; i<params->end; i++)
349 int32_t m = params->uncached_rows[i];
350 l=params->kernel_cache->totdoc2active[m];
352 for(j=0;j<params->kernel_cache->activenum;j++)
354 k=params->kernel_cache->active2totdoc[j];
356 if((params->kernel_cache->index[k] != -1) && (l != -1) && (!params->needs_computation[k])) {
357 cache[j]=params->kernel_cache->buffer[((
KERNELCACHE_IDX) params->kernel_cache->activenum)
358 *params->kernel_cache->index[k]+l];
362 if (k>=params->num_vectors)
363 k=2*params->num_vectors-1-k;
365 cache[j]=params->kernel->kernel(m, k);
370 params->needs_computation[m]=0;
384 for(int32_t i=0;i<num_rows;i++)
391 int32_t* uncached_rows = SG_MALLOC(int32_t, num_rows);
393 pthread_t* threads = SG_MALLOC(pthread_t, nthreads-1);
394 S_KTHREAD_PARAM* params = SG_MALLOC(S_KTHREAD_PARAM, nthreads-1);
395 int32_t num_threads=nthreads-1;
398 uint8_t* needs_computation=SG_CALLOC(uint8_t, num_vec);
405 for (int32_t i=0; i<num_rows; i++)
414 needs_computation[idx]=1;
415 uncached_rows[num]=idx;
416 cache[num]= kernel_cache_clean_and_malloc(idx);
419 SG_ERROR(
"Kernel cache full! => increase cache size\n")
434 for (int32_t t=0; t<num_threads; t++)
436 params[t].kernel =
this;
438 params[t].cache = cache;
439 params[t].uncached_rows = uncached_rows;
440 params[t].needs_computation = needs_computation;
441 params[t].num_uncached = num;
442 params[t].start = t*step;
443 params[t].end = (t+1)*step;
447 int code=pthread_create(&threads[t], NULL,
448 CKernel::cache_multiple_kernel_row_helper, (
void*)¶ms[t]);
452 SG_WARNING(
"Thread creation failed (thread %d of %d) "
453 "with error:'%s'\n",t, num_threads, strerror(code));
464 S_KTHREAD_PARAM last_param;
465 last_param.kernel =
this;
467 last_param.cache = cache;
468 last_param.uncached_rows = uncached_rows;
469 last_param.needs_computation = needs_computation;
470 last_param.start = end;
471 last_param.num_uncached = num;
472 last_param.end = num;
475 cache_multiple_kernel_row_helper(&last_param);
478 for (int32_t t=0; t<num_threads; t++)
480 if (pthread_join(threads[t], NULL) != 0)
481 SG_WARNING(
"pthread_join of thread %d/%d failed\n", t, num_threads)
484 SG_FREE(needs_computation);
488 SG_FREE(uncached_rows);
496 int32_t totdoc, int32_t numshrink, int32_t *after)
499 register int32_t i,j,jj,scount;
503 keep=SG_MALLOC(int32_t, totdoc);
504 for(j=0;j<totdoc;j++) {
508 for(jj=0;(jj<
kernel_cache.activenum) && (scount<numshrink);jj++) {
531 for(j=0;j<totdoc;j++) {
532 if((keep[j]) && (
kernel_cache.totdoc2active[j] != -1)) {
579 int32_t CKernel::kernel_cache_malloc()
595 void CKernel::kernel_cache_free(int32_t cacheidx)
603 int32_t CKernel::kernel_cache_free_lru()
605 register int32_t k,least_elem=-1,least_time;
617 if(least_elem != -1) {
618 kernel_cache_free(least_elem);
631 if((result = kernel_cache_malloc()) == -1) {
632 if(kernel_cache_free_lru()) {
633 result = kernel_cache_malloc();
644 #endif //USE_SVMLIGHT
662 SG_DEBUG(
"entering CKernel::remove_lhs_and_rhs\n")
675 #endif //USE_SVMLIGHT
676 SG_DEBUG(
"leaving CKernel::remove_lhs_and_rhs\n")
689 #endif //USE_SVMLIGHT
703 #endif //USE_SVMLIGHT
706 #define ENUM_CASE(n) case n: SG_INFO(#n " ") break;
713 "SLOWBUTMEMEFFICIENT");
829 int32_t count, int32_t *IDX,
float64_t * weights)
831 SG_ERROR(
"kernel does not support linadd optimization\n")
837 SG_ERROR(
"kernel does not support linadd optimization\n")
843 SG_ERROR(
"kernel does not support linadd optimization\n")
848 int32_t num_vec, int32_t* vec_idx,
float64_t* target, int32_t num_suppvec,
851 SG_ERROR(
"kernel does not support batch computation\n")
856 SG_ERROR(
"kernel does not support linadd optimization, add_to_normal not implemented\n")
861 SG_ERROR(
"kernel does not support linadd optimization, clear_normal not implemented\n")
870 int32_t vector_idx,
float64_t * subkernel_contrib)
872 SG_ERROR(
"kernel compute_by_subkernel not implemented\n")
892 SG_ERROR(
"number of subkernel weights should be one ...\n")
902 REQUIRE(casted,
"CKernel::obtain_from_generic(): Error, provided object"
903 " of class \"%s\" is not a subclass of CKernel!\n",
914 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
917 for (int32_t i=0; i<num_suppvec; i++)
961 SG_ADD(&
num_lhs,
"num_lhs",
"Number of feature vectors on left hand side.",
963 SG_ADD(&
num_rhs,
"num_rhs",
"Number of feature vectors on right hand side.",
994 #endif //USE_SVMLIGHT
1035 "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
1037 "Invalid block size (%d) at starting index (%d, %d)! "
1038 "Please use smaller blocks!", block_size, block_begin, block_begin)
1039 REQUIRE(block_size>=1,
"Invalid block size (%d)!\n", block_size)
1046 #pragma omp parallel for
1047 for (
index_t i=0; i<block_size; ++i)
1051 for (
index_t j=i+1; j<block_size; ++j)
1066 #pragma omp parallel for
1067 for (
index_t i=0; i<block_size; ++i)
1087 block_begin_col>=0 && block_begin_col<
num_rhs,
1088 "Invalid block begin index (%d, %d)!\n",
1089 block_begin_row, block_begin_col)
1091 block_begin_col+block_size_col<=
num_rhs,
1092 "Invalid block size (%d, %d) at starting index (%d, %d)! "
1093 "Please use smaller blocks!", block_size_row, block_size_col,
1094 block_begin_row, block_begin_col)
1095 REQUIRE(block_size_row>=1 && block_size_col>=1,
1096 "Invalid block size (%d, %d)!\n", block_size_row, block_size_col)
1099 if (no_diag && block_size_row!=block_size_col)
1101 SG_WARNING(
"Not removing the main diagonal since block is not square!\n");
1108 #pragma omp parallel for
1109 for (
index_t i=0; i<block_size_row; ++i)
1112 for (
index_t j=0; j<block_size_col; ++j)
1115 kernel(i+block_begin_row, j+block_begin_col);
1127 index_t block_size,
bool no_diag)
1134 "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
1136 "Invalid block size (%d) at starting index (%d, %d)! "
1137 "Please use smaller blocks!", block_size, block_begin, block_begin)
1138 REQUIRE(block_size>=1,
"Invalid block size (%d)!\n", block_size)
1147 #pragma omp parallel for
1148 for (
index_t i=0; i<block_size; ++i)
1152 for (
index_t j=i+1; j<block_size; ++j)
1155 #pragma omp critical
1167 #pragma omp parallel for
1168 for (
index_t i=0; i<block_size; ++i)
1181 block_begin,
index_t block_size,
bool no_diag)
1188 "Invalid block begin index (%d, %d)!\n", block_begin, block_begin)
1190 "Invalid block size (%d) at starting index (%d, %d)! "
1191 "Please use smaller blocks!", block_size, block_begin, block_begin)
1192 REQUIRE(block_size>=1,
"Invalid block size (%d)!\n", block_size)
1203 #pragma omp parallel for
1204 for (
index_t i=0; i<block_size; ++i)
1208 for (
index_t j=i+1; j<block_size; ++j)
1211 #pragma omp critical
1225 #pragma omp parallel for
1226 for (
index_t i=0; i<block_size; ++i)
1229 row_sum(i, 0)+=diag;
1230 row_sum(i, 1)+=diag*diag;
1241 index_t block_size_col,
bool no_diag)
1247 block_begin_col>=0 && block_begin_col<
num_rhs,
1248 "Invalid block begin index (%d, %d)!\n",
1249 block_begin_row, block_begin_col)
1251 block_begin_col+block_size_col<=
num_rhs,
1252 "Invalid block size (%d, %d) at starting index (%d, %d)! "
1253 "Please use smaller blocks!", block_size_row, block_size_col,
1254 block_begin_row, block_begin_col)
1255 REQUIRE(block_size_row>=1 && block_size_col>=1,
1256 "Invalid block size (%d, %d)!\n", block_size_row, block_size_col)
1259 if (no_diag && block_size_row!=block_size_col)
1261 SG_WARNING(
"Not removing the main diagonal since block is not square!\n");
1272 #pragma omp parallel for
1273 for (
index_t i=0; i<block_size_row; ++i)
1276 for (
index_t j=0; j<block_size_col; ++j)
1279 kernel(i+block_begin_row, j+block_begin_col);
1280 #pragma omp critical
1283 sum[j+block_size_row]+=k;
1296 int32_t i_start=params->
start;
1297 int32_t i_end=params->
end;
1299 T* result=params->
result;
1301 int32_t n=params->
n;
1302 int32_t m=params->
m;
1306 int64_t total=total_start;
1308 for (int32_t i=i_start; i<i_end; i++)
1315 for (int32_t j=j_start; j<n; j++)
1320 if (symmetric && i!=j)
1327 if (symmetric && i!=j)
1353 int64_t total_num = int64_t(m)*n;
1356 bool symmetric= (
lhs &&
lhs==
rhs && m==n);
1358 SG_DEBUG(
"returning kernel matrix of size %dx%d\n", m, n)
1360 result=SG_MALLOC(T, total_num);
1363 if (num_threads < 2)
1376 get_kernel_matrix_helper<T>((
void*) ¶ms);
1380 pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
1382 int64_t step= total_num/num_threads;
1387 for (t=0; t<num_threads; t++)
1390 params[t].
result = result;
1400 int code=pthread_create(&threads[t], NULL,
1401 CKernel::get_kernel_matrix_helper<T>, (
void*)¶ms[t]);
1405 SG_WARNING(
"Thread creation failed (thread %d of %d) "
1406 "with error:'%s'\n",t, num_threads, strerror(code));
1413 params[t].
result = result;
1422 get_kernel_matrix_helper<T>(¶ms[t]);
1424 for (t=0; t<num_threads; t++)
1426 if (pthread_join(threads[t], NULL) != 0)
1427 SG_WARNING(
"pthread_join of thread %d/%d failed\n", t, num_threads)
1443 template void* CKernel::get_kernel_matrix_helper<float64_t>(
void* p);
1444 template void* CKernel::get_kernel_matrix_helper<float32_t>(
void* p);
virtual void clear_normal()
virtual const char * get_name() const =0
virtual void load_serializable_post()
virtual bool init(CFeatures *lhs, CFeatures *rhs)
int32_t get_num_support_vectors()
virtual bool support_compatible_class() const
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
virtual void set_matrix(const bool *matrix, int32_t num_feat, int32_t num_vec)
virtual void compute_by_subkernel(int32_t vector_idx, float64_t *subkernel_contrib)
void cache_multiple_kernel_rows(int32_t *key, int32_t varnum)
virtual bool get_feature_class_compatibility(EFeatureClass rhs) const
int32_t get_num_threads() const
int32_t num_rhs
number of feature vectors on right hand side
static void * get_kernel_matrix_helper(void *p)
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual bool set_normalizer(CKernelNormalizer *normalizer)
virtual float64_t sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
virtual int32_t get_num_vectors() const =0
virtual void save_serializable_pre()
virtual bool delete_optimization()
int32_t kernel_cache_space_available()
float64_t kernel(int32_t idx_a, int32_t idx_b)
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from kernel
virtual int32_t get_num_vec_lhs()
SGMatrix< float64_t > get_kernel_matrix()
int32_t cache_size
cache_size in MB
void kernel_cache_shrink(int32_t totdoc, int32_t num_shrink, int32_t *after)
bool get_is_initialized()
virtual SGMatrix< float64_t > row_wise_sum_squared_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
float64_t combined_kernel_weight
virtual void register_params()
virtual void remove_lhs_and_rhs()
virtual CKernelNormalizer * get_normalizer()
Class SGObject is the base class of all shogun objects.
virtual SGVector< float64_t > row_col_wise_sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
void cache_kernel_row(int32_t x)
#define SG_OBJ_PROGRESS(o,...)
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
virtual SGVector< float64_t > get_subkernel_weights()
KERNEL_CACHE kernel_cache
kernel cache
virtual EFeatureType get_feature_type()=0
KERNELCACHE_ELEM * kernel_matrix
A File access base class.
virtual void save_serializable_post()
virtual float64_t compute_optimized(int32_t vector_idx)
EOptimizationType get_optimization_type()
virtual void save_serializable_post()
float64_t get_alpha(int32_t idx)
float64_t get_combined_kernel_weight()
virtual SGVector< float64_t > row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
virtual EFeatureClass get_feature_class() const =0
Identity Kernel Normalization, i.e. no normalization is applied.
int32_t num_lhs
number of feature vectors on left hand side
The class Kernel Normalizer defines a function to post-process kernel values.
int32_t get_support_vector(int32_t idx)
static bool cancel_computations()
virtual int32_t get_num_vec_rhs()
virtual void set_subkernel_weights(SGVector< float64_t > weights)
virtual bool init_normalizer()
bool optimization_initialized
EOptimizationType opt_type
virtual void load_serializable_post()
CFeatures * rhs
feature vectors to occur on right hand side
static CKernel * obtain_from_generic(CSGObject *kernel)
all of classes and functions are contained in the shogun namespace
virtual bool init(CKernel *k)=0
virtual void compute_batch(int32_t num_vec, int32_t *vec_idx, float64_t *target, int32_t num_suppvec, int32_t *IDX, float64_t *alphas, float64_t factor=1.0)
virtual EKernelType get_kernel_type()=0
virtual bool init_optimization(int32_t count, int32_t *IDX, float64_t *weights)
CFeatures * lhs
feature vectors to occur on left hand side
The class Features is the base class of all feature objects.
virtual void save_serializable_pre()
void kernel_cache_cleanup()
virtual void remove_lhs()
int32_t kernel_cache_check(int32_t cacheidx)
virtual int32_t get_num_subkernels()
bool init_optimization_svm(CSVM *svm)
A generic Support Vector Machine Interface.
void kernel_cache_reset_lru()
CKernelNormalizer * normalizer
virtual SGVector< float64_t > get_kernel_row(int32_t i)
virtual bool has_features()
void kernel_cache_init(int32_t size, bool regression_hack=false)
virtual void add_to_normal(int32_t vector_idx, float64_t weight)
void set_const(T const_elem)
float64_t KERNELCACHE_ELEM
void set_const(T const_elem)
void resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack=false)
virtual EFeatureType get_feature_type() const =0
virtual EFeatureClass get_feature_class()=0