SHOGUN  v3.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  virtual EKernelType get_kernel_type() { return K_CUSTOM; }
106 
111  virtual EFeatureType get_feature_type() { return F_ANY; }
112 
117  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 int32_t get_num_vec_lhs()
344  {
347  }
348 
355  virtual int32_t get_num_vec_rhs()
356  {
359  }
360 
367  virtual bool has_features()
368  {
369  return (get_num_vec_lhs()>0) && (get_num_vec_rhs()>0);
370  }
371 
377  {
378  REQUIRE(!m_row_subset_stack, "%s::get_float32_kernel_matrix(): "
379  "Not possible with row subset active! If you want to"
380  " create a %s from another one with a subset, use "
381  "get_kernel_matrix() and the SGMatrix constructor!\n",
382  get_name(), get_name());
383 
384  REQUIRE(!m_col_subset_stack, "%s::get_float32_kernel_matrix(): "
385  "Not possible with collumn subset active! If you want to"
386  " create a %s from another one with a subset, use "
387  "get_kernel_matrix() and the SGMatrix constructor!\n",
388  get_name(), get_name());
389 
390  return kmatrix;
391  }
392 
393  protected:
394 
403  virtual float64_t compute(int32_t row, int32_t col)
404  {
405  REQUIRE(kmatrix.matrix, "%s::compute(%d, %d): No kenrel matrix "
406  "set!\n", get_name(), row, col);
407 
410 
411  if (upper_diagonal)
412  {
413  if (real_row <= real_col)
414  {
415  int64_t r=real_row;
416  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + real_col];
417  }
418  else
419  {
420  int64_t c=real_col;
421  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + real_row];
422  }
423  }
424  else
425  return kmatrix(real_row, real_col);
426  }
427 
428  protected:
429 
432 
435 
440 
442  bool m_free_km;
443 };
444 
445 }
446 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation