SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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();
32  w.resize_vector(feat_dim);
33  w.zero();
34  set_w(w);
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(get_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 
virtual float64_t do_inner_loop(float64_t cooling_eps)=0
Abstract class CLatentModel It represents the application specific model and contains most of the app...
Definition: LatentModel.h:33
Latent Features class The class if for representing features for latent learning, e...
virtual void set_w(const SGVector< float64_t > src_w)
int32_t index_t
Definition: common.h:72
static CLatentFeatures * obtain_from_generic(CFeatures *base_feats)
virtual CLatentLabels * apply_latent()=0
#define SG_ERROR(...)
Definition: SGIO.h:128
Parameter * m_parameters
Definition: SGObject.h:567
#define SG_REF(x)
Definition: SGObject.h:52
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:38
#define ASSERT(x)
Definition: SGIO.h:200
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
double float64_t
Definition: common.h:60
void set_model(CLatentModel *latent_model)
static T max(T a, T b)
Definition: Math.h:164
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
virtual SGVector< float64_t > get_w() const
virtual void argmax_h(const SGVector< float64_t > &w)
Definition: LatentModel.cpp:68
#define SG_UNREF(x)
Definition: SGObject.h:53
#define SG_DEBUG(...)
Definition: SGIO.h:106
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual int32_t get_dim() const =0
virtual bool train_machine(CFeatures *data=NULL)
void resize_vector(int32_t n)
Definition: SGVector.cpp:286
virtual int32_t get_num_vectors() const
Definition: LatentModel.cpp:43
void set_features(CLatentFeatures *feats)
Definition: LatentModel.cpp:61
abstract class for latent labels As latent labels always depends on the given application, this class only defines the API that the user has to implement for latent labels.
Definition: LatentLabels.h:26

SHOGUN Machine Learning Toolbox - Documentation