SHOGUN  v2.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  set_w(SGVector<float64_t> (feat_dim));
34 }
35 
37 {
39 }
40 
42 {
43  if (m_model == NULL)
44  SG_ERROR("LatentModel is not set!\n");
45 
47  m_model->set_features(lf);
48 
49  return apply_latent();
50 }
51 
53 {
54  ASSERT(latent_model != NULL);
56  SG_REF(latent_model);
57  m_model = latent_model;
58 }
59 
61 {
63 }
64 
66 {
67  if (m_model == NULL)
68  SG_ERROR("LatentModel is not set!\n");
69 
70  SG_DEBUG("Initialise PSI (x,h)\n");
72 
73  /*
74  * define variables for calculating the stopping
75  * criterion for the outer loop
76  */
77  float64_t decrement = 0.0, primal_obj = 0.0, prev_po = 0.0;
78  float64_t inner_eps = 0.5*m_C*m_epsilon;
79  bool stop = false;
80  int32_t iter = 0;
81 
82  /* do CCCP */
83  SG_DEBUG("Starting CCCP\n");
84  while ((iter < 2)||(!stop&&(iter < m_max_iter)))
85  {
86  SG_DEBUG("iteration: %d\n", iter);
87  /* do the SVM optimisation with fixed h* */
88  SG_DEBUG("Do the inner loop of CCCP: optimize for w for fixed h*\n");
89  primal_obj = do_inner_loop(inner_eps);
90 
91  /* calculate the decrement */
92  decrement = prev_po - primal_obj;
93  prev_po = primal_obj;
94  SG_DEBUG("decrement: %f\n", decrement);
95  SG_DEBUG("primal objective: %f\n", primal_obj);
96 
97  /* check the stopping criterion */
98  stop = (inner_eps < (0.5*m_C*m_epsilon+1E-8)) && (decrement < m_C*m_epsilon);
99 
100  inner_eps = -decrement*0.01;
101  inner_eps = CMath::max(inner_eps, 0.5*m_C*m_epsilon);
102  SG_DEBUG("inner epsilon: %f\n", inner_eps);
103 
104  /* find argmaxH */
105  SG_DEBUG("Find and set h_i = argmax_h (w, psi(x_i,h))\n");
106  m_model->argmax_h(w);
107 
108  SG_DEBUG("Recalculating PSI (x,h) with the new h variables\n");
110 
111  /* increment iteration counter */
112  iter++;
113  }
114 
115  return true;
116 }
117 
118 void CLinearLatentMachine::init()
119 {
120  m_C = 10.0;
121  m_epsilon = 1E-3;
122  m_max_iter = 400;
124  SG_REF(features);
125  m_model = NULL;
126 
127  m_parameters->add(&m_C, "C", "Cost constant.");
128  m_parameters->add(&m_epsilon, "epsilon", "Convergence precision.");
129  m_parameters->add(&m_max_iter, "max_iter", "Maximum iterations.");
130  m_parameters->add((CSGObject**) &m_model, "latent_model", "Latent Model.");
131 }
132 

SHOGUN Machine Learning Toolbox - Documentation