SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Kernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/lib/config.h>
13 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
15 #include <shogun/io/File.h>
16 #include <shogun/lib/Time.h>
17 #include <shogun/lib/Signal.h>
18 
19 #include <shogun/base/Parallel.h>
20 
21 #include <shogun/kernel/Kernel.h>
24 #include <shogun/base/Parameter.h>
25 
27 
28 #include <string.h>
29 #include <unistd.h>
30 #include <math.h>
31 
32 #ifdef HAVE_PTHREAD
33 #include <pthread.h>
34 #endif
35 
36 using namespace shogun;
37 
39 {
40  init();
42 }
43 
44 CKernel::CKernel(int32_t size) : CSGObject()
45 {
46  init();
47 
48  if (size<10)
49  size=10;
50 
51  cache_size=size;
53 }
54 
55 
56 CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, int32_t size) : CSGObject()
57 {
58  init();
59 
60  if (size<10)
61  size=10;
62 
63  cache_size=size;
64 
66  init(p_lhs, p_rhs);
68 }
69 
71 {
72  if (get_is_initialized())
73  SG_ERROR("Kernel still initialized on destruction.\n");
74 
77 
78  SG_INFO("Kernel deleted (%p).\n", this);
79 }
80 
81 
82 
83 bool CKernel::init(CFeatures* l, CFeatures* r)
84 {
85  //make sure features were indeed supplied
86  ASSERT(l);
87  ASSERT(r);
88 
89  //make sure features are compatible
92 
93  //remove references to previous features
95 
96  //increase reference counts
97  SG_REF(l);
98  if (l==r)
99  lhs_equals_rhs=true;
100  else // l!=r
101  SG_REF(r);
102 
103  lhs=l;
104  rhs=r;
105 
108 
111 
112  return true;
113 }
114 
116 {
117  SG_REF(n);
118  if (lhs && rhs)
119  n->init(this);
120 
122  normalizer=n;
123 
124  return (normalizer!=NULL);
125 }
126 
128 {
130  return normalizer;
131 }
132 
134 {
135  return normalizer->init(this);
136 }
137 
139 {
141 }
142 
143 
144 
145 void CKernel::load(CFile* loader)
146 {
149 }
150 
151 void CKernel::save(CFile* writer)
152 {
153  SGMatrix<float64_t> k_matrix=get_kernel_matrix<float64_t>();
155  writer->set_matrix(k_matrix.matrix, k_matrix.num_rows, k_matrix.num_cols);
156  SG_FREE(k_matrix.matrix);
158 }
159 
161 {
162  if (rhs!=lhs)
163  SG_UNREF(rhs);
164  rhs = NULL;
165  num_rhs=0;
166 
167  SG_UNREF(lhs);
168  lhs = NULL;
169  num_lhs=0;
170  lhs_equals_rhs=false;
171 
172 
173 }
174 
176 {
177  if (rhs==lhs)
178  rhs=NULL;
179  SG_UNREF(lhs);
180  lhs = NULL;
181  num_lhs=0;
182  lhs_equals_rhs=false;
183 
184 }
185 
188 {
189  if (rhs!=lhs)
190  SG_UNREF(rhs);
191  rhs = NULL;
192  num_rhs=0;
193  lhs_equals_rhs=false;
194 
195 
196 }
197 
198 #define ENUM_CASE(n) case n: SG_INFO(#n " "); break;
199 
201 {
202  SG_INFO( "%p - \"%s\" weight=%1.2f OPT:%s", this, get_name(),
204  get_optimization_type()==FASTBUTMEMHUNGRY ? "FASTBUTMEMHUNGRY" :
205  "SLOWBUTMEMEFFICIENT");
206 
207  switch (get_kernel_type())
208  {
262  }
263 
264  switch (get_feature_class())
265  {
276  ENUM_CASE(C_WD)
281  }
282 
283  switch (get_feature_type())
284  {
299  }
300  SG_INFO( "\n");
301 }
302 #undef ENUM_CASE
303 
305  int32_t count, int32_t *IDX, float64_t * weights)
306 {
307  SG_ERROR( "kernel does not support linadd optimization\n");
308  return false ;
309 }
310 
312 {
313  SG_ERROR( "kernel does not support linadd optimization\n");
314  return false;
315 }
316 
318 {
319  SG_ERROR( "kernel does not support linadd optimization\n");
320  return 0;
321 }
322 
324  int32_t num_vec, int32_t* vec_idx, float64_t* target, int32_t num_suppvec,
325  int32_t* IDX, float64_t* weights, float64_t factor)
326 {
327  SG_ERROR( "kernel does not support batch computation\n");
328 }
329 
330 void CKernel::add_to_normal(int32_t vector_idx, float64_t weight)
331 {
332  SG_ERROR( "kernel does not support linadd optimization, add_to_normal not implemented\n");
333 }
334 
336 {
337  SG_ERROR( "kernel does not support linadd optimization, clear_normal not implemented\n");
338 }
339 
341 {
342  return 1;
343 }
344 
346  int32_t vector_idx, float64_t * subkernel_contrib)
347 {
348  SG_ERROR( "kernel compute_by_subkernel not implemented\n");
349 }
350 
351 const float64_t* CKernel::get_subkernel_weights(int32_t &num_weights)
352 {
353  num_weights=1 ;
354  return &combined_kernel_weight ;
355 }
356 
358 {
359  ASSERT(weights.vector);
360  if (weights.vlen!=1)
361  SG_ERROR( "number of subkernel weights should be one ...\n");
362 
363  combined_kernel_weight = weights.vector[0] ;
364 }
365 
367 {
368  int32_t num_suppvec=svm->get_num_support_vectors();
369  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
370  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
371 
372  for (int32_t i=0; i<num_suppvec; i++)
373  {
374  sv_idx[i] = svm->get_support_vector(i);
375  sv_weight[i] = svm->get_alpha(i);
376  }
377  bool ret = init_optimization(num_suppvec, sv_idx, sv_weight);
378 
379  SG_FREE(sv_idx);
380  SG_FREE(sv_weight);
381  return ret;
382 }
383 
385 {
387  if (lhs_equals_rhs)
388  rhs=lhs;
389 }
390 
392 {
394 
395  if (lhs_equals_rhs)
396  rhs=NULL;
397 }
398 
400 {
402 
403  if (lhs_equals_rhs)
404  rhs=lhs;
405 }
406 
408  m_parameters->add(&cache_size, "cache_size",
409  "Cache size in MB.");
410  m_parameters->add((CSGObject**) &lhs, "lhs",
411  "Feature vectors to occur on left hand side.");
412  m_parameters->add((CSGObject**) &rhs, "rhs",
413  "Feature vectors to occur on right hand side.");
414  m_parameters->add(&lhs_equals_rhs, "lhs_equals_rhs",
415  "If features on lhs are the same as on rhs.");
416  m_parameters->add(&num_lhs, "num_lhs",
417  "Number of feature vectors on left hand side.");
418  m_parameters->add(&num_rhs, "num_rhs",
419  "Number of feature vectors on right hand side.");
420  m_parameters->add(&combined_kernel_weight, "combined_kernel_weight",
421  "Combined kernel weight.");
423  "optimization_initialized",
424  "Optimization is initialized.");
425  m_parameters->add((machine_int_t*) &opt_type, "opt_type",
426  "Optimization type.");
427  m_parameters->add(&properties, "properties",
428  "Kernel properties.");
429  m_parameters->add((CSGObject**) &normalizer, "normalizer",
430  "Normalize the kernel.");
431 }
432 
433 
434 void CKernel::init()
435 {
436  cache_size=10;
437  kernel_matrix=NULL;
438  lhs=NULL;
439  rhs=NULL;
440  num_lhs=0;
441  num_rhs=0;
446  normalizer=NULL;
447 
448 
449 
451 }

SHOGUN Machine Learning Toolbox - Documentation