diff options
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | include/msvmmaj_sv.h | 19 | ||||
| -rw-r--r-- | src/msvmmaj_matrix.c | 4 | ||||
| -rw-r--r-- | src/msvmmaj_sv.c | 45 | ||||
| -rw-r--r-- | src/msvmmaj_train.c | 5 |
5 files changed, 76 insertions, 3 deletions
@@ -20,6 +20,7 @@ lib/libmsvmmaj.a: \ src/msvmmaj_lapack.o \ src/msvmmaj_matrix.o \ src/msvmmaj_pred.o \ + src/msvmmaj_sv.o \ src/msvmmaj_train.o \ src/msvmmaj_train_dataset.o \ src/strutil.o \ @@ -34,6 +35,7 @@ lib/libmsvmmaj.a: \ src/msvmmaj_kernel.o \ src/msvmmaj_lapack.o \ src/msvmmaj_pred.o \ + src/msvmmaj_sv.o \ src/msvmmaj_train.o \ src/msvmmaj_train_dataset.o \ src/strutil.o \ @@ -80,6 +82,10 @@ src/msvmmaj_pred.o: @$(CC) -c -o src/msvmmaj_pred.o src/msvmmaj_pred.c $(CFLAGS) $(INCLUDE) @echo msvmmaj_pred.o... +src/msvmmaj_sv.o: + @$(CC) -c -o src/msvmmaj_sv.o src/msvmmaj_sv.c $(CFLAGS) $(INCLUDE) + @echo msvmmaj_sv.o... + src/msvmmaj_train.o: @$(CC) -c -o src/msvmmaj_train.o src/msvmmaj_train.c $(CFLAGS) $(INCLUDE) @echo msvmmaj_train.o... diff --git a/include/msvmmaj_sv.h b/include/msvmmaj_sv.h new file mode 100644 index 0000000..e37ffc4 --- /dev/null +++ b/include/msvmmaj_sv.h @@ -0,0 +1,19 @@ +/** + * @file msvmmaj_sv.h + * @author Gertjan van den Burg + * @date May, 2014 + * @brief Header file for msvmmaj_sv.c + * + * @details + * Contains function declarations for functions used to count support vectors. + * + */ + +#ifndef MSVMMAJ_SV_H +#define MSVMMAJ_SV_H + +#include "globals.h" + +long msvmmaj_num_sv(struct MajModel *model, struct MajData *data); + +#endif diff --git a/src/msvmmaj_matrix.c b/src/msvmmaj_matrix.c index ffa0c21..ecb0efd 100644 --- a/src/msvmmaj_matrix.c +++ b/src/msvmmaj_matrix.c @@ -27,7 +27,7 @@ * @param[in] cols number of columns of M * @param[in] i row index of element to write to * @param[in] j column index of element to write to - * @param[out] val value to write to specified element of M + * @param[in] val value to write to specified element of M */ void matrix_set(double *M, long cols, long i, long j, double val) { @@ -44,7 +44,7 @@ void matrix_set(double *M, long cols, long i, long j, double val) * @param[in] cols number of columns of M * @param[in] i row index (starting from 0) * @param[in] j column index (starting from 0) - * @returns matrix element at (i, j) + * @return matrix element at (i, j) */ double matrix_get(double *M, long cols, long i, long j) { diff --git a/src/msvmmaj_sv.c b/src/msvmmaj_sv.c new file mode 100644 index 0000000..1358d4e --- /dev/null +++ b/src/msvmmaj_sv.c @@ -0,0 +1,45 @@ +/** + * @file msvmmaj_sv.c + * @author Gertjan van den Burg + * @date May, 2014 + * @brief Calculate the number of support vectors + * + * @details + * The function in this file can be used to calculate the number of support + * vectors are left in a model. + * + */ + +#include "msvmmaj.h" +#include "msvmmaj_matrix.h" + +/** + * @brief Calculate the number of support vectors in a model + * + * @details + * If an object is correctly classified, the number of classes for which the + * error q is larger than 1, is K-1 (i.e., there is no error w.r.t. any of the + * other classes). All objects for which this is not the case are thus support + * vectors. + * + * @param[in] model MajModel with solution + * @param[in] data MajData to be used + * @return number of support vectors with this solution + * + */ +long msvmmaj_num_sv(struct MajModel *model, struct MajData *data) +{ + long i, j, num_correct, num_sv = 0; + double value; + + for (i=0; i<data->n; i++) { + num_correct = 0; + for (j=0; j<data->K; j++) { + value = matrix_get(model->Q, data->K, i, j); + num_correct += (value > 1); + } + num_sv += (num_correct < data->K - 1); + } + + return num_sv; +} diff --git a/src/msvmmaj_train.c b/src/msvmmaj_train.c index 97ee6a1..bbd2663 100644 --- a/src/msvmmaj_train.c +++ b/src/msvmmaj_train.c @@ -17,6 +17,7 @@ #include "msvmmaj.h" #include "msvmmaj_lapack.h" #include "msvmmaj_matrix.h" +#include "msvmmaj_sv.h" #include "msvmmaj_train.h" #include "util.h" @@ -92,8 +93,10 @@ void msvmmaj_optimize(struct MajModel *model, struct MajData *data) it++; } - note("optimization finished, iter = %li, error = %8.8f\n", it-1, + note("optimization finished, iter = %li, error = %15.16f\n", it-1, (Lbar - L)/L); + note("number of support vectors: %li\n", msvmmaj_num_sv(model, data)); + model->training_error = (Lbar - L)/L; for (i=0; i<K-1; i++) |
