LinearWordKernel.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/LinearWordKernel.h"
00015 #include "features/WordFeatures.h"
00016 
00017 CLinearWordKernel::CLinearWordKernel()
00018 : CSimpleKernel<uint16_t>(0), normal(NULL)
00019 {
00020 }
00021 
00022 CLinearWordKernel::CLinearWordKernel(CWordFeatures* l, CWordFeatures* r)
00023 : CSimpleKernel<uint16_t>(0), normal(NULL)
00024 {
00025     init(l, r);
00026 }
00027 
00028 CLinearWordKernel::~CLinearWordKernel()
00029 {
00030     cleanup();
00031 }
00032 
00033 bool CLinearWordKernel::init(CFeatures* l, CFeatures* r)
00034 {
00035     CSimpleKernel<uint16_t>::init(l, r);
00036     return init_normalizer();
00037 }
00038 
00039 void CLinearWordKernel::cleanup()
00040 {
00041     delete_optimization();
00042 
00043     CKernel::cleanup();
00044 }
00045 
00046 bool CLinearWordKernel::load_init(FILE* src)
00047 {
00048     return false;
00049 }
00050 
00051 bool CLinearWordKernel::save_init(FILE* dest)
00052 {
00053     return false;
00054 }
00055 
00056 void CLinearWordKernel::clear_normal()
00057 {
00058     int32_t num = lhs->get_num_vectors();
00059     CMath::fill_vector(normal, num, 0.0);
00060 }
00061 
00062 void CLinearWordKernel::add_to_normal(int32_t idx, float64_t weight)
00063 {
00064     int32_t vlen;
00065     bool vfree;
00066     uint16_t* vec=((CWordFeatures*) lhs)->get_feature_vector(idx, vlen, vfree);
00067 
00068     for (int32_t i=0; i<vlen; i++)
00069         normal[i]+= weight*normalizer->normalize_lhs(vec[i], idx);
00070 
00071     ((CWordFeatures*) lhs)->free_feature_vector(vec, idx, vfree);
00072 }
00073 
00074 float64_t CLinearWordKernel::compute(int32_t idx_a, int32_t idx_b)
00075 {
00076     int32_t alen, blen;
00077     bool afree, bfree;
00078 
00079     uint16_t* avec=((CWordFeatures*) lhs)->get_feature_vector(
00080         idx_a, alen, afree);
00081     uint16_t* bvec=((CWordFeatures*) rhs)->get_feature_vector(
00082         idx_b, blen, bfree);
00083     ASSERT(alen==blen);
00084 
00085     float64_t result=CMath::dot(avec, bvec, alen);
00086 
00087     ((CWordFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00088     ((CWordFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00089 
00090     return result;
00091 }
00092 
00093 bool CLinearWordKernel::init_optimization(
00094     int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas)
00095 {
00096     int32_t alen;
00097     bool afree;
00098 
00099     int32_t num_feat=((CWordFeatures*) lhs)->get_num_features();
00100     ASSERT(num_feat);
00101 
00102     normal=new float64_t[num_feat];
00103     CMath::fill_vector(normal, num_feat, 0.0);
00104 
00105     for (int32_t i=0; i<num_suppvec; i++)
00106     {
00107         uint16_t* avec=((CWordFeatures*) lhs)->get_feature_vector(
00108             sv_idx[i], alen, afree);
00109         ASSERT(avec);
00110 
00111         for (int32_t j=0; j<num_feat; j++)
00112             normal[j]+=alphas[i] * normalizer->normalize_lhs(
00113                 ((float64_t) avec[j]), sv_idx[i]);
00114 
00115         ((CWordFeatures*) lhs)->free_feature_vector(avec, 0, afree);
00116     }
00117 
00118     set_is_initialized(true);
00119     return true;
00120 }
00121 
00122 bool CLinearWordKernel::delete_optimization()
00123 {
00124     delete[] normal;
00125     normal=NULL;
00126     set_is_initialized(false);
00127 
00128     return true;
00129 }
00130 
00131 float64_t CLinearWordKernel::compute_optimized(int32_t idx_b) 
00132 {
00133     int32_t blen;
00134     bool bfree;
00135 
00136     uint16_t* bvec=((CWordFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00137 
00138     float64_t result=0;
00139     {
00140         for (int32_t i=0; i<blen; i++)
00141             result+= normal[i] * ((float64_t) bvec[i]);
00142     }
00143 
00144     ((CWordFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00145 
00146     return normalizer->normalize_rhs(result, idx_b);
00147 }

SHOGUN Machine Learning Toolbox - Documentation