CustomKernel.cpp

Go to the documentation of this file.
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-2009 Soeren Sonnenburg
00008  * Written (W) 2012 Heiko Strathmann
00009  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #include <shogun/lib/common.h>
00013 #include <shogun/kernel/CustomKernel.h>
00014 #include <shogun/features/Features.h>
00015 #include <shogun/features/DummyFeatures.h>
00016 #include <shogun/io/SGIO.h>
00017 #include <shogun/base/ParameterMap.h>
00018 
00019 using namespace shogun;
00020 
00021 void
00022 CCustomKernel::init()
00023 {
00024     m_row_subset_stack=new CSubsetStack();
00025     SG_REF(m_row_subset_stack)
00026     m_col_subset_stack=new CSubsetStack();
00027     SG_REF(m_col_subset_stack)
00028     m_free_km=true;
00029 
00030     SG_ADD((CSGObject**)&m_row_subset_stack, "row_subset_stack",
00031             "Subset stack of rows", MS_NOT_AVAILABLE);
00032     SG_ADD((CSGObject**)&m_col_subset_stack, "col_subset_stack",
00033             "Subset stack of columns", MS_NOT_AVAILABLE);
00034     SG_ADD(&m_free_km, "free_km", "Whether kernel matrix should be freed in "
00035             "destructor", MS_NOT_AVAILABLE);
00036     SG_ADD(&kmatrix, "kmatrix", "Kernel matrix.", MS_NOT_AVAILABLE);
00037     SG_ADD(&upper_diagonal, "upper_diagonal", "Upper diagonal", MS_NOT_AVAILABLE);
00038 
00039     /* new parameter from param version 0 to 1 */
00040     m_parameter_map->put(
00041             new SGParamInfo("free_km", CT_SCALAR, ST_NONE, PT_BOOL, 1),
00042             new SGParamInfo()
00043     );
00044 
00045     /* new parameter from param version 0 to 1 */
00046     m_parameter_map->put(
00047             new SGParamInfo("row_subset_stack", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1),
00048             new SGParamInfo()
00049     );
00050 
00051     /* new parameter from param version 0 to 1 */
00052     m_parameter_map->put(
00053             new SGParamInfo("col_subset_stack", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1),
00054             new SGParamInfo()
00055     );
00056     m_parameter_map->finalize_map();
00057 }
00058 
00059 CCustomKernel::CCustomKernel()
00060 : CKernel(10), kmatrix(), upper_diagonal(false)
00061 {
00062     SG_DEBUG("created CCustomKernel\n");
00063     init();
00064 }
00065 
00066 CCustomKernel::CCustomKernel(CKernel* k)
00067 : CKernel(10)
00068 {
00069     SG_DEBUG("created CCustomKernel\n");
00070     init();
00071 
00072     /* if constructed from a custom kernel, use same kernel matrix */
00073     if (k->get_kernel_type()==K_CUSTOM)
00074     {
00075         CCustomKernel* casted=(CCustomKernel*)k;
00076         set_full_kernel_matrix_from_full(casted->get_float32_kernel_matrix());
00077         m_free_km=false;
00078     }
00079     else
00080         set_full_kernel_matrix_from_full(k->get_kernel_matrix());
00081 }
00082 
00083 CCustomKernel::CCustomKernel(SGMatrix<float64_t> km)
00084 : CKernel(10), upper_diagonal(false)
00085 {
00086     SG_DEBUG("Entering CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
00087     init();
00088     set_full_kernel_matrix_from_full(km);
00089     SG_DEBUG("Leaving CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
00090 }
00091 
00092 CCustomKernel::CCustomKernel(SGMatrix<float32_t> km)
00093 : CKernel(10), upper_diagonal(false)
00094 {
00095     SG_DEBUG("Entering CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
00096     init();
00097     set_full_kernel_matrix_from_full(km);
00098     SG_DEBUG("Leaving CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
00099 }
00100 
00101 CCustomKernel::~CCustomKernel()
00102 {
00103     SG_DEBUG("Entering CCustomKernel::~CCustomKernel()\n");
00104     cleanup();
00105     SG_UNREF(m_row_subset_stack);
00106     SG_UNREF(m_col_subset_stack);
00107     SG_DEBUG("Leaving CCustomKernel::~CCustomKernel()\n");
00108 }
00109 
00110 bool CCustomKernel::dummy_init(int32_t rows, int32_t cols)
00111 {
00112     return init(new CDummyFeatures(rows), new CDummyFeatures(cols));
00113 }
00114 
00115 bool CCustomKernel::init(CFeatures* l, CFeatures* r)
00116 {
00117     /* make it possible to call with NULL values since features are useless
00118      * for custom kernel matrix */
00119     if (!l)
00120         l=lhs;
00121 
00122     if (!r)
00123         r=rhs;
00124 
00125     CKernel::init(l, r);
00126 
00127     SG_DEBUG( "num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), kmatrix.num_rows);
00128     SG_DEBUG( "num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), kmatrix.num_cols);
00129     ASSERT(l->get_num_vectors()==kmatrix.num_rows);
00130     ASSERT(r->get_num_vectors()==kmatrix.num_cols);
00131     return init_normalizer();
00132 }
00133 
00134 void CCustomKernel::cleanup_custom()
00135 {
00136     SG_DEBUG("Entering CCustomKernel::cleanup_custom()\n");
00137     remove_all_row_subsets();
00138     remove_all_col_subsets();
00139 
00140     kmatrix=SGMatrix<float32_t>();
00141     upper_diagonal=false;
00142 
00143     SG_DEBUG("Leaving CCustomKernel::cleanup_custom()\n");
00144 }
00145 
00146 void CCustomKernel::cleanup()
00147 {
00148     remove_all_row_subsets();
00149     remove_all_col_subsets();
00150     cleanup_custom();
00151     CKernel::cleanup();
00152 }
00153 
00154 void CCustomKernel::add_row_subset(SGVector<index_t> subset)
00155 {
00156     m_row_subset_stack->add_subset(subset);
00157     row_subset_changed_post();
00158 }
00159 
00160 void CCustomKernel::remove_row_subset()
00161 {
00162     m_row_subset_stack->remove_subset();
00163     row_subset_changed_post();
00164 }
00165 
00166 void CCustomKernel::remove_all_row_subsets()
00167 {
00168     m_row_subset_stack->remove_all_subsets();
00169     row_subset_changed_post();
00170 }
00171 
00172 void CCustomKernel::row_subset_changed_post()
00173 {
00174     if (m_row_subset_stack->has_subsets())
00175         num_lhs=m_row_subset_stack->get_size();
00176     else
00177         num_lhs=kmatrix.num_rows;
00178 }
00179 
00180 void CCustomKernel::add_col_subset(SGVector<index_t> subset)
00181 {
00182     m_col_subset_stack->add_subset(subset);
00183     col_subset_changed_post();
00184 }
00185 
00186 void CCustomKernel::remove_col_subset()
00187 {
00188     m_col_subset_stack->remove_subset();
00189     col_subset_changed_post();
00190 }
00191 
00192 void CCustomKernel::remove_all_col_subsets()
00193 {
00194     m_col_subset_stack->remove_all_subsets();
00195     col_subset_changed_post();
00196 }
00197 
00198 void CCustomKernel::col_subset_changed_post()
00199 {
00200     if (m_col_subset_stack->has_subsets())
00201         num_rhs=m_col_subset_stack->get_size();
00202     else
00203         num_rhs=kmatrix.num_cols;
00204 }
00205 
00206 void CCustomKernel::print_kernel_matrix(const char* prefix) const
00207 {
00208     index_t num_rows=m_row_subset_stack->has_subsets()
00209             ? m_row_subset_stack->get_size() : kmatrix.num_rows;
00210     index_t num_cols=m_col_subset_stack->has_subsets()
00211             ? m_col_subset_stack->get_size() : kmatrix.num_cols;
00212     for (index_t i=0; i<num_rows; ++i)
00213     {
00214         for (index_t j=0; j<num_cols; ++j)
00215         {
00216             index_t real_i=m_row_subset_stack->subset_idx_conversion(i);
00217             index_t real_j=m_col_subset_stack->subset_idx_conversion(j);
00218             SG_PRINT("%s%4.2f", prefix, kmatrix.matrix[kmatrix.num_rows*real_j+real_i]);
00219             if (j<num_cols-1)
00220                 SG_PRINT(", \t");
00221         }
00222         SG_PRINT("\n");
00223     }
00224 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation