SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ProductKernel.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  * Copyright (C) 2012 Jacob Walker
8  *
9  * Code adapted from CCombinedKernel
10  */
11 
12 #ifndef _PRODUCTKERNEL_H___
13 #define _PRODUCTKERNEL_H___
14 
15 #include <shogun/lib/config.h>
16 
18 #include <shogun/io/SGIO.h>
19 #include <shogun/kernel/Kernel.h>
20 
23 
24 namespace shogun
25 {
26 class CFeatures;
27 class CCombinedFeatures;
28 class CDynamicObjectArray;
29 
41 class CProductKernel : public CKernel
42 {
43  public:
48  CProductKernel(int32_t size=10);
49 
50  virtual ~CProductKernel();
51 
58  virtual bool init(CFeatures* lhs, CFeatures* rhs);
59 
61  virtual void cleanup();
62 
67  virtual EKernelType get_kernel_type() { return K_PRODUCT; }
68 
73  virtual EFeatureType get_feature_type() { return F_UNKNOWN; }
74 
80 
85  virtual const char* get_name() const { return "ProductKernel"; }
86 
88  void list_kernels();
89 
95  inline CKernel* get_kernel(int32_t idx)
96  {
97  return (CKernel*) kernel_array->get_element(idx);
98  }
99 
107  inline bool insert_kernel(CKernel* k, int32_t idx)
108  {
109  ASSERT(k)
111 
112  if (!(k->has_property(KP_LINADD)))
114 
115  return kernel_array->insert_element(k, idx);
116  }
117 
123  inline bool append_kernel(CKernel* k)
124  {
125  ASSERT(k)
127 
128  if (!(k->has_property(KP_LINADD)))
130 
131  int32_t n = get_num_subkernels();
133  return n+1==get_num_subkernels();
134  }
135 
141  inline bool delete_kernel(int32_t idx)
142  {
143  return kernel_array->delete_element(idx);
144  }
145 
150  inline int32_t get_num_subkernels()
151  {
152  return kernel_array->get_num_elements();
153  }
154 
159  virtual bool has_features()
160  {
161  return initialized;
162  }
163 
165  virtual void remove_lhs();
166 
168  virtual void remove_rhs();
169 
171  virtual void remove_lhs_and_rhs();
172 
174  bool precompute_subkernels();
175 
180  {
181  return dynamic_cast<CProductKernel*>(n);
182  }
183 
192  index_t index=-1);
193 
199  {
201  return kernel_array;
202  }
203 
204  protected:
211  virtual float64_t compute(int32_t x, int32_t y);
212 
219  {
220  ASSERT(k)
221 
222  if (k->get_num_vec_lhs())
223  {
224  if (num_lhs)
227 
228  if (!get_num_subkernels())
229  {
230  initialized=true;
231 #ifdef USE_SVMLIGHT
232  cache_reset();
233 #endif //USE_SVMLIGHT
234  }
235  }
236  else
237  initialized=false;
238 
239  if (k->get_num_vec_rhs())
240  {
241  if (num_rhs)
244 
245  if (!get_num_subkernels())
246  {
247  initialized=true;
248 #ifdef USE_SVMLIGHT
249  cache_reset();
250 #endif //USE_SVMLIGHT
251  }
252  }
253  else
254  initialized=false;
255  }
256 
257  private:
258  void init();
259 
260  protected:
265 };
266 }
267 #endif /* _PRODUCTKERNEL_H__ */
EKernelType
Definition: Kernel.h:57
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1070
The Product kernel is used to combine a number of kernels into a single ProductKernel object by eleme...
Definition: ProductKernel.h:41
virtual EFeatureClass get_feature_class()
Definition: ProductKernel.h:79
virtual EFeatureType get_feature_type()
Definition: ProductKernel.h:73
virtual bool has_features()
bool delete_kernel(int32_t idx)
bool insert_element(CSGObject *e, int32_t index)
SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
parameter struct
CProductKernel(int32_t size=10)
void cache_reset()
Definition: Kernel.h:603
virtual void remove_lhs_and_rhs()
virtual EKernelType get_kernel_type()
Definition: ProductKernel.h:67
CDynamicObjectArray * get_array()
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
#define SG_REF(x)
Definition: SGObject.h:54
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
virtual void remove_rhs()
bool has_property(EKernelProperty p)
Definition: Kernel.h:724
virtual void remove_lhs()
#define ASSERT(x)
Definition: SGIO.h:201
void adjust_num_lhs_rhs_initialized(CKernel *k)
double float64_t
Definition: common.h:50
virtual const char * get_name() const
Definition: ProductKernel.h:85
void unset_property(EKernelProperty p)
Definition: Kernel.h:891
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1068
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
CDynamicObjectArray * kernel_array
bool insert_kernel(CKernel *k, int32_t idx)
CKernel * get_kernel(int32_t idx)
Definition: ProductKernel.h:95
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CProductKernel * KernelToProductKernel(CKernel *n)
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual float64_t compute(int32_t x, int32_t y)
The Kernel base class.
Definition: Kernel.h:159
CSGObject * get_element(int32_t index) const
bool append_kernel(CKernel *k)
virtual bool init(CFeatures *lhs, CFeatures *rhs)

SHOGUN Machine Learning Toolbox - Documentation