/usr/include/shark/ObjectiveFunctions/RadiusMarginQuotient.h is in libshark-dev 3.0.1+ds1-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /*!
*
*
* \brief Radius Margin Quotient for SVM model selection
*
*
*
* \author T.Glasmachers, O.Krause
* \date 2012
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SHARK_OBJECTIVEFUNCTIONS_RADIUSMARGINQUOTIENT_H
#define SHARK_OBJECTIVEFUNCTIONS_RADIUSMARGINQUOTIENT_H
#include <shark/ObjectiveFunctions/AbstractObjectiveFunction.h>
#include <shark/Algorithms/QP/SvmProblems.h>
#include <shark/Models/Kernels/KernelHelpers.h>
#include <shark/LinAlg/CachedMatrix.h>
#include <shark/LinAlg/KernelMatrix.h>
namespace shark {
///
/// \brief radius margin quotions for binary SVMs
///
/// \par
/// The RadiusMarginQuotient is the quotient \f$ R^2 / \rho^2 \f$
/// of the radius R of the smallest sphere containing the
/// training data and the margin \f$\rho\f$ of a binary hard margin
/// support vector machine. Both distances depend on the
/// kernel function, and thus on its parameters.
/// The radius margin quotient is a common objective
/// function for the adaptation of the kernel parameters
/// of a binary hard-margin SVM.
///
template<class InputType, class CacheType = float>
class RadiusMarginQuotient : public SingleObjectiveFunction
{
public:
typedef CacheType QpFloatType;
typedef KernelMatrix<InputType, QpFloatType> KernelMatrixType;
typedef CachedMatrix< KernelMatrixType > CachedMatrixType;
typedef LabeledData<InputType, unsigned int> DatasetType;
typedef AbstractKernelFunction<InputType> KernelType;
/// \brief Constructor.
RadiusMarginQuotient(DatasetType const& dataset, KernelType* kernel)
: mep_kernel(kernel),m_dataset(dataset)
{
m_features |= HAS_VALUE;
if (mep_kernel->hasFirstParameterDerivative())
m_features |= HAS_FIRST_DERIVATIVE;
}
/// \brief From INameable: return the class name.
std::string name() const
{ return "RadiusMarginQuotient"; }
std::size_t numberOfVariables()const{
return mep_kernel->numberOfParameters();
}
/// \brief Evaluate the radius margin quotient.
///
/// \par
/// The parameters are passed into the kernel, and the
/// radius-margin quotient is computed w.r.t. the
/// kernel-induced metric.
double eval(SearchPointType const& parameters) const{
SIZE_CHECK(parameters.size() == mep_kernel->numberOfParameters());
SHARK_CHECK(! m_dataset.empty(), "[RadiusMarginQuotient::eval] call setDataset first");
m_evaluationCounter++;
mep_kernel->setParameterVector(parameters);
Result result = computeRadiusMargin();
return result.w2 * result.R2;
}
/// \brief Evaluate the radius margin quotient and its first derivative.
///
/// \par
/// The parameters are passed into the kernel, and the
/// radius-margin quotient and its derivative are computed
/// w.r.t. the kernel-induced metric.
double evalDerivative(SearchPointType const& parameters, FirstOrderDerivative& derivative) const{
SHARK_CHECK(! m_dataset.empty(), "[RadiusMarginQuotient::evalDerivative] call setDataset first");
SIZE_CHECK(parameters.size() == mep_kernel->numberOfParameters());
m_evaluationCounter++;
mep_kernel->setParameterVector(parameters);
Result result = computeRadiusMargin();
derivative = calculateKernelMatrixParameterDerivative(
*mep_kernel, m_dataset.inputs(),
result.w2*(RealDiagonalMatrix(result.beta)-outer_prod(result.beta,result.beta))
-result.R2*outer_prod(result.alpha,result.alpha)
);
return result.w2 * result.R2;
}
protected:
struct Result{
RealVector alpha;
RealVector beta;
double w2;
double R2;
};
Result computeRadiusMargin()const{
std::size_t ell = m_dataset.numberOfElements();
QpStoppingCondition stop;
Result result;
{
KernelMatrixType km(*mep_kernel, m_dataset.inputs());
CachedMatrixType cache(&km);
typedef CSVMProblem<CachedMatrixType> SVMProblemType;
typedef SvmShrinkingProblem<SVMProblemType> ProblemType;
SVMProblemType svmProblem(cache,m_dataset.labels(),1e100);
ProblemType problem(svmProblem);
QpSolver< ProblemType> solver(problem);
QpSolutionProperties prop;
solver.solve(stop, &prop);
result.w2 = 2.0 * prop.value;
result.alpha = problem.getUnpermutedAlpha();
}
{
// create and solve the radius problem (also a quadratic program)
KernelMatrixType km(*mep_kernel, m_dataset.inputs());
CachedMatrixType cache(&km);
typedef BoxedSVMProblem<CachedMatrixType> SVMProblemType;
typedef SvmShrinkingProblem<SVMProblemType> ProblemType;
// Setup the problem
RealVector linear(ell);
for (std::size_t i=0; i<ell; i++){
linear(i) = 0.5 * km(i, i);
}
SVMProblemType svmProblem(cache,linear,0.0,1.0);
ProblemType problem(svmProblem);
//solve it
QpSolver< ProblemType> solver(problem);
QpSolutionProperties prop;
solver.solve(stop, &prop);
result.R2 = 2.0 * prop.value;
result.beta = problem.getUnpermutedAlpha();
}
return result;
}
KernelType* mep_kernel; ///< underlying parameterized kernel object
DatasetType m_dataset; ///< labeled data for radius and (hard) margin computation
};
}
#endif
|