SHOGUN
v3.2.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-2010 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 * Copyright (C) 2010 Berlin Institute of Technology 00010 */ 00011 00012 #include <shogun/lib/common.h> 00013 #include <shogun/io/SGIO.h> 00014 #include <shogun/features/Features.h> 00015 #include <shogun/features/DotFeatures.h> 00016 #include <shogun/kernel/LinearKernel.h> 00017 00018 using namespace shogun; 00019 00020 CLinearKernel::CLinearKernel() 00021 : CDotKernel(0) 00022 { 00023 properties |= KP_LINADD; 00024 } 00025 00026 CLinearKernel::CLinearKernel(CDotFeatures* l, CDotFeatures* r) 00027 : CDotKernel(0) 00028 { 00029 properties |= KP_LINADD; 00030 init(l,r); 00031 } 00032 00033 CLinearKernel::~CLinearKernel() 00034 { 00035 cleanup(); 00036 } 00037 00038 bool CLinearKernel::init(CFeatures* l, CFeatures* r) 00039 { 00040 CDotKernel::init(l, r); 00041 00042 return init_normalizer(); 00043 } 00044 00045 void CLinearKernel::cleanup() 00046 { 00047 delete_optimization(); 00048 00049 CKernel::cleanup(); 00050 } 00051 00052 void CLinearKernel::add_to_normal(int32_t idx, float64_t weight) 00053 { 00054 ((CDotFeatures*) lhs)->add_to_dense_vec( 00055 normalizer->normalize_lhs(weight, idx), idx, normal.vector, normal.size()); 00056 set_is_initialized(true); 00057 } 00058 00059 bool CLinearKernel::init_optimization( 00060 int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas) 00061 { 00062 clear_normal(); 00063 00064 for (int32_t i=0; i<num_suppvec; i++) 00065 add_to_normal(sv_idx[i], alphas[i]); 00066 00067 set_is_initialized(true); 00068 return true; 00069 } 00070 00071 bool CLinearKernel::init_optimization(CKernelMachine* km) 00072 { 00073 clear_normal(); 00074 00075 int32_t num_suppvec=km->get_num_support_vectors(); 00076 00077 for (int32_t i=0; i<num_suppvec; i++) 00078 add_to_normal(km->get_support_vector(i), km->get_alpha(i)); 00079 00080 set_is_initialized(true); 00081 return true; 00082 } 00083 00084 bool CLinearKernel::delete_optimization() 00085 { 00086 normal = SGVector<float64_t>(); 00087 set_is_initialized(false); 00088 00089 return true; 00090 } 00091 00092 float64_t CLinearKernel::compute_optimized(int32_t idx) 00093 { 00094 ASSERT(get_is_initialized()) 00095 float64_t result = ((CDotFeatures*) rhs)-> 00096 dense_dot(idx, normal.vector, normal.size()); 00097 return normalizer->normalize_rhs(result, idx); 00098 }