SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinearLatentMachine.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 Viktor Gal
8  * Copyright (C) 2012 Viktor Gal
9  */
10 
11 #include <typeinfo>
12 
16 
17 using namespace shogun;
18 
20  : CLinearMachine()
21 {
22  init();
23 }
24 
26  : CLinearMachine()
27 {
28  init();
29  m_C= C;
30  set_model(model);
31 
32  index_t feat_dim = m_model->get_dim();
33  w.resize_vector(feat_dim);
34  w.zero();
35 }
36 
38 {
40 }
41 
43 {
44  if (m_model == NULL)
45  SG_ERROR("LatentModel is not set!\n")
46 
48  m_model->set_features(lf);
49 
50  return apply_latent();
51 }
52 
54 {
55  ASSERT(latent_model != NULL)
56  SG_REF(latent_model);
58  m_model = latent_model;
59 }
60 
62 {
63  if (m_model == NULL)
64  SG_ERROR("LatentModel is not set!\n")
65 
66  SG_DEBUG("PSI size: %d\n", m_model->get_dim())
67  SG_DEBUG("Number of training data: %d\n", m_model->get_num_vectors())
68  SG_DEBUG("Initialise PSI (x,h)\n")
70 
71  /*
72  * define variables for calculating the stopping
73  * criterion for the outer loop
74  */
75  float64_t decrement = 0.0, primal_obj = 0.0, prev_po = 0.0;
76  float64_t inner_eps = 0.5*m_C*m_epsilon;
77  bool stop = false;
78  m_cur_iter = 0;
79 
80  /* do CCCP */
81  SG_DEBUG("Starting CCCP\n")
82  while ((m_cur_iter < 2)||(!stop&&(m_cur_iter < m_max_iter)))
83  {
84  SG_DEBUG("iteration: %d\n", m_cur_iter)
85  /* do the SVM optimisation with fixed h* */
86  SG_DEBUG("Do the inner loop of CCCP: optimize for w for fixed h*\n")
87  primal_obj = do_inner_loop(inner_eps);
88 
89  /* calculate the decrement */
90  decrement = prev_po - primal_obj;
91  prev_po = primal_obj;
92  SG_DEBUG("decrement: %f\n", decrement)
93  SG_DEBUG("primal objective: %f\n", primal_obj)
94 
95  /* check the stopping criterion */
96  stop = (inner_eps < (0.5*m_C*m_epsilon+1E-8)) && (decrement < m_C*m_epsilon);
97 
98  inner_eps = -decrement*0.01;
99  inner_eps = CMath::max(inner_eps, 0.5*m_C*m_epsilon);
100  SG_DEBUG("inner epsilon: %f\n", inner_eps)
101 
102  /* find argmaxH */
103  SG_DEBUG("Find and set h_i = argmax_h (w, psi(x_i,h))\n")
104  m_model->argmax_h(w);
105 
106  SG_DEBUG("Recalculating PSI (x,h) with the new h variables\n")
108 
109  /* increment iteration counter */
110  m_cur_iter++;
111  }
112 
113  return true;
114 }
115 
116 void CLinearLatentMachine::init()
117 {
118  m_C = 10.0;
119  m_epsilon = 1E-3;
120  m_max_iter = 400;
121  m_model = NULL;
122 
123  m_parameters->add(&m_C, "C", "Cost constant.");
124  m_parameters->add(&m_epsilon, "epsilon", "Convergence precision.");
125  m_parameters->add(&m_max_iter, "max_iter", "Maximum iterations.");
126  m_parameters->add((CSGObject**) &m_model, "latent_model", "Latent Model.");
127 }
128 

SHOGUN Machine Learning Toolbox - Documentation