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  CRealNumber* y = new CRealNumber(ypred);
110  SG_REF(y);
111 
112  ret->psi_pred = get_joint_feature_vector(feat_idx, y);
113  ret->score = max_score;
114  ret->argmax = y;
115  if ( training )
116  {
117  ret->delta = CStructuredModel::delta_loss(feat_idx, y);
119  feat_idx, feat_idx);
121  ret->psi_truth.vector, dim);
122  }
123 
124  return ret;
125 }
126 
128 {
131  ASSERT(rn1 != NULL)
132  ASSERT(rn2 != NULL)
133 
134  return delta_loss(rn1->value, rn2->value);
135 }
136 
138 {
139  REQUIRE(y1_idx >= 0 || y1_idx < m_labels->get_num_labels(),
140  "The label index must be inside [0, num_labels-1]\n");
141 
143  float64_t ret = delta_loss(rn1->value, y2);
144  SG_UNREF(rn1);
145 
146  return ret;
147 }
148 
150 {
151  return (y1 == y2) ? 0 : 1;
152 }
153 
155  float64_t regularization,
163 {
165 }
166 
167 void CMulticlassModel::init()
168 {
169  SG_ADD(&m_num_classes, "m_num_classes", "The number of classes",
171 
172  m_num_classes = 0;
173 }
174 

SHOGUN Machine Learning Toolbox - Documentation