iRoCS Toolbox  1.1.0
Kernel_RBF.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: RBF 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.7 2005/10/26 07:06:38 ronneber
38 ** - added gradient_of_k_function()
39 **
40 ** Revision 1.6 2005/06/06 21:23:31 haasdonk
41 ** added updateCache() with two FV-lists, required for classification with precomputed kernel-matrices
42 **
43 ** Revision 1.5 2005/03/29 17:56:25 ronneber
44 ** - added clearCache()
45 **
46 ** Revision 1.4 2004/09/13 10:04:04 ronneber
47 ** - documentation update
48 **
49 ** Revision 1.3 2004/09/08 14:30:00 ronneber
50 ** - adapted to new ParamInfo class
51 **
52 ** Revision 1.2 2004/09/03 07:10:51 ronneber
53 ** - updateCache now takes an Accessor
54 **
55 ** Revision 1.1 2004/08/26 08:36:59 ronneber
56 ** initital import
57 **
58 ** Revision 1.6 2003/05/19 10:53:44 ronneber
59 ** - converted from MapTools to ParamMapWrapper
60 ** - added name()
61 **
62 ** Revision 1.5 2002/09/05 13:05:39 pigorsch
63 ** - modfified to use new MapTools
64 **
65 ** Revision 1.4 2002/05/21 18:22:40 ronneber
66 ** - now all parameters can be queried
67 **
68 ** Revision 1.3 2002/05/10 11:34:14 ronneber
69 ** - added methods to modify Parameters later
70 **
71 ** Revision 1.2 2002/05/10 11:07:03 ronneber
72 ** - removed FV template for all public classes, because Feature Vector type
73 ** can be extracted automatically from passed iterators or other
74 ** parameters -- this makes the public interface much more intuitive
75 **
76 ** Revision 1.1 2002/03/26 12:44:02 ronneber
77 ** restructured for autoconf
78 **
79 ** Revision 1.2 2002/01/14 13:52:58 pigorsch
80 ** * use FV.dotProduct(...)
81 **
82 ** Revision 1.1 2001/12/11 11:03:00 pigorsch
83 ** Initial Revision
84 **
85 **
86 **
87 **************************************************************************/
88 
89 #ifndef KERNEL_RBF_HH
90 #define KERNEL_RBF_HH
91 
92 #ifdef HAVE_CONFIG_H
93 #include <config.hh>
94 #endif
95 
96 // std includes
97 #include <cmath>
98 #include <map>
99 #include <set>
100 #include <string>
101 
102 // libsvmtl includes
103 #include "ProgressReporter.hh"
104 
105 // requirements of template parameters
107 
108 namespace svt
109 {
110  /*======================================================================*/
117  /*======================================================================*/
119  {
120  public:
121  Kernel_RBF(double gamma=1.)
122  :p_gamma(gamma)
123  {};
124 
126  {};
127 
128  void setGamma( double gamma) { p_gamma = gamma; }
129  double gamma() const { return p_gamma; }
130 
131 
132  template< typename ForwardIter, typename Accessor>
133  void updateCache( const ForwardIter&,
134  const ForwardIter&,
135  Accessor,
136  ProgressReporter*) const
137  {
138  // nothing to do here
139  }
140 
141  // new updateCache-Syntax:
142  template< typename ForwardIter1, typename Accessor1,
143  typename ForwardIter2, typename Accessor2 >
144  void updateCache( const ForwardIter1&,
145  const ForwardIter1&,
146  Accessor1,
147  const ForwardIter2&,
148  const ForwardIter2&,
149  Accessor2,
150  ProgressReporter* = 0) const
151  {
152  // nothing to do here
153  }
154 
155  void clearCache() const
156  {
157  // nothing to do here
158  }
159 
160  template< typename FV>
161  double k_function( const FV& x, const FV& y) const
162  {
163  return exp(-p_gamma*(x.square() - 2*x.dotProduct(y) + y.square()));
164  }
165 
166  template< typename FV, typename FVGradient>
167  void gradient_of_k_function( const FV& x, const FV& y,
168  FVGradient& gradient) const
169  {
170  gradient = x - y;
171  gradient *= -2 * p_gamma * k_function( x, y);
172  }
173 
174  template<typename STDATA>
175  void loadParameters( STDATA& stData)
176  {
178  if( stData.valueExists( "gamma"))
179  {
180  stData.getValue( "gamma", p_gamma);
181  }
182  else if( stData.valueExists( "feature_vector_dim"))
183  {
184  unsigned int fvDim = 1;
185  stData.getValue( "feature_vector_dim", fvDim);
186  p_gamma = 1.0/fvDim;
187  }
188  }
189 
190  template<typename STDATA>
191  void saveParameters( STDATA& stData) const
192  {
194  stData.setValue( "kernel_type", name());
195  stData.setValue( "gamma", p_gamma);
196  }
197 
198  static const char* name()
199  {
200  return "rbf";
201  }
202 
203  static const char* description()
204  {
205  return "radial basis function kernel: exp(-gamma*|u-v|^2)";
206  }
207 
208  /*======================================================================*/
217  /*======================================================================*/
218  static void getParamInfos( std::vector<ParamInfo>& p)
219  {
220  p.push_back(
221  ParamInfo( "gamma", "g", "value",
222  "gamma for rbf-kernel. (default 1.0, or "
223  "1/k, when feature_vector_dim is given"));
224  }
225 
226 
227  protected:
228  double p_gamma;
229  };
230 }
231 
232 #endif
233 
234 
235 
236 
237 
238 
double gamma() const
Definition: Kernel_RBF.hh:129
void saveParameters(STDATA &stData) const
Definition: Kernel_RBF.hh:191
#define CHECK_MEMBER_TEMPLATE(c)
The Kernel_RBF class specifies a radial basis function kernel exp(-gamma*|u-v|^2) ...
Definition: Kernel_RBF.hh:118
void gradient_of_k_function(const FV &x, const FV &y, FVGradient &gradient) const
Definition: Kernel_RBF.hh:167
void clearCache() const
Definition: Kernel_RBF.hh:155
void updateCache(const ForwardIter &, const ForwardIter &, Accessor, ProgressReporter *) const
Definition: Kernel_RBF.hh:133
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().
Definition: Kernel_RBF.hh:218
void updateCache(const ForwardIter1 &, const ForwardIter1 &, Accessor1, const ForwardIter2 &, const ForwardIter2 &, Accessor2, ProgressReporter *=0) const
Definition: Kernel_RBF.hh:144
void setGamma(double gamma)
Definition: Kernel_RBF.hh:128
void loadParameters(STDATA &stData)
Definition: Kernel_RBF.hh:175
static const char * description()
Definition: Kernel_RBF.hh:203
static const char * name()
Definition: Kernel_RBF.hh:198
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.
Definition: ParamInfo.hh:82
Kernel_RBF(double gamma=1.)
Definition: Kernel_RBF.hh:121
double k_function(const FV &x, const FV &y) const
Definition: Kernel_RBF.hh:161