SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
CustomKernel.h
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) 2012 Heiko Strathmann
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _CUSTOMKERNEL_H___
13 #define _CUSTOMKERNEL_H___
14 
15 #include <shogun/lib/config.h>
16 
18 #include <shogun/lib/common.h>
19 #include <shogun/kernel/Kernel.h>
21 
22 namespace shogun
23 {
36 class CCustomKernel: public CKernel
37 {
38  void init();
39 
40  public:
42  CCustomKernel();
43 
50 
59 
68 
72  virtual ~CCustomKernel();
73 
86  virtual bool dummy_init(int32_t rows, int32_t cols);
87 
96  virtual bool init(CFeatures* l, CFeatures* r);
97 
99  virtual void cleanup();
100 
102  void cleanup_custom();
103 
108  virtual EKernelType get_kernel_type() { return K_CUSTOM; }
109 
114  virtual EFeatureType get_feature_type() { return F_ANY; }
115 
120  virtual EFeatureClass get_feature_class() { return C_ANY; }
121 
126  virtual const char* get_name() const { return "CustomKernel"; }
127 
140  SGVector<float64_t> tri_kernel_matrix)
141  {
143  {
144  SG_ERROR("%s::set_triangle_kernel_matrix_from_triangle not"
145  " possible with subset. Remove first\n", get_name());
146  }
147  return set_triangle_kernel_matrix_from_triangle_generic(tri_kernel_matrix);
148  }
149 
161  template <class T>
163  SGVector<T> tri_kernel_matrix)
164  {
166  {
167  SG_ERROR("%s::set_triangle_kernel_matrix_from_triangle_generic "
168  "not possible with subset. Remove first\n", get_name());
169  }
170  ASSERT(tri_kernel_matrix.vector)
171 
172  int64_t len = tri_kernel_matrix.vlen;
173  int64_t cols = (int64_t) floor(-0.5 + CMath::sqrt(0.25+2*len));
174 
175  if (cols*(cols+1)/2 != len)
176  {
177  SG_ERROR("km should be a vector containing a lower triangle matrix, with len=cols*(cols+1)/2 elements\n")
178  return false;
179  }
180 
181  cleanup_custom();
182  SG_DEBUG("using custom kernel of size %dx%d\n", cols,cols)
183 
184  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, len), cols, cols);
185  upper_diagonal=true;
186 
187  for (int64_t i=0; i<len; i++)
188  kmatrix.matrix[i]=tri_kernel_matrix.vector[i];
189 
190  m_is_symmetric=true;
191  dummy_init(cols,cols);
192  return true;
193  }
194 
205  SGMatrix<float64_t> full_kernel_matrix)
206  {
207  return set_triangle_kernel_matrix_from_full_generic(full_kernel_matrix);
208  }
209 
217  template <class T>
219  SGMatrix<T> full_kernel_matrix)
220  {
222  {
223  SG_ERROR("%s::set_triangle_kernel_matrix_from_full_generic "
224  "not possible with subset. Remove first\n", get_name());
225  }
226 
227  int32_t rows = full_kernel_matrix.num_rows;
228  int32_t cols = full_kernel_matrix.num_cols;
229  ASSERT(rows==cols)
230 
231  cleanup_custom();
232  SG_DEBUG("using custom kernel of size %dx%d\n", cols,cols)
233 
234  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, cols*(cols+1)/2), rows, cols);
235  upper_diagonal = true;
236 
237  for (int64_t row=0; row<rows; row++)
238  {
239  for (int64_t col=row; col<cols; col++)
240  {
241  int64_t idx=row * cols - row*(row+1)/2 + col;
242  kmatrix.matrix[idx] = full_kernel_matrix.matrix[col*rows+row];
243  }
244  }
245 
246  m_is_symmetric=true;
247  dummy_init(rows, cols);
248  return true;
249  }
250 
264  SGMatrix<float32_t> full_kernel_matrix, bool check_symmetry=false)
265  {
267  {
268  SG_ERROR("%s::set_full_kernel_matrix_from_full "
269  "not possible with subset. Remove first\n", get_name());
270  }
271 
272  cleanup_custom();
273  kmatrix=full_kernel_matrix;
274 
275  if (check_symmetry)
277 
279  return true;
280  }
281 
295  SGMatrix<float64_t> full_kernel_matrix, bool check_symmetry=false)
296  {
298  {
299  SG_ERROR("%s::set_full_kernel_matrix_from_full "
300  "not possible with subset. Remove first\n", get_name());
301  }
302 
303  cleanup_custom();
304  int32_t rows=full_kernel_matrix.num_rows;
305  int32_t cols=full_kernel_matrix.num_cols;
306  SG_DEBUG("using custom kernel of size %dx%d\n", rows,cols)
307 
308  kmatrix=SGMatrix<float32_t>(rows,cols);
309  upper_diagonal = false;
310 
311  for (int64_t i=0; i<int64_t(rows) * cols; i++)
312  kmatrix.matrix[i]=full_kernel_matrix.matrix[i];
313 
314  if (check_symmetry)
316 
318  return true;
319  }
320 
321 #ifdef HAVE_LINALG_LIB
322 
339  virtual float64_t sum_symmetric_block(index_t block_begin,
340  index_t block_size, bool no_diag=true);
341 
363  virtual float64_t sum_block(index_t block_begin_row,
364  index_t block_begin_col, index_t block_size_row,
365  index_t block_size_col, bool no_diag=false);
366 
384  block_begin, index_t block_size, bool no_diag=true);
385 
408  index_t block_begin, index_t block_size, bool no_diag=true);
409 
439  index_t block_begin_row, index_t block_begin_col,
440  index_t block_size_row, index_t block_size_col,
441  bool no_diag=false);
442 #endif // HAVE_LINALG_LIB
443 
454  virtual void add_row_subset(SGVector<index_t> subset);
455 
465  virtual void add_row_subset_in_place(SGVector<index_t> subset);
466 
469  virtual void remove_row_subset();
470 
473  virtual void remove_all_row_subsets();
474 
476  virtual void row_subset_changed_post();
477 
488  virtual void add_col_subset(SGVector<index_t> subset);
489 
499  virtual void add_col_subset_in_place(SGVector<index_t> subset);
500 
503  virtual void remove_col_subset();
504 
507  virtual void remove_all_col_subsets();
508 
510  virtual void col_subset_changed_post();
511 
518  virtual int32_t get_num_vec_lhs()
519  {
522  }
523 
530  virtual int32_t get_num_vec_rhs()
531  {
534  }
535 
542  virtual bool has_features()
543  {
544  return (get_num_vec_lhs()>0) && (get_num_vec_rhs()>0);
545  }
546 
552  {
553  REQUIRE(!m_row_subset_stack, "%s::get_float32_kernel_matrix(): "
554  "Not possible with row subset active! If you want to"
555  " create a %s from another one with a subset, use "
556  "get_kernel_matrix() and the SGMatrix constructor!\n",
557  get_name(), get_name());
558 
559  REQUIRE(!m_col_subset_stack, "%s::get_float32_kernel_matrix(): "
560  "Not possible with collumn subset active! If you want to"
561  " create a %s from another one with a subset, use "
562  "get_kernel_matrix() and the SGMatrix constructor!\n",
563  get_name(), get_name());
564 
565  return kmatrix;
566  }
567 
568  protected:
569 
578  virtual float64_t compute(int32_t row, int32_t col)
579  {
580  REQUIRE(kmatrix.matrix, "%s::compute(%d, %d): No kenrel matrix "
581  "set!\n", get_name(), row, col);
582 
585 
586  if (upper_diagonal)
587  {
588  if (real_row <= real_col)
589  {
590  int64_t r=real_row;
591  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + real_col];
592  }
593  else
594  {
595  int64_t c=real_col;
596  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + real_row];
597  }
598  }
599  else
600  return kmatrix(real_row, real_col);
601  }
602 
603  protected:
604 
607 
610 
613 
616 
619 
621  bool m_free_km;
622 };
623 
624 }
625 #endif /* _CUSTOMKERNEL_H__ */
EKernelType
Definition: Kernel.h:57
virtual EKernelType get_kernel_type()
Definition: CustomKernel.h:108
int32_t index_t
Definition: common.h:62
virtual void add_row_subset(SGVector< index_t > subset)
SGMatrix< float32_t > kmatrix
Definition: CustomKernel.h:606
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1070
index_t get_size() const
Definition: SubsetStack.h:80
The Custom Kernel allows for custom user provided kernel matrices.
Definition: CustomKernel.h:36
virtual bool has_features()
Definition: CustomKernel.h:542
SGMatrix< float32_t > get_float32_kernel_matrix()
Definition: CustomKernel.h:551
virtual int32_t get_num_vec_lhs()
Definition: CustomKernel.h:518
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)
Definition: Kernel.cpp:1080
CSubsetStack * m_col_subset_stack
Definition: CustomKernel.h:618
bool set_triangle_kernel_matrix_from_full_generic(SGMatrix< T > full_kernel_matrix)
Definition: CustomKernel.h:218
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual int32_t get_num_vec_rhs()
Definition: CustomKernel.h:530
index_t num_cols
Definition: SGMatrix.h:376
virtual void cleanup()
virtual void remove_all_row_subsets()
index_t num_rows
Definition: SGMatrix.h:374
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
class to add subset support to another class. A CSubsetStackStack instance should be added and wrappe...
Definition: SubsetStack.h:37
virtual SGMatrix< float64_t > row_wise_sum_squared_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:1180
virtual void remove_col_subset()
bool set_triangle_kernel_matrix_from_triangle(SGVector< float64_t > tri_kernel_matrix)
Definition: CustomKernel.h:139
virtual void remove_row_subset()
virtual void add_col_subset(SGVector< index_t > subset)
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
virtual void row_subset_changed_post()
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)
Definition: Kernel.cpp:1239
virtual EFeatureClass get_feature_class()
Definition: CustomKernel.h:120
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:1027
double float64_t
Definition: common.h:50
bool set_full_kernel_matrix_from_full(SGMatrix< float64_t > full_kernel_matrix, bool check_symmetry=false)
Definition: CustomKernel.h:294
bool set_full_kernel_matrix_from_full(SGMatrix< float32_t > full_kernel_matrix, bool check_symmetry=false)
Definition: CustomKernel.h:263
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
virtual SGVector< float64_t > row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:1126
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1068
virtual bool dummy_init(int32_t rows, int32_t cols)
virtual void remove_all_col_subsets()
virtual const char * get_name() const
Definition: CustomKernel.h:126
float float32_t
Definition: common.h:49
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
bool is_symmetric()
Definition: SGMatrix.cpp:150
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual EFeatureType get_feature_type()
Definition: CustomKernel.h:114
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual void col_subset_changed_post()
virtual bool has_subsets() const
Definition: SubsetStack.h:89
bool set_triangle_kernel_matrix_from_full(SGMatrix< float64_t > full_kernel_matrix)
Definition: CustomKernel.h:204
The Kernel base class.
Definition: Kernel.h:159
bool set_triangle_kernel_matrix_from_triangle_generic(SGVector< T > tri_kernel_matrix)
Definition: CustomKernel.h:162
virtual void add_col_subset_in_place(SGVector< index_t > subset)
static float32_t sqrt(float32_t x)
Definition: Math.h:459
virtual float64_t compute(int32_t row, int32_t col)
Definition: CustomKernel.h:578
CSubsetStack * m_row_subset_stack
Definition: CustomKernel.h:615
virtual void add_row_subset_in_place(SGVector< index_t > subset)

SHOGUN Machine Learning Toolbox - Documentation