|
SHOGUN v0.9.3
|
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 CCustomKernel::CCustomKernel() 00020 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00021 { 00022 } 00023 00024 CCustomKernel::CCustomKernel(CKernel* k) 00025 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00026 { 00027 if (k->lhs_equals_rhs()) 00028 { 00029 int32_t cols=k->get_num_vec_lhs(); 00030 SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols); 00031 00032 kmatrix= new float32_t[int64_t(cols)*(cols+1)/2]; 00033 00034 upper_diagonal=true; 00035 num_rows=cols; 00036 num_cols=cols; 00037 00038 for (int32_t row=0; row<num_rows; row++) 00039 { 00040 for (int32_t col=row; col<num_cols; col++) 00041 kmatrix[int64_t(row) * num_cols - int64_t(row)*(row+1)/2 + col]=k->kernel(row,col); 00042 } 00043 } 00044 else 00045 { 00046 int32_t rows=k->get_num_vec_lhs(); 00047 int32_t cols=k->get_num_vec_rhs(); 00048 kmatrix= new float32_t[int64_t(rows)*cols]; 00049 00050 upper_diagonal=false; 00051 num_rows=rows; 00052 num_cols=cols; 00053 00054 for (int32_t row=0; row<num_rows; row++) 00055 { 00056 for (int32_t col=0; col<num_cols; col++) 00057 kmatrix[int64_t(row) * num_cols + col]=k->kernel(row,col); 00058 } 00059 } 00060 00061 dummy_init(num_rows, num_cols); 00062 00063 } 00064 00065 CCustomKernel::CCustomKernel(const float64_t* km, int32_t rows, int32_t cols) 00066 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00067 { 00068 set_full_kernel_matrix_from_full(km, rows, cols); 00069 } 00070 00071 CCustomKernel::CCustomKernel(const float32_t* km, int32_t rows, int32_t cols) 00072 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false) 00073 { 00074 set_full_kernel_matrix_from_full(km, rows, cols); 00075 } 00076 00077 CCustomKernel::~CCustomKernel() 00078 { 00079 cleanup(); 00080 } 00081 00082 bool CCustomKernel::dummy_init(int32_t rows, int32_t cols) 00083 { 00084 return init(new CDummyFeatures(rows), new CDummyFeatures(cols)); 00085 } 00086 00087 bool CCustomKernel::init(CFeatures* l, CFeatures* r) 00088 { 00089 CKernel::init(l, r); 00090 00091 SG_DEBUG( "num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), num_rows); 00092 SG_DEBUG( "num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), num_cols); 00093 ASSERT(l->get_num_vectors()==num_rows); 00094 ASSERT(r->get_num_vectors()==num_cols); 00095 return init_normalizer(); 00096 } 00097 00098 void CCustomKernel::cleanup_custom() 00099 { 00100 SG_DEBUG("cleanup up custom kernel\n"); 00101 delete[] kmatrix; 00102 kmatrix=NULL; 00103 upper_diagonal=false; 00104 num_cols=0; 00105 num_rows=0; 00106 } 00107 00108 void CCustomKernel::cleanup() 00109 { 00110 cleanup_custom(); 00111 CKernel::cleanup(); 00112 } 00113