SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2012 Soeren Sonnenburg and Sergey Lisitsyn 00008 * Written (W) 2012 Heiko Strathmann 00009 * Copyright (C) 1999-2012 Fraunhofer Institute FIRST and Max-Planck-Society 00010 */ 00011 00012 #ifndef _KERNELMULTICLASSMACHINE_H___ 00013 #define _KERNELMULTICLASSMACHINE_H___ 00014 00015 #include <shogun/lib/common.h> 00016 #include <shogun/features/Features.h> 00017 #include <shogun/kernel/Kernel.h> 00018 #include <shogun/machine/KernelMachine.h> 00019 #include <shogun/machine/MulticlassMachine.h> 00020 00021 namespace shogun 00022 { 00023 00024 class CKernel; 00025 class CKernelMachine; 00026 00028 class CKernelMulticlassMachine : public CMulticlassMachine 00029 { 00030 public: 00032 CKernelMulticlassMachine() : CMulticlassMachine(), m_kernel(NULL) 00033 { 00034 SG_ADD((CSGObject**)&m_kernel,"kernel", "The kernel to be used", MS_AVAILABLE); 00035 } 00036 00043 CKernelMulticlassMachine(CMulticlassStrategy *strategy, CKernel* kernel, CKernelMachine* machine, CLabels* labs) : 00044 CMulticlassMachine(strategy,(CMachine*)machine,labs), m_kernel(NULL) 00045 { 00046 set_kernel(kernel); 00047 SG_ADD((CSGObject**)&m_kernel,"kernel", "The kernel to be used", MS_AVAILABLE); 00048 } 00049 00051 virtual ~CKernelMulticlassMachine() 00052 { 00053 SG_UNREF(m_kernel); 00054 } 00055 00057 virtual const char* get_name() const 00058 { 00059 return "KernelMulticlassMachine"; 00060 } 00061 00066 void set_kernel(CKernel* k) 00067 { 00068 ((CKernelMachine*)m_machine)->set_kernel(k); 00069 SG_REF(k); 00070 SG_UNREF(m_kernel); 00071 m_kernel=k; 00072 } 00073 00078 CKernel* get_kernel() 00079 { 00080 SG_REF(m_kernel); 00081 return m_kernel; 00082 } 00083 00090 virtual void store_model_features(); 00091 00092 protected: 00093 00095 virtual bool init_machine_for_train(CFeatures* data) 00096 { 00097 if (data) 00098 m_kernel->init(data,data); 00099 00100 ((CKernelMachine*)m_machine)->set_kernel(m_kernel); 00101 00102 return true; 00103 } 00104 00106 virtual bool init_machines_for_apply(CFeatures* data) 00107 { 00108 if (data) 00109 { 00110 /* set data to rhs for this kernel */ 00111 CFeatures* lhs=m_kernel->get_lhs(); 00112 m_kernel->init(lhs, data); 00113 SG_UNREF(lhs); 00114 } 00115 00116 /* set kernel to all sub-machines */ 00117 for (int32_t i=0; i<m_machines->get_num_elements(); i++) 00118 { 00119 CKernelMachine *machine= 00120 (CKernelMachine*)m_machines->get_element(i); 00121 machine->set_kernel(m_kernel); 00122 SG_UNREF(machine); 00123 } 00124 00125 return true; 00126 } 00127 00129 virtual bool is_ready() 00130 { 00131 if (m_kernel && m_kernel->get_num_vec_lhs() && m_kernel->get_num_vec_rhs()) 00132 return true; 00133 00134 return false; 00135 } 00136 00138 virtual CMachine* get_machine_from_trained(CMachine* machine) 00139 { 00140 return new CKernelMachine((CKernelMachine*)machine); 00141 } 00142 00144 virtual int32_t get_num_rhs_vectors() 00145 { 00146 return m_kernel->get_num_vec_rhs(); 00147 } 00148 00153 virtual void add_machine_subset(SGVector<index_t> subset) 00154 { 00155 SG_NOTIMPLEMENTED; 00156 } 00157 00159 virtual void remove_machine_subset() 00160 { 00161 SG_NOTIMPLEMENTED; 00162 } 00163 00164 protected: 00165 00167 CKernel* m_kernel; 00168 00169 }; 00170 } 00171 #endif