SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tron.cpp
Go to the documentation of this file.
1 #include <math.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdarg.h>
5 
6 #include <shogun/lib/config.h>
7 #include <shogun/lib/Signal.h>
8 #include <shogun/lib/Time.h>
9 
10 #ifdef HAVE_LAPACK
14 
15 using namespace shogun;
16 
17 CTron::CTron(const function *f, float64_t e, int32_t it)
18 : CSGObject()
19 {
20  this->fun_obj=const_cast<function *>(f);
21  this->eps=e;
22  this->max_iter=it;
23 }
24 
26 {
27 }
28 
29 void CTron::tron(float64_t *w, float64_t max_train_time)
30 {
31  // Parameters for updating the iterates.
32  float64_t eta0 = 1e-4, eta1 = 0.25, eta2 = 0.75;
33 
34  // Parameters for updating the trust region size delta.
35  float64_t sigma1 = 0.25, sigma2 = 0.5, sigma3 = 4.;
36 
37  int32_t i, cg_iter;
38  float64_t delta, snorm, one=1.0;
39  float64_t alpha, f, fnew, prered, actred, gs;
40 
41  /* calling external lib */
42  int n = (int) fun_obj->get_nr_variable();
43  int search = 1, iter = 1, inc = 1;
44  double *s = SG_MALLOC(double, n);
45  double *r = SG_MALLOC(double, n);
46  double *w_new = SG_MALLOC(double, n);
47  double *g = SG_MALLOC(double, n);
48 
49  for (i=0; i<n; i++)
50  w[i] = 0;
51 
52  f = fun_obj->fun(w);
53  fun_obj->grad(w, g);
54  delta = cblas_dnrm2(n, g, inc);
55  float64_t gnorm1 = delta;
56  float64_t gnorm = gnorm1;
57 
58  if (gnorm <= eps*gnorm1)
59  search = 0;
60 
61  iter = 1;
62 
63  CSignal::clear_cancel();
64  CTime start_time;
65 
66  while (iter <= max_iter && search && (!CSignal::cancel_computations()))
67  {
68  if (max_train_time > 0 && start_time.cur_time_diff() > max_train_time)
69  break;
70 
71  cg_iter = trcg(delta, g, s, r);
72 
73  memcpy(w_new, w, sizeof(float64_t)*n);
74  cblas_daxpy(n, one, s, inc, w_new, inc);
75 
76  gs = cblas_ddot(n, g, inc, s, inc);
77  prered = -0.5*(gs-cblas_ddot(n, s, inc, r, inc));
78  fnew = fun_obj->fun(w_new);
79 
80  // Compute the actual reduction.
81  actred = f - fnew;
82 
83  // On the first iteration, adjust the initial step bound.
84  snorm = cblas_dnrm2(n, s, inc);
85  if (iter == 1)
86  delta = CMath::min(delta, snorm);
87 
88  // Compute prediction alpha*snorm of the step.
89  if (fnew - f - gs <= 0)
90  alpha = sigma3;
91  else
92  alpha = CMath::max(sigma1, -0.5*(gs/(fnew - f - gs)));
93 
94  // Update the trust region bound according to the ratio of actual to predicted reduction.
95  if (actred < eta0*prered)
96  delta = CMath::min(CMath::max(alpha, sigma1)*snorm, sigma2*delta);
97  else if (actred < eta1*prered)
98  delta = CMath::max(sigma1*delta, CMath::min(alpha*snorm, sigma2*delta));
99  else if (actred < eta2*prered)
100  delta = CMath::max(sigma1*delta, CMath::min(alpha*snorm, sigma3*delta));
101  else
102  delta = CMath::max(delta, CMath::min(alpha*snorm, sigma3*delta));
103 
104  SG_INFO("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter);
105 
106  if (actred > eta0*prered)
107  {
108  iter++;
109  memcpy(w, w_new, sizeof(float64_t)*n);
110  f = fnew;
111  fun_obj->grad(w, g);
112 
113  gnorm = cblas_dnrm2(n, g, inc);
114  if (gnorm < eps*gnorm1)
115  break;
116  SG_SABS_PROGRESS(gnorm, -CMath::log10(gnorm), -CMath::log10(1), -CMath::log10(eps*gnorm1), 6);
117  }
118  if (f < -1.0e+32)
119  {
120  SG_WARNING("f < -1.0e+32\n");
121  break;
122  }
123  if (CMath::abs(actred) <= 0 && CMath::abs(prered) <= 0)
124  {
125  SG_WARNING("actred and prered <= 0\n");
126  break;
127  }
128  if (CMath::abs(actred) <= 1.0e-12*CMath::abs(f) &&
129  CMath::abs(prered) <= 1.0e-12*CMath::abs(f))
130  {
131  SG_WARNING("actred and prered too small\n");
132  break;
133  }
134  }
135 
136  SG_DONE();
137 
138  SG_FREE(g);
139  SG_FREE(r);
140  SG_FREE(w_new);
141  SG_FREE(s);
142 }
143 
144 int32_t CTron::trcg(float64_t delta, double* g, double* s, double* r)
145 {
146  /* calling external lib */
147  int i, cg_iter;
148  int n = (int) fun_obj->get_nr_variable();
149  int inc = 1;
150  double one = 1;
151  double *Hd = SG_MALLOC(double, n);
152  double *d = SG_MALLOC(double, n);
153  double rTr, rnewTrnew, alpha, beta, cgtol;
154 
155  for (i=0; i<n; i++)
156  {
157  s[i] = 0;
158  r[i] = -g[i];
159  d[i] = r[i];
160  }
161  cgtol = 0.1* cblas_dnrm2(n, g, inc);
162 
163  cg_iter = 0;
164  rTr = cblas_ddot(n, r, inc, r, inc);
165  while (1)
166  {
167  if (cblas_dnrm2(n, r, inc) <= cgtol)
168  break;
169  cg_iter++;
170  fun_obj->Hv(d, Hd);
171 
172  alpha = rTr/cblas_ddot(n, d, inc, Hd, inc);
173  cblas_daxpy(n, alpha, d, inc, s, inc);
174  if (cblas_dnrm2(n, s, inc) > delta)
175  {
176  SG_INFO("cg reaches trust region boundary\n");
177  alpha = -alpha;
178  cblas_daxpy(n, alpha, d, inc, s, inc);
179 
180  double std = cblas_ddot(n, s, inc, d, inc);
181  double sts = cblas_ddot(n, s, inc, s, inc);
182  double dtd = cblas_ddot(n, d, inc, d, inc);
183  double dsq = delta*delta;
184  double rad = sqrt(std*std + dtd*(dsq-sts));
185  if (std >= 0)
186  alpha = (dsq - sts)/(std + rad);
187  else
188  alpha = (rad - std)/dtd;
189  cblas_daxpy(n, alpha, d, inc, s, inc);
190  alpha = -alpha;
191  cblas_daxpy(n, alpha, Hd, inc, r, inc);
192  break;
193  }
194  alpha = -alpha;
195  cblas_daxpy(n, alpha, Hd, inc, r, inc);
196  rnewTrnew = cblas_ddot(n, r, inc, r, inc);
197  beta = rnewTrnew/rTr;
198  cblas_dscal(n, beta, d, inc);
199  cblas_daxpy(n, one, r, inc, d, inc);
200  rTr = rnewTrnew;
201  }
202 
203  SG_FREE(d);
204  SG_FREE(Hd);
205 
206  return(cg_iter);
207 }
208 
209 float64_t CTron::norm_inf(int32_t n, float64_t *x)
210 {
211  float64_t dmax = CMath::abs(x[0]);
212  for (int32_t i=1; i<n; i++)
213  if (CMath::abs(x[i]) >= dmax)
214  dmax = CMath::abs(x[i]);
215  return(dmax);
216 }
217 #endif //HAVE_LAPACK

SHOGUN Machine Learning Toolbox - Documentation