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

SHOGUN Machine Learning Toolbox - Documentation