SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
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...
int32_t index_t
Definition: common.h:62
static CLatentFeatures * obtain_from_generic(CFeatures *base_feats)
virtual CLatentLabels * apply_latent()=0
#define SG_ERROR(...)
Definition: SGIO.h:129
Parameter * m_parameters
Definition: SGObject.h:546
#define SG_REF(x)
Definition: SGObject.h:54
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
double float64_t
Definition: common.h:50
void set_model(CLatentModel *latent_model)
SGVector< float64_t > w
static T max(T a, T b)
Definition: Math.h:168
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
virtual void argmax_h(const SGVector< float64_t > &w)
Definition: LatentModel.cpp:68
#define SG_UNREF(x)
Definition: SGObject.h:55
#define SG_DEBUG(...)
Definition: SGIO.h:107
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:257
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