TensorProductPairKernel.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) 2009 Soeren Sonnenburg
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "lib/Mathematics.h"
00013 #include "kernel/TensorProductPairKernel.h"
00014 #include "features/SimpleFeatures.h"
00015 #include "lib/io.h"
00016 
00017 using namespace shogun;
00018 
00019 CTensorProductPairKernel::CTensorProductPairKernel(void)
00020 : CDotKernel(0), subkernel(NULL)
00021 {
00022     SG_UNSTABLE("CTensorProductPairKernel::"
00023                 "CTensorProductPairKernel(void)", "\n");
00024 }
00025 
00026 CTensorProductPairKernel::CTensorProductPairKernel(int32_t size, CKernel* s)
00027 : CDotKernel(size), subkernel(s)
00028 {
00029     SG_REF(subkernel);
00030 }
00031 
00032 CTensorProductPairKernel::CTensorProductPairKernel(CSimpleFeatures<int32_t>* l, CSimpleFeatures<int32_t>* r, CKernel* s)
00033 : CDotKernel(10), subkernel(s)
00034 {
00035     SG_REF(subkernel);
00036     init(l, r);
00037 }
00038 
00039 CTensorProductPairKernel::~CTensorProductPairKernel()
00040 {
00041     SG_UNREF(subkernel);
00042     cleanup();
00043 }
00044 
00045 bool CTensorProductPairKernel::init(CFeatures* l, CFeatures* r)
00046 {
00047     CDotKernel::init(l, r);
00048     init_normalizer();
00049     return true;
00050 }
00051 
00052 float64_t CTensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b)
00053 {
00054   int32_t alen, blen;
00055   bool afree, bfree;
00056 
00057   int32_t* avec=((CSimpleFeatures<int32_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00058   int32_t* bvec=((CSimpleFeatures<int32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00059 
00060   ASSERT(alen==2);
00061   ASSERT(blen==2);
00062 
00063   CKernel* k=subkernel;
00064   ASSERT(k && k->has_features());
00065 
00066   int32_t a=avec[0];
00067   int32_t b=avec[1];
00068   int32_t c=bvec[0];
00069   int32_t d=bvec[1];
00070 
00071   float64_t result = k->kernel(a,c)*k->kernel(b,d) + k->kernel(a,d)*k->kernel(b,c);
00072 
00073   ((CSimpleFeatures<int32_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00074   ((CSimpleFeatures<int32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00075 
00076   return result;
00077 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation