Prechádzať zdrojové kódy

Save each adjustment step as a ppm image

rexim 3 rokov pred
rodič
commit
5d84174d9e
3 zmenil súbory, kde vykonal 39 pridanie a 14 odobranie
  1. 1 1
      .gitignore
  2. 10 4
      config.h
  3. 28 9
      main.c

+ 1 - 1
.gitignore

@@ -1,2 +1,2 @@
 main
-*.ppm
+data/

+ 10 - 4
config.h

@@ -1,11 +1,17 @@
 #ifndef CONFIG_H_
 #define CONFIG_H_
 
-#define WIDTH 50
-#define HEIGHT 50
+#define WIDTH 20
+#define HEIGHT 20
 #define PPM_SCALER 25
-#define BIAS 10.0
-#define SAMPLE_SIZE 2000
+#define PPM_COLOR_INTENSITY 200
+#define BIAS 20.0
+#define SAMPLE_SIZE 25
 #define TRAIN_PASSES 2000
 
+#define DATA_FOLDER "data"
+
+#define TRAIN_SEED 69
+#define CHECK_SEED 420
+
 #endif // CONFIG_H_

+ 28 - 9
main.c

@@ -8,6 +8,9 @@
 #include <limits.h>
 #include <float.h>
 
+#include <sys/stat.h>
+#include <sys/types.h>
+
 #include "./config.h"
 
 typedef float Layer[HEIGHT][WIDTH];
@@ -76,9 +79,9 @@ void layer_save_as_ppm(Layer layer, const char *file_path)
         for (int x = 0; x < WIDTH * PPM_SCALER; ++x) {
             float s = (layer[y / PPM_SCALER][x / PPM_SCALER] - min) / (max - min);
             char pixel[3] = {
-                (char) floorf(255 * (1.0f - s)),
-                (char) floorf(255 * s),
-                0
+                (char) floorf(PPM_COLOR_INTENSITY * (1.0f - s)),
+                (char) floorf(PPM_COLOR_INTENSITY * s),
+                0,
             };
 
             fwrite(pixel, sizeof(pixel), 1, f);
@@ -177,18 +180,27 @@ void layer_random_circle(Layer layer)
 
 int train_pass(Layer inputs, Layer weights)
 {
+    static char file_path[256];
+    static int count = 0;
+
     int adjusted = 0;
 
     for (int i = 0; i < SAMPLE_SIZE; ++i) {
         layer_random_rect(inputs);
         if (feed_forward(inputs, weights) > BIAS) {
             sub_inputs_from_weights(inputs, weights);
+            snprintf(file_path, sizeof(file_path), DATA_FOLDER"/weights-%03d.ppm", count++);
+            printf("[INFO] saving %s\n", file_path);
+            layer_save_as_ppm(weights, file_path);
             adjusted += 1;
         }
 
         layer_random_circle(inputs);
         if (feed_forward(inputs, weights) < BIAS) {
             add_inputs_from_weights(inputs, weights);
+            snprintf(file_path, sizeof(file_path), DATA_FOLDER"/weights-%03d.ppm", count++);
+            printf("[INFO] saving %s\n", file_path);
+            layer_save_as_ppm(weights, file_path);
             adjusted += 1;
         }
     }
@@ -220,20 +232,27 @@ static Layer weights;
 
 int main(void)
 {
-    srand(420);
+    printf("[INFO] creating %s\n", DATA_FOLDER);
+    if (mkdir(DATA_FOLDER, 0755) < 0 && errno != EEXIST) {
+        fprintf(stderr, "ERROR: could not create folder %s: %s", DATA_FOLDER,
+                strerror(errno));
+        exit(1);
+    }
+
+    srand(CHECK_SEED);
     int adj = check_pass(inputs, weights);
-    printf("The fail rate of untrained model is %f\n", adj / (SAMPLE_SIZE * 2.0));
+    printf("[INFO] fail rate of untrained model is %f\n", adj / (SAMPLE_SIZE * 2.0));
 
     for (int i = 0; i < TRAIN_PASSES; ++i) {
-        srand(69);
+        srand(TRAIN_SEED);
         int adj = train_pass(inputs, weights);
-        printf("adjusted %d times\n", adj);
+        printf("[INFO] Pass %d: adjusted %d times\n", i, adj);
         if (adj <= 0) break;
     }
 
-    srand(420);
+    srand(CHECK_SEED);
     adj = check_pass(inputs, weights);
-    printf("The fail rate of trained model is %f\n", adj / (SAMPLE_SIZE * 2.0));
+    printf("[INFO] fail rate of trained model is %f\n", adj / (SAMPLE_SIZE * 2.0));
 
     return 0;
 }