SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MulticlassModel.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) 2013 Thoralf Klein
8  * Written (W) 2012 Fernando José Iglesias García
9  * Copyright (C) 2012 Fernando José Iglesias García
10  */
11 
16 
17 using namespace shogun;
18 
21 {
22  init();
23 }
24 
26 : CStructuredModel(features, labels)
27 {
28  init();
29 }
30 
32 {
33 }
34 
36 {
37  return new CMulticlassSOLabels(num_labels);
38 }
39 
41 {
42  // TODO make the casts safe!
43  int32_t num_classes = ((CMulticlassSOLabels*) m_labels)->get_num_classes();
44  int32_t feats_dim = ((CDotFeatures*) m_features)->get_dim_feature_space();
45 
46  return feats_dim*num_classes;
47 }
48 
50 {
52  psi.zero();
53 
55  get_computed_dot_feature_vector(feat_idx);
57  ASSERT(r != NULL)
58  float64_t label_value = r->value;
59 
60  for ( index_t i = 0, j = label_value*x.vlen ; i < x.vlen ; ++i, ++j )
61  psi[j] = x[i];
62 
63  return psi;
64 }
65 
68  int32_t feat_idx,
69  bool const training)
70 {
72  int32_t feats_dim = df->get_dim_feature_space();
73 
74  if ( training )
75  {
77  m_num_classes = ml->get_num_classes();
78  }
79  else
80  {
81  REQUIRE(m_num_classes > 0, "The model needs to be trained before "
82  "using it for prediction\n");
83  }
84 
85  int32_t dim = get_dim();
86  ASSERT(dim == w.vlen)
87 
88  // Find the class that gives the maximum score
89 
90  float64_t score = 0, ypred = 0;
91  float64_t max_score = -CMath::INFTY;
92 
93  for ( int32_t c = 0 ; c < m_num_classes ; ++c )
94  {
95  score = df->dense_dot(feat_idx, w.vector+c*feats_dim, feats_dim);
96  if ( training )
97  score += delta_loss(feat_idx, c);
98 
99  if ( score > max_score )
100  {
101  max_score = score;
102  ypred = c;
103  }
104  }
105 
106  // Build the CResultSet object to return
107  CResultSet* ret = new CResultSet();
108  SG_REF(ret);
109  ret->psi_computed = true;
110  CRealNumber* y = new CRealNumber(ypred);
111  SG_REF(y);
112 
113  ret->psi_pred = get_joint_feature_vector(feat_idx, y);
114  ret->score = max_score;
115  ret->argmax = y;
116  if ( training )
117  {
118  ret->delta = CStructuredModel::delta_loss(feat_idx, y);
120  feat_idx, feat_idx);
121  ret->score -= CMath::dot(w.vector,
122  ret->psi_truth.vector, dim);
123  }
124 
125  return ret;
126 }
127 
129 {
132  ASSERT(rn1 != NULL)
133  ASSERT(rn2 != NULL)
134 
135  return delta_loss(rn1->value, rn2->value);
136 }
137 
139 {
140  REQUIRE(y1_idx >= 0 || y1_idx < m_labels->get_num_labels(),
141  "The label index must be inside [0, num_labels-1]\n");
142 
144  float64_t ret = delta_loss(rn1->value, y2);
145  SG_UNREF(rn1);
146 
147  return ret;
148 }
149 
151 {
152  return (y1 == y2) ? 0 : 1;
153 }
154 
156  float64_t regularization,
164 {
166 }
167 
168 void CMulticlassModel::init()
169 {
170  SG_ADD(&m_num_classes, "m_num_classes", "The number of classes",
172 
173  m_num_classes = 0;
174 }
175 
SGVector< float64_t > psi_truth
Base class of the labels used in Structured Output (SO) problems.
int32_t index_t
Definition: common.h:62
static const float64_t INFTY
infinity
Definition: Math.h:2048
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
virtual void init_primal_opt(float64_t regularization, SGMatrix< float64_t > &A, SGVector< float64_t > a, SGMatrix< float64_t > B, SGVector< float64_t > &b, SGVector< float64_t > &lb, SGVector< float64_t > &ub, SGMatrix< float64_t > &C)
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual float64_t delta_loss(CStructuredData *y1, CStructuredData *y2)
virtual CResultSet * argmax(SGVector< float64_t > w, int32_t feat_idx, bool const training=true)
SGVector< float64_t > get_joint_feature_vector(int32_t feat_idx, int32_t lab_idx)
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:54
static CRealNumber * obtain_from_generic(CStructuredData *base_data)
virtual SGVector< float64_t > get_joint_feature_vector(int32_t feat_idx, CStructuredData *y)
virtual int32_t get_dim_feature_space() const =0
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
float64_t delta_loss(int32_t ytrue_idx, CStructuredData *ypred)
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
Definition: Math.h:627
virtual int32_t get_dim() const
Class CStructuredModel that represents the application specific model and contains most of the applic...
#define SG_UNREF(x)
Definition: SGObject.h:55
CStructuredLabels * m_labels
virtual CStructuredLabels * structured_labels_factory(int32_t num_labels=0)
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
CStructuredData * argmax
virtual CStructuredData * get_label(int32_t idx)
SGVector< float64_t > psi_pred
Class CRealNumber to be used in the application of Structured Output (SO) learning to multiclass clas...
#define SG_ADD(...)
Definition: SGObject.h:84
Class CMulticlassSOLabels to be used in the application of Structured Output (SO) learning to multicl...
static SGMatrix< T > create_identity_matrix(index_t size, T scale)
Base class of the components of StructuredLabels.

SHOGUN Machine Learning Toolbox - Documentation