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-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 "lib/common.h" 00013 #include "lib/io.h" 00014 #include "features/Features.h" 00015 #include "features/DotFeatures.h" 00016 #include "kernel/LinearKernel.h" 00017 00018 using namespace shogun; 00019 00020 CLinearKernel::CLinearKernel() 00021 : CDotKernel(0), normal(NULL), normal_length(0) 00022 { 00023 properties |= KP_LINADD; 00024 } 00025 00026 CLinearKernel::CLinearKernel(CDotFeatures* l, CDotFeatures* r) 00027 : CDotKernel(0), normal(NULL), normal_length(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::clear_normal() 00053 { 00054 int32_t num = ((CDotFeatures*) lhs)->get_dim_feature_space(); 00055 if (normal==NULL) 00056 { 00057 normal = new float64_t[num]; 00058 normal_length=num; 00059 } 00060 00061 memset(normal, 0, sizeof(float64_t)*normal_length); 00062 00063 set_is_initialized(true); 00064 } 00065 00066 void CLinearKernel::add_to_normal(int32_t idx, float64_t weight) 00067 { 00068 ((CDotFeatures*) lhs)->add_to_dense_vec( 00069 normalizer->normalize_lhs(weight, idx), idx, normal, normal_length); 00070 set_is_initialized(true); 00071 } 00072 00073 bool CLinearKernel::init_optimization( 00074 int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas) 00075 { 00076 clear_normal(); 00077 00078 for (int32_t i=0; i<num_suppvec; i++) 00079 add_to_normal(sv_idx[i], alphas[i]); 00080 00081 set_is_initialized(true); 00082 return true; 00083 } 00084 00085 bool CLinearKernel::delete_optimization() 00086 { 00087 delete[] normal; 00088 normal_length=0; 00089 normal=NULL; 00090 set_is_initialized(false); 00091 00092 return true; 00093 } 00094 00095 float64_t CLinearKernel::compute_optimized(int32_t idx) 00096 { 00097 ASSERT(get_is_initialized()); 00098 float64_t result = ((CDotFeatures*) rhs)-> 00099 dense_dot(idx, normal, normal_length); 00100 return normalizer->normalize_rhs(result, idx); 00101 }