SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShareBoostOptimizer.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) 2012 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
11 #include <algorithm>
12 
16 
17 using namespace shogun;
18 
20 {
21  int32_t N = m_sb->m_multiclass_strategy->get_num_classes() * m_sb->m_activeset.vlen;
22  float64_t *W = SG_CALLOC(float64_t, N); // should use this function, if sse is enabled for liblbfgs
23  float64_t objval;
24  lbfgs_parameter_t param;
25  lbfgs_parameter_init(&param);
26 
27  std::fill(W, W+N, 0);
28 
29  lbfgs_progress_t progress = m_verbose ? &ShareBoostOptimizer::lbfgs_progress : NULL;
30 
31  lbfgs(N, W, &objval, &ShareBoostOptimizer::lbfgs_evaluate, progress, this, &param);
32 
33  int32_t w_len = m_sb->m_activeset.vlen;
34  for (int32_t i=0; i < m_sb->m_multiclass_strategy->get_num_classes(); ++i)
35  {
36  CLinearMachine *machine = dynamic_cast<CLinearMachine *>(m_sb->m_machines->get_element(i));
37  SGVector<float64_t> w(w_len);
38  std::copy(W + i*w_len, W + (i+1)*w_len, w.vector);
39  machine->set_w(w);
40  SG_UNREF(machine);
41  }
42 
43  SG_FREE(W);
44 }
45 
46 float64_t ShareBoostOptimizer::lbfgs_evaluate(void *userdata, const float64_t *W,
47  float64_t *grad, const int32_t n, const float64_t step)
48 {
49  ShareBoostOptimizer *optimizer = static_cast<ShareBoostOptimizer *>(userdata);
50 
51  optimizer->m_sb->compute_pred(W);
52  optimizer->m_sb->compute_rho();
53 
54  int32_t m = optimizer->m_sb->m_activeset.vlen;
55  int32_t k = optimizer->m_sb->m_multiclass_strategy->get_num_classes();
56 
57  SGMatrix<float64_t> fea = optimizer->m_sb->m_fea;
58  CMulticlassLabels *lab = dynamic_cast<CMulticlassLabels *>(optimizer->m_sb->m_labels);
59 
60  // compute gradient
61  for (int32_t i=0; i < m; ++i)
62  {
63  for (int32_t j=0; j < k; ++j)
64  {
65  int32_t idx = j*m + i;
66  float64_t g=0;
67  for (int32_t ii=0; ii < fea.num_cols; ++ii)
68  g += fea(optimizer->m_sb->m_activeset[i], ii) *
69  (optimizer->m_sb->m_rho(j,ii)/optimizer->m_sb->m_rho_norm[ii] -
70  (j == lab->get_int_label(ii)));
71  g /= fea.num_cols;
72  grad[idx] = g;
73  }
74  }
75 
76  // compute objective function
77  float64_t objval = 0;
78  for (int32_t ii=0; ii < fea.num_cols; ++ii)
79  {
80  objval += CMath::log(optimizer->m_sb->m_rho_norm[ii]);
81  }
82  objval /= fea.num_cols;
83 
84  return objval;
85 }
86 
87 int ShareBoostOptimizer::lbfgs_progress(
88  void *instance,
89  const float64_t *x,
90  const float64_t *g,
91  const float64_t fx,
92  const float64_t xnorm,
93  const float64_t gnorm,
94  const float64_t step,
95  int n,
96  int k,
97  int ls
98  )
99 {
100  if (k != 1 && k % 100 != 0)
101  return 0;
102 
103  SG_SPRINT("Iteration %d:\n", k)
104  SG_SPRINT(" fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1])
105  SG_SPRINT(" xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step)
106  SG_SPRINT("\n")
107  return 0;
108 }

SHOGUN Machine Learning Toolbox - Documentation