GaussianKernel.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 "kernel/GaussianKernel.h"
00013 #include "features/Features.h"
00014 #include "features/RealFeatures.h"
00015 #include "lib/io.h"
00016 
00017 CGaussianKernel::CGaussianKernel(int32_t size, float64_t w)
00018 : CSimpleKernel<float64_t>(size), width(w)
00019 {
00020 }
00021 
00022 CGaussianKernel::CGaussianKernel(
00023     CRealFeatures* l, CRealFeatures* r, float64_t w, int32_t size)
00024 : CSimpleKernel<float64_t>(size), width(w)
00025 {
00026     init(l,r);
00027 }
00028 
00029 CGaussianKernel::~CGaussianKernel()
00030 {
00031 }
00032 
00033 bool CGaussianKernel::init(CFeatures* l, CFeatures* r)
00034 {
00035     CSimpleKernel<float64_t>::init(l, r);
00036     return init_normalizer();
00037 }
00038 
00039 bool CGaussianKernel::load_init(FILE* src)
00040 {
00041     return false;
00042 }
00043 
00044 bool CGaussianKernel::save_init(FILE* dest)
00045 {
00046     return false;
00047 }
00048 
00049 float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
00050 {
00051     int32_t alen, blen;
00052     bool afree, bfree;
00053 
00054     float64_t* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00055     float64_t* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00056     ASSERT(alen==blen);
00057 
00058     float64_t result=0;
00059     for (int32_t i=0; i<alen; i++)
00060         result+=CMath::sq(avec[i]-bvec[i]);
00061 
00062     result=exp(-result/width);
00063 
00064     ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00065     ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00066 
00067     return result;
00068 }

SHOGUN Machine Learning Toolbox - Documentation