SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
KernelMulticlassMachine.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  * Written (W) 2012 Heiko Strathmann
9  * Copyright (C) 2012 Chiyuan Zhang
10  */
11 
12 #include <shogun/lib/Set.h>
15 #include <shogun/kernel/Kernel.h>
17 
18 using namespace shogun;
19 
21 {
22  CKernel *kernel= m_kernel;
23  if (!kernel)
24  SG_ERROR("%s::store_model_features(): kernel is needed to store SV "
25  "features.\n", get_name());
26 
27  CFeatures* lhs=kernel->get_lhs();
28  CFeatures* rhs=kernel->get_rhs();
29  if (!lhs)
30  {
31  SG_ERROR("%s::store_model_features(): kernel lhs is needed to store "
32  "SV features.\n", get_name());
33  }
34 
35  /* this map will be abused as a map */
36  CSet<index_t> all_sv;
37  for (index_t i=0; i<m_machines->get_num_elements(); ++i)
38  {
40  for (index_t j=0; j<machine->get_num_support_vectors(); ++j)
41  all_sv.add(machine->get_support_vector(j));
42 
43  SG_UNREF(machine);
44  }
45 
46  /* convert map to vector of SV */
47  SGVector<index_t> sv_idx(all_sv.get_num_elements());
48  for (index_t i=0; i<sv_idx.vlen; ++i)
49  sv_idx[i]=*all_sv.get_element_ptr(i);
50 
51  CFeatures* sv_features=lhs->copy_subset(sv_idx);
52 
53  /* now, features are replaced by concatenated SV features */
54  kernel->init(sv_features, rhs);
55 
56  /* was SG_REF'ed by copy_subset */
57  SG_UNREF(sv_features);
58 
59  /* now the old SV indices have to be mapped to the new features */
60 
61  /* update SV of all machines */
62  for (int32_t i=0; i<m_machines->get_num_elements(); ++i)
63  {
65 
66  /* for each machine, replace SV by index in sv_idx array */
67  for (int32_t j=0; j<machine->get_num_support_vectors(); ++j)
68  {
69  /* get index of SV in old features */
70  index_t current_sv_idx=machine->get_support_vector(j);
71 
72  /* the position of this old index in the map is the position of
73  * the SV in the new features */
74  index_t new_sv_idx=all_sv.index_of(current_sv_idx);
75 
76  machine->set_support_vector(j, new_sv_idx);
77  }
78 
79  SG_UNREF(machine);
80  }
81 
82  SG_UNREF(lhs);
83  SG_UNREF(rhs);
84 }
85 
87 {
88  SG_ADD((CSGObject**)&m_kernel,"kernel", "The kernel to be used", MS_AVAILABLE);
89 }
90 
98  CMulticlassMachine(strategy,(CMachine*)machine,labs), m_kernel(NULL)
99 {
100  set_kernel(kernel);
101  SG_ADD((CSGObject**)&m_kernel,"kernel", "The kernel to be used", MS_AVAILABLE);
102 }
103 
106 {
108 }
109 
115 {
116  ((CKernelMachine*)m_machine)->set_kernel(k);
117  SG_REF(k);
119  m_kernel=k;
120 }
121 
123 {
124  SG_REF(m_kernel);
125  return m_kernel;
126 }
127 
129 {
130  if (data)
131  m_kernel->init(data,data);
132 
133  ((CKernelMachine*)m_machine)->set_kernel(m_kernel);
134 
135  return true;
136 }
137 
139 {
140  if (data)
141  {
142  /* set data to rhs for this kernel */
143  CFeatures* lhs=m_kernel->get_lhs();
144  m_kernel->init(lhs, data);
145  SG_UNREF(lhs);
146  }
147 
148  /* set kernel to all sub-machines */
149  for (int32_t i=0; i<m_machines->get_num_elements(); i++)
150  {
151  CKernelMachine *machine=
153  machine->set_kernel(m_kernel);
154  SG_UNREF(machine);
155  }
156 
157  return true;
158 }
159 
161 {
163  return true;
164 
165  return false;
166 }
167 
169 {
170  return new CKernelMachine((CKernelMachine*)machine);
171 }
172 
174 {
175  return m_kernel->get_num_vec_rhs();
176 }
177 
179 {
181 }
182 
184 {
186 }
187 
188 
virtual void add_machine_subset(SGVector< index_t > subset)
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
int32_t index_of(const T &element)
Definition: Set.h:153
CMachine * get_machine(int32_t num) const
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
CFeatures * get_rhs()
Definition: Kernel.h:511
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
A generic KernelMachine interface.
Definition: KernelMachine.h:51
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
#define SG_REF(x)
Definition: SGObject.h:54
A generic learning machine interface.
Definition: Machine.h:143
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
the class CSet, a set based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: Set.h:51
experimental abstract generic multiclass machine class
bool set_support_vector(int32_t idx, int32_t val)
virtual CMachine * get_machine_from_trained(CMachine *machine)
virtual const char * get_name() const
int32_t get_support_vector(int32_t idx)
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
T * get_element_ptr(int32_t index)
Definition: Set.h:189
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual bool init_machine_for_train(CFeatures *data)
virtual CFeatures * copy_subset(SGVector< index_t > indices)
Definition: Features.cpp:340
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool init_machines_for_apply(CFeatures *data)
int32_t get_num_elements() const
Definition: Set.h:168
The Kernel base class.
Definition: Kernel.h:159
void add(const T &element)
Definition: Set.h:109
CSGObject * get_element(int32_t index) const
class MulticlassStrategy used to construct generic multiclass classifiers with ensembles of binary cl...
void set_kernel(CKernel *k)
#define SG_ADD(...)
Definition: SGObject.h:84
CFeatures * get_lhs()
Definition: Kernel.h:505

SHOGUN Machine Learning Toolbox - Documentation