SHOGUN v0.9.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-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include "lib/common.h" 00012 #include "kernel/CustomKernel.h" 00013 #include "features/Features.h" 00014 #include "features/DummyFeatures.h" 00015 #include "lib/io.h" 00016 00017 using namespace shogun; 00018 00019 void 00020 CCustomKernel::init(void) 00021 { 00022 m_parameters->add_matrix(&kmatrix, &num_rows, &num_cols, "kmatrix", 00023 "Kernel matrix."); 00024 m_parameters->add(&upper_diagonal, "upper_diagonal"); 00025 } 00026 00027 CCustomKernel::CCustomKernel() 00028 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00029 { 00030 init(); 00031 } 00032 00033 CCustomKernel::CCustomKernel(CKernel* k) 00034 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00035 { 00036 init(); 00037 00038 if (k->get_lhs_equals_rhs()) 00039 { 00040 int32_t cols=k->get_num_vec_lhs(); 00041 SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols); 00042 00043 kmatrix= new float32_t[(int64_t(cols)+1)*cols/2]; 00044 00045 upper_diagonal=true; 00046 num_rows=cols; 00047 num_cols=cols; 00048 00049 for (int32_t row=0; row<num_rows; row++) 00050 { 00051 for (int32_t col=row; col<num_cols; col++) 00052 kmatrix[int64_t(row) * num_cols - (int64_t(row)+1)*row/2 + col]=k->kernel(row,col); 00053 } 00054 } 00055 else 00056 { 00057 int32_t rows=k->get_num_vec_lhs(); 00058 int32_t cols=k->get_num_vec_rhs(); 00059 kmatrix= new float32_t[int64_t(rows)*cols]; 00060 00061 upper_diagonal=false; 00062 num_rows=rows; 00063 num_cols=cols; 00064 00065 for (int32_t row=0; row<num_rows; row++) 00066 { 00067 for (int32_t col=0; col<num_cols; col++) 00068 kmatrix[int64_t(row) * num_cols + col]=k->kernel(row,col); 00069 } 00070 } 00071 00072 dummy_init(num_rows, num_cols); 00073 00074 } 00075 00076 CCustomKernel::CCustomKernel(const float64_t* km, int32_t rows, int32_t cols) 00077 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00078 { 00079 init(); 00080 00081 set_full_kernel_matrix_from_full(km, rows, cols); 00082 } 00083 00084 CCustomKernel::CCustomKernel(const float32_t* km, int32_t rows, int32_t cols) 00085 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00086 { 00087 init(); 00088 00089 set_full_kernel_matrix_from_full(km, rows, cols); 00090 } 00091 00092 CCustomKernel::~CCustomKernel() 00093 { 00094 cleanup(); 00095 } 00096 00097 bool CCustomKernel::dummy_init(int32_t rows, int32_t cols) 00098 { 00099 return init(new CDummyFeatures(rows), new CDummyFeatures(cols)); 00100 } 00101 00102 bool CCustomKernel::init(CFeatures* l, CFeatures* r) 00103 { 00104 CKernel::init(l, r); 00105 00106 SG_DEBUG( "num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), num_rows); 00107 SG_DEBUG( "num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), num_cols); 00108 ASSERT(l->get_num_vectors()==num_rows); 00109 ASSERT(r->get_num_vectors()==num_cols); 00110 return init_normalizer(); 00111 } 00112 00113 void CCustomKernel::cleanup_custom() 00114 { 00115 SG_DEBUG("cleanup up custom kernel\n"); 00116 delete[] kmatrix; 00117 kmatrix=NULL; 00118 upper_diagonal=false; 00119 num_cols=0; 00120 num_rows=0; 00121 } 00122 00123 void CCustomKernel::cleanup() 00124 { 00125 cleanup_custom(); 00126 CKernel::cleanup(); 00127 } 00128