SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConjugateIndex.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) 2011 Sergey Lisitsyn
8  * Copyright (C) 2011 Sergey Lisitsyn
9  */
10 
12 #ifdef HAVE_LAPACK
13 #include <shogun/machine/Machine.h>
15 #include <shogun/labels/Labels.h>
19 #include <shogun/lib/Signal.h>
20 
21 using namespace shogun;
22 
24 {
25  m_classes = NULL;
26  m_features = NULL;
27 };
28 
29 CConjugateIndex::CConjugateIndex(CFeatures* train_features, CLabels* train_labels) : CMachine()
30 {
31  m_features = NULL;
32  set_features(train_features);
33  set_labels(train_labels);
34  m_classes = NULL;
35 };
36 
38 {
39  clean_classes();
41 };
42 
44 {
45  ASSERT(features->get_feature_class()==C_DENSE);
46  SG_REF(features);
49 }
50 
52 {
54  return m_features;
55 }
56 
58 {
59  if (m_classes)
60  {
61  for (int32_t i=0; i<m_num_classes; i++)
63 
64  delete[] m_classes;
65  }
66 }
67 
69 {
70  if (data)
71  set_features(data);
72 
75 
76  m_num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
77  ASSERT(m_num_classes>=2);
78  clean_classes();
79 
80  int32_t num_vectors;
81  int32_t num_features;
82  float64_t* feature_matrix = m_features->get_feature_matrix(num_features,num_vectors);
83 
85  for (int32_t i=0; i<m_num_classes; i++)
86  m_classes[i] = SGMatrix<float64_t>(num_features,num_features);
87 
88  m_feature_vector = SGVector<float64_t>(num_features);
89 
90  SG_PROGRESS(0,0,m_num_classes-1);
91 
92  for (int32_t label=0; label<m_num_classes; label++)
93  {
94  int32_t count = 0;
95  for (int32_t i=0; i<num_vectors; i++)
96  {
97  if (((CMulticlassLabels*) m_labels)->get_int_label(i) == label)
98  count++;
99  }
100 
101  SGMatrix<float64_t> class_feature_matrix(num_features,count);
102  SGMatrix<float64_t> matrix(count,count);
103  SGMatrix<float64_t> helper_matrix(num_features,count);
104 
105  count = 0;
106  for (int32_t i=0; i<num_vectors; i++)
107  {
108  if (((CMulticlassLabels*) m_labels)->get_label(i) == label)
109  {
110  memcpy(class_feature_matrix.matrix+count*num_features,
111  feature_matrix+i*num_features,
112  sizeof(float64_t)*num_features);
113  count++;
114  }
115  }
116 
117  cblas_dgemm(CblasColMajor,CblasTrans,CblasNoTrans,
118  count,count,num_features,
119  1.0,class_feature_matrix.matrix,num_features,
120  class_feature_matrix.matrix,num_features,
121  0.0,matrix.matrix,count);
122 
124 
125  cblas_dgemm(CblasColMajor,CblasNoTrans,CblasTrans,
126  count,num_features,count,
127  1.0,matrix.matrix,count,
128  class_feature_matrix.matrix,num_features,
129  0.0,helper_matrix.matrix,count);
130 
131  cblas_dgemm(CblasColMajor,CblasNoTrans,CblasNoTrans,
132  num_features,num_features,count,
133  1.0,class_feature_matrix.matrix,num_features,
134  helper_matrix.matrix,count,
135  0.0,m_classes[label].matrix,num_features);
136 
137  SG_PROGRESS(label+1,0,m_num_classes);
138  }
139  SG_DONE();
140 
141  return true;
142 };
143 
145 {
146  if (data)
147  set_features(data);
148 
150 
151  ASSERT(m_classes);
154 
155  int32_t num_vectors = m_features->get_num_vectors();
156 
157  CMulticlassLabels* predicted_labels = new CMulticlassLabels(num_vectors);
158 
159  for (int32_t i=0; i<num_vectors;i++)
160  {
161  SG_PROGRESS(i,0,num_vectors-1);
162  predicted_labels->set_label(i,apply_one(i));
163  }
164  SG_DONE();
165 
166  return predicted_labels;
167 };
168 
170 {
171  int32_t num_features = feature_vector.vlen;
172  float64_t norm = cblas_ddot(num_features,feature_vector.vector,1,
173  feature_vector.vector,1);
174 
175  cblas_dgemv(CblasColMajor,CblasNoTrans,
176  num_features,num_features,
177  1.0,m_classes[label].matrix,num_features,
178  feature_vector.vector,1,
179  0.0,m_feature_vector.vector,1);
180 
181  float64_t product = cblas_ddot(num_features,feature_vector.vector,1,
183  return product/norm;
184 };
185 
187 {
188  int32_t predicted_label = 0;
189  float64_t max_conjugate_index = 0.0;
190  float64_t current_conjugate_index;
191 
192  SGVector<float64_t> feature_vector = m_features->get_feature_vector(index);
193  for (int32_t i=0; i<m_num_classes; i++)
194  {
195  current_conjugate_index = conjugate_index(feature_vector,i);
196 
197  if (current_conjugate_index > max_conjugate_index)
198  {
199  max_conjugate_index = current_conjugate_index;
200  predicted_label = i;
201  }
202  }
203 
204  return predicted_label;
205 };
206 
207 #endif /* HAVE_LAPACK */

SHOGUN Machine Learning Toolbox - Documentation