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     ((CSimpleFeatures<float64_t>*) lhs)->add_to_dense_vec(
00068         normalizer->normalize_lhs(weight, idx), idx, normal, normal_length);
00069     set_is_initialized(true);
00070 }
00071 
00072 float64_t CLinearKernel::compute(int32_t idx_a, int32_t idx_b)
00073 {
00074   int32_t alen, blen;
00075   bool afree, bfree;
00076 
00077   float64_t* avec=
00078     ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00079   float64_t* bvec=
00080     ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00081 
00082   ASSERT(alen==blen);
00083 
00084   float64_t result=CMath::dot(avec, bvec, alen);
00085 
00086   ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00087   ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00088 
00089   return result;
00090 }
00091 
00092 bool CLinearKernel::init_optimization(
00093     int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas)
00094 {
00095     clear_normal();
00096 
00097     for (int32_t i=0; i<num_suppvec; i++)
00098         add_to_normal(sv_idx[i], alphas[i]);
00099 
00100     set_is_initialized(true);
00101     return true;
00102 }
00103 
00104 bool CLinearKernel::delete_optimization()
00105 {
00106     delete[] normal;
00107     normal_length=0;
00108     normal=NULL;
00109     set_is_initialized(false);
00110 
00111     return true;
00112 }
00113 
00114 float64_t CLinearKernel::compute_optimized(int32_t idx)
00115 {
00116     ASSERT(get_is_initialized());
00117     float64_t result = ((CSimpleFeatures<float64_t>*) rhs)->
00118         dense_dot(idx, normal, normal_length);
00119     return normalizer->normalize_rhs(result, idx);
00120 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation