36 #ifndef SHARK_ALGORITHMS_TRAINERS_LASSOREGRESSION_H 37 #define SHARK_ALGORITHMS_TRAINERS_LASSOREGRESSION_H 68 template <
class InputVectorType = RealVector>
89 {
return "LASSO regression"; }
139 void train(ModelType& model, DataType
const& dataset){
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 diag(dim);
157 RealVector difference = -label;
158 UIntVector index(dim);
161 for (
size_t i=0; i<dim; i++){
162 diag[i] = norm_sqr(row(data,i));
166 RealVector pref(dim, 1.0);
167 double prefsum = (double)dim;
170 const double gain_learning_rate = 1.0 / dim;
171 double average_gain = 0.0;
176 std::size_t iter = 0;
177 std::size_t steps = 0;
183 double psum = prefsum;
187 for (std::size_t i=0; i<dim; i++)
191 if (psum >= 1e-6 && p < psum)
192 n = (dim - pos) * p / psum;
194 n = (double)(dim - pos);
196 unsigned int m = (
unsigned int)floor(n);
198 if ((
double)rand() / (double)RAND_MAX < prob) m++;
199 for (std::size_t j=0; j<m; j++)
201 index[pos] = (
unsigned int)i;
207 for (std::size_t i=0; i<dim; i++)
209 std::size_t r = rand() % dim;
214 for (
size_t s=0; s<dim; s++)
216 std::size_t i = index[s];
221 double grad = inner_prod(difference, row(data,i));
233 gain = 0.5 * d * delta * delta;
235 else if (grad < -lambda)
239 gain = 0.5 * d * delta * delta;
245 vio = std::fabs(grad);
250 gain = delta * (grad - 0.5 * d * delta);
251 double g0 = grad - a * d - 2.0 *
lambda;
255 gain = dd * (grad - 0.5 * d * dd);
259 else gain = 0.5 * d * delta * delta;
264 vio = std::fabs(grad);
269 gain = delta * (grad - 0.5 * d * delta);
270 double g0 = grad - a * d + 2.0 *
lambda;
274 gain = dd * (grad - 0.5 * d * dd);
278 else gain = 0.5 * d * delta * delta;
282 if (vio > maxvio) maxvio = vio;
286 noalias(difference) += delta*row(data,i);
292 average_gain += gain / (double)dim;
295 double change = CHANGE_RATE * (gain / average_gain - 1.0);
296 double newpref = pref[i] * std::exp(change);
297 newpref = std::min(std::max(newpref, PREF_MIN), PREF_MAX);
298 prefsum += newpref - pref[i];
300 average_gain = (1.0 - gain_learning_rate) * average_gain + gain_learning_rate * gain;
314 noalias(pref) = blas::repeat(1.0, dim);
315 prefsum = (double)dim;
316 if (verbose) std::cout <<
"*" << std::flush;
322 if (verbose) std::cout <<
"." << std::flush;
327 RealMatrix mat(1, w.size());