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 {
35 class CCustomKernel: public CKernel
36 {
37  void init();
38 
39  public:
41  CCustomKernel();
42 
49 
58 
67 
71  virtual ~CCustomKernel();
72 
85  virtual bool dummy_init(int32_t rows, int32_t cols);
86 
95  virtual bool init(CFeatures* l, CFeatures* r);
96 
98  virtual void cleanup();
99 
101  void cleanup_custom();
102 
107  virtual EKernelType get_kernel_type() { return K_CUSTOM; }
108 
113  virtual EFeatureType get_feature_type() { return F_ANY; }
114 
119  virtual EFeatureClass get_feature_class() { return C_ANY; }
120 
125  virtual const char* get_name() const { return "CustomKernel"; }
126 
139  SGVector<float64_t> tri_kernel_matrix)
140  {
142  {
143  SG_ERROR("%s::set_triangle_kernel_matrix_from_triangle not"
144  " possible with subset. Remove first\n", get_name());
145  }
146  return set_triangle_kernel_matrix_from_triangle_generic(tri_kernel_matrix);
147  }
148 
160  template <class T>
162  SGVector<T> tri_kernel_matrix)
163  {
165  {
166  SG_ERROR("%s::set_triangle_kernel_matrix_from_triangle_generic "
167  "not possible with subset. Remove first\n", get_name());
168  }
169  ASSERT(tri_kernel_matrix.vector)
170 
171  int64_t len = tri_kernel_matrix.vlen;
172  int64_t cols = (int64_t) floor(-0.5 + CMath::sqrt(0.25+2*len));
173 
174  if (cols*(cols+1)/2 != len)
175  {
176  SG_ERROR("km should be a vector containing a lower triangle matrix, with len=cols*(cols+1)/2 elements\n")
177  return false;
178  }
179 
180  cleanup_custom();
181  SG_DEBUG("using custom kernel of size %dx%d\n", cols,cols)
182 
183  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, len), cols, cols);
184  upper_diagonal=true;
185 
186  for (int64_t i=0; i<len; i++)
187  kmatrix.matrix[i]=tri_kernel_matrix.vector[i];
188 
189  dummy_init(cols,cols);
190  return true;
191  }
192 
203  SGMatrix<float64_t> full_kernel_matrix)
204  {
205  return set_triangle_kernel_matrix_from_full_generic(full_kernel_matrix);
206  }
207 
215  template <class T>
217  SGMatrix<T> full_kernel_matrix)
218  {
220  {
221  SG_ERROR("%s::set_triangle_kernel_matrix_from_full_generic "
222  "not possible with subset. Remove first\n", get_name());
223  }
224 
225  int32_t rows = full_kernel_matrix.num_rows;
226  int32_t cols = full_kernel_matrix.num_cols;
227  ASSERT(rows==cols)
228 
229  cleanup_custom();
230  SG_DEBUG("using custom kernel of size %dx%d\n", cols,cols)
231 
232  kmatrix=SGMatrix<float32_t>(SG_MALLOC(float32_t, cols*(cols+1)/2), rows, cols);
233  upper_diagonal = true;
234 
235  for (int64_t row=0; row<rows; row++)
236  {
237  for (int64_t col=row; col<cols; col++)
238  {
239  int64_t idx=row * cols - row*(row+1)/2 + col;
240  kmatrix.matrix[idx] = full_kernel_matrix.matrix[col*rows+row];
241  }
242  }
243 
244  dummy_init(rows, cols);
245  return true;
246  }
247 
257  SGMatrix<float32_t> full_kernel_matrix)
258  {
260  {
261  SG_ERROR("%s::set_full_kernel_matrix_from_full "
262  "not possible with subset. Remove first\n", get_name());
263  }
264 
265  cleanup_custom();
266  kmatrix=full_kernel_matrix;
268  return true;
269  }
270 
280  SGMatrix<float64_t> full_kernel_matrix)
281  {
283  {
284  SG_ERROR("%s::set_full_kernel_matrix_from_full "
285  "not possible with subset. Remove first\n", get_name());
286  }
287 
288  cleanup_custom();
289  int32_t rows=full_kernel_matrix.num_rows;
290  int32_t cols=full_kernel_matrix.num_cols;
291  SG_DEBUG("using custom kernel of size %dx%d\n", rows,cols)
292 
293  kmatrix=SGMatrix<float32_t>(rows,cols);
294  upper_diagonal = false;
295 
296  for (int64_t i=0; i<int64_t(rows) * cols; i++)
297  kmatrix.matrix[i]=full_kernel_matrix.matrix[i];
298 
299  dummy_init(kmatrix.num_rows, kmatrix.num_cols);
300  return true;
301  }
302 
308  virtual void add_row_subset(SGVector<index_t> subset);
309 
312  virtual void remove_row_subset();
313 
316  virtual void remove_all_row_subsets();
317 
319  virtual void row_subset_changed_post();
320 
326  virtual void add_col_subset(SGVector<index_t> subset);
327 
330  virtual void remove_col_subset();
331 
334  virtual void remove_all_col_subsets();
335 
337  virtual void col_subset_changed_post();
338 
345  virtual int32_t get_num_vec_lhs()
346  {
349  }
350 
357  virtual int32_t get_num_vec_rhs()
358  {
361  }
362 
369  virtual bool has_features()
370  {
371  return (get_num_vec_lhs()>0) && (get_num_vec_rhs()>0);
372  }
373 
379  {
380  REQUIRE(!m_row_subset_stack, "%s::get_float32_kernel_matrix(): "
381  "Not possible with row subset active! If you want to"
382  " create a %s from another one with a subset, use "
383  "get_kernel_matrix() and the SGMatrix constructor!\n",
384  get_name(), get_name());
385 
386  REQUIRE(!m_col_subset_stack, "%s::get_float32_kernel_matrix(): "
387  "Not possible with collumn subset active! If you want to"
388  " create a %s from another one with a subset, use "
389  "get_kernel_matrix() and the SGMatrix constructor!\n",
390  get_name(), get_name());
391 
392  return kmatrix;
393  }
394 
395  protected:
396 
405  virtual float64_t compute(int32_t row, int32_t col)
406  {
407  REQUIRE(kmatrix.matrix, "%s::compute(%d, %d): No kenrel matrix "
408  "set!\n", get_name(), row, col);
409 
412 
413  if (upper_diagonal)
414  {
415  if (real_row <= real_col)
416  {
417  int64_t r=real_row;
418  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + real_col];
419  }
420  else
421  {
422  int64_t c=real_col;
423  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + real_row];
424  }
425  }
426  else
427  return kmatrix(real_row, real_col);
428  }
429 
430  protected:
431 
434 
437 
442 
444  bool m_free_km;
445 };
446 
447 }
448 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation