SHOGUN  6.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  SGVector<float64_t> w = get_w();
44  index_t num_examples = m_model->get_num_vectors();
45  CLatentLabels* hs = new CLatentLabels(num_examples);
46  CBinaryLabels* ys = new CBinaryLabels(num_examples);
47  hs->set_labels(ys);
48  m_model->set_labels(hs);
49 
50  for (index_t i = 0; i < num_examples; ++i)
51  {
52  /* find h for the example */
53  CData* h = m_model->infer_latent_variable(w, i);
54  hs->add_latent_label(h);
55  }
56 
57  /* compute the y labels */
58  CDotFeatures* x = m_model->get_psi_feature_vectors();
59  x->dense_dot_range(ys->get_labels().vector, 0, num_examples, NULL, w.vector, w.vlen, 0.0);
60 
61  return hs;
62 }
63 
64 float64_t CLatentSVM::do_inner_loop(float64_t cooling_eps)
65 {
66  CLabels* ys = m_model->get_labels()->get_labels();
67  CDotFeatures* feats = (m_model->get_caching() ?
68  m_model->get_cached_psi_features() :
69  m_model->get_psi_feature_vectors());
70  CSVMOcas svm(m_C, feats, ys);
71  svm.set_epsilon(cooling_eps);
72  svm.train();
73  SG_UNREF(ys);
74  SG_UNREF(feats);
75 
76  /* copy the resulting w */
77  set_w(svm.get_w().clone());
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:51
int32_t index_t
Definition: common.h:72
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
#define SG_ERROR(...)
Definition: SGIO.h:128
Features that support dot products among other operations.
Definition: DotFeatures.h:44
shogun vector
dummy data holder
Definition: Data.h:25
double float64_t
Definition: common.h:60
#define SG_UNREF(x)
Definition: SGObject.h:53
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