104 random::rng_type& rng,
108 bool verbose =
false)
118 std::size_t ell =
m_data.size();
119 RealMatrix alpha(ell,
m_classes + 1, 0.0);
123 RealVector pref(ell, 1.0);
124 double prefsum = (double)ell;
126 std::vector<std::size_t> schedule(ell);
129 for (std::size_t i=0; i<ell; i++) schedule[i] = i;
133 std::size_t active = ell;
136 std::size_t epoch = 0;
137 std::size_t steps = 0;
140 double objective = 0.0;
141 double max_violation = 0.0;
144 const double gain_learning_rate = 1.0 / ell;
145 double average_gain = 0.0;
155 double psum = prefsum;
158 for (std::size_t i=0; i<ell; i++)
161 double num = (psum < 1e-6) ? ell - pos : std::min((
double)(ell - pos), (ell - pos) * p / psum);
162 std::size_t n = (std::size_t)std::floor(num);
163 double prob = num - n;
166 for (std::size_t j=0; j<n; j++)
181 std::shuffle(schedule.begin(),schedule.begin()+active,rng);
187 std::shuffle(schedule.begin(),schedule.end(),rng);
192 size_t nPoints = ell;
196 for (std::size_t j=0; j<nPoints; j++)
200 const std::size_t i = schedule[j];
202 const unsigned int y_i =
m_data[i].label;
204 blas::dense_vector_adaptor<double> a = row(alpha, i);
207 RealVector wx = prod(w,x_i);
213 max_violation = std::max(max_violation, kkt);
227 std::swap(schedule[j], schedule[active]);
234 if (epoch == 0) average_gain += gain / (double)ell;
238 constexpr double CHANGE_RATE = 0.2;
239 constexpr double PREF_MIN = 0.05;
240 constexpr double PREF_MAX = 20.0;
242 double change = CHANGE_RATE * (gain / average_gain - 1.0);
243 double newpref = std::min(PREF_MAX, std::max(PREF_MIN, pref(i) * std::exp(change)));
244 prefsum += newpref - pref(i);
246 average_gain = (1.0 - gain_learning_rate) * average_gain + gain_learning_rate * gain;
262 if (prop != NULL) prop->type =
QpTimeout;
269 std::cout <<
"#" << std::flush;
281 for (std::size_t i=0; i<ell; i++) pref(i) = 1.0;
282 prefsum = (double)ell;
295 if (verbose) std::cout <<
"." << std::flush;
299 canstop = (active == ell);
308 for (std::size_t d=0; d<
m_dim; d++) objective -= w(j, d) * w(j, d);
311 for (std::size_t i=0; i<ell; i++)
313 for (std::size_t j=0; j<
m_classes; j++) objective += alpha(i, j);
319 prop->accuracy = max_violation;
320 prop->iterations = ell * epoch;
321 prop->value = objective;
322 prop->seconds = timer.
lastLap();
328 std::cout << std::endl;
329 std::cout <<
"training time (seconds): " << timer.
lastLap() << std::endl;
330 std::cout <<
"number of epochs: " << epoch << std::endl;
331 std::cout <<
"number of iterations: " << (ell * epoch) << std::endl;
332 std::cout <<
"number of non-zero steps: " << steps << std::endl;
333 std::cout <<
"dual accuracy: " << max_violation << std::endl;
334 std::cout <<
"dual objective value: " << objective << std::endl;
377 virtual double solveSub(
double epsilon, RealVector& gradient,
double q,
double C,
unsigned int y, blas::dense_vector_adaptor<double>& alpha, RealVector& mu) = 0;