SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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_EIGEN3
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_EIGEN3
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__ */

SHOGUN Machine Learning Toolbox - Documentation