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