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

SHOGUN Machine Learning Toolbox - Documentation