SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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);
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 

SHOGUN Machine Learning Toolbox - Documentation