SparseLinearKernel.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/SparseFeatures.h"
00015 #include "kernel/SparseLinearKernel.h"
00016 #include "kernel/SparseKernel.h"
00017 
00018 using namespace shogun;
00019 
00020 CSparseLinearKernel::CSparseLinearKernel()
00021 : CSparseKernel<float64_t>(0), normal(NULL), normal_length(0)
00022 {
00023     properties |= KP_LINADD;
00024 }
00025 
00026 CSparseLinearKernel::CSparseLinearKernel(
00027     CSparseFeatures<float64_t>* l, CSparseFeatures<float64_t>* r)
00028 : CSparseKernel<float64_t>(0), normal(NULL), normal_length(0)
00029 {
00030     properties |= KP_LINADD;
00031     init(l,r);
00032 }
00033 
00034 CSparseLinearKernel::~CSparseLinearKernel()
00035 {
00036     cleanup();
00037 }
00038 
00039 bool CSparseLinearKernel::init(CFeatures* l, CFeatures* r)
00040 {
00041     CSparseKernel<float64_t>::init(l, r);
00042     return init_normalizer();
00043 }
00044 
00045 void CSparseLinearKernel::cleanup()
00046 {
00047     delete_optimization();
00048 
00049     CKernel::cleanup();
00050 }
00051 
00052 void CSparseLinearKernel::clear_normal()
00053 {
00054     int32_t num=((CSparseFeatures<float64_t>*) lhs)->get_num_features();
00055     if (normal==NULL)
00056     {
00057         normal=new float64_t[num];
00058         normal_length=num;
00059     }
00060 
00061     memset(normal, 0, sizeof(float64_t)*normal_length);
00062     set_is_initialized(true);
00063 }
00064 
00065 void CSparseLinearKernel::add_to_normal(int32_t idx, float64_t weight)
00066 {
00067     ((CSparseFeatures<float64_t>*) rhs)->add_to_dense_vec(
00068         normalizer->normalize_lhs(weight, idx), idx, normal, normal_length);
00069     set_is_initialized(true);
00070 }
00071   
00072 float64_t CSparseLinearKernel::compute(int32_t idx_a, int32_t idx_b)
00073 {
00074   int32_t alen=0;
00075   int32_t blen=0;
00076   bool afree=false;
00077   bool bfree=false;
00078 
00079   TSparseEntry<float64_t>* avec=((CSparseFeatures<float64_t>*) lhs)->
00080     get_sparse_feature_vector(idx_a, alen, afree);
00081   TSparseEntry<float64_t>* bvec=((CSparseFeatures<float64_t>*) rhs)->
00082     get_sparse_feature_vector(idx_b, blen, bfree);
00083 
00084   float64_t result=((CSparseFeatures<float64_t>*) lhs)->
00085     sparse_dot(1.0, avec,alen, bvec,blen);
00086 
00087   ((CSparseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00088   ((CSparseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00089 
00090   return result;
00091 }
00092 
00093 bool CSparseLinearKernel::init_optimization(
00094     int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas)
00095 {
00096     clear_normal();
00097 
00098     for (int32_t i=0; i<num_suppvec; i++)
00099         add_to_normal(sv_idx[i], alphas[i]);
00100 
00101     set_is_initialized(true);
00102     return true;
00103 }
00104 
00105 bool CSparseLinearKernel::delete_optimization()
00106 {
00107     delete[] normal;
00108     normal_length=0;
00109     normal=NULL;
00110     set_is_initialized(false);
00111 
00112     return true;
00113 }
00114 
00115 float64_t CSparseLinearKernel::compute_optimized(int32_t idx)
00116 {
00117     ASSERT(get_is_initialized());
00118     float64_t result = ((CSparseFeatures<float64_t>*) rhs)->
00119         dense_dot(1.0, idx, normal, normal_length, 0.0);
00120     return normalizer->normalize_rhs(result, idx);
00121 }

SHOGUN Machine Learning Toolbox - Documentation