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

SHOGUN Machine Learning Toolbox - Documentation