aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGertjan van den Burg <burg@ese.eur.nl>2014-06-12 11:49:39 +0200
committerGertjan van den Burg <burg@ese.eur.nl>2014-06-12 11:49:39 +0200
commitf226aea54c5d0b64afe4ab07ecc27d639b94c1e1 (patch)
treef9cecc05999f48a6dcdccf2007ee1db0c74f86cd
parentfixed merge (diff)
downloadgensvm-f226aea54c5d0b64afe4ab07ecc27d639b94c1e1.tar.gz
gensvm-f226aea54c5d0b64afe4ab07ecc27d639b94c1e1.zip
work on reallocation of model and work on bugfix free
-rw-r--r--include/msvmmaj_train_dataset.h2
-rw-r--r--src/libMSVMMaj.c6
-rw-r--r--src/msvmmaj_kernel.c1
-rw-r--r--src/msvmmaj_pred.c1
-rw-r--r--src/msvmmaj_train.c16
-rw-r--r--src/msvmmaj_train_dataset.c121
-rw-r--r--src/trainMSVMMajdataset.c5
7 files changed, 105 insertions, 47 deletions
diff --git a/include/msvmmaj_train_dataset.h b/include/msvmmaj_train_dataset.h
index 2afcde8..0889626 100644
--- a/include/msvmmaj_train_dataset.h
+++ b/include/msvmmaj_train_dataset.h
@@ -135,7 +135,7 @@ double cross_validation(struct MajModel *model, struct MajData *data,
void make_model_from_task(struct Task *task, struct MajModel *model);
void copy_model(struct MajModel *from, struct MajModel *to);
-void msvmmaj_reallocate_model(struct MajModel *model, long n);
+void msvmmaj_reallocate_model(struct MajModel *model, long n, long m);
void print_progress_string(struct Task *task, long N);
#endif
diff --git a/src/libMSVMMaj.c b/src/libMSVMMaj.c
index a0bef97..65ff33d 100644
--- a/src/libMSVMMaj.c
+++ b/src/libMSVMMaj.c
@@ -126,7 +126,8 @@ void msvmmaj_simplex_diff(struct MajModel *model, struct MajData *data)
* calculated with MajModel::V.
*
*/
-void msvmmaj_calculate_errors(struct MajModel *model, struct MajData *data, double *ZV)
+void msvmmaj_calculate_errors(struct MajModel *model, struct MajData *data,
+ double *ZV)
{
long i, j, k;
double a, value;
@@ -156,7 +157,8 @@ void msvmmaj_calculate_errors(struct MajModel *model, struct MajData *data, doub
for (j=0; j<K-1; j++) {
a = matrix_get(ZV, K-1, i, j);
for (k=0; k<K; k++) {
- value = a * matrix3_get(model->UU, K-1, K, i, j, k);
+ value = a * matrix3_get(model->UU, K-1, K, i,
+ j, k);
matrix_add(model->Q, K, i, k, value);
}
}
diff --git a/src/msvmmaj_kernel.c b/src/msvmmaj_kernel.c
index a2c1193..edcd68f 100644
--- a/src/msvmmaj_kernel.c
+++ b/src/msvmmaj_kernel.c
@@ -118,7 +118,6 @@ void msvmmaj_make_kernel(struct MajModel *model, struct MajData *data)
long num_eigen = msvmmaj_make_eigen(K, n, &P, &Sigma);
//printf("num eigen: %li\n", num_eigen);
data->m = num_eigen;
- model->m = num_eigen;
// copy eigendecomp to data
data->Z = Calloc(double, n*(num_eigen+1));
diff --git a/src/msvmmaj_pred.c b/src/msvmmaj_pred.c
index b551f12..6e2b3b9 100644
--- a/src/msvmmaj_pred.c
+++ b/src/msvmmaj_pred.c
@@ -196,7 +196,6 @@ void msvmmaj_predict_labels_kernel(struct MajData *data_test,
}
}
predy[i] = label;
- //printf("predy[%li] = %li\n", i, label);
}
free(ZV);
diff --git a/src/msvmmaj_train.c b/src/msvmmaj_train.c
index 0f42ff6..2333544 100644
--- a/src/msvmmaj_train.c
+++ b/src/msvmmaj_train.c
@@ -84,7 +84,7 @@ void msvmmaj_optimize(struct MajModel *model, struct MajData *data)
msvmmaj_get_update(model, data, B, ZAZ, ZAZV, ZAZVT);
if (it > 50)
msvmmaj_step_doubling(model);
-
+
Lbar = L;
L = msvmmaj_get_loss(model, data, ZV);
@@ -217,6 +217,10 @@ double msvmmaj_get_loss(struct MajModel *model, struct MajData *data,
* @f]
* solving this system is done through dposv().
*
+ * @todo
+ * Consider allocating IPIV and WORK at a higher level, they probably don't
+ * change much during the iterations.
+ *
* @param [in,out] model model to be updated
* @param [in] data data used in model
* @param [in] B pre-allocated matrix used for linear coefficients
@@ -408,6 +412,7 @@ void msvmmaj_get_update(struct MajModel *model, struct MajData *data, double *B,
0.0,
ZAZV,
K-1);
+
cblas_dgemm(
CblasRowMajor,
CblasTrans,
@@ -446,6 +451,7 @@ void msvmmaj_get_update(struct MajModel *model, struct MajData *data, double *B,
// because we have used the upper part in the BLAS
// calls above in Row-major order, and Lapack uses
// column major order.
+
status = dposv(
'L',
m+1,
@@ -489,10 +495,12 @@ void msvmmaj_get_update(struct MajModel *model, struct MajData *data, double *B,
if (status != 0)
fprintf(stderr, "Received nonzero status from "
"dsysv: %i\n", status);
+ free(WORK);
+ free(IPIV);
}
- // Return to Row-major order. The matrix ZAZVT contains the
- // solution after the dposv/dsysv call.
+ // Return to Row-major order. The matrix ZAZVT contains the solution
+ // after the dposv/dsysv call.
for (i=0; i<m+1; i++)
for (j=0; j<K-1; j++)
ZAZV[i*(K-1)+j] = ZAZVT[j*(m+1)+i];
@@ -509,7 +517,7 @@ void msvmmaj_get_update(struct MajModel *model, struct MajData *data, double *B,
model->V = ZAZVT;
ZAZVT = ptr;
*/
-
+
for (i=0; i<m+1; i++) {
for (j=0; j<K-1; j++) {
matrix_set(
diff --git a/src/msvmmaj_train_dataset.c b/src/msvmmaj_train_dataset.c
index 336be69..29b410b 100644
--- a/src/msvmmaj_train_dataset.c
+++ b/src/msvmmaj_train_dataset.c
@@ -330,9 +330,13 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype)
task = get_next_task(nq);
make_model_from_task(task, model);
- model->n = task->train_data->n;
- model->m = task->train_data->m;
- model->K = task->train_data->K;
+ if (i == 0) {
+ model->n = 0;
+ model->m = task->train_data->m;
+ model->K = task->train_data->K;
+ msvmmaj_allocate_model(model);
+ msvmmaj_seed_model_V(NULL, model);
+ }
time[i] = 0.0;
note("(%02li/%02li:%03li)\t", i+1, N, task->ID);
@@ -401,6 +405,8 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype)
p += 1.0;
}
+ free(nq->tasks);
+ free(nq);
free(task);
free(model);
free(perf);
@@ -448,13 +454,13 @@ double cross_validation(struct MajModel *model, struct MajData *data,
msvmmaj_make_cv_split(data->n, folds, cv_idx);
for (f=0; f<folds; f++) {
- //printf("Fold: %li\n", f);
+ note(".");
msvmmaj_get_tt_split(data, train_data, test_data, cv_idx, f);
msvmmaj_make_kernel(model, train_data);
// reallocate the model if necessary for the new train split
- msvmmaj_reallocate_model(model, train_data->n);
+ msvmmaj_reallocate_model(model, train_data->n, train_data->m);
msvmmaj_initialize_weights(train_data, model);
@@ -466,7 +472,7 @@ double cross_validation(struct MajModel *model, struct MajData *data,
// calculate prediction performance on test set
predy = Calloc(long, test_data->n);
- msvmmaj_predict_labels(test_data, model, predy);
+ msvmmaj_predict_labels(test_data, train_data, model, predy);
performance = msvmmaj_prediction_perf(test_data, predy);
total_perf += performance * test_data->n;
@@ -540,44 +546,72 @@ void start_training_cv(struct Queue *q)
msvmmaj_free_model(model);
}
-void msvmmaj_reallocate_model(struct MajModel *model, long n)
+void msvmmaj_reallocate_model(struct MajModel *model, long n, long m)
{
long K = model->K;
-
- model->UU = (double *) realloc(model->UU, n*K*(K-1)*sizeof(double));
- if (model->UU == NULL) {
- fprintf(stderr, "Failed to reallocate UU\n");
- exit(1);
- }
-
- model->Q = (double *) realloc(model->Q, n*K*sizeof(double));
- if (model->Q == NULL) {
- fprintf(stderr, "Failed to reallocate Q\n");
- exit(1);
- }
-
- model->H = (double *) realloc(model->H, n*K*sizeof(double));
- if (model->H == NULL) {
- fprintf(stderr, "Failed to reallocate H\n");
- exit(1);
+
+ if (model->n == n && model->m == m)
+ return;
+ if (model->n != n) {
+ model->UU = (double *) realloc(model->UU,
+ n*K*(K-1)*sizeof(double));
+ if (model->UU == NULL) {
+ fprintf(stderr, "Failed to reallocate UU\n");
+ exit(1);
+ }
+
+ model->Q = (double *) realloc(model->Q, n*K*sizeof(double));
+ if (model->Q == NULL) {
+ fprintf(stderr, "Failed to reallocate Q\n");
+ exit(1);
+ }
+
+ model->H = (double *) realloc(model->H, n*K*sizeof(double));
+ if (model->H == NULL) {
+ fprintf(stderr, "Failed to reallocate H\n");
+ exit(1);
+ }
+
+ model->R = (double *) realloc(model->R, n*K*sizeof(double));
+ if (model->R == NULL) {
+ fprintf(stderr, "Failed to reallocate R\n");
+ exit(1);
+ }
+
+ model->rho = (double *) realloc(model->rho, n*sizeof(double));
+ if (model->rho == NULL) {
+ fprintf(stderr, "Failed to reallocte rho\n");
+ exit(1);
+ }
+
+ model->n = n;
}
+ if (model->m != m) {
+ model->W = (double *) realloc(model->W,
+ m*(K-1)*sizeof(double));
+ if (model->W == NULL) {
+ fprintf(stderr, "Failed to reallocate W\n");
+ exit(1);
+ }
- model->R = (double *) realloc(model->R, n*K*sizeof(double));
- if (model->R == NULL) {
- fprintf(stderr, "Failed to reallocate R\n");
- exit(1);
- }
+ model->V = (double *) realloc(model->V,
+ (m+1)*(K-1)*sizeof(double));
+ if (model->V == NULL) {
+ fprintf(stderr, "Failed to reallocate V\n");
+ exit(1);
+ }
- model->rho = (double *) realloc(model->rho, n*sizeof(double));
- if (model->rho == NULL) {
- fprintf(stderr, "Failed to reallocte rho\n");
- exit(1);
+ model->Vbar = (double *) realloc(model->Vbar,
+ (m+1)*(K-1)*sizeof(double));
+ if (model->Vbar == NULL) {
+ fprintf(stderr, "Failed to reallocate Vbar\n");
+ exit(1);
+ }
+
+ model->m = m;
}
-
- model->n = n;
}
-
-
+
/**
* @brief Run the grid search for a train/test dataset
*
@@ -677,13 +711,24 @@ void start_training_tt(struct Queue *q)
*/
void free_queue(struct Queue *q)
{
+ printf("\there 0\n");
long i;
+ printf("\there 1\n");
for (i=0; i<q->N; i++) {
+ printf("\there 2\n");
+ fflush(stdout);
free(q->tasks[i]->kernelparam);
+ printf("\there 3\n");
+ fflush(stdout);
free(q->tasks[i]);
+ printf("\there 4\n");
+ fflush(stdout);
}
+ printf("\there 5\n");
free(q->tasks);
+ printf("\there 6\n");
free(q);
+ printf("\there 7\n");
}
/**
@@ -775,7 +820,7 @@ void print_progress_string(struct Task *task, long N)
task->kernelparam[1]);
if (task->kerneltype == K_POLY || task->kerneltype == K_SIGMOID ||
task->kerneltype == K_RBF)
- sprintf(buffer + strlen(buffer), "g = %2.2f\t",
+ sprintf(buffer + strlen(buffer), "g = %3.3f\t",
task->kernelparam[0]);
sprintf(buffer + strlen(buffer), "eps = %g\tw = %i\tk = %2.2f\t"
"l = %f\tp = %2.2f\t", task->epsilon,
diff --git a/src/trainMSVMMajdataset.c b/src/trainMSVMMajdataset.c
index 1929584..35f4611 100644
--- a/src/trainMSVMMajdataset.c
+++ b/src/trainMSVMMajdataset.c
@@ -112,10 +112,15 @@ int main(int argc, char **argv)
consistency_repeats(q, training->repeats, training->traintype);
}
+ printf("here 0\n");
free_queue(q);
+ printf("here 1\n");
free(training);
+ printf("here 2\n");
msvmmaj_free_data(train_data);
+ printf("here 3\n");
msvmmaj_free_data(test_data);
+ printf("here 4\n");
note("Done.\n");
return 0;