iRoCS Toolbox  1.1.0
Kernel_POLY.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: POLY kernel functor
26 ** $RCSfile$
27 ** $Revision: 2825 $$Name$
28 ** $Date: 2009-09-15 17:04:15 +0200 (Tue, 15 Sep 2009) $
29 ** Copyright: LGPL $Author: ronneber $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.6 2005/06/06 21:23:31 haasdonk
38 ** added updateCache() with two FV-lists, required for classification with precomputed kernel-matrices
39 **
40 ** Revision 1.5 2005/03/29 17:56:07 ronneber
41 ** - added clearCache()
42 **
43 ** Revision 1.4 2004/09/13 10:03:35 ronneber
44 ** - documentation update
45 **
46 ** Revision 1.3 2004/09/08 14:29:37 ronneber
47 ** - adapted to new ParamInfo class
48 **
49 ** Revision 1.2 2004/09/03 07:10:43 ronneber
50 ** - updateCache now takes an Accessor
51 **
52 ** Revision 1.1 2004/08/26 08:36:59 ronneber
53 ** initital import
54 **
55 ** Revision 1.6 2003/05/19 10:53:12 ronneber
56 ** - converted from MapTools to ParamMapWrapper
57 ** - added name()
58 **
59 ** Revision 1.5 2002/09/05 13:05:39 pigorsch
60 ** - modfified to use new MapTools
61 **
62 ** Revision 1.4 2002/05/21 18:22:40 ronneber
63 ** - now all parameters can be queried
64 **
65 ** Revision 1.3 2002/05/10 11:34:14 ronneber
66 ** - added methods to modify Parameters later
67 **
68 ** Revision 1.2 2002/05/10 11:07:03 ronneber
69 ** - removed FV template for all public classes, because Feature Vector type
70 ** can be extracted automatically from passed iterators or other
71 ** parameters -- this makes the public interface much more intuitive
72 **
73 ** Revision 1.1 2002/03/26 12:44:02 ronneber
74 ** restructured for autoconf
75 **
76 ** Revision 1.2 2002/01/14 13:52:47 pigorsch
77 ** * use FV.dotProduct(...)
78 **
79 ** Revision 1.1 2001/12/11 11:03:00 pigorsch
80 ** Initial Revision
81 **
82 **
83 **
84 **************************************************************************/
85 
86 #ifndef KERNEL_POLY_HH
87 #define KERNEL_POLY_HH
88 
89 #ifdef HAVE_CONFIG_H
90 #include <config.hh>
91 #endif
92 
93 // std includes
94 #include <cmath>
95 #include <map>
96 #include <set>
97 #include <string>
98 
99 // libsvmtl includes
100 #include "ProgressReporter.hh"
101 
102 // requirements of template parameters
104 
105 namespace svt
106 {
107  /*======================================================================*/
114  /*======================================================================*/
116  {
117  public:
119  double gamma=1.,
120  double coef0=0.,
121  double degree=3.)
122  :p_gamma(gamma),
123  p_coef0(coef0),
125  {};
126 
128  {};
129 
130  void setGamma( double gamma) { p_gamma = gamma; }
131  void setCoef0( double coef0) { p_coef0 = coef0; }
132  void setDegree( double degree) { p_degree = degree; }
133  double gamma() const { return p_gamma; }
134  double coef0() const { return p_coef0; }
135  double degree() const { return p_degree; }
136 
137 
138  template< typename ForwardIter, typename Accessor>
139  void updateCache( const ForwardIter&,
140  const ForwardIter&,
141  Accessor,
142  ProgressReporter*) const
143  {
144  // nothing to do here
145  }
146 
147  // new updateCache-Syntax:
148  template< typename ForwardIter1, typename Accessor1,
149  typename ForwardIter2, typename Accessor2 >
150  void updateCache( const ForwardIter1&,
151  const ForwardIter1&,
152  Accessor1,
153  const ForwardIter2&,
154  const ForwardIter2&,
155  Accessor2,
156  ProgressReporter* = 0) const
157  {
158  // nothing to do here
159  }
160 
161  void clearCache() const
162  {
163  // nothing to do here
164  }
165 
166  template< typename FV>
167  double k_function( const FV& x, const FV& y) const
168  {
169  return pow(p_gamma*x.dotProduct(y)+p_coef0, p_degree);
170  }
171 
172  template<typename STDATA>
173  void loadParameters( STDATA& stData)
174  {
176  stData.getValue( "gamma", p_gamma);
177  stData.getValue( "coef0", p_coef0);
178  stData.getValue( "degree", p_degree);
179 
180  }
181 
182  template<typename STDATA>
183  void saveParameters( STDATA& stData) const
184  {
186  stData.setValue( "kernel_type", name());
187  stData.setValue( "gamma", p_gamma);
188  stData.setValue( "coef0", p_coef0);
189  stData.setValue( "degree", p_degree);
190  }
191 
192  static const char* name()
193  {
194  return "poly";
195  }
196 
197  static const char* description()
198  {
199  return "polynomial kernel: (gamma*u'*v + coef0)^degree";
200  }
201  /*======================================================================*/
210  /*======================================================================*/
211  static void getParamInfos( std::vector<ParamInfo>& p)
212  {
213  p.push_back(
214  ParamInfo( "gamma", "g", "value",
215  "gamma for polynomial kernel. (default 1)"));
216  p.push_back(
217  ParamInfo( "coef0", "r", "value",
218  "coef0 for polynomial kernel. (default 0)"));
219  p.push_back(
220  ParamInfo( "degree", "d", "value",
221  "degree for polynomial kernel. (default 3)"));
222  }
223 
224  protected:
225  double p_gamma;
226  double p_coef0;
227  double p_degree;
228  };
229 }
230 
231 
232 #endif
void saveParameters(STDATA &stData) const
Definition: Kernel_POLY.hh:183
void updateCache(const ForwardIter1 &, const ForwardIter1 &, Accessor1, const ForwardIter2 &, const ForwardIter2 &, Accessor2, ProgressReporter *=0) const
Definition: Kernel_POLY.hh:150
#define CHECK_MEMBER_TEMPLATE(c)
double k_function(const FV &x, const FV &y) const
Definition: Kernel_POLY.hh:167
Kernel_POLY(double gamma=1., double coef0=0., double degree=3.)
Definition: Kernel_POLY.hh:118
void setDegree(double degree)
Definition: Kernel_POLY.hh:132
static const char * description()
Definition: Kernel_POLY.hh:197
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().
Definition: Kernel_POLY.hh:211
double coef0() const
Definition: Kernel_POLY.hh:134
void setGamma(double gamma)
Definition: Kernel_POLY.hh:130
void clearCache() const
Definition: Kernel_POLY.hh:161
void updateCache(const ForwardIter &, const ForwardIter &, Accessor, ProgressReporter *) const
Definition: Kernel_POLY.hh:139
double gamma() const
Definition: Kernel_POLY.hh:133
void loadParameters(STDATA &stData)
Definition: Kernel_POLY.hh:173
double degree() const
Definition: Kernel_POLY.hh:135
static const char * name()
Definition: Kernel_POLY.hh:192
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.
Definition: ParamInfo.hh:82
void setCoef0(double coef0)
Definition: Kernel_POLY.hh:131
Polynomial< CoeffT > pow(const Polynomial< CoeffT > &p, int exponential)
Power operator.
The Kernel_POLY class specifies a polynomial kernel function: (gamma*u&#39;*v + coef0)^degree.
Definition: Kernel_POLY.hh:115