diff options
Diffstat (limited to 'src/libMSVMMaj.c')
| -rw-r--r-- | src/libMSVMMaj.c | 133 |
1 files changed, 99 insertions, 34 deletions
diff --git a/src/libMSVMMaj.c b/src/libMSVMMaj.c index 9544830..a0bef97 100644 --- a/src/libMSVMMaj.c +++ b/src/libMSVMMaj.c @@ -1,6 +1,6 @@ /** * @file libMSVMMaj.c - * @author Gertjan van den Burg (burg@ese.eur.nl) + * @author Gertjan van den Burg * @date August 8, 2013 * @brief Main functions for the MSVMMaj algorithm * @@ -16,24 +16,23 @@ #include <math.h> #include "libMSVMMaj.h" -#include "MSVMMaj.h" -#include "matrix.h" +#include "msvmmaj.h" +#include "msvmmaj_matrix.h" inline double rnd() { return (double) rand()/0x7FFFFFFF; } /** - * @name msvmmaj_simplex_gen * @brief Generate matrix of simplex vertex coordinates - * @ingroup libMSVMMaj * + * @details * Generate the simplex matrix. Each row of the created * matrix contains the coordinate vector of a single * vertex of the K-simplex in K-1 dimensions. The simplex * generated is a special simplex with edges of length 1. * The simplex matrix U must already have been allocated. * - * @param [in] K number of classes - * @param [in,out] U simplex matrix of size K * (K-1) + * @param[in] K number of classes + * @param[in,out] U simplex matrix of size K * (K-1) */ void msvmmaj_simplex_gen(long K, double *U) { @@ -51,10 +50,18 @@ void msvmmaj_simplex_gen(long K, double *U) } } -/*! - Generate the category matrix R. The category matrix has 1's everywhere - except at the column corresponding to the label of instance i. -*/ +/** + * @brief Generate the category matrix + * + * @details + * Generate the category matrix R. The category matrix has 1's everywhere + * except at the column corresponding to the label of instance i, there the + * element is 0. + * + * @param[in,out] model corresponding MajModel + * @param[in] dataset corresponding MajData + * + */ void msvmmaj_category_matrix(struct MajModel *model, struct MajData *dataset) { long i, j; @@ -70,8 +77,19 @@ void msvmmaj_category_matrix(struct MajModel *model, struct MajData *dataset) } } -/*! - * Simplex diff +/** + * @brief Generate the simplex difference matrix + * + * @details + * The simplex difference matrix is a 3D matrix which is constructed + * as follows. For each instance i, the difference vectors between the row of + * the simplex matrix corresponding to the class label of instance i and the + * other rows of the simplex matrix are calculated. These difference vectors + * are stored in a matrix, which is one horizontal slice of the 3D matrix. + * + * @param[in,out] model the corresponding MajModel + * @param[in] data the corresponding MajData + * */ void msvmmaj_simplex_diff(struct MajModel *model, struct MajData *data) { @@ -92,13 +110,22 @@ void msvmmaj_simplex_diff(struct MajModel *model, struct MajData *data) } } -/*! - Calculate the errors Q based on the current value of V. - It is assumed that the memory for Q has already been allocated. - In addition, the matrix ZV is calculated here. It is assigned to a - pre-allocated block of memory, since it would be inefficient to keep - reassigning this block at every iteration. -*/ +/** + * @brief Calculate the scalar errors + * + * @details + * Calculate the scalar errors q based on the current estimate of V, and + * store these in Q. It is assumed that the memory for Q has already been + * allocated. In addition, the matrix ZV is calculated here. It is assigned + * to a pre-allocated block of memory, which is passed to this function. + * + * @param[in,out] model the corresponding MajModel + * @param[in] data the corresponding MajData + * @param[in,out] ZV a pointer to a memory block for ZV. On exit + * this block is updated with the new ZV matrix + * calculated with MajModel::V. + * + */ void msvmmaj_calculate_errors(struct MajModel *model, struct MajData *data, double *ZV) { long i, j, k; @@ -136,9 +163,23 @@ void msvmmaj_calculate_errors(struct MajModel *model, struct MajData *data, doub } } -/*! - Calculate the Huber hinge errors for each error in the matrix Q. -*/ +/** + * @brief Calculate the Huber hinge errors + * + * @details + * For each of the scalar errors in Q the Huber hinge errors are + * calculated. The Huber hinge is here defined as + * @f[ + * h(q) = + * \begin{dcases} + * 1 - q - \frac{\kappa + 1}{2} & \text{if } q \leq -\kappa \\ + * \frac{1}{2(\kappa + 1)} ( 1 - q)^2 & \text{if } q \in (-\kappa, 1] \\ + * 0 & \text{if } q > 1 + * \end{dcases} + * @f] + * + * @param[in,out] model the corresponding MajModel + */ void msvmmaj_calculate_huber(struct MajModel *model) { long i, j; @@ -159,10 +200,9 @@ void msvmmaj_calculate_huber(struct MajModel *model) } /** - * @name msvmmaj_seed_model_V * @brief seed the matrix V from an existing model or using rand - * @ingroup libMSVMMaj * + * @details * The matrix V must be seeded before the main_loop() can start. * This can be done by either seeding it with random numbers or * using the solution from a previous model on the same dataset @@ -170,8 +210,8 @@ void msvmmaj_calculate_huber(struct MajModel *model) * significant improvement in the number of iterations necessary * because the seeded model V is closer to the optimal V. * - * @param [in] from_model model from which to copy V - * @param [in,out] to_model model to which V will be copied + * @param[in] from_model MajModel from which to copy V + * @param[in,out] to_model MajModel to which V will be copied */ void msvmmaj_seed_model_V(struct MajModel *from_model, struct MajModel *to_model) { @@ -193,10 +233,17 @@ void msvmmaj_seed_model_V(struct MajModel *from_model, struct MajModel *to_model } } -/*! - * Step doubling +/** + * @brief Use step doubling + * + * @details + * Step doubling can be used to speed up the Majorization algorithm. Instead + * of using the value at the minimimum of the majorization function, the value + * ``opposite'' the majorization point is used. This can essentially cut the + * number of iterations necessary to reach the minimum in half. + * + * @param[in] model MajModel containing the augmented parameters */ - void msvmmaj_step_doubling(struct MajModel *model) { long i, j; @@ -207,15 +254,33 @@ void msvmmaj_step_doubling(struct MajModel *model) for (i=0; i<m+1; i++) { for (j=0; j<K-1; j++) { matrix_mul(model->V, K-1, i, j, 2.0); - matrix_add(model->V, K-1, i, j, -matrix_get(model->Vbar, K-1, i, j)); + matrix_add(model->V, K-1, i, j, + -matrix_get(model->Vbar, K-1, i, j)); } } } -/*! - * initialize_weights +/** + * @brief Initialize instance weights + * + * @details + * Instance weights can for instance be used to add additional weights to + * instances of certain classes. Two default weight possibilities are + * implemented here. The first is unit weights, where each instance gets + * weight 1. + * + * The second are group size correction weights, which are calculated as + * @f[ + * \rho_i = \frac{n}{Kn_k} , + * @f] + * where @f$ n_k @f$ is the number of instances in group @f$ k @f$ and + * @f$ y_i = k @f$. + * + * @param[in] data MajData with the dataset + * @param[in,out] model MajModel with the weight specification. On + * exit MajModel::rho contains the instance + * weights. */ - void msvmmaj_initialize_weights(struct MajData *data, struct MajModel *model) { long *groups; |
