00001 #ifndef Impala_Core_Training_TrainDataSrcKernelMatrix_h 00002 #define Impala_Core_Training_TrainDataSrcKernelMatrix_h 00003 00004 #include "Core/Training/TrainDataSrc.h" 00005 #include "Core/Matrix/DistributedAccess.h" 00006 #include "Core/Table/AnnotationTable.h" 00007 #include "Link/Svm/LinkSvm.h" 00008 00009 namespace Impala 00010 { 00011 namespace Core 00012 { 00013 namespace Training 00014 { 00015 00016 00023 class TrainDataSrcKernelMatrix : public TrainDataSrc 00024 { 00025 public: 00026 TrainDataSrcKernelMatrix(Matrix::Mat* kernelMatrix) 00027 : TrainDataSrc(0) 00028 { 00029 mMatrix = kernelMatrix; 00030 } 00031 00032 virtual ~TrainDataSrcKernelMatrix() 00033 { 00034 } 00035 00036 virtual svm_problem* MakeSvmProblem() 00037 { 00038 return MakeProblem(0, mMatrix->CW()); 00039 } 00040 00041 virtual svm_problem* MakeSvmProblem(int i) 00042 { 00043 if(i >= mMatrix->CH()) 00044 { 00045 ILOG_WARNING("MakeSvmProblem(int) : index out of range"); 00046 return MakeEmptyProblem(); 00047 } 00048 svm_problem* p = MakeProblem(i, i+1); 00049 return p; 00050 } 00051 virtual int GetVectorLength() 00052 { 00053 return 1; 00054 } 00055 00056 virtual int 00057 Size() 00058 { 00059 ILOG_DEBUG("correct Size called"); 00060 return mMatrix->CH(); 00061 } 00062 00063 virtual Quid 00064 GetQuid(int i) 00065 { 00066 ILOG_DEBUG("correct GetQuid called, but a matrix doesn't have quids"); 00067 return 0; 00068 } 00069 00070 00071 private: 00072 Matrix::Mat* mMatrix; 00073 00085 svm_problem* MakeProblem(int start, int end) 00086 { 00087 ILOG_VAR(Core.Training.Svm.KernelSvmProblem); 00088 int vectorLength = mMatrix->CW(); 00089 svm_problem* problem = new svm_problem; 00090 problem->l = end - start; 00091 problem->y = new double[problem->l]; 00092 problem->x = new struct svm_node *[problem->l]; 00093 struct svm_node* nodes = new struct svm_node[problem->l*(2+vectorLength)]; 00094 for(int i=0 ; i<problem->l ; i++) 00095 { 00096 problem->y[i] = 0; 00097 problem->x[i] = &nodes[i*(vectorLength+2)]; 00098 problem->x[i][0].index = 0; 00099 problem->x[i][0].value = 0; // empty column expected by libsvm 00100 const double* values = mMatrix->CPB(0, start+i); 00101 int j; 00102 for(j=0 ; j<vectorLength ; j++) 00103 { 00104 problem->x[i][j+1].index = j+1; 00105 problem->x[i][j+1].value = values[j]; 00106 } 00107 problem->x[i][j+1].index = -1; 00108 } 00109 return problem; 00110 } 00111 }; 00112 00113 }//namespace Core 00114 }//namespace Training 00115 }//namespace Impala 00116 00117 00118 #endif