From d572dc7e667a0e3e95be3f3e72fd6d395e69da6b Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Sat, 13 Jun 2015 05:25:18 +0000
Subject: [PATCH] inet label script
---
src/matrix.c | 79 ++++++++++++++++++++++++++++-----------
1 files changed, 56 insertions(+), 23 deletions(-)
diff --git a/src/matrix.c b/src/matrix.c
index 562a364..2e7001e 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -13,14 +13,47 @@
free(m.vals);
}
+float matrix_topk_accuracy(matrix truth, matrix guess, int k)
+{
+ int *indexes = calloc(k, sizeof(int));
+ int n = truth.cols;
+ int i,j;
+ int correct = 0;
+ for(i = 0; i < truth.rows; ++i){
+ top_k(guess.vals[i], n, k, indexes);
+ for(j = 0; j < k; ++j){
+ int class = indexes[j];
+ if(truth.vals[i][class]){
+ ++correct;
+ break;
+ }
+ }
+ }
+ free(indexes);
+ return (float)correct/truth.rows;
+}
+
+void matrix_add_matrix(matrix from, matrix to)
+{
+ assert(from.rows == to.rows && from.cols == to.cols);
+ int i,j;
+ for(i = 0; i < from.rows; ++i){
+ for(j = 0; j < from.cols; ++j){
+ to.vals[i][j] += from.vals[i][j];
+ }
+ }
+}
+
matrix make_matrix(int rows, int cols)
{
+ int i;
matrix m;
m.rows = rows;
m.cols = cols;
- m.vals = calloc(m.rows, sizeof(double *));
- int i;
- for(i = 0; i < m.rows; ++i) m.vals[i] = calloc(m.cols, sizeof(double));
+ m.vals = calloc(m.rows, sizeof(float *));
+ for(i = 0; i < m.rows; ++i){
+ m.vals[i] = calloc(m.cols, sizeof(float));
+ }
return m;
}
@@ -30,7 +63,7 @@
matrix h;
h.rows = n;
h.cols = m->cols;
- h.vals = calloc(h.rows, sizeof(double *));
+ h.vals = calloc(h.rows, sizeof(float *));
for(i = 0; i < n; ++i){
int index = rand()%m->rows;
h.vals[i] = m->vals[index];
@@ -39,9 +72,9 @@
return h;
}
-double *pop_column(matrix *m, int c)
+float *pop_column(matrix *m, int c)
{
- double *col = calloc(m->rows, sizeof(double));
+ float *col = calloc(m->rows, sizeof(float));
int i, j;
for(i = 0; i < m->rows; ++i){
col[i] = m->vals[i][c];
@@ -55,30 +88,30 @@
matrix csv_to_matrix(char *filename)
{
- FILE *fp = fopen(filename, "r");
- if(!fp) file_error(filename);
+ FILE *fp = fopen(filename, "r");
+ if(!fp) file_error(filename);
matrix m;
m.cols = -1;
- char *line;
+ char *line;
- int n = 0;
- int size = 1024;
- m.vals = calloc(size, sizeof(double*));
- while((line = fgetl(fp))){
+ int n = 0;
+ int size = 1024;
+ m.vals = calloc(size, sizeof(float*));
+ while((line = fgetl(fp))){
if(m.cols == -1) m.cols = count_fields(line);
- if(n == size){
- size *= 2;
- m.vals = realloc(m.vals, size*sizeof(double*));
- }
- m.vals[n] = parse_fields(line, m.cols);
- free(line);
- ++n;
- }
- m.vals = realloc(m.vals, n*sizeof(double*));
+ if(n == size){
+ size *= 2;
+ m.vals = realloc(m.vals, size*sizeof(float*));
+ }
+ m.vals[n] = parse_fields(line, m.cols);
+ free(line);
+ ++n;
+ }
+ m.vals = realloc(m.vals, n*sizeof(float*));
m.rows = n;
- return m;
+ return m;
}
void print_matrix(matrix m)
--
Gitblit v1.10.0