From df9c3ca0b62f1a20071bee3a55d24d673c5d11e0 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Fri, 30 Jan 2015 16:22:52 +0100 Subject: first working version of new kernel GenSVM --- src/gensvm_pred.c | 149 +++++++++++++----------------------------------------- 1 file changed, 35 insertions(+), 114 deletions(-) (limited to 'src/gensvm_pred.c') diff --git a/src/gensvm_pred.c b/src/gensvm_pred.c index 88678d7..7baae07 100644 --- a/src/gensvm_pred.c +++ b/src/gensvm_pred.c @@ -12,6 +12,7 @@ */ #include +#include #include "libGenSVM.h" #include "gensvm.h" @@ -19,19 +20,6 @@ #include "gensvm_matrix.h" #include "gensvm_pred.h" -#include "util.h" // testing - -void gensvm_predict_labels(struct GenData *data_test, - struct GenData *data_train, struct GenModel *model, - long *predy) -{ - if (model->kerneltype == K_LINEAR) - gensvm_predict_labels_linear(data_test, model, predy); - else - gensvm_predict_labels_kernel(data_test, data_train, model, - predy); -} - /** * @brief Predict class labels of data given and output in predy * @@ -46,24 +34,40 @@ void gensvm_predict_labels(struct GenData *data_test, * @param[in] model GenModel with optimized V * @param[out] predy pre-allocated vector to record predictions in */ -void gensvm_predict_labels_linear(struct GenData *data, - struct GenModel *model, long *predy) +void gensvm_predict_labels(struct GenData *testdata, struct GenModel *model, + long *predy) { - long i, j, k, label; - double norm, min_dist; - - long n = data->n; // note that model->n is the size of the training sample. - long m = data->m; - long K = model->K; //data->K does not necessarily equal the original K. - - double *S = Calloc(double, K-1); - double *ZV = Calloc(double, n*(K-1)); - double *U = Calloc(double, K*(K-1)); + long i, j, k, n, m, K, label; + double norm, min_dist, *S, *ZV, *U; + + n = testdata->n; + m = testdata->r; + K = model->K; + + // allocate necessary memory + S = Calloc(double, K-1); + if (S == NULL) { + fprintf(stderr, "Failed to allocate memory for S in " + "gensvm_predict_labels.\n"); + exit(1); + } + ZV = Calloc(double, n*(K-1)); + if (ZV == NULL) { + fprintf(stderr, "Failed to allocate memory for ZV in " + "gensvm_predict_labels.\n"); + exit(1); + } + U = Calloc(double, K*(K-1)); + if (U == NULL) { + fprintf(stderr, "Failed to allocate memory for U in " + "gensvm_predict_labels.\n"); + exit(1); + } - // Get the simplex matrix + // Generate the simplex matrix gensvm_simplex_gen(K, U); - // Generate the simplex-space vectors + // Generate the simplex space vectors cblas_dgemm( CblasRowMajor, CblasNoTrans, @@ -72,7 +76,7 @@ void gensvm_predict_labels_linear(struct GenData *data, K-1, m+1, 1.0, - data->Z, + testdata->Z, m+1, model->V, K-1, @@ -81,96 +85,16 @@ void gensvm_predict_labels_linear(struct GenData *data, K-1); // Calculate the distance to each of the vertices of the simplex. - // The closest vertex defines the class label. + // The closest vertex defines the class label for (i=0; in; - long n_test = data_test->n; - long r = model->m; - long K = model->K; - - double *K2 = NULL; - gensvm_make_crosskernel(model, data_train, data_test, &K2); - - double *S = Calloc(double, K-1); - double *ZV = Calloc(double, n_test*(r+1)); - double *KPS = Calloc(double, n_test*(r+1)); - double *U = Calloc(double, K*(K-1)); - - gensvm_simplex_gen(K, U); - - // were doing the computations explicitly since P is included in - // data_train->Z. Might want to look at this some more if it turns out - // to be slow. - - double value, rowvalue; - for (i=0; iZ, r+1, k, - j); - value += rowvalue; - } - value *= matrix_get(data_train->J, 1, j, 0); - matrix_set(KPS, r+1, i, j, value); - } - matrix_set(KPS, r+1, i, 0, 1.0); - } - - cblas_dgemm( - CblasRowMajor, - CblasNoTrans, - CblasNoTrans, - n_test, - K-1, - r+1, - 1.0, - KPS, - r+1, - model->V, - K-1, - 0.0, - ZV, - K-1); - - for (i=0; i