SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _CUSTOMKERNEL_H___
12 #define _CUSTOMKERNEL_H___
13 
15 #include <shogun/lib/common.h>
16 #include <shogun/kernel/Kernel.h>
18 
19 namespace shogun
20 {
29 class CCustomKernel: public CKernel
30 {
31  void init();
32 
33  public:
35  CCustomKernel();
36 
43 
52 
56  virtual ~CCustomKernel();
57 
68  virtual bool dummy_init(int32_t rows, int32_t cols);
69 
76  virtual bool init(CFeatures* l, CFeatures* r);
77 
79  virtual void cleanup();
80 
85  inline virtual EKernelType get_kernel_type() { return K_CUSTOM; }
86 
91  inline virtual EFeatureType get_feature_type() { return F_ANY; }
92 
97  inline virtual EFeatureClass get_feature_class() { return C_ANY; }
98 
103  virtual const char* get_name() const { return "CustomKernel"; }
104 
115  SGVector<float64_t> tri_kernel_matrix)
116  {
117  return set_triangle_kernel_matrix_from_triangle_generic(tri_kernel_matrix);
118  }
119 
129  template <class T>
131  SGVector<T> tri_kernel_matrix)
132  {
133  ASSERT(tri_kernel_matrix.vector);
134 
135  int64_t len = tri_kernel_matrix.vlen;
136  int64_t cols = (int64_t) floor(-0.5 + CMath::sqrt(0.25+2*len));
137 
138  if (cols*(cols+1)/2 != len)
139  {
140  SG_ERROR("km should be a vector containing a lower triangle matrix, with len=cols*(cols+1)/2 elements\n");
141  return false;
142  }
143 
144  cleanup_custom();
145  SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols);
146 
148  kmatrix.num_rows=cols;
149  kmatrix.num_cols=cols;
150  upper_diagonal=true;
151 
152  for (int64_t i=0; i<len; i++)
153  kmatrix.matrix[i]=tri_kernel_matrix.vector[i];
154 
155  dummy_init(cols,cols);
156  return true;
157  }
158 
167  SGMatrix<float64_t> full_kernel_matrix)
168  {
169  return set_triangle_kernel_matrix_from_full_generic(full_kernel_matrix);
170  }
171 
177  template <class T>
179  SGMatrix<T> full_kernel_matrix)
180  {
181  int32_t rows = full_kernel_matrix.num_rows;
182  int32_t cols = full_kernel_matrix.num_cols;
183  ASSERT(rows==cols);
184 
185  cleanup_custom();
186  SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols);
187 
188  kmatrix.matrix = SG_MALLOC(float32_t, int64_t(rows)*cols);
189  kmatrix.num_rows = rows;
190  kmatrix.num_cols = cols;
191  upper_diagonal = false;
192 
193  for (int64_t row=0; row<rows; row++)
194  {
195  for (int64_t col=row; col<cols; col++)
196  {
197  int64_t idx=row * cols - row*(row+1)/2 + col;
198  kmatrix.matrix[idx] = full_kernel_matrix.matrix[col*rows+row];
199  }
200  }
201 
202  dummy_init(rows, cols);
203  return true;
204  }
205 
213  SGMatrix<float32_t> full_kernel_matrix)
214  {
215  cleanup_custom();
216  kmatrix.matrix = full_kernel_matrix.matrix;
217  kmatrix.num_rows=full_kernel_matrix.num_rows;
218  kmatrix.num_cols=full_kernel_matrix.num_cols;
220  return true;
221  }
222 
230  SGMatrix<float64_t> full_kernel_matrix)
231  {
232  cleanup_custom();
233  int32_t rows=full_kernel_matrix.num_rows;
234  int32_t cols=full_kernel_matrix.num_cols;
235  SG_DEBUG( "using custom kernel of size %dx%d\n", rows,cols);
236 
237  kmatrix.matrix = SG_MALLOC(float32_t, int64_t(rows)*cols);
238  kmatrix.num_rows = rows;
239  kmatrix.num_cols = cols;
240  upper_diagonal = false;
241 
242  for (int32_t row=0; row<rows; row++)
243  {
244  for (int32_t col=0; col<cols; col++)
245  kmatrix.matrix[int64_t(row) * cols + col] =
246  full_kernel_matrix.matrix[int64_t(col)*rows+row];
247  }
248 
249  dummy_init(rows, cols);
250 
251  full_kernel_matrix.free_matrix();
252  return true;
253  }
254 
259  virtual inline int32_t get_num_vec_lhs()
260  {
261  return kmatrix.num_rows;
262  }
263 
268  virtual inline int32_t get_num_vec_rhs()
269  {
270  return kmatrix.num_cols;
271  }
272 
277  virtual inline bool has_features()
278  {
279  return (kmatrix.num_rows>0) && (kmatrix.num_cols>0);
280  }
281 
282  protected:
283 
290  inline virtual float64_t compute(int32_t row, int32_t col)
291  {
293 
294  if (upper_diagonal)
295  {
296  if (row <= col)
297  {
298  int64_t r=row;
299  return kmatrix.matrix[r*kmatrix.num_rows - r*(r+1)/2 + col];
300  }
301  else
302  {
303  int64_t c=col;
304  return kmatrix.matrix[c*kmatrix.num_cols - c*(c+1)/2 + row];
305  }
306  }
307  else
308  {
309  int64_t r=row;
310  return kmatrix.matrix[r*kmatrix.num_cols+col];
311  }
312  }
313 
314  private:
315 
317  void cleanup_custom();
318 
319  protected:
320 
323 
326 };
327 
328 }
329 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation