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-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/LinearByteKernel.h"
00015 #include "features/ByteFeatures.h"
00016 
00017 CLinearByteKernel::CLinearByteKernel(INT size, bool dr, DREAL s)
00018 : CSimpleKernel<BYTE>(size), scale(s), do_rescale(dr), initialized(false),
00019     normal(NULL)
00020 {
00021 }
00022 
00023 CLinearByteKernel::CLinearByteKernel(
00024     CByteFeatures* l, CByteFeatures* r, bool dr, DREAL s)
00025 : CSimpleKernel<BYTE>(10), scale(s), do_rescale(dr), initialized(false),
00026     normal(NULL)
00027 {
00028     init(l, r);
00029 }
00030 
00031 CLinearByteKernel::~CLinearByteKernel()
00032 {
00033     cleanup();
00034 }
00035 
00036 bool CLinearByteKernel::init(CFeatures* l, CFeatures* r)
00037 {
00038     CSimpleKernel<BYTE>::init(l, r);
00039 
00040     if (!initialized)
00041         init_rescale() ;
00042 
00043     SG_INFO( "rescaling kernel by %g (num:%d)\n",scale, CMath::min(l->get_num_vectors(), r->get_num_vectors()));
00044 
00045     return true;
00046 }
00047 
00048 void CLinearByteKernel::init_rescale()
00049 {
00050     if (!do_rescale)
00051         return ;
00052     LONGREAL sum=0;
00053     scale=1.0;
00054     for (INT i=0; (i<lhs->get_num_vectors() && i<rhs->get_num_vectors()); i++)
00055             sum+=compute(i, i);
00056 
00057     if ( sum > (pow((double) 2, (double) 8*sizeof(LONG))) ) {
00058       SG_ERROR( "the sum %lf does not fit into integer of %d bits expect bogus results.\n", sum, 8*sizeof(LONG));
00059    }
00060     scale=sum/CMath::min(lhs->get_num_vectors(), rhs->get_num_vectors());
00061     initialized=true;
00062 }
00063 
00064 void CLinearByteKernel::cleanup()
00065 {
00066     delete_optimization();
00067 
00068     CKernel::cleanup();
00069 }
00070 
00071 bool CLinearByteKernel::load_init(FILE* src)
00072 {
00073     return false;
00074 }
00075 
00076 bool CLinearByteKernel::save_init(FILE* dest)
00077 {
00078     return false;
00079 }
00080 
00081 void CLinearByteKernel::clear_normal()
00082 {
00083     int num = lhs->get_num_vectors();
00084 
00085     for (int i=0; i<num; i++)
00086         normal[i]=0;
00087 }
00088 
00089 void CLinearByteKernel::add_to_normal(INT idx, DREAL weight) 
00090 {
00091     INT vlen;
00092     bool vfree;
00093     BYTE* vec=((CByteFeatures*) lhs)->get_feature_vector(idx, vlen, vfree);
00094 
00095     for (int i=0; i<vlen; i++)
00096         normal[i]+= weight*vec[i];
00097 
00098     ((CByteFeatures*) lhs)->free_feature_vector(vec, idx, vfree);
00099 }
00100   
00101 DREAL CLinearByteKernel::compute(INT idx_a, INT idx_b)
00102 {
00103   INT alen, blen;
00104   bool afree, bfree;
00105 
00106   BYTE* avec=((CByteFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00107   BYTE* bvec=((CByteFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00108   ASSERT(alen==blen);
00109 
00110   double sum=0;
00111   for (INT i=0; i<alen; i++)
00112       sum+=((LONG) avec[i])*((LONG) bvec[i]);
00113   DREAL result=sum/scale;
00114 
00115   ((CByteFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00116   ((CByteFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00117 
00118   return result;
00119 }
00120 
00121 bool CLinearByteKernel::init_optimization(INT num_suppvec, INT* sv_idx, DREAL* alphas) 
00122 {
00123     SG_DEBUG("drin gelandet yeah\n");
00124     INT alen;
00125     bool afree;
00126 
00127     int num_feat=((CByteFeatures*) lhs)->get_num_features();
00128     ASSERT(num_feat);
00129 
00130     normal=new DREAL[num_feat];
00131     for (INT i=0; i<num_feat; i++)
00132         normal[i]=0;
00133 
00134     for (int i=0; i<num_suppvec; i++)
00135     {
00136         BYTE* avec=((CByteFeatures*) lhs)->get_feature_vector(sv_idx[i], alen, afree);
00137         ASSERT(avec);
00138 
00139         for (int j=0; j<num_feat; j++)
00140             normal[j]+= alphas[i] * ((double) avec[j]);
00141 
00142         ((CByteFeatures*) lhs)->free_feature_vector(avec, 0, afree);
00143     }
00144 
00145     set_is_initialized(true);
00146     return true;
00147 }
00148 
00149 bool CLinearByteKernel::delete_optimization()
00150 {
00151     delete[] normal;
00152     normal=NULL;
00153 
00154     set_is_initialized(false);
00155 
00156     return true;
00157 }
00158 
00159 DREAL CLinearByteKernel::compute_optimized(INT idx_b) 
00160 {
00161     INT blen;
00162     bool bfree;
00163 
00164     BYTE* bvec=((CByteFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00165 
00166     double result=0;
00167     {
00168         for (INT i=0; i<blen; i++)
00169             result+= normal[i] * ((double) bvec[i]);
00170     }
00171     result/=scale;
00172 
00173     ((CByteFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00174 
00175     return result;
00176 }

SHOGUN Machine Learning Toolbox - Documentation