Orfeo Toolbox  4.0
otbSVMCrossValidationCostFunction.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbSVMCrossValidationCostFunction_txx
19 #define __otbSVMCrossValidationCostFunction_txx
20 
22 
23 namespace otb
24 {
25 template<class TModel>
27 ::SVMCrossValidationCostFunction() : m_Model(), m_NumberOfCrossValidationFolders(10), m_DerivativeStep(0.001)
28 {}
29 template<class TModel>
32 {}
33 template<class TModel>
35 ::MeasureType
37 ::GetValue(const ParametersType& parameters) const
38 {
39  // Check the input model
40  if (!m_Model)
41  {
42  itkExceptionMacro(<< "Model is null, can not evaluate accuracy.");
43  }
44 
45  // Check for a positive and non-null C
46  if (parameters[0] <= 0)
47  {
48  return 0;
49  }
50 
51  // Updates vm_parameters according to current parameters
52  this->UpdateParameters(m_Model->GetParameters(), parameters);
53 
54  return m_Model->CrossValidation(m_NumberOfCrossValidationFolders);
55 }
56 
57 template<class TModel>
58 void
60 ::GetDerivative(const ParametersType& parameters, DerivativeType& derivative) const
61 {
62  // Set derivative size
63  derivative.SetSize(parameters.Size());
65 
66  for (unsigned int i = 0; i < parameters.Size(); ++i)
67  {
68  MeasureType y1, y2;
69  ParametersType x1, x2;
70 
71  x1 = parameters;
72  x1[i] -= m_DerivativeStep;
73  y1 = this->GetValue(x1);
74 
75  x2 = parameters;
76  x2[i] += m_DerivativeStep;
77  y2 = this->GetValue(x2);
78 
79  derivative[i] = (y2 - y1) / (2 * m_DerivativeStep);
80  otbMsgDevMacro( << "x1= " << x1 << " x2= " << x2 << ", y1= " << y1 << ", y2= " << y2 );
81  }
82  otbMsgDevMacro( "Position: " << parameters << ", Value: " << this->GetValue(parameters)
83  << ", Derivatives: " << derivative );
84 }
85 
86 template<class TModel>
87 unsigned int
90 {
91  if (!m_Model)
92  {
93  itkExceptionMacro(<< "Model is null, can not evaluate number of parameters.");
94  }
95 
96  switch (m_Model->GetKernelType())
97  {
98  case LINEAR:
99  // C
100  return 1;
101 
102  case POLY:
103  // C, gamma and coef0
104  return 3;
105 
106  case RBF:
107  // C and gamma
108  return 2;
109 
110  case SIGMOID:
111  // C, gamma and coef0
112  return 3;
113 
114  default:
115  // C
116  return 1;
117  }
118 }
119 
120 template<class TModel>
121 void
123 ::UpdateParameters(struct svm_parameter& svm_parameters, const ParametersType& parameters) const
124 {
125  switch (m_Model->GetKernelType())
126  {
127  case LINEAR:
128  // C
129  svm_parameters.C = parameters[0];
130  break;
131 
132  case POLY:
133  // C, gamma and coef0
134  svm_parameters.C = parameters[0];
135  svm_parameters.gamma = parameters[1];
136  svm_parameters.coef0 = parameters[2];
137  break;
138 
139  case RBF:
140  // C and gamma
141  svm_parameters.C = parameters[0];
142  svm_parameters.gamma = parameters[1];
143  break;
144 
145  case SIGMOID:
146  // C, gamma and coef0
147  svm_parameters.C = parameters[0];
148  svm_parameters.gamma = parameters[1];
149  svm_parameters.coef0 = parameters[2];
150  break;
151 
152  default:
153  svm_parameters.C = parameters[0];
154  break;
155  }
156 }
157 
158 } // namespace otb
159 
160 #endif

Generated at Sat Mar 8 2014 16:21:46 for Orfeo Toolbox with doxygen 1.8.3.1