SparsePolyKernel.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 "kernel/SparsePolyKernel.h"
00014 #include "kernel/SqrtDiagKernelNormalizer.h"
00015 #include "features/SparseFeatures.h"
00016
00017 CSparsePolyKernel::CSparsePolyKernel(int32_t size, int32_t d, bool i)
00018 : CSparseKernel<float64_t>(size), degree(d), inhomogene(i)
00019 {
00020 set_normalizer(new CSqrtDiagKernelNormalizer());
00021 }
00022
00023 CSparsePolyKernel::CSparsePolyKernel(
00024 CSparseFeatures<float64_t>* l, CSparseFeatures<float64_t>* r,
00025 int32_t size, int32_t d, bool i)
00026 : CSparseKernel<float64_t>(size),degree(d),inhomogene(i)
00027 {
00028 set_normalizer(new CSqrtDiagKernelNormalizer());
00029 init(l,r);
00030 }
00031
00032 CSparsePolyKernel::~CSparsePolyKernel()
00033 {
00034 cleanup();
00035 }
00036
00037 bool CSparsePolyKernel::init(CFeatures* l, CFeatures* r)
00038 {
00039 CSparseKernel<float64_t>::init(l,r);
00040 return init_normalizer();
00041 }
00042
00043 void CSparsePolyKernel::cleanup()
00044 {
00045 CKernel::cleanup();
00046 }
00047
00048 bool CSparsePolyKernel::load_init(FILE* src)
00049 {
00050 return false;
00051 }
00052
00053 bool CSparsePolyKernel::save_init(FILE* dest)
00054 {
00055 return false;
00056 }
00057
00058 float64_t CSparsePolyKernel::compute(int32_t idx_a, int32_t idx_b)
00059 {
00060 int32_t alen=0;
00061 int32_t blen=0;
00062 bool afree=false;
00063 bool bfree=false;
00064
00065 TSparseEntry<float64_t>* avec=((CSparseFeatures<float64_t>*) lhs)->
00066 get_sparse_feature_vector(idx_a, alen, afree);
00067 TSparseEntry<float64_t>* bvec=((CSparseFeatures<float64_t>*) rhs)->
00068 get_sparse_feature_vector(idx_b, blen, bfree);
00069
00070 float64_t result=((CSparseFeatures<float64_t>*) lhs)->sparse_dot(1.0,avec, alen, bvec, blen);
00071
00072 if (inhomogene)
00073 result+=1;
00074
00075 result=CMath::pow(result, degree);
00076
00077 ((CSparseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00078 ((CSparseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00079
00080 return result;
00081 }