From a723e1c62a27aeb39aaf7fcdeb3beb4e89fba32d Mon Sep 17 00:00:00 2001
From: Alexey <AlexeyAB@users.noreply.github.com>
Date: Wed, 15 Aug 2018 20:52:09 +0000
Subject: [PATCH] Merge pull request #766 from HotChick91/AlexeyAB-mask
---
src/matrix.c | 108 +++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 79 insertions(+), 29 deletions(-)
diff --git a/src/matrix.c b/src/matrix.c
index 68e6f8d..08e6109 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -13,16 +13,53 @@
free(m.vals);
}
-double matrix_accuracy(matrix truth, matrix guess)
+float matrix_topk_accuracy(matrix truth, matrix guess, int k)
{
- int k = truth.cols;
- int i;
- int count = 0;
+ int *indexes = calloc(k, sizeof(int));
+ int n = truth.cols;
+ int i,j;
+ int correct = 0;
for(i = 0; i < truth.rows; ++i){
- int class = max_index(guess.vals[i], k);
- if(truth.vals[i][class]) ++count;
+ top_k(guess.vals[i], n, k, indexes);
+ for(j = 0; j < k; ++j){
+ int class_id = indexes[j];
+ if(truth.vals[i][class_id]){
+ ++correct;
+ break;
+ }
+ }
}
- return (double)count/truth.rows;
+ free(indexes);
+ return (float)correct/truth.rows;
+}
+
+void scale_matrix(matrix m, float scale)
+{
+ int i,j;
+ for(i = 0; i < m.rows; ++i){
+ for(j = 0; j < m.cols; ++j){
+ m.vals[i][j] *= scale;
+ }
+ }
+}
+
+matrix resize_matrix(matrix m, int size)
+{
+ int i;
+ if (m.rows == size) return m;
+ if (m.rows < size) {
+ m.vals = realloc(m.vals, size*sizeof(float*));
+ for (i = m.rows; i < size; ++i) {
+ m.vals[i] = calloc(m.cols, sizeof(float));
+ }
+ } else if (m.rows > size) {
+ for (i = size; i < m.rows; ++i) {
+ free(m.vals[i]);
+ }
+ m.vals = realloc(m.vals, size*sizeof(float*));
+ }
+ m.rows = size;
+ return m;
}
void matrix_add_matrix(matrix from, matrix to)
@@ -42,9 +79,9 @@
matrix m;
m.rows = rows;
m.cols = cols;
- m.vals = calloc(m.rows, sizeof(double *));
+ m.vals = calloc(m.rows, sizeof(float *));
for(i = 0; i < m.rows; ++i){
- m.vals[i] = calloc(m.cols, sizeof(double));
+ m.vals[i] = calloc(m.cols, sizeof(float));
}
return m;
}
@@ -55,7 +92,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];
@@ -64,9 +101,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];
@@ -80,30 +117,43 @@
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 matrix_to_csv(matrix m)
+{
+ int i, j;
+
+ for(i = 0; i < m.rows; ++i){
+ for(j = 0; j < m.cols; ++j){
+ if(j > 0) printf(",");
+ printf("%.17g", m.vals[i][j]);
+ }
+ printf("\n");
+ }
}
void print_matrix(matrix m)
--
Gitblit v1.10.0