SigmoidKernel.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/config.h"
00012 #include "lib/common.h"
00013 #include "lib/io.h"
00014 #include "lib/lapack.h"
00015 #include "kernel/SigmoidKernel.h"
00016 #include "features/Features.h"
00017 #include "features/RealFeatures.h"
00018 
00019 CSigmoidKernel::CSigmoidKernel(int32_t size, float64_t g, float64_t c)
00020 : CSimpleKernel<float64_t>(size),gamma(g), coef0(c)
00021 {
00022 }
00023 
00024 CSigmoidKernel::CSigmoidKernel(
00025     CRealFeatures* l, CRealFeatures* r, int32_t size, float64_t g, float64_t c)
00026 : CSimpleKernel<float64_t>(size),gamma(g), coef0(c)
00027 {
00028     init(l,r);
00029 }
00030 
00031 CSigmoidKernel::~CSigmoidKernel()
00032 {
00033     cleanup();
00034 }
00035 
00036 bool CSigmoidKernel::init(CFeatures* l, CFeatures* r)
00037 {
00038     CSimpleKernel<float64_t>::init(l, r);
00039     return init_normalizer();
00040 }
00041 
00042 void CSigmoidKernel::cleanup()
00043 {
00044 }
00045 
00046 bool CSigmoidKernel::load_init(FILE* src)
00047 {
00048     return false;
00049 }
00050 
00051 bool CSigmoidKernel::save_init(FILE* dest)
00052 {
00053     return false;
00054 }
00055 
00056 float64_t CSigmoidKernel::compute(int32_t idx_a, int32_t idx_b)
00057 {
00058     int32_t alen, blen;
00059     bool afree, bfree;
00060 
00061     float64_t* avec=
00062         ((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00063     float64_t* bvec=
00064         ((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00065     ASSERT(alen==blen);
00066 
00067 #ifndef HAVE_LAPACK
00068     float64_t result=0;
00069     {
00070         for (int32_t i=0; i<alen; i++)
00071             result+=avec[i]*bvec[i];
00072     }
00073 #else
00074     int skip=1; /* calling external lib */
00075     float64_t result = cblas_ddot(
00076         (int) alen, (double*) avec, skip, (double*) bvec, skip);
00077 #endif
00078 
00079     ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00080     ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00081 
00082     return tanh(gamma*result+coef0);
00083 }

SHOGUN Machine Learning Toolbox - Documentation