SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DomainAdaptationSVMLinear.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-2011 Christian Widmer
8  * Copyright (C) 2007-2011 Max-Planck-Society
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifdef HAVE_LAPACK
14 
16 #include <shogun/io/SGIO.h>
17 #include <shogun/base/Parameter.h>
18 #include <shogun/labels/Labels.h>
21 #include <iostream>
22 #include <vector>
23 
24 using namespace shogun;
25 
26 
28 {
29  init(NULL, 0.0);
30 }
31 
32 
34 {
35  init(pre_svm, B_param);
36 
37 }
38 
39 
41 {
42 
44  SG_DEBUG("deleting DomainAdaptationSVMLinear\n")
45 }
46 
47 
48 void CDomainAdaptationSVMLinear::init(CLinearMachine* pre_svm, float64_t B_param)
49 {
50 
51  if (pre_svm)
52  {
53  // increase reference counts
54  SG_REF(pre_svm);
55 
56  // set bias of parent svm to zero
57  pre_svm->set_bias(0.0);
58  }
59 
60  this->presvm = pre_svm;
61  this->B = B_param;
62  this->train_factor = 1.0;
63 
65 
66  // invoke sanity check
68 
69  // serialization code
70  m_parameters->add((CSGObject**) &presvm, "presvm", "SVM to regularize against");
71  m_parameters->add(&B, "B", "Regularization strenth B.");
72  m_parameters->add(&train_factor, "train_factor", "train_factor");
73 
74 }
75 
76 
78 {
79 
80  if (!presvm) {
81 
82  SG_WARNING("presvm is null")
83 
84  } else {
85 
86  if (presvm->get_bias() != 0) {
87  SG_ERROR("presvm bias not set to zero")
88  }
89 
91  SG_ERROR("feature types do not agree")
92  }
93  }
94 
95  return true;
96 
97 }
98 
99 
101 {
102 
103  CDotFeatures* tmp_data;
104 
106  SG_ERROR("DomainAdaptationSVMLinear requires binary labels\n")
107 
108  if (train_data)
109  {
110  if (!train_data->has_property(FP_DOT))
111  SG_ERROR("DotFeatures expected\n")
112 
113  if (((CBinaryLabels*) m_labels)->get_num_labels() != train_data->get_num_vectors())
114  SG_ERROR("Number of training vectors does not match number of labels\n")
115 
116  tmp_data = (CDotFeatures*) train_data;
117  }
118  else
119  {
120  tmp_data = features;
121  }
122 
123  CBinaryLabels* labels = (CBinaryLabels*) get_labels();
124  int32_t num_training_points = labels->get_num_labels();
125 
126  std::vector<float64_t> lin_term = std::vector<float64_t>(num_training_points);
127 
128  if (presvm)
129  {
130  ASSERT(presvm->get_bias() == 0.0)
131 
132  // bias of parent SVM was set to zero in constructor, already contains B
133  CBinaryLabels* parent_svm_out = presvm->apply_binary(tmp_data);
134 
135  SG_DEBUG("pre-computing linear term from presvm\n")
136 
137  // pre-compute linear term
138  for (int32_t i=0; i!=num_training_points; i++)
139  {
140  lin_term[i] = train_factor * B * labels->get_value(i) * parent_svm_out->get_value(i) - 1.0;
141  }
142 
143  // set linear term for QP
144  this->set_linear_term(
145  SGVector<float64_t>(&lin_term[0], lin_term.size()));
146 
147  }
148 
149  SG_UNREF(labels);
150 
151  /*
152  // warm-start liblinear
153  //TODO test this code, measure speed-ups
154  //presvm w stored in presvm
155  float64_t* tmp_w;
156  presvm->get_w(tmp_w, w_dim);
157 
158  //copy vector
159  float64_t* tmp_w_copy = SG_MALLOC(float64_t, w_dim);
160  std::copy(tmp_w, tmp_w + w_dim, tmp_w_copy);
161 
162  for (int32_t i=0; i!=w_dim; i++)
163  {
164  tmp_w_copy[i] = B * tmp_w_copy[i];
165  }
166 
167  //set w (copied in setter)
168  set_w(tmp_w_copy, w_dim);
169  SG_FREE(tmp_w_copy);
170  */
171 
172  bool success = false;
173 
174  //train SVM
175  if (train_data)
176  {
177  success = CLibLinear::train_machine(train_data);
178  } else {
179  success = CLibLinear::train_machine();
180  }
181 
182  //ASSERT(presvm)
183 
184  return success;
185 
186 }
187 
188 
190 {
191  return presvm;
192 }
193 
194 
196 {
197  return B;
198 }
199 
200 
202 {
203  return train_factor;
204 }
205 
206 
208 {
209  train_factor = factor;
210 }
211 
212 
214 {
215  ASSERT(presvm->get_bias()==0.0)
216 
217  int32_t num_examples = data->get_num_vectors();
218 
219  CBinaryLabels* out_current = CLibLinear::apply_binary(data);
220 
221  SGVector<float64_t> out_combined(num_examples);
222  if (presvm)
223  {
224  // recursive call if used on DomainAdaptationSVM object
225  CBinaryLabels* out_presvm = presvm->apply_binary(data);
226 
227 
228  // combine outputs
229  for (int32_t i=0; i!=num_examples; i++)
230  out_combined[i] = out_current->get_value(i) + B*out_presvm->get_value(i);
231 
232  SG_UNREF(out_presvm);
233  }
234 
235  SG_UNREF(out_current);
236 
237  return new CBinaryLabels(out_combined);
238 }
239 
240 #endif //HAVE_LAPACK
241 

SHOGUN Machine Learning Toolbox - Documentation