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

SHOGUN Machine Learning Toolbox - Documentation