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_LINALG_LIB
322 #ifdef HAVE_EIGEN3
323 
340  virtual float64_t sum_symmetric_block(index_t block_begin,
341  index_t block_size, bool no_diag=true);
342 
364  virtual float64_t sum_block(index_t block_begin_row,
365  index_t block_begin_col, index_t block_size_row,
366  index_t block_size_col, bool no_diag=false);
367 
385  block_begin, index_t block_size, bool no_diag=true);
386 
409  index_t block_begin, index_t block_size, bool no_diag=true);
410 
440  index_t block_begin_row, index_t block_begin_col,
441  index_t block_size_row, index_t block_size_col,
442  bool no_diag=false);
443 #endif // HAVE_EIGEN3
444 #endif // HAVE_LINALG_LIB
445 
456  virtual void add_row_subset(SGVector<index_t> subset);
457 
467  virtual void add_row_subset_in_place(SGVector<index_t> subset);
468 
471  virtual void remove_row_subset();
472 
475  virtual void remove_all_row_subsets();
476 
478  virtual void row_subset_changed_post();
479 
490  virtual void add_col_subset(SGVector<index_t> subset);
491 
501  virtual void add_col_subset_in_place(SGVector<index_t> subset);
502 
505  virtual void remove_col_subset();
506 
509  virtual void remove_all_col_subsets();
510 
512  virtual void col_subset_changed_post();
513 
520  virtual int32_t get_num_vec_lhs()
521  {
524  }
525 
532  virtual int32_t get_num_vec_rhs()
533  {
536  }
537 
544  virtual bool has_features()
545  {
546  return (get_num_vec_lhs()>0) && (get_num_vec_rhs()>0);
547  }
548 
554  {
555  REQUIRE(!m_row_subset_stack, "%s::get_float32_kernel_matrix(): "
556  "Not possible with row subset active! If you want to"
557  " create a %s from another one with a subset, use "
558  "get_kernel_matrix() and the SGMatrix constructor!\n",
559  get_name(), get_name());
560 
561  REQUIRE(!m_col_subset_stack, "%s::get_float32_kernel_matrix(): "
562  "Not possible with collumn subset active! If you want to"
563  " create a %s from another one with a subset, use "
564  "get_kernel_matrix() and the SGMatrix constructor!\n",
565  get_name(), get_name());
566 
567  return kmatrix;
568  }
569 
570  protected:
571 
580  virtual float64_t compute(int32_t row, int32_t col)
581  {
582  REQUIRE(kmatrix.matrix, "%s::compute(%d, %d): No kenrel matrix "
583  "set!\n", get_name(), row, col);
584 
587 
588  if (upper_diagonal)
589  {
590  if (real_row <= real_col)
591  {
592  int64_t r=real_row;
593  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + real_col];
594  }
595  else
596  {
597  int64_t c=real_col;
598  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + real_row];
599  }
600  }
601  else
602  return kmatrix(real_row, real_col);
603  }
604 
605  protected:
606 
609 
612 
615 
618 
621 
623  bool m_free_km;
624 };
625 
626 }
627 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation