37 #ifndef SHARK_ALGORITHMS_QP_QPMCBOXDECOMP_H 38 #define SHARK_ALGORITHMS_QP_QPMCBOXDECOMP_H 42 #include <shark/Algorithms/QP/Impl/AnalyticProblems.h> 49 template <
class Matrix>
59 template<
class Problem>
60 double operator()(Problem& problem, std::size_t& i, std::size_t& j){
62 return problem.selectWorkingSet(i,j);
78 RealMatrix
const& linearMat,
100 && linearMat.size1() == kernel.size(),
101 "[QpMcDecomp::QpMcDecomp] dimension conflict" 109 unsigned int y = target.
element(i);
116 for (std::size_t p=0; p<
m_cardP; p++, v++)
121 double Q =
m_M(
m_classes * (y * m_cardP + p) + y, p) * k;
144 return solutionMatrix;
153 return solutionGradientMatrix;
179 double maxViolation = 0.0;
186 maxViolation =
std::max(maxViolation,g);
190 maxViolation =
std::max(maxViolation,-g);
222 unsigned int r =
m_cardP * y + p;
246 unsigned int rv =
m_cardP*yv+pv;
247 unsigned int rw =
m_cardP*yw+pw;
277 double largest = 0.0;
289 if (largest < 10.0 * epsilon)
304 if ((v == 0.0 && g <= 0.0) || (v ==
m_C && g >= 0.0))
340 if (mu == 0.0)
continue;
345 unsigned int r =
m_cardP * yv + pv;
355 for (std::size_t b=0; b<row.
size; b++)
363 double upd = mu * def * k;
366 std::size_t f = ex.
avar[b];
392 double maxViolation = 0.0;
399 if (ga >maxViolation && aa <
m_C)
404 else if (-ga > maxViolation && aa > 0.0)
410 if (maxViolation == 0.0)
return maxViolation;
414 std::size_t ii = vari.
i;
416 unsigned int pi = vari.
p;
423 double bestgain = gi * gi / di;
428 unsigned int ya = exa.
y;
432 for (std::size_t pf=0, b=0; pf <
m_cardP; pf++)
434 std::size_t f = exa.
var[pf];
435 double qif = def * k[a];
441 if(f >= m_activeVar || f == i)
449 if (!(af > 0.0 && gf < 0.0) && !(af < m_C && gf > 0.0))
452 double gain = detail::maximumGainQuadratic2D(di,df,qif,di,gi,gf);
453 if( gain > bestgain){
473 for (std::size_t b=0; b<row.
size; b++){
478 double upd = mu* def * k;
479 for (std::size_t b=0; b<ex.
active; b++)
496 std::size_t ih = exv->
active - 1;
497 std::size_t
h = exv->
avar[ih];
538 for (std::size_t v = 0; v <
m_cardP; v++)
638 template<
class Matrix>
648 bool sumToZero =
false 650 std::size_t classes = bias.size();
651 std::size_t numExamples = m_problem->getNumExamples();
652 std::size_t
cardP = m_problem->cardP();
653 RealVector stepsize(classes, 0.01);
654 RealVector prev(classes,0);
655 RealVector step(classes);
664 RealMatrix dualGradient = m_problem->solutionGradient();
666 RealVector grad(classes,0);
668 for (std::size_t i=0; i<numExamples; i++){
669 for (std::size_t p=0; p<
cardP; p++){
670 double g = dualGradient(i,p);
673 unsigned int y = m_problem->label(i);
675 for (std::size_t b=0; b<row.
size; b++)
684 grad -=
sum(grad) / classes;
688 for (std::size_t c=0; c<classes; c++)
692 step(c) = -stepsize(c);
694 step(c) = stepsize(c);
696 double gg = prev(c) * grad(c);
707 step -=
sum(step) / classes;
712 performBiasUpdate(step,nu);
720 void performBiasUpdate(
723 std::size_t numExamples = m_problem->getNumExamples();
724 std::size_t
cardP = m_problem->cardP();
725 RealMatrix deltaLinear(numExamples,cardP,0.0);
726 for (std::size_t i=0; i<numExamples; i++){
727 for (std::size_t p=0; p<
cardP; p++){
728 unsigned int y = m_problem->label(i);
731 for (std::size_t b=0; b<row.
size; b++)
737 m_problem->addDeltaLinear(deltaLinear);