summaryrefslogtreecommitdiff
path: root/OASIS/c/src/ekd_svm.h
blob: 259eb5cb0d1469938e8b12ced37677621fa2d337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef EKD_SVM_H
#define EKD_SVM_H

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>

#include "ekd_pca.h"
#include "ekd_blas.h"

#define NUM_DNEG_IMAGES 134
#define NUM_DPOS_IMAGES 99
#define NUM_IMAGES 233
#define IMAGE_WIDTH 144
#define IMAGE_HEIGHT 176
#define FEATURE_VECTOR_SIZE 25344
#define MAX_SVM_PASSES 100
#define GAMMA 0.9
#define SVM_TOLERANCE 1e-3

void svm_preprocess(float** input_matrix, uint32_t rows, uint32_t cols);
float rbf_kernel(float* sample_1, float* sample_2, uint32_t features, float gamma);
void kernel_matrix_transform(float** input_matrix, float** resultant_matrix, uint32_t samples, uint32_t features, float gamma);
void kernel_row_transform(float** input_matrix, float* query_row, float* resultant_row, uint32_t samples, uint32_t features, float gamma);
float get_error(uint32_t index, float* alphas, float** kernel_matrix, int32_t* labels, uint32_t num_samples);
uint32_t find_second_alpha(uint32_t first_index, float first_error_val, float* alphas, float** kernel_matrix, int32_t* labels, uint32_t num_samples);
void calculate_bounds(float first_label, float second_label, float first_alpha, float second_alpha, float penalty, float* lower_bound, float* upper_bound);
float clip_second_alpha_new(float second_alpha_new, float lower_bound, float upper_bound);
void update_alphas(float first_alpha_new, float second_alpha_new, float* alphas, uint32_t first_index, uint32_t second_index);
float calculate_bias(float first_error_val, float second_error_val, float first_alpha_new, float first_alpha, float second_alpha_new, float second_alpha, float kernel_val_ii, float kernel_val_ij, float kernel_val_jj, float first_label, float second_label, float penalty);
void update_errors(float* errors, uint32_t num_samples, uint32_t first_index, uint32_t second_index, float first_label, float second_label, float first_alpha_new, float first_alpha, float second_alpha_new, float second_alpha, float* bias, float** kernel_matrix);
uint8_t optimize_alphas(uint32_t first_index, uint32_t second_index, float* alphas, float* bias, float* errors, float** kernel_matrix, int32_t* labels, uint32_t num_samples, float penalty);
uint8_t verify_kkt(uint32_t first_index, float tolerance, float* alphas, float* bias, float* errors, float** kernel_matrix, int32_t* labels, uint32_t num_samples, float penalty);
void train_svm(float** data_matrix, uint32_t samples, uint32_t features, float* alphas, float* bias, int32_t* labels, float penalty);
float test_svm(float** data_test, int32_t* labels_test, uint32_t num_test, uint32_t num_components, float* alphas, float bias);

#endif /* EKD_SVM_H */