SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StructuredModel.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 Fernando José Iglesias García
8  * Copyright (C) 2012 Fernando José Iglesias García
9  */
10 
12 
13 using namespace shogun;
14 
16 {
17  init();
18 }
19 
21  CFeatures* features,
22  CStructuredLabels* labels)
23 : CSGObject()
24 {
25  init();
26 
27  m_features = features;
28  m_labels = labels;
29 
30  SG_REF(features);
31  SG_REF(labels);
32 }
33 
35 {
38 }
39 
48 {
49  SG_ERROR("init_opt is not implemented for %s!\n", get_name());
50 }
51 
53 {
55  SG_REF(labels);
56  m_labels = labels;
57 }
58 
60 {
62  return m_labels;
63 }
64 
66 {
68  SG_REF(features);
69  m_features = features;
70 }
71 
73 {
75  return m_features;
76 }
77 
79  int32_t feat_idx,
80  int32_t lab_idx)
81 {
82  CStructuredData* label = m_labels->get_label(lab_idx);
83  SGVector< float64_t > ret = get_joint_feature_vector(feat_idx, label);
84  SG_UNREF(label);
85 
86  return ret;
87 }
88 
90  int32_t feat_idx,
91  CStructuredData* y)
92 {
93  SG_ERROR("compute_joint_feature(int32_t, CStructuredData*) is not "
94  "implemented for %s!\n", get_name());
95 
96  return SGVector< float64_t >();
97 }
98 
100 {
101  REQUIRE(ytrue_idx >= 0 || ytrue_idx < m_labels->get_num_labels(),
102  "The label index must be inside [0, num_labels-1]\n");
103 
104  CStructuredData* ytrue = m_labels->get_label(ytrue_idx);
105  float64_t ret = delta_loss(ytrue, ypred);
106  SG_UNREF(ytrue);
107 
108  return ret;
109 }
110 
112 {
113  SG_ERROR("delta_loss(CStructuredData*, CStructuredData*) is not "
114  "implemented for %s!\n", get_name());
115 
116  return 0.0;
117 }
118 
119 void CStructuredModel::init()
120 {
121  SG_ADD((CSGObject**) &m_labels, "m_labels", "Structured labels",
123  SG_ADD((CSGObject**) &m_features, "m_features", "Feature vectors",
125 
126  m_features = NULL;
127  m_labels = NULL;
128 }
129 
131 {
132  // Nothing to do here
133  return true;
134 }
135 
137 {
138  return 0;
139 }
140 
142 {
143  return 0;
144 }
145 
147 {
148  int32_t from=0, to=0;
149  if (info)
150  {
151  from = info->_from;
152  to = (info->N == 0) ? m_features->get_num_vectors() : from+info->N;
153  }
154  else
155  {
156  from = 0;
157  to = m_features->get_num_vectors();
158  }
159 
160  int32_t dim = this->get_dim();
161  float64_t R = 0.0;
162  for (int32_t i=0; i<dim; i++)
163  subgrad[i] = 0;
164 
165  for (int32_t i=from; i<to; i++)
166  {
167  CResultSet* result = this->argmax(SGVector<float64_t>(W,dim,false), i, true);
168  SGVector<float64_t> psi_pred = result->psi_pred;
169  SGVector<float64_t> psi_truth = result->psi_truth;
170  SGVector<float64_t>::vec1_plus_scalar_times_vec2(subgrad, 1.0, psi_pred.vector, dim);
171  SGVector<float64_t>::vec1_plus_scalar_times_vec2(subgrad, -1.0, psi_truth.vector, dim);
172  R += result->score;
173  SG_UNREF(result);
174  }
175 
176  return R;
177 }

SHOGUN Machine Learning Toolbox - Documentation