35 #ifndef SHARK_ALGORITHMS_TRAINERS_MISSING_FEATURE_SVM_H 36 #define SHARK_ALGORITHMS_TRAINERS_MISSING_FEATURE_SVM_H 71 template <
class InputType,
class CacheType =
float>
85 base_type(kernel, C, offset, unconstrained),
91 {
return "MissingFeatureSvmTrainer"; }
100 trainWithOffset(svm,dataset);
102 trainWithoutOffset(svm,dataset);
106 { m_maxIterations = newIterations; }
111 std::size_t m_maxIterations;
123 for(std::size_t iteration = 0; iteration != m_maxIterations; ++iteration){
130 kernelMatrix.setScalingCoefficients(scalingCoefficients);
131 CachedMatrixType matrix(&kernelMatrix);
132 SVMProblemType svmProblem(matrix,dataset.
labels(),this->
C());
138 RealVector alpha = problem.getUnpermutedAlpha();
141 const double classifierNorm = svm.
computeNorm(alpha, scalingCoefficients);
143 for (std::size_t i = 0; i < scalingCoefficients.size(); ++i)
154 if(iteration == m_maxIterations-1)
155 column(svm.
alpha(),0)= alpha;
166 RealVector scalingCoefficients(datasetSize, 1.0);
172 for(std::size_t iteration = 0; iteration != m_maxIterations; ++iteration){
179 kernelMatrix.setScalingCoefficients(scalingCoefficients);
180 CachedMatrixType matrix(&kernelMatrix);
181 SVMProblemType svmProblem(matrix,dataset.
labels(),this->
C());
187 RealVector unpermutedAlpha = problem.getUnpermutedAlpha();
190 const double classifierNorm = svm.
computeNorm(unpermutedAlpha, scalingCoefficients);
192 for (std::size_t i = 0; i < scalingCoefficients.size(); ++i)
203 if(iteration == m_maxIterations-1){
206 column(svm.
alpha(),0)= unpermutedAlpha;
207 double lowerBound = -1e100;
208 double upperBound = 1e100;
210 std::size_t freeVars = 0;
213 for (std::size_t i = 0; i < datasetSize; i++)
217 const double value = problem.gradient(i);
218 if (problem.alpha(i) == problem.boxMin(i)){
219 lowerBound = std::max(value,lowerBound);
221 else if (problem.alpha(i) == problem.boxMax(i)){
222 upperBound = std::min(value,upperBound);
232 svm.
offset(0) = sum / freeVars;
237 svm.
offset(0) = 0.5 * (lowerBound + upperBound);