SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 }
int32_t lbfgs(int32_t n, float64_t *x, float64_t *ptr_fx, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress, void *instance, lbfgs_parameter_t *_param, lbfgs_adjust_step_t proc_adjust_step)
Definition: lbfgs.cpp:208
virtual void set_w(const SGVector< float64_t > src_w)
CLabels * m_labels
Definition: Machine.h:361
index_t num_cols
Definition: SGMatrix.h:376
Multiclass Labels for multi-class classification.
index_t vlen
Definition: SGVector.h:494
#define SG_SPRINT(...)
Definition: SGIO.h:180
CMulticlassStrategy * m_multiclass_strategy
int32_t get_int_label(int32_t idx)
double float64_t
Definition: common.h:50
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static float64_t log(float64_t v)
Definition: Math.h:922
CSGObject * get_element(int32_t index) const
int(* lbfgs_progress_t)(void *instance, const float64_t *x, const float64_t *g, const float64_t fx, const float64_t xnorm, const float64_t gnorm, const float64_t step, int n, int k, int ls)
Definition: lbfgs.h:385
void lbfgs_parameter_init(lbfgs_parameter_t *param)
Definition: lbfgs.cpp:203

SHOGUN Machine Learning Toolbox - Documentation