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.

202 lines
3.9 KiB

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