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  dummy_init(cols,cols);
191  return true;
192  }
193 
204  SGMatrix<float64_t> full_kernel_matrix)
205  {
206  return set_triangle_kernel_matrix_from_full_generic(full_kernel_matrix);
207  }
208 
216  template <class T>
218  SGMatrix<T> full_kernel_matrix)
219  {
221  {
222  SG_ERROR("%s::set_triangle_kernel_matrix_from_full_generic "
223  "not possible with subset. Remove first\n", get_name());
224  }
225 
226  int32_t rows = full_kernel_matrix.num_rows;
227  int32_t cols = full_kernel_matrix.num_cols;
228  ASSERT(rows==cols)
229 
230  cleanup_custom();
231  SG_DEBUG("using custom kernel of size %dx%d\n", cols,cols)
232 
233  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, cols*(cols+1)/2), rows, cols);
234  upper_diagonal = true;
235 
236  for (int64_t row=0; row<rows; row++)
237  {
238  for (int64_t col=row; col<cols; col++)
239  {
240  int64_t idx=row * cols - row*(row+1)/2 + col;
241  kmatrix.matrix[idx] = full_kernel_matrix.matrix[col*rows+row];
242  }
243  }
244 
245  dummy_init(rows, cols);
246  return true;
247  }
248 
258  SGMatrix<float32_t> full_kernel_matrix)
259  {
261  {
262  SG_ERROR("%s::set_full_kernel_matrix_from_full "
263  "not possible with subset. Remove first\n", get_name());
264  }
265 
266  cleanup_custom();
267  kmatrix=full_kernel_matrix;
269  return true;
270  }
271 
281  SGMatrix<float64_t> full_kernel_matrix)
282  {
284  {
285  SG_ERROR("%s::set_full_kernel_matrix_from_full "
286  "not possible with subset. Remove first\n", get_name());
287  }
288 
289  cleanup_custom();
290  int32_t rows=full_kernel_matrix.num_rows;
291  int32_t cols=full_kernel_matrix.num_cols;
292  SG_DEBUG("using custom kernel of size %dx%d\n", rows,cols)
293 
294  kmatrix=SGMatrix<float32_t>(rows,cols);
295  upper_diagonal = false;
296 
297  for (int64_t i=0; i<int64_t(rows) * cols; i++)
298  kmatrix.matrix[i]=full_kernel_matrix.matrix[i];
299 
300  dummy_init(kmatrix.num_rows, kmatrix.num_cols);
301  return true;
302  }
303 
314  virtual void add_row_subset(SGVector<index_t> subset);
315 
325  virtual void add_row_subset_in_place(SGVector<index_t> subset);
326 
329  virtual void remove_row_subset();
330 
333  virtual void remove_all_row_subsets();
334 
336  virtual void row_subset_changed_post();
337 
348  virtual void add_col_subset(SGVector<index_t> subset);
349 
359  virtual void add_col_subset_in_place(SGVector<index_t> subset);
360 
363  virtual void remove_col_subset();
364 
367  virtual void remove_all_col_subsets();
368 
370  virtual void col_subset_changed_post();
371 
378  virtual int32_t get_num_vec_lhs()
379  {
382  }
383 
390  virtual int32_t get_num_vec_rhs()
391  {
394  }
395 
402  virtual bool has_features()
403  {
404  return (get_num_vec_lhs()>0) && (get_num_vec_rhs()>0);
405  }
406 
412  {
413  REQUIRE(!m_row_subset_stack, "%s::get_float32_kernel_matrix(): "
414  "Not possible with row subset active! If you want to"
415  " create a %s from another one with a subset, use "
416  "get_kernel_matrix() and the SGMatrix constructor!\n",
417  get_name(), get_name());
418 
419  REQUIRE(!m_col_subset_stack, "%s::get_float32_kernel_matrix(): "
420  "Not possible with collumn subset active! If you want to"
421  " create a %s from another one with a subset, use "
422  "get_kernel_matrix() and the SGMatrix constructor!\n",
423  get_name(), get_name());
424 
425  return kmatrix;
426  }
427 
428  protected:
429 
438  virtual float64_t compute(int32_t row, int32_t col)
439  {
440  REQUIRE(kmatrix.matrix, "%s::compute(%d, %d): No kenrel matrix "
441  "set!\n", get_name(), row, col);
442 
445 
446  if (upper_diagonal)
447  {
448  if (real_row <= real_col)
449  {
450  int64_t r=real_row;
451  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + real_col];
452  }
453  else
454  {
455  int64_t c=real_col;
456  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + real_row];
457  }
458  }
459  else
460  return kmatrix(real_row, real_col);
461  }
462 
463  protected:
464 
467 
470 
475 
477  bool m_free_km;
478 };
479 
480 }
481 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation