/* Performs some basic tests. Feel free to define additional tests. */ void test_basic() { Row *sparse_matrix; unsigned char small[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; unsigned char empty[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char tiny[] = { 56 }; unsigned char sample[] = { 0xFF, 0x01, 0x02, 0xFF, 0XFF, 0XFF, 0x03, 0x04, 0x05, 0x06, 0xFF, 0x07 }; sparse_matrix = dense_to_sparse(small, 3, 3); test_sparse("small", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(empty, 3, 3); test_sparse("empty", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(tiny, 1, 1); test_sparse("tiny", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(sample, 3, 4); test_sparse("sample", sparse_matrix); free_sparse(sparse_matrix); }
int main(int argc, char **argv) { Image img; Row *sparse_matrix; if ( argc != 2 ) { test_basic(); } else { img = load_bmp(argv[1]); sparse_matrix = dense_to_sparse(img.data, img.width, img.height); test_sparse(argv[1], sparse_matrix); free_sparse(sparse_matrix); } return 0; }
int copy_predict_values (char *predict, struct model *model_, npy_intp *predict_dims, char *dec_values, int nr_class) { npy_intp i; struct feature_node **predict_nodes; predict_nodes = dense_to_sparse((double *) predict, predict_dims, model_->bias); if (predict_nodes == NULL) return -1; for(i=0; i<predict_dims[0]; ++i) { predict_values(model_, predict_nodes[i], ((double *) dec_values) + i*nr_class); free(predict_nodes[i]); } free(predict_nodes); return 0; }
int copy_predict(char *train, struct model *model_, npy_intp *train_dims, char *dec_values) { int *t = (int *) dec_values; int i, n; struct feature_node **train_nodes; n = train_dims[0]; train_nodes = dense_to_sparse((double *) train, train_dims, model_->bias); if (train_nodes == NULL) return -1; for(i=0; i<n; ++i) { *t = predict(model_, train_nodes[i]); free(train_nodes[i]); ++t; } free(train_nodes); return 0; }
int copy_prob_predict(char *predict, struct model *model_, npy_intp *predict_dims, char *dec_values) { struct feature_node **predict_nodes; int i; int n, m; n = predict_dims[0]; m = model_->nr_class; predict_nodes = dense_to_sparse((double *) predict, predict_dims, model_->bias); if (predict_nodes == NULL) return -1; for(i=0; i<n; ++i) { predict_probability(model_, predict_nodes[i], ((double *) dec_values) + i*m); free(predict_nodes[i]); } free(predict_nodes); return 0; }
struct problem * set_problem(char *X,char *Y, npy_intp *dims, double bias) { struct problem *problem; /* not performant but simple */ problem = malloc(sizeof(struct problem)); if (problem == NULL) return NULL; problem->l = (int) dims[0]; if (bias > 0) { problem->n = (int) dims[1] + 1; } else { problem->n = (int) dims[1]; } problem->y = (double *) Y; problem->x = dense_to_sparse((double *) X, dims, bias); problem->bias = bias; if (problem->x == NULL) { free(problem); return NULL; } return problem; }
Sparse(const Dense& dense) { m = dense_to_sparse(dense.m); v = dense_to_sparse(dense.v); }
/* Performs some basic tests. Feel free to define additional tests. */ void test_basic() { Row *sparse_matrix; unsigned char small[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; unsigned char empty[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char tiny[] = { 56 }; unsigned char empty_center[] = { 0x01, 0x02, 0x03, 0x04, 0xFF, 0x06, 0x07, 0x08, 0x09 }; unsigned char white_margins_empty_spots[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0x02, 0x02, 0x02, 0x02, 0xFF, 0xFF, 0x03, 0xFF, 0xFF, 0x03, 0xFF, 0xFF, 0xFF, 0x04, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char theres_a_seven_in_there[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char skips_middle_line[] = { 0x01, 0x02, 0x03, 0xFF, 0xFF, 0xFF, 0x07, 0x08, 0x09 }; sparse_matrix = dense_to_sparse(small, 3, 3); test_sparse("small", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(empty, 3, 3); test_sparse("empty", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(tiny, 1, 1); test_sparse("tiny", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(empty_center, 3, 3); test_sparse("empty_center", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(white_margins_empty_spots, 6, 6); test_sparse("white_margins_empty_spots", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(theres_a_seven_in_there, 6, 6); test_sparse("theres_a_seven_in_there", sparse_matrix); free_sparse(sparse_matrix); sparse_matrix = dense_to_sparse(skips_middle_line, 3, 3); test_sparse("skips_middle_line", sparse_matrix); free_sparse(sparse_matrix); }