142 const double CHANGE_RATE = 0.2;
143 const double PREF_MIN = 0.05;
144 const double PREF_MAX = 20.0;
147 const bool verbose =
false;
150 RealVector w(dim, 0.0);
156 RealVector label = column(label_mat,0);
158 RealVector diag(dim);
159 RealVector difference = -label;
160 UIntVector index(dim);
163 for (
size_t i=0; i<dim; i++){
164 diag[i] = norm_sqr(row(data,i));
168 RealVector pref(dim, 1.0);
169 double prefsum = (double)dim;
172 const double gain_learning_rate = 1.0 / dim;
173 double average_gain = 0.0;
178 std::size_t iter = 0;
179 std::size_t steps = 0;
185 double psum = prefsum;
189 for (std::size_t i=0; i<dim; i++)
193 if (psum >= 1e-6 && p < psum)
194 n = (dim - pos) * p / psum;
196 n = (double)(dim - pos);
198 unsigned int m = (
unsigned int)floor(n);
200 if ((
double)rand() / (double)RAND_MAX < prob) m++;
201 for (std::size_t j=0; j<m; j++)
203 index[pos] = (
unsigned int)i;
209 for (std::size_t i=0; i<dim; i++)
211 std::size_t r = rand() % dim;
212 std::swap(index[r], index[i]);
216 for (
size_t s=0; s<dim; s++)
218 std::size_t i = index[s];
223 double grad = inner_prod(difference, row(data,i));
235 gain = 0.5 * d * delta * delta;
241 gain = 0.5 * d * delta * delta;
247 vio = std::fabs(grad);
252 gain = delta * (grad - 0.5 * d * delta);
253 double g0 = grad - a * d - 2.0 *
lambda;
257 gain = dd * (grad - 0.5 * d * dd);
261 else gain = 0.5 * d * delta * delta;
266 vio = std::fabs(grad);
271 gain = delta * (grad - 0.5 * d * delta);
272 double g0 = grad - a * d + 2.0 *
lambda;
276 gain = dd * (grad - 0.5 * d * dd);
280 else gain = 0.5 * d * delta * delta;
284 if (vio > maxvio) maxvio = vio;
288 noalias(difference) += delta*row(data,i);
294 average_gain += gain / (double)dim;
297 double change = CHANGE_RATE * (gain / average_gain - 1.0);
298 double newpref = pref[i] * std::exp(change);
299 newpref = std::min(std::max(newpref, PREF_MIN), PREF_MAX);
300 prefsum += newpref - pref[i];
302 average_gain = (1.0 - gain_learning_rate) * average_gain + gain_learning_rate * gain;
316 noalias(pref) = blas::repeat(1.0, dim);
317 prefsum = (double)dim;
318 if (verbose) std::cout <<
"*" << std::flush;
324 if (verbose) std::cout <<
"." << std::flush;
329 RealMatrix mat(1, w.size());