SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnlineLibLinear.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2007-2010 Soeren Sonnenburg
8  * Written (W) 2011 Shashwat Lal Das
9  * Copyright (c) 2007-2009 The LIBLINEAR Project.
10  * Copyright (C) 2007-2010 Fraunhofer Institute FIRST and Max-Planck-Society
11  */
12 
15 #include <shogun/lib/Time.h>
16 
17 using namespace shogun;
18 
21 {
22  init();
23 }
24 
26 {
27  init();
28  C1=C_reg;
29  C2=C_reg;
30  use_bias=true;
31 }
32 
34  float64_t C_reg, CStreamingDotFeatures* traindat)
35 {
36  init();
37  C1=C_reg;
38  C2=C_reg;
39  use_bias=true;
40 
41  set_features(traindat);
42 }
43 
45 {
46  init();
47  C1 = mch->C1;
48  C2 = mch->C2;
49  use_bias = mch->use_bias;
50 
51  set_features(mch->features);
52 
53  w_dim = mch->w_dim;
54  if (w_dim > 0)
55  {
57  memcpy(w, mch->w, w_dim*sizeof(float32_t));
58  }
59  else
60  {
61  w = NULL;
62  }
63  bias = mch->bias;
64 }
65 
66 
67 void COnlineLibLinear::init()
68 {
69  C1=1;
70  C2=1;
71  use_bias=false;
72 
73  m_parameters->add(&C1, "C1", "C Cost constant 1.");
74  m_parameters->add(&C2, "C2", "C Cost constant 2.");
75  m_parameters->add(&use_bias, "use_bias", "Indicates if bias is used.");
76 }
77 
79 {
80 }
81 
83 {
84  Cp = C1;
85  Cn = C2;
86  PGmax_old = CMath::INFTY;
87  PGmin_old = -CMath::INFTY;
88  PGmax_new = -CMath::INFTY;
89  PGmin_new = CMath::INFTY;
90 
91  diag[0]=0;diag[1]=0;diag[2]=0;
92  upper_bound[0]=Cn;upper_bound[1]=0;upper_bound[2]=Cp;
93 
94  bias = 0;
95 
96  PGmax_new = -CMath::INFTY;
97  PGmin_new = CMath::INFTY;
98 
99  v = 0;
100  nSV = 0;
101 }
102 
104 {
105  float64_t gap = PGmax_new - PGmin_new;
106 
107  SG_DONE();
108  SG_INFO("Optimization finished.\n");
109 
110  // calculate objective value
111  for (int32_t i=0; i<w_dim; i++)
112  v += w[i]*w[i];
113  v += bias*bias;
114 
115  SG_INFO("Objective value = %lf\n", v/2);
116  SG_INFO("nSV = %d\n", nSV);
117  SG_INFO("gap = %g\n", gap);
118 }
119 
121 {
122  alpha_current = 0;
123  if (label > 0)
124  y_current = +1;
125  else
126  y_current = -1;
127 
128  QD = diag[y_current + 1];
129  // Dot product of vector with itself
130  QD += SGVector<float32_t>::dot(ex.vector, ex.vector, ex.vlen);
131 
132  if (ex.vlen > w_dim)
133  {
134  w = SG_REALLOC(float32_t, w, ex.vlen);
135  memset(&w[w_dim], 0, (ex.vlen - w_dim)*sizeof(float32_t));
136  w_dim = ex.vlen;
137  }
138 
140  if (use_bias)
141  G += bias;
142  G = G*y_current - 1;
143  // LINEAR TERM PART?
144 
145  C = upper_bound[y_current + 1];
146  G += alpha_current*diag[y_current + 1]; // Can be eliminated, since diag = 0 vector
147 
148  PG = 0;
149  if (alpha_current == 0) // This condition will always be true in the online version
150  {
151  if (G > PGmax_old)
152  {
153  return;
154  }
155  else if (G < 0)
156  PG = G;
157  }
158  else if (alpha_current == C)
159  {
160  if (G < PGmin_old)
161  {
162  return;
163  }
164  else if (G > 0)
165  PG = G;
166  }
167  else
168  PG = G;
169 
170  PGmax_new = CMath::max(PGmax_new, PG);
171  PGmin_new = CMath::min(PGmin_new, PG);
172 
173  if (fabs(PG) > 1.0e-12)
174  {
175  float64_t alpha_old = alpha_current;
176  alpha_current = CMath::min(CMath::max(alpha_current - G/QD, 0.0), C);
177  d = (alpha_current - alpha_old) * y_current;
178 
179  for (int32_t i=0; i < w_dim; ++i)
180  w[i] += d*ex[i];
181 
182 
183  if (use_bias)
184  bias += d;
185  }
186 
187  v += alpha_current*(alpha_current*diag[y_current + 1] - 2);
188  if (alpha_current > 0)
189  nSV++;
190 }
191 
193 {
195  dynamic_cast<CStreamingDenseFeatures<float32_t> *>(feature);
196  if (feat == NULL)
197  SG_ERROR("Expected streaming dense feature <float32_t>\n");
198 
199  train_one(feat->get_vector(), label);
200 }
201 

SHOGUN Machine Learning Toolbox - Documentation