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-2010 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  * Copyright (C) 2010 Berlin Institute of Technology
00010  */
00011 
00012 #include <shogun/lib/common.h>
00013 #include <shogun/io/SGIO.h>
00014 #include <shogun/features/Features.h>
00015 #include <shogun/features/DotFeatures.h>
00016 #include <shogun/kernel/LinearKernel.h>
00017 
00018 using namespace shogun;
00019 
00020 CLinearKernel::CLinearKernel()
00021 : CDotKernel(0), normal(NULL), normal_length(0)
00022 {
00023     properties |= KP_LINADD;
00024 }
00025 
00026 CLinearKernel::CLinearKernel(CDotFeatures* l, CDotFeatures* r)
00027 : CDotKernel(0), normal(NULL), normal_length(0)
00028 {
00029     properties |= KP_LINADD;
00030     init(l,r);
00031 }
00032 
00033 CLinearKernel::~CLinearKernel()
00034 {
00035     cleanup();
00036 }
00037 
00038 bool CLinearKernel::init(CFeatures* l, CFeatures* r)
00039 {
00040     CDotKernel::init(l, r);
00041 
00042     return init_normalizer();
00043 }
00044 
00045 void CLinearKernel::cleanup()
00046 {
00047     delete_optimization();
00048 
00049     CKernel::cleanup();
00050 }
00051 
00052 void CLinearKernel::clear_normal()
00053 {
00054     int32_t num = ((CDotFeatures*) lhs)->get_dim_feature_space();
00055     if (normal==NULL)
00056     {
00057         normal = SG_MALLOC(float64_t, num);
00058         normal_length=num;
00059     }
00060 
00061     memset(normal, 0, sizeof(float64_t)*normal_length);
00062 
00063     set_is_initialized(true);
00064 }
00065 
00066 void CLinearKernel::add_to_normal(int32_t idx, float64_t weight) 
00067 {
00068     ((CDotFeatures*) lhs)->add_to_dense_vec(
00069         normalizer->normalize_lhs(weight, idx), idx, normal, normal_length);
00070     set_is_initialized(true);
00071 }
00072 
00073 bool CLinearKernel::init_optimization(
00074     int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas)
00075 {
00076     clear_normal();
00077 
00078     for (int32_t i=0; i<num_suppvec; i++)
00079         add_to_normal(sv_idx[i], alphas[i]);
00080 
00081     set_is_initialized(true);
00082     return true;
00083 }
00084 
00085 bool CLinearKernel::init_optimization(CKernelMachine* km)
00086 {
00087     clear_normal();
00088 
00089     int32_t num_suppvec=km->get_num_support_vectors();
00090 
00091     for (int32_t i=0; i<num_suppvec; i++)
00092         add_to_normal(km->get_support_vector(i), km->get_alpha(i));
00093 
00094     set_is_initialized(true);
00095     return true;
00096 }
00097 
00098 bool CLinearKernel::delete_optimization()
00099 {
00100     SG_FREE(normal);
00101     normal_length=0;
00102     normal=NULL;
00103     set_is_initialized(false);
00104 
00105     return true;
00106 }
00107 
00108 float64_t CLinearKernel::compute_optimized(int32_t idx)
00109 {
00110     ASSERT(get_is_initialized());
00111     float64_t result = ((CDotFeatures*) rhs)->
00112         dense_dot(idx, normal, normal_length);
00113     return normalizer->normalize_rhs(result, idx);
00114 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation