void normalize_array(float *a, int n) { int i; float mu = mean_array(a,n); float sigma = sqrt(variance_array(a,n)); for(i = 0; i < n; ++i){ a[i] = (a[i] - mu)/sigma; } mu = mean_array(a,n); sigma = sqrt(variance_array(a,n)); }
float variance_array(float *a, int n) { int i; float sum = 0; float mean = mean_array(a, n); for(i = 0; i < n; ++i) sum += (a[i] - mean)*(a[i]-mean); float variance = sum/n; return variance; }
void calculate_loss(float *output, float *delta, int n, float thresh) { int i; float mean = mean_array(output, n); float var = variance_array(output, n); for(i = 0; i < n; ++i){ if(delta[i] > mean + thresh*sqrt(var)) delta[i] = output[i]; else delta[i] = 0; } }
int main (int argc, char* argv[]) { //create a pointer, which will be used to store the array int *A; //check if the user ran the program correctly if (argc < 2) { fprintf(stderr, "Usage: mean_median file1 [file2 ...]\n"); } //if the program is run correctly argv[1] will be the first //command line argument. ie name of the input file char *file = argv[1]; //open file for reading FILE *fp = fopen(file, "rb"); if (fp == NULL) return 1; //call read_array function which will allocate space for A //read the file and put the numbers in array A //the read_array function must return the size of the array int n = read_array(fp, &A); if (n < 0) { return 1; } //print array A of size n print_array(A, n); //sort select_sort(A, n); //print again print_array(A, n); //calculate mean and median of array A double mean = mean_array(A, n); double med = median_array(A, n); //print results printf("mean = %f med = %f\n", mean, med); //cleanup free(A); fclose(fp); return 0; }
void print_network(network net) { int i,j; for(i = 0; i < net.n; ++i){ layer l = net.layers[i]; float *output = l.output; int n = l.outputs; float mean = mean_array(output, n); float vari = variance_array(output, n); fprintf(stderr, "Layer %d - Mean: %f, Variance: %f\n",i,mean, vari); if(n > 100) n = 100; for(j = 0; j < n; ++j) fprintf(stderr, "%f, ", output[j]); if(n == 100)fprintf(stderr,".....\n"); fprintf(stderr, "\n"); } }
void print_statistics(float *a, int n) { float m = mean_array(a, n); float v = variance_array(a, n); printf("MSE: %.6f, Mean: %.6f, Variance: %.6f\n", mse_array(a, n), m, v); }