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-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 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/SimpleFeatures.h"
00018 
00019 using namespace shogun;
00020 
00021 CSigmoidKernel::CSigmoidKernel(int32_t size, float64_t g, float64_t c)
00022 : CSimpleKernel<float64_t>(size),gamma(g), coef0(c)
00023 {
00024 }
00025 
00026 CSigmoidKernel::CSigmoidKernel(
00027     CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, int32_t size, float64_t g, float64_t c)
00028 : CSimpleKernel<float64_t>(size),gamma(g), coef0(c)
00029 {
00030     init(l,r);
00031 }
00032 
00033 CSigmoidKernel::~CSigmoidKernel()
00034 {
00035     cleanup();
00036 }
00037 
00038 bool CSigmoidKernel::init(CFeatures* l, CFeatures* r)
00039 {
00040     CSimpleKernel<float64_t>::init(l, r);
00041     return init_normalizer();
00042 }
00043 
00044 void CSigmoidKernel::cleanup()
00045 {
00046 }
00047 
00048 float64_t CSigmoidKernel::compute(int32_t idx_a, int32_t idx_b)
00049 {
00050     int32_t alen, blen;
00051     bool afree, bfree;
00052 
00053     float64_t* avec=
00054         ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00055     float64_t* bvec=
00056         ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00057     ASSERT(alen==blen);
00058 
00059 #ifndef HAVE_LAPACK
00060     float64_t result=0;
00061     {
00062         for (int32_t i=0; i<alen; i++)
00063             result+=avec[i]*bvec[i];
00064     }
00065 #else
00066     int skip=1; /* calling external lib */
00067     float64_t result = cblas_ddot(
00068         (int) alen, (double*) avec, skip, (double*) bvec, skip);
00069 #endif
00070 
00071     ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00072     ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00073 
00074     return tanh(gamma*result+coef0);
00075 }

SHOGUN Machine Learning Toolbox - Documentation