diff options
| author | Gertjan van den Burg <burg@ese.eur.nl> | 2015-02-20 16:39:52 +0100 |
|---|---|---|
| committer | Gertjan van den Burg <burg@ese.eur.nl> | 2015-02-20 16:39:52 +0100 |
| commit | 6fa450d7d56cf3535648fb02f92f042df6c928fc (patch) | |
| tree | 74b2f523913a9a891d8be64e39abc3c9219b33ac | |
| parent | added init and free functions for training struct and queue (diff) | |
| download | gensvm-6fa450d7d56cf3535648fb02f92f042df6c928fc.tar.gz gensvm-6fa450d7d56cf3535648fb02f92f042df6c928fc.zip | |
fixed some memory leaks and made prctile actual percentile
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/gensvm_train_dataset.h | 6 | ||||
| -rw-r--r-- | src/gensvm_init.c | 1 | ||||
| -rw-r--r-- | src/gensvm_train_dataset.c | 23 |
4 files changed, 22 insertions, 10 deletions
@@ -1,6 +1,6 @@ VERSION=0.1 CC=gcc -CFLAGS=-Wall -O3 -DVERSION=$(VERSION) +CFLAGS=-Wall -O3 -DVERSION=$(VERSION) -g INCLUDE= -Iinclude LIB= -Llib diff --git a/include/gensvm_train_dataset.h b/include/gensvm_train_dataset.h index 0dc4319..d201aa5 100644 --- a/include/gensvm_train_dataset.h +++ b/include/gensvm_train_dataset.h @@ -18,6 +18,10 @@ #include "globals.h" #include "types.h" +// forward declarations +struct GenData; +struct GenModel; + /** * @brief A structure for a single task in the queue. * @@ -142,6 +146,4 @@ void start_training(struct Queue *q); double gensvm_cross_validation(struct GenModel *model, struct GenData **train_folds, struct GenData **test_folds, int folds, long n_total); - - #endif diff --git a/src/gensvm_init.c b/src/gensvm_init.c index 45373f1..6228d9a 100644 --- a/src/gensvm_init.c +++ b/src/gensvm_init.c @@ -305,6 +305,7 @@ void gensvm_free_data(struct GenData *data) free(data->Z); free(data->RAW); } + free(data->kernelparam); free(data->y); free(data->Sigma); free(data); diff --git a/src/gensvm_train_dataset.c b/src/gensvm_train_dataset.c index eee4bf9..f0c4e93 100644 --- a/src/gensvm_train_dataset.c +++ b/src/gensvm_train_dataset.c @@ -63,12 +63,12 @@ void make_queue(struct Training *training, struct Queue *queue, N *= training->Nc > 0 ? training->Nc : 1; N *= training->Nd > 0 ? training->Nd : 1; - queue->tasks = Malloc(struct Task *, N); + queue->tasks = Calloc(struct Task *, N); queue->N = N; // initialize all tasks for (i=0; i<N; i++) { - task = Malloc(struct Task, 1); + task = Calloc(struct Task, 1); task->ID = i; task->train_data = train_data; task->test_data = test_data; @@ -232,11 +232,14 @@ int doublesort(const void *elem1, const void *elem2) * * @param[in] values array of doubles * @param[in] N length of the array - * @param[in] p percentile to calculate ( 0 <= p <= 1.0 ). + * @param[in] p percentile to calculate ( 0 <= p <= 100.0 ). * @returns the p-th percentile of the values */ double prctile(double *values, long N, double p) { + if (N == 1) + return values[0]; + long i; double pi, pr, boundary; double *local = Malloc(double, N); @@ -244,6 +247,7 @@ double prctile(double *values, long N, double p) local[i] = values[i]; qsort(local, N, sizeof(double), doublesort); + p /= 100.0; p = p*N + 0.5; pi = maximum(minimum(floor(p), N-1), 1); pr = maximum(minimum(p - pi, 1), 0); @@ -383,9 +387,9 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype) p = 0.0; bool breakout = false; while (breakout == false) { - pi = prctile(mean, N, (100.0-p)/100.0); - pr = prctile(std, N, p/100.0); - pt = prctile(time, N, p/100.0); + pi = prctile(mean, N, (100.0-p)); + pr = prctile(std, N, p); + pt = prctile(time, N, p); for (i=0; i<N; i++) if ((pi - mean[i] < 0.0001) && (std[i] - pr < 0.0001) && @@ -408,9 +412,11 @@ void consistency_repeats(struct Queue *q, long repeats, TrainType traintype) p += 1.0; } + // make sure no double free occurs with the copied kernelparam + model->kernelparam = NULL; + gensvm_free_model(model); free(nq->tasks); free(nq); - free(model); free(perf); free(std); free(mean); @@ -640,6 +646,8 @@ void start_training(struct Queue *q) note("\nTotal elapsed training time: %8.8f seconds\n", elapsed_time(main_s, main_e)); + // make sure no double free occurs with the copied kernelparam + model->kernelparam = NULL; gensvm_free_model(model); for (f=0; f<folds; f++) { gensvm_free_data(train_folds[f]); @@ -647,6 +655,7 @@ void start_training(struct Queue *q) } free(train_folds); free(test_folds); + free(cv_idx); } |
