32 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOOERRORCSVM_H 33 #define SHARK_OBJECTIVEFUNCTIONS_LOOERRORCSVM_H 49 template<
class InputType,
class CacheType =
float>
58 LooErrorCSvm(DatasetType
const& dataset, KernelType* kernel,
bool withOffset)
59 : mep_dataset(&dataset)
61 , m_withOffset(withOffset)
70 {
return "LooErrorCSvm"; }
79 double eval(
const RealVector& params){
81 return eval(params, stop);
90 double C = params.back();
99 KernelMatrixType km(*mep_kernel, mep_dataset->
inputs());
100 CachedMatrixType matrix(&km);
101 SVMProblemType svmProblem(matrix,mep_dataset->
labels(),C);
102 std::size_t ell = km.size();
110 ProblemType problem(svmProblem);
113 RealVector alphaFull(problem.dimensions());
114 for(std::size_t i = 0; i != problem.dimensions(); ++i){
115 alphaFull(i) = problem.alpha(i);
122 for (std::size_t i=0; i<ell; i++)
125 if (alphaFull(i) == 0.0)
continue;
126 problem.deactivateVariable(i);
135 column(svm.alpha(),0)= problem.getUnpermutedAlpha();
136 svm.offset(0) = computeBias(problem);
137 std::size_t elementIndex = i;
138 unsigned int target = mep_dataset->
element(elementIndex).label;
139 mistakes += loss(target, svm(mep_dataset->
element(elementIndex).input));
141 problem.activateVariable(i);
143 return mistakes / (double)ell;
149 ProblemType problem(svmProblem);
152 RealVector alphaFull(problem.dimensions());
153 for(std::size_t i = 0; i != problem.dimensions(); ++i){
154 alphaFull(i) = problem.alpha(i);
161 for (std::size_t i=0; i<ell; i++)
164 if (alphaFull(i) == 0.0)
continue;
165 problem.deactivateVariable(i);
175 column(svm.alpha(),0)= problem.getUnpermutedAlpha();
176 std::size_t elementIndex = i;
177 unsigned int target = mep_dataset->
element(elementIndex).label;
178 mistakes += loss(target, svm(mep_dataset->
element(elementIndex).input));
180 problem.activateVariable(i);
182 return mistakes / (double)ell;
188 template<
class Problem>
189 double computeBias(Problem
const& problem){
190 double lowerBound = -1e100;
191 double upperBound = 1e100;
193 std::size_t freeVars = 0;
194 std::size_t ell = problem.dimensions();
195 for (std::size_t i=0; i<ell; i++)
197 double value = problem.gradient(i);
198 if (problem.alpha(i) == problem.boxMin(i))
200 lowerBound = std::max(value,lowerBound);
202 else if (problem.alpha(i) == problem.boxMax(i))
204 upperBound = std::min(value,upperBound);
213 return sum / freeVars;
215 return 0.5 * (lowerBound + upperBound);
218 const DatasetType* mep_dataset;
219 KernelType* mep_kernel;