SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MulticlassOneVsRestStrategy.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 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
15 
16 using namespace shogun;
17 
20 {
21 }
22 
24  : CMulticlassStrategy(prob_heuris)
25 {
26 }
27 
29 {
30  for (int32_t i=0; i < m_orig_labels->get_num_labels(); ++i)
31  {
32  if (((CMulticlassLabels*) m_orig_labels)->get_int_label(i)==m_train_iter)
33  ((CBinaryLabels*) m_train_labels)->set_label(i, +1.0);
34  else
35  ((CBinaryLabels*) m_train_labels)->set_label(i, -1.0);
36  }
37 
38  // increase m_train_iter *after* setting labels
40 
41  return SGVector<int32_t>();
42 }
43 
45 {
48 
49  return CMath::arg_max(outputs.vector, 1, outputs.vlen);
50 }
51 
53 {
54  float64_t* outputs_ = SG_MALLOC(float64_t, outputs.vlen);
55  int32_t* indices_ = SG_MALLOC(int32_t, outputs.vlen);
56  for (int32_t i=0; i<outputs.vlen; i++)
57  {
58  outputs_[i] = outputs[i];
59  indices_[i] = i;
60  }
61  CMath::qsort_backward_index(outputs_,indices_,outputs.vlen);
62  SGVector<index_t> result(n_outputs);
63  for (int32_t i=0; i<n_outputs; i++)
64  result[i] = indices_[i];
65  SG_FREE(outputs_);
66  SG_FREE(indices_);
67  return result;
68 }
69 
71 {
72  switch(get_prob_heuris_type())
73  {
74  case OVA_NORM:
75  rescale_heuris_norm(outputs);
76  break;
77  case OVA_SOFTMAX:
78  SG_ERROR("%s::rescale_outputs(): Need to specify sigmoid parameters!\n", get_name());
79  break;
80  case PROB_HEURIS_NONE:
81  break;
82  default:
83  SG_ERROR("%s::rescale_outputs(): Unknown OVA probability heuristic type!\n", get_name());
84  break;
85  }
86 }
87 
89  const SGVector<float64_t> As, const SGVector<float64_t> Bs)
90 {
92  rescale_heuris_softmax(outputs,As,Bs);
93  else
94  rescale_outputs(outputs);
95 }
96 
98 {
99  if (m_num_classes != outputs.vlen)
100  {
101  SG_ERROR("%s::rescale_heuris_norm(): size(outputs) = %d != m_num_classes = %d\n",
102  get_name(), outputs.vlen, m_num_classes);
103  }
104 
105  float64_t norm = SGVector<float64_t>::sum(outputs);
106  norm += 1E-10;
107  for (int32_t i=0; i<outputs.vlen; i++)
108  outputs[i] /= norm;
109 }
110 
112  const SGVector<float64_t> As, const SGVector<float64_t> Bs)
113 {
114  if (m_num_classes != outputs.vlen)
115  {
116  SG_ERROR("%s::rescale_heuris_softmax(): size(outputs) = %d != m_num_classes = %d\n",
117  get_name(), outputs.vlen, m_num_classes);
118  }
119 
120  for (int32_t i=0; i<outputs.vlen; i++)
121  outputs[i] = CMath::exp(-As[i]*outputs[i]-Bs[i]);
122 
123  float64_t norm = SGVector<float64_t>::sum(outputs);
124  norm += 1E-10;
125  for (int32_t i=0; i<outputs.vlen; i++)
126  outputs[i] /= norm;
127 }
void rescale_heuris_norm(SGVector< float64_t > outputs)
CMulticlassLabels * m_orig_labels
original multiclass labels
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
Definition: Math.h:262
virtual int32_t get_num_labels() const
virtual SGVector< int32_t > train_prepare_next()
#define SG_ERROR(...)
Definition: SGIO.h:129
CRejectionStrategy * m_rejection_strategy
rejection strategy
virtual int32_t decide_label(SGVector< float64_t > outputs)
int32_t m_num_classes
number of classes in this problem
Multiclass Labels for multi-class classification.
index_t vlen
Definition: SGVector.h:494
int32_t m_train_iter
index of current iterations
virtual void rescale_outputs(SGVector< float64_t > outputs)
void rescale_heuris_softmax(SGVector< float64_t > outputs, const SGVector< float64_t > As, const SGVector< float64_t > Bs)
double float64_t
Definition: common.h:50
virtual SGVector< index_t > decide_label_multiple_output(SGVector< float64_t > outputs, int32_t n_outputs)
static const int32_t REJECTION_LABEL
Definition: DenseLabels.h:267
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:354
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual bool reject(SGVector< float64_t > outputs) const =0
CBinaryLabels * m_train_labels
labels used to train the submachines
static float64_t exp(float64_t x)
Definition: Math.h:621
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
class MulticlassStrategy used to construct generic multiclass classifiers with ensembles of binary cl...
virtual SGVector< int32_t > train_prepare_next()
static void qsort_backward_index(T1 *output, T2 *index, int32_t size)
Definition: Math.h:2246
EProbHeuristicType get_prob_heuris_type()

SHOGUN Machine Learning Toolbox - Documentation