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.

184 lines
4.3 KiB

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