SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CustomKernel.cpp
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 #include <shogun/lib/common.h>
17 #include <shogun/io/SGIO.h>
19 
20 using namespace shogun;
21 
22 void
23 CCustomKernel::init()
24 {
29  m_free_km=true;
30 
31  SG_ADD((CSGObject**)&m_row_subset_stack, "row_subset_stack",
32  "Subset stack of rows", MS_NOT_AVAILABLE);
33  SG_ADD((CSGObject**)&m_col_subset_stack, "col_subset_stack",
34  "Subset stack of columns", MS_NOT_AVAILABLE);
35  SG_ADD(&m_free_km, "free_km", "Whether kernel matrix should be freed in "
36  "destructor", MS_NOT_AVAILABLE);
37  SG_ADD(&kmatrix, "kmatrix", "Kernel matrix.", MS_NOT_AVAILABLE);
38  SG_ADD(&upper_diagonal, "upper_diagonal", "Upper diagonal", MS_NOT_AVAILABLE);
39 
40  /* new parameter from param version 0 to 1 */
41  m_parameter_map->put(
42  new SGParamInfo("free_km", CT_SCALAR, ST_NONE, PT_BOOL, 1),
43  new SGParamInfo()
44  );
45 
46  /* new parameter from param version 0 to 1 */
47  m_parameter_map->put(
48  new SGParamInfo("row_subset_stack", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1),
49  new SGParamInfo()
50  );
51 
52  /* new parameter from param version 0 to 1 */
53  m_parameter_map->put(
54  new SGParamInfo("col_subset_stack", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1),
55  new SGParamInfo()
56  );
57  m_parameter_map->finalize_map();
58 }
59 
61 : CKernel(10), kmatrix(), upper_diagonal(false)
62 {
63  SG_DEBUG("created CCustomKernel\n")
64  init();
65 }
66 
68 : CKernel(10)
69 {
70  SG_DEBUG("created CCustomKernel\n")
71  init();
72 
73  /* if constructed from a custom kernel, use same kernel matrix */
74  if (k->get_kernel_type()==K_CUSTOM)
75  {
76  CCustomKernel* casted=(CCustomKernel*)k;
78  m_free_km=false;
79  }
80  else
82 }
83 
85 : CKernel(10), upper_diagonal(false)
86 {
87  SG_DEBUG("Entering CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n")
88  init();
90  SG_DEBUG("Leaving CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n")
91 }
92 
94 : CKernel(10), upper_diagonal(false)
95 {
96  SG_DEBUG("Entering CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n")
97  init();
99  SG_DEBUG("Leaving CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n")
100 }
101 
103 {
104  SG_DEBUG("Entering CCustomKernel::~CCustomKernel()\n")
105  cleanup();
108  SG_DEBUG("Leaving CCustomKernel::~CCustomKernel()\n")
109 }
110 
111 bool CCustomKernel::dummy_init(int32_t rows, int32_t cols)
112 {
113  return init(new CDummyFeatures(rows), new CDummyFeatures(cols));
114 }
115 
116 bool CCustomKernel::init(CFeatures* l, CFeatures* r)
117 {
118  /* make it possible to call with NULL values since features are useless
119  * for custom kernel matrix */
120  if (!l)
121  l=lhs;
122 
123  if (!r)
124  r=rhs;
125 
126  /* Make sure l and r should not be NULL */
127  REQUIRE(l, "CFeatures l should not be NULL\n")
128  REQUIRE(r, "CFeatures r should not be NULL\n")
129  /* Make sure l and r have the same type of CFeatures */
131  "Different FeatureClass: l is %d, r is %d\n",
134  "Different FeatureType: l is %d, r is %d\n",
136 
137  /* If l and r are the type of CIndexFeatures,
138  * the init function adds a subset to kernel matrix.
139  * Then call get_kernel_matrix will get the submatrix
140  * of the kernel matrix.
141  */
143  {
144  CIndexFeatures* l_idx = (CIndexFeatures*)l;
145  CIndexFeatures* r_idx = (CIndexFeatures*)r;
146 
149 
152 
153  return true;
154  }
155 
156  /* For other types of CFeatures do the default actions below */
157  CKernel::init(l, r);
158 
159  SG_DEBUG("num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), kmatrix.num_rows)
160  SG_DEBUG("num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), kmatrix.num_cols)
163  return init_normalizer();
164 }
165 
167 {
168  SG_DEBUG("Entering CCustomKernel::cleanup_custom()\n")
171 
173  upper_diagonal=false;
174 
175  SG_DEBUG("Leaving CCustomKernel::cleanup_custom()\n")
176 }
177 
179 {
182  cleanup_custom();
184 }
185 
187 {
190 }
191 
193 {
196 }
197 
199 {
202 }
203 
205 {
208 }
209 
211 {
214  else
216 }
217 
219 {
222 }
223 
225 {
228 }
229 
231 {
234 }
235 
237 {
240 }
241 
243 {
246  else
248 }

SHOGUN Machine Learning Toolbox - Documentation