SHOGUN  v2.0.0
 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 
16 #include <shogun/lib/common.h>
17 #include <shogun/kernel/Kernel.h>
19 
20 namespace shogun
21 {
33 class CCustomKernel: public CKernel
34 {
35  void init();
36 
37  public:
39  CCustomKernel();
40 
47 
56 
65 
69  virtual ~CCustomKernel();
70 
83  virtual bool dummy_init(int32_t rows, int32_t cols);
84 
93  virtual bool init(CFeatures* l, CFeatures* r);
94 
96  virtual void cleanup();
97 
99  void cleanup_custom();
100 
105  inline virtual EKernelType get_kernel_type() { return K_CUSTOM; }
106 
111  inline virtual EFeatureType get_feature_type() { return F_ANY; }
112 
117  inline virtual EFeatureClass get_feature_class() { return C_ANY; }
118 
123  virtual const char* get_name() const { return "CustomKernel"; }
124 
137  SGVector<float64_t> tri_kernel_matrix)
138  {
140  {
141  SG_ERROR("%s::set_triangle_kernel_matrix_from_triangle not"
142  " possible with subset. Remove first\n", get_name());
143  }
144  return set_triangle_kernel_matrix_from_triangle_generic(tri_kernel_matrix);
145  }
146 
158  template <class T>
160  SGVector<T> tri_kernel_matrix)
161  {
163  {
164  SG_ERROR("%s::set_triangle_kernel_matrix_from_triangle_generic "
165  "not possible with subset. Remove first\n", get_name());
166  }
167  ASSERT(tri_kernel_matrix.vector);
168 
169  int64_t len = tri_kernel_matrix.vlen;
170  int64_t cols = (int64_t) floor(-0.5 + CMath::sqrt(0.25+2*len));
171 
172  if (cols*(cols+1)/2 != len)
173  {
174  SG_ERROR("km should be a vector containing a lower triangle matrix, with len=cols*(cols+1)/2 elements\n");
175  return false;
176  }
177 
178  cleanup_custom();
179  SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols);
180 
181  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, len), cols, cols);
182  upper_diagonal=true;
183 
184  for (int64_t i=0; i<len; i++)
185  kmatrix.matrix[i]=tri_kernel_matrix.vector[i];
186 
187  dummy_init(cols,cols);
188  return true;
189  }
190 
201  SGMatrix<float64_t> full_kernel_matrix)
202  {
203  return set_triangle_kernel_matrix_from_full_generic(full_kernel_matrix);
204  }
205 
213  template <class T>
215  SGMatrix<T> full_kernel_matrix)
216  {
218  {
219  SG_ERROR("%s::set_triangle_kernel_matrix_from_full_generic "
220  "not possible with subset. Remove first\n", get_name());
221  }
222 
223  int32_t rows = full_kernel_matrix.num_rows;
224  int32_t cols = full_kernel_matrix.num_cols;
225  ASSERT(rows==cols);
226 
227  cleanup_custom();
228  SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols);
229 
230  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, cols*(cols+1)/2), rows, cols);
231  upper_diagonal = true;
232 
233  for (int64_t row=0; row<rows; row++)
234  {
235  for (int64_t col=row; col<cols; col++)
236  {
237  int64_t idx=row * cols - row*(row+1)/2 + col;
238  kmatrix.matrix[idx] = full_kernel_matrix.matrix[col*rows+row];
239  }
240  }
241 
242  dummy_init(rows, cols);
243  return true;
244  }
245 
255  SGMatrix<float32_t> full_kernel_matrix)
256  {
258  {
259  SG_ERROR("%s::set_full_kernel_matrix_from_full "
260  "not possible with subset. Remove first\n", get_name());
261  }
262 
263  cleanup_custom();
264  kmatrix=full_kernel_matrix;
266  return true;
267  }
268 
278  SGMatrix<float64_t> full_kernel_matrix)
279  {
281  {
282  SG_ERROR("%s::set_full_kernel_matrix_from_full "
283  "not possible with subset. Remove first\n", get_name());
284  }
285 
286  cleanup_custom();
287  int32_t rows=full_kernel_matrix.num_rows;
288  int32_t cols=full_kernel_matrix.num_cols;
289  SG_DEBUG( "using custom kernel of size %dx%d\n", rows,cols);
290 
291  kmatrix=SGMatrix<float32_t>(rows,cols);
292  upper_diagonal = false;
293 
294  for (int64_t i=0; i<int64_t(rows) * cols; i++)
295  kmatrix.matrix[i]=full_kernel_matrix.matrix[i];
296 
297  dummy_init(kmatrix.num_rows, kmatrix.num_cols);
298  return true;
299  }
300 
306  virtual void add_row_subset(SGVector<index_t> subset);
307 
310  virtual void remove_row_subset();
311 
314  virtual void remove_all_row_subsets();
315 
317  virtual void row_subset_changed_post();
318 
324  virtual void add_col_subset(SGVector<index_t> subset);
325 
328  virtual void remove_col_subset();
329 
332  virtual void remove_all_col_subsets();
333 
335  virtual void col_subset_changed_post();
336 
343  virtual inline int32_t get_num_vec_lhs()
344  {
347  }
348 
355  virtual inline int32_t get_num_vec_rhs()
356  {
359  }
360 
367  virtual inline bool has_features()
368  {
369  return (get_num_vec_lhs()>0) && (get_num_vec_rhs()>0);
370  }
371 
376  void print_kernel_matrix(const char* prefix="") const;
377 
383  {
384  return kmatrix;
385  }
386 
387  protected:
388 
397  inline virtual float64_t compute(int32_t row, int32_t col)
398  {
400 
403 
404  if (upper_diagonal)
405  {
406  if (real_row <= real_col)
407  {
408  int64_t r=real_row;
409  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + real_col];
410  }
411  else
412  {
413  int64_t c=real_col;
414  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + real_row];
415  }
416  }
417  else
418  {
419  int64_t r=real_row;
420  return kmatrix.matrix[r*kmatrix.num_cols+real_col];
421  }
422  }
423 
424  protected:
425 
428 
431 
436 
438  bool m_free_km;
439 };
440 
441 }
442 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation