SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
LatentSVM.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 
12 #ifdef USE_GPL_SHOGUN
13 
14 #include <typeinfo>
15 
18 
19 using namespace shogun;
20 
21 CLatentSVM::CLatentSVM()
23 {
24 }
25 
26 CLatentSVM::CLatentSVM(CLatentModel* model, float64_t C)
27  : CLinearLatentMachine(model, C)
28 {
29 }
30 
31 CLatentSVM::~CLatentSVM()
32 {
33 }
34 
35 CLatentLabels* CLatentSVM::apply_latent()
36 {
37  if (!m_model)
38  SG_ERROR("LatentModel is not set!\n")
39 
40  if (m_model->get_num_vectors() < 1)
41  return NULL;
42 
43  index_t num_examples = m_model->get_num_vectors();
44  CLatentLabels* hs = new CLatentLabels(num_examples);
45  CBinaryLabels* ys = new CBinaryLabels(num_examples);
46  hs->set_labels(ys);
47  m_model->set_labels(hs);
48 
49  for (index_t i = 0; i < num_examples; ++i)
50  {
51  /* find h for the example */
52  CData* h = m_model->infer_latent_variable(w, i);
53  hs->add_latent_label(h);
54  }
55 
56  /* compute the y labels */
57  CDotFeatures* x = m_model->get_psi_feature_vectors();
58  x->dense_dot_range(ys->get_labels().vector, 0, num_examples, NULL, w.vector, w.vlen, 0.0);
59 
60  return hs;
61 }
62 
63 float64_t CLatentSVM::do_inner_loop(float64_t cooling_eps)
64 {
65  CLabels* ys = m_model->get_labels()->get_labels();
66  CDotFeatures* feats = (m_model->get_caching() ?
67  m_model->get_cached_psi_features() :
68  m_model->get_psi_feature_vectors());
69  CSVMOcas svm(m_C, feats, ys);
70  svm.set_epsilon(cooling_eps);
71  svm.train();
72  SG_UNREF(ys);
73  SG_UNREF(feats);
74 
75  /* copy the resulting w */
76  SGVector<float64_t> cur_w = svm.get_w();
77  memcpy(w.vector, cur_w.vector, cur_w.vlen*sizeof(float64_t));
78 
79  return svm.compute_primal_objective();
80 }
81 
82 #endif //USE_GPL_SHOGUN
83 
Abstract class CLatentModel It represents the application specific model and contains most of the app...
Definition: LatentModel.h:33
virtual void dense_dot_range(float64_t *output, int32_t start, int32_t stop, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
Definition: DotFeatures.cpp:67
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
#define SG_ERROR(...)
Definition: SGIO.h:129
Features that support dot products among other operations.
Definition: DotFeatures.h:44
index_t vlen
Definition: SGVector.h:494
dummy data holder
Definition: Data.h:25
double float64_t
Definition: common.h:50
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
abstract implementaion of Linear Machine with latent variable This is the base implementation of all ...
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
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