92 {
return "MissingFeatureSvmTrainer"; }
101 trainWithOffset(svm,dataset);
103 trainWithoutOffset(svm,dataset);
107 { m_maxIterations = newIterations; }
112 std::size_t m_maxIterations;
124 for(std::size_t iteration = 0; iteration != m_maxIterations; ++iteration){
131 kernelMatrix.setScalingCoefficients(scalingCoefficients);
132 CachedMatrixType matrix(&kernelMatrix);
133 SVMProblemType svmProblem(matrix,dataset.
labels(),this->C());
139 RealVector alpha = problem.getUnpermutedAlpha();
142 const double classifierNorm = svm.
computeNorm(alpha, scalingCoefficients);
144 for (std::size_t i = 0; i < scalingCoefficients.size(); ++i)
155 if(iteration == m_maxIterations-1)
156 column(svm.
alpha(),0)= alpha;
163 void trainWithOffset(MissingFeaturesKernelExpansion<InputType>& svm, LabeledData<InputType, unsigned int>
const& dataset)
166 std::size_t datasetSize = dataset.numberOfElements();
167 RealVector scalingCoefficients(datasetSize, 1.0);
173 for(std::size_t iteration = 0; iteration != m_maxIterations; ++iteration){
175 typedef ExampleModifiedKernelMatrix<InputType, QpFloatType>
MatrixType;
176 typedef CachedMatrix<MatrixType> CachedMatrixType;
177 typedef CSVMProblem<CachedMatrixType> SVMProblemType;
178 typedef SvmShrinkingProblem<SVMProblemType> ProblemType;
180 kernelMatrix.setScalingCoefficients(scalingCoefficients);
181 CachedMatrixType matrix(&kernelMatrix);
182 SVMProblemType svmProblem(matrix,dataset.labels(),this->C());
186 QpSolver< ProblemType > solver(problem);
188 RealVector unpermutedAlpha = problem.getUnpermutedAlpha();
191 const double classifierNorm = svm.computeNorm(unpermutedAlpha, scalingCoefficients);
193 for (std::size_t i = 0; i < scalingCoefficients.size(); ++i)
196 scalingCoefficients(i) = svm.computeNorm(
199 dataset.element(i).input
204 if(iteration == m_maxIterations-1){
207 column(svm.alpha(),0)= unpermutedAlpha;
208 double lowerBound = -1e100;
209 double upperBound = 1e100;
211 std::size_t freeVars = 0;
214 for (std::size_t i = 0; i < datasetSize; i++)
218 const double value = problem.gradient(i);
219 if (problem.alpha(i) == problem.boxMin(i)){
220 lowerBound = std::max(value,lowerBound);
222 else if (problem.alpha(i) == problem.boxMax(i)){
223 upperBound = std::min(value,upperBound);
233 svm.offset(0) = sum / freeVars;
238 svm.offset(0) = 0.5 * (lowerBound + upperBound);
245 svm.setScalingCoefficients(scalingCoefficients);