SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
11 #include <typeinfo>
12 
16 
17 using namespace shogun;
18 
21 {
22 }
23 
25  : CLinearLatentMachine(model, C)
26 {
27 }
28 
30 {
31 }
32 
34 {
35  if (!m_model)
36  SG_ERROR("LatentModel is not set!\n")
37 
38  if (m_model->get_num_vectors() < 1)
39  return NULL;
40 
41  index_t num_examples = m_model->get_num_vectors();
42  CLatentLabels* hs = new CLatentLabels(num_examples);
43  CBinaryLabels* ys = new CBinaryLabels(num_examples);
44  hs->set_labels(ys);
45  m_model->set_labels(hs);
46 
47  for (index_t i = 0; i < num_examples; ++i)
48  {
49  /* find h for the example */
51  hs->add_latent_label(h);
52  }
53 
54  /* compute the y labels */
56  x->dense_dot_range(ys->get_labels().vector, 0, num_examples, NULL, w.vector, w.vlen, 0.0);
57 
58  return hs;
59 }
60 
62 {
64  CDotFeatures* feats = (m_model->get_caching() ?
67  CSVMOcas svm(m_C, feats, ys);
68  svm.set_epsilon(cooling_eps);
69  svm.train();
70  SG_UNREF(ys);
71  SG_UNREF(feats);
72 
73  /* copy the resulting w */
74  SGVector<float64_t> cur_w = svm.get_w();
75  memcpy(w.vector, cur_w.vector, cur_w.vlen*sizeof(float64_t));
76 
77  return svm.compute_primal_objective();
78 }
79 
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
virtual ~CLatentSVM()
Definition: LatentSVM.cpp:29
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
void set_epsilon(float64_t eps)
Definition: SVMOcas.h:91
CLatentLabels * get_labels() const
Definition: LatentModel.cpp:55
virtual float64_t do_inner_loop(float64_t cooling_eps)
Definition: LatentSVM.cpp:61
#define SG_ERROR(...)
Definition: SGIO.h:129
bool get_caching() const
Definition: LatentModel.h:124
virtual CLatentLabels * apply_latent()
Definition: LatentSVM.cpp:33
Features that support dot products among other operations.
Definition: DotFeatures.h:44
virtual float64_t compute_primal_objective() const
Definition: SVMOcas.cpp:361
void set_labels(CLatentLabels *labs)
Definition: LatentModel.cpp:48
SGVector< float64_t > get_labels()
Definition: DenseLabels.cpp:82
index_t vlen
Definition: SGVector.h:494
CLabels * get_labels() const
dummy data holder
Definition: Data.h:25
void set_labels(CLabels *labels)
double float64_t
Definition: common.h:50
virtual CData * infer_latent_variable(const SGVector< float64_t > &w, index_t idx)=0
SGVector< float64_t > w
virtual SGVector< float64_t > get_w() const
#define SG_UNREF(x)
Definition: SGObject.h:52
CDotFeatures * get_cached_psi_features() const
virtual CDotFeatures * get_psi_feature_vectors()=0
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 ...
virtual bool train(CFeatures *data=NULL)
Definition: Machine.cpp:47
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
class SVMOcas
Definition: SVMOcas.h:34
virtual int32_t get_num_vectors() const
Definition: LatentModel.cpp:43
void add_latent_label(CData *label)
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