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-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/common.h> 00012 #include <shogun/io/SGIO.h> 00013 #include <shogun/mathematics/Math.h> 00014 #include <shogun/kernel/string/LinearStringKernel.h> 00015 #include <shogun/features/StringFeatures.h> 00016 00017 using namespace shogun; 00018 00019 CLinearStringKernel::CLinearStringKernel() 00020 : CStringKernel<char>(0), normal(NULL) 00021 { 00022 } 00023 00024 CLinearStringKernel::CLinearStringKernel( 00025 CStringFeatures<char>* l, CStringFeatures<char>* r) 00026 : CStringKernel<char>(0), normal(NULL) 00027 { 00028 init(l, r); 00029 } 00030 00031 CLinearStringKernel::~CLinearStringKernel() 00032 { 00033 cleanup(); 00034 } 00035 00036 bool CLinearStringKernel::init(CFeatures *l, CFeatures *r) 00037 { 00038 CStringKernel<char>::init(l, r); 00039 return init_normalizer(); 00040 } 00041 00042 void CLinearStringKernel::cleanup() 00043 { 00044 delete_optimization(); 00045 00046 CKernel::cleanup(); 00047 } 00048 00049 void CLinearStringKernel::clear_normal() 00050 { 00051 memset(normal, 0, lhs->get_num_vectors()*sizeof(float64_t)); 00052 } 00053 00054 void CLinearStringKernel::add_to_normal(int32_t idx, float64_t weight) 00055 { 00056 int32_t vlen; 00057 bool vfree; 00058 char* vec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx, vlen, vfree); 00059 00060 for (int32_t i=0; i<vlen; i++) 00061 normal[i] += weight*normalizer->normalize_lhs(vec[i], idx); 00062 00063 ((CStringFeatures<char>*) lhs)->free_feature_vector(vec, idx, vfree); 00064 } 00065 00066 float64_t CLinearStringKernel::compute(int32_t idx_a, int32_t idx_b) 00067 { 00068 int32_t alen, blen; 00069 bool free_avec, free_bvec; 00070 00071 char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec); 00072 char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec); 00073 ASSERT(alen==blen) 00074 float64_t result=SGVector<float64_t>::dot(avec, bvec, alen); 00075 ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec); 00076 ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); 00077 return result; 00078 } 00079 00080 bool CLinearStringKernel::init_optimization( 00081 int32_t num_suppvec, int32_t *sv_idx, float64_t *alphas) 00082 { 00083 int32_t num_feat = ((CStringFeatures<char>*) lhs)->get_max_vector_length(); 00084 ASSERT(num_feat) 00085 00086 normal = SG_MALLOC(float64_t, num_feat); 00087 ASSERT(normal) 00088 clear_normal(); 00089 00090 for (int32_t i = 0; i<num_suppvec; i++) 00091 { 00092 int32_t alen; 00093 bool free_avec; 00094 char *avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(sv_idx[i], alen, free_avec); 00095 ASSERT(avec) 00096 00097 for (int32_t j = 0; j<num_feat; j++) 00098 { 00099 normal[j] += alphas[i]* 00100 normalizer->normalize_lhs(((float64_t) avec[j]), sv_idx[i]); 00101 } 00102 ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, sv_idx[i], free_avec); 00103 } 00104 set_is_initialized(true); 00105 return true; 00106 } 00107 00108 bool CLinearStringKernel::delete_optimization() 00109 { 00110 SG_FREE(normal); 00111 normal = NULL; 00112 set_is_initialized(false); 00113 return true; 00114 } 00115 00116 float64_t CLinearStringKernel::compute_optimized(int32_t idx_b) 00117 { 00118 int32_t blen; 00119 bool free_bvec; 00120 char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec); 00121 float64_t result=normalizer->normalize_rhs(SGVector<float64_t>::dot(normal, bvec, blen), idx_b); 00122 ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); 00123 return result; 00124 }