LinearStringKernel.cpp

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

SHOGUN Machine Learning Toolbox - Documentation