37 #ifndef SHARK_ALGORITHMS_QP_QPBOXLINEAR_H 38 #define SHARK_ALGORITHMS_QP_QPBOXLINEAR_H 66 template <
class InputT>
92 for (std::size_t i=0; i<
m_data.size(); i++)
94 auto const& x_i =
m_data[i];
105 for(std::size_t i = 0; i !=
m_data.size(); ++i){
106 double y_i = (
m_data[i].label > 0) ? +1.0 : -1.0;
140 std::size_t ell =
m_data.size();
141 double prefsum = sum(
m_pref);
142 std::vector<std::size_t> schedule(ell);
145 std::size_t epoch = 0;
146 std::size_t steps = 0;
149 double max_violation = 0.0;
150 const double gain_learning_rate = 1.0 / ell;
151 double average_gain = 0.0;
158 double psum = prefsum;
161 for (std::size_t i=0; i<ell; i++)
164 double num = (psum < 1e-6) ? ell - pos : std::min((
double)(ell - pos), (ell - pos) * p / psum);
165 std::size_t n = (std::size_t)std::floor(num);
166 double prob = num - n;
168 for (std::size_t j=0; j<n; j++)
181 for (std::size_t j=0; j<ell; j++)
184 std::size_t i = schedule[j];
185 auto const& e_i =
m_data[i];
186 double y_i = (e_i.label > 0) ? +1.0 : -1.0;
190 double wyx = y_i * inner_prod(
m_weights, e_i.input);
191 double g = 1.0 -
m_offset * y_i - wyx - reg * a;
192 double pg = (a == 0.0 && g < 0.0) ? 0.0 : (a == bound && g > 0.0 ? 0.0 : g);
195 max_violation = std::max(max_violation, std::abs(pg));
204 double new_a = a + mu;
212 else if (new_a >= bound)
220 noalias(
m_weights) += (mu * y_i) * e_i.input;
221 gain = mu * (g - 0.5 * q * mu);
228 if (epoch == 0) average_gain += gain / (double)ell;
232 constexpr
double CHANGE_RATE = 0.2;
233 constexpr
double PREF_MIN = 0.05;
234 constexpr
double PREF_MAX = 20.0;
236 double change = CHANGE_RATE * (gain / average_gain - 1.0);
237 double newpref = std::min(PREF_MAX, std::max(PREF_MIN,
m_pref(i) * std::exp(change)));
238 prefsum += newpref -
m_pref(i);
240 average_gain = (1.0 - gain_learning_rate) * average_gain + gain_learning_rate * gain;
256 if (prop != NULL) prop->type =
QpTimeout;
262 if (verbose) std::cout <<
"#" << std::flush;
272 for (std::size_t i=0; i<ell; i++)
m_pref[i] = 1.0;
273 prefsum = (double)ell;
278 if (verbose) std::cout <<
"." << std::flush;
286 std::size_t free_SV = 0;
287 std::size_t bounded_SV = 0;
288 double objective = -0.5 * norm_sqr(
m_weights);
289 for (std::size_t i=0; i<ell; i++)
295 objective -= reg/2.0 * a * a;
296 if (a == bound) bounded_SV++;
304 prop->accuracy = max_violation;
305 prop->iterations = ell * epoch;
306 prop->value = objective;
307 prop->seconds = timer.
lastLap();
313 std::cout << std::endl;
314 std::cout <<
"training time (seconds): " << timer.
lastLap() << std::endl;
315 std::cout <<
"number of epochs: " << epoch << std::endl;
316 std::cout <<
"number of iterations: " << (ell * epoch) << std::endl;
317 std::cout <<
"number of non-zero steps: " << steps << std::endl;
318 std::cout <<
"dual accuracy: " << max_violation << std::endl;
319 std::cout <<
"dual objective value: " << objective << std::endl;
320 std::cout <<
"number of free support vectors: " << free_SV << std::endl;
321 std::cout <<
"number of bounded support vectors: " << bounded_SV << std::endl;