You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "matrix.h"
  2. #include "utils.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <math.h>
  8. void free_matrix(matrix m)
  9. {
  10. int i;
  11. for(i = 0; i < m.rows; ++i) free(m.vals[i]);
  12. free(m.vals);
  13. }
  14. float matrix_topk_accuracy(matrix truth, matrix guess, int k)
  15. {
  16. int *indexes = (int*)calloc(k, sizeof(int));
  17. int n = truth.cols;
  18. int i,j;
  19. int correct = 0;
  20. for(i = 0; i < truth.rows; ++i){
  21. top_k(guess.vals[i], n, k, indexes);
  22. for(j = 0; j < k; ++j){
  23. int cls = indexes[j];
  24. if(truth.vals[i][cls]){
  25. ++correct;
  26. break;
  27. }
  28. }
  29. }
  30. free(indexes);
  31. return (float)correct/truth.rows;
  32. }
  33. void scale_matrix(matrix m, float scale)
  34. {
  35. int i,j;
  36. for(i = 0; i < m.rows; ++i){
  37. for(j = 0; j < m.cols; ++j){
  38. m.vals[i][j] *= scale;
  39. }
  40. }
  41. }
  42. matrix resize_matrix(matrix m, int size)
  43. {
  44. int i;
  45. if (m.rows == size) return m;
  46. if (m.rows < size) {
  47. m.vals = (float**) realloc(m.vals, size*sizeof(float*));
  48. for (i = m.rows; i < size; ++i) {
  49. m.vals[i] = (float*)calloc(m.cols, sizeof(float));
  50. }
  51. } else if (m.rows > size) {
  52. for (i = size; i < m.rows; ++i) {
  53. free(m.vals[i]);
  54. }
  55. m.vals = (float**) realloc(m.vals, size*sizeof(float*));
  56. }
  57. m.rows = size;
  58. return m;
  59. }
  60. void matrix_add_matrix(matrix from, matrix to)
  61. {
  62. assert(from.rows == to.rows && from.cols == to.cols);
  63. int i,j;
  64. for(i = 0; i < from.rows; ++i){
  65. for(j = 0; j < from.cols; ++j){
  66. to.vals[i][j] += from.vals[i][j];
  67. }
  68. }
  69. }
  70. matrix make_matrix(int rows, int cols)
  71. {
  72. int i;
  73. matrix m;
  74. m.rows = rows;
  75. m.cols = cols;
  76. m.vals = (float**)calloc(m.rows, sizeof(float *));
  77. for(i = 0; i < m.rows; ++i){
  78. m.vals[i] = (float*)calloc(m.cols, sizeof(float));
  79. }
  80. return m;
  81. }
  82. matrix hold_out_matrix(matrix *m, int n)
  83. {
  84. int i;
  85. matrix h;
  86. h.rows = n;
  87. h.cols = m->cols;
  88. h.vals = (float**)calloc(h.rows, sizeof(float *));
  89. for(i = 0; i < n; ++i){
  90. int index = rand()%m->rows;
  91. h.vals[i] = m->vals[index];
  92. m->vals[index] = m->vals[--(m->rows)];
  93. }
  94. return h;
  95. }
  96. float *pop_column(matrix *m, int c)
  97. {
  98. float *col = (float*)calloc(m->rows, sizeof(float));
  99. int i, j;
  100. for(i = 0; i < m->rows; ++i){
  101. col[i] = m->vals[i][c];
  102. for(j = c; j < m->cols-1; ++j){
  103. m->vals[i][j] = m->vals[i][j+1];
  104. }
  105. }
  106. --m->cols;
  107. return col;
  108. }
  109. matrix csv_to_matrix(char *filename)
  110. {
  111. FILE *fp = fopen(filename, "r");
  112. if(!fp) file_error(filename);
  113. struct S_matrix m;
  114. m.cols = -1;
  115. char *line;
  116. int n = 0;
  117. int size = 1024;
  118. m.vals = (float**)calloc(size, sizeof(float*));
  119. while((line = fgetl(fp))){
  120. if(m.cols == -1) m.cols = count_fields(line);
  121. if(n == size){
  122. size *= 2;
  123. m.vals = (float**)realloc(m.vals, size*sizeof(float*));
  124. }
  125. m.vals[n] = parse_fields(line, m.cols);
  126. free(line);
  127. ++n;
  128. }
  129. m.vals = (float**)realloc(m.vals, n*sizeof(float*));
  130. m.rows = n;
  131. return m;
  132. }
  133. void matrix_to_csv(matrix m)
  134. {
  135. int i, j;
  136. for(i = 0; i < m.rows; ++i){
  137. for(j = 0; j < m.cols; ++j){
  138. if(j > 0) printf(",");
  139. printf("%.17g", m.vals[i][j]);
  140. }
  141. printf("\n");
  142. }
  143. }
  144. void print_matrix(matrix m)
  145. {
  146. int i, j;
  147. printf("%d X %d Matrix:\n",m.rows, m.cols);
  148. printf(" __");
  149. for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
  150. printf("__ \n");
  151. printf("| ");
  152. for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
  153. printf(" |\n");
  154. for(i = 0; i < m.rows; ++i){
  155. printf("| ");
  156. for(j = 0; j < m.cols; ++j){
  157. printf("%15.7f ", m.vals[i][j]);
  158. }
  159. printf(" |\n");
  160. }
  161. printf("|__");
  162. for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
  163. printf("__|\n");
  164. }