00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "fastlib/fastlib.h"
00033
00034 const fx_entry_doc kernel_matrix_generator_entries[] = {
00035 {"data", FX_REQUIRED, FX_STR, NULL,
00036 " File consists of data points in lines.\n"},
00037 {"kernel", FX_REQUIRED, FX_STR, NULL,
00038 " The kernel type: gaussian / polynomial.\n"},
00039 {"bandwidth", FX_PARAM, FX_DOUBLE, NULL,
00040 " The gaussian kernel bandwidth, default = 1.\n"},
00041 {"degree", FX_PARAM, FX_DOUBLE, NULL,
00042 " The polynomial kernel degree, default = 1.\n"},
00043 {"rate", FX_PARAM, FX_INT, NULL,
00044 " The sampling rate, default = 20 choose 1.\n"},
00045 {"output", FX_PARAM, FX_STR, NULL,
00046 " File to hold output kernel matrix,"
00047 " default = \"kernel_matrix.txt\".\n"},
00048 FX_ENTRY_DOC_DONE
00049 };
00050
00051 const fx_submodule_doc kernel_matrix_generator_submodules[] = {
00052 FX_SUBMODULE_DOC_DONE
00053 };
00054
00055 const fx_module_doc kernel_matrix_generator_doc = {
00056 kernel_matrix_generator_entries, kernel_matrix_generator_submodules,
00057 "This is a program generating the kernel matrix of "
00058 "a set of points.\n"
00059 };
00060
00061 void gen_gaussian_kernel(const Matrix& ref, Matrix* kernel_matrix,
00062 int rate) {
00063 double bandwidth = fx_param_double(fx_root, "bandwidth", 1);
00064
00065
00066 GaussianKernel kernel;
00067 kernel.Init(bandwidth);
00068
00069 int n_cols = (ref.n_cols()-1)/rate+1;
00070 kernel_matrix->Init(n_cols, n_cols);
00071 for(index_t r = 0; r < ref.n_cols(); r+=rate) {
00072 const double *r_col = ref.GetColumnPtr(r);
00073
00074 for(index_t q = 0; q < ref.n_cols(); q+=rate) {
00075 const double *q_col = ref.GetColumnPtr(q);
00076 double dsqd = la::DistanceSqEuclidean(ref.n_rows(), q_col,
00077 r_col);
00078 kernel_matrix->set(q/rate, r/rate, kernel.EvalUnnormOnSq(dsqd));
00079 }
00080 }
00081 }
00082
00083 void gen_polynomial_kernel(const Matrix& ref, Matrix* kernel_matrix,
00084 int rate) {
00085 double degree = fx_param_double(fx_root, "degree", 1);
00086
00087 int n_cols = (ref.n_cols()-1)/rate+1;
00088 kernel_matrix->Init(n_cols, n_cols);
00089 for(index_t r = 0; r < ref.n_cols(); r+=rate) {
00090 const double *r_col = ref.GetColumnPtr(r);
00091
00092 for(index_t q = 0; q < ref.n_cols(); q+=rate) {
00093 const double *q_col = ref.GetColumnPtr(q);
00094 double dsqd = la::Dot(ref.n_rows(), q_col, r_col) + 1;
00095 kernel_matrix->set(q/rate, r/rate, pow(dsqd, degree));
00096 }
00097 }
00098 }
00099
00100 int main(int argc, char *argv[]) {
00101
00102
00103 fx_init(argc, argv, &kernel_matrix_generator_doc);
00104
00105 Matrix references;
00106 const char *references_file_name = fx_param_str_req(fx_root, "data");
00107 data::Load(references_file_name, &references);
00108
00109 printf("nrows = %d ncols = %d\n", references.n_rows(),
00110 references.n_cols());
00111
00112 const char* kernel_type = fx_param_str_req(fx_root, "kernel");
00113 int rate = fx_param_int(fx_root, "rate", 20);
00114
00115 #define RUNNING 1
00116 #if RUNNING==1
00117
00118 Matrix kernel_matrix;
00119 if (strcmp(kernel_type, "gaussian") == 0)
00120 gen_gaussian_kernel(references, &kernel_matrix, rate);
00121 else if (strcmp(kernel_type, "polynomial") == 0)
00122 gen_polynomial_kernel(references, &kernel_matrix, rate);
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 const char *file_name = fx_param_str(fx_root, "output",
00148 "kernel_matrix.txt");
00149 data::Save(file_name, kernel_matrix);
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 fx_done(fx_root);
00160 #endif
00161 return 0;
00162 }