From a392bbd0c957a00e3782c96e7ced84a29ff9dd88 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Tue, 15 Mar 2016 05:33:02 +0000
Subject: [PATCH] Play along w/ alphago

---
 src/network.c |   84 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/src/network.c b/src/network.c
index 7f19318..e6fb51e 100644
--- a/src/network.c
+++ b/src/network.c
@@ -8,10 +8,13 @@
 
 #include "crop_layer.h"
 #include "connected_layer.h"
+#include "rnn_layer.h"
+#include "crnn_layer.h"
+#include "local_layer.h"
 #include "convolutional_layer.h"
+#include "activation_layer.h"
 #include "deconvolutional_layer.h"
 #include "detection_layer.h"
-#include "region_layer.h"
 #include "normalization_layer.h"
 #include "maxpool_layer.h"
 #include "avgpool_layer.h"
@@ -19,6 +22,7 @@
 #include "softmax_layer.h"
 #include "dropout_layer.h"
 #include "route_layer.h"
+#include "shortcut_layer.h"
 
 int get_current_batch(network net)
 {
@@ -26,6 +30,17 @@
     return batch_num;
 }
 
+void reset_momentum(network net)
+{
+    if (net.momentum == 0) return;
+    net.learning_rate = 0;
+    net.momentum = 0;
+    net.decay = 0;
+    #ifdef GPU
+        if(gpu_index >= 0) update_network_gpu(net);
+    #endif
+}
+
 float get_current_rate(network net)
 {
     int batch_num = get_current_batch(net);
@@ -41,6 +56,7 @@
             for(i = 0; i < net.num_steps; ++i){
                 if(net.steps[i] > batch_num) return rate;
                 rate *= net.scales[i];
+                if(net.steps[i] > batch_num - 1) reset_momentum(net);
             }
             return rate;
         case EXP:
@@ -60,10 +76,18 @@
     switch(a){
         case CONVOLUTIONAL:
             return "convolutional";
+        case ACTIVE:
+            return "activation";
+        case LOCAL:
+            return "local";
         case DECONVOLUTIONAL:
             return "deconvolutional";
         case CONNECTED:
             return "connected";
+        case RNN:
+            return "rnn";
+        case CRNN:
+            return "crnn";
         case MAXPOOL:
             return "maxpool";
         case AVGPOOL:
@@ -72,8 +96,6 @@
             return "softmax";
         case DETECTION:
             return "detection";
-        case REGION:
-            return "region";
         case DROPOUT:
             return "dropout";
         case CROP:
@@ -82,6 +104,8 @@
             return "cost";
         case ROUTE:
             return "route";
+        case SHORTCUT:
+            return "shortcut";
         case NORMALIZATION:
             return "normalization";
         default:
@@ -107,6 +131,7 @@
 {
     int i;
     for(i = 0; i < net.n; ++i){
+        state.index = i;
         layer l = net.layers[i];
         if(l.delta){
             scal_cpu(l.outputs * l.batch, 0, l.delta, 1);
@@ -115,14 +140,20 @@
             forward_convolutional_layer(l, state);
         } else if(l.type == DECONVOLUTIONAL){
             forward_deconvolutional_layer(l, state);
+        } else if(l.type == ACTIVE){
+            forward_activation_layer(l, state);
+        } else if(l.type == LOCAL){
+            forward_local_layer(l, state);
         } else if(l.type == NORMALIZATION){
             forward_normalization_layer(l, state);
         } else if(l.type == DETECTION){
             forward_detection_layer(l, state);
-        } else if(l.type == REGION){
-            forward_region_layer(l, state);
         } else if(l.type == CONNECTED){
             forward_connected_layer(l, state);
+        } else if(l.type == RNN){
+            forward_rnn_layer(l, state);
+        } else if(l.type == CRNN){
+            forward_crnn_layer(l, state);
         } else if(l.type == CROP){
             forward_crop_layer(l, state);
         } else if(l.type == COST){
@@ -137,6 +168,8 @@
             forward_dropout_layer(l, state);
         } else if(l.type == ROUTE){
             forward_route_layer(l, net);
+        } else if(l.type == SHORTCUT){
+            forward_shortcut_layer(l, state);
         }
         state.input = l.output;
     }
@@ -155,6 +188,12 @@
             update_deconvolutional_layer(l, rate, net.momentum, net.decay);
         } else if(l.type == CONNECTED){
             update_connected_layer(l, update_batch, rate, net.momentum, net.decay);
+        } else if(l.type == RNN){
+            update_rnn_layer(l, update_batch, rate, net.momentum, net.decay);
+        } else if(l.type == CRNN){
+            update_crnn_layer(l, update_batch, rate, net.momentum, net.decay);
+        } else if(l.type == LOCAL){
+            update_local_layer(l, update_batch, rate, net.momentum, net.decay);
         }
     }
 }
@@ -173,14 +212,10 @@
     int count = 0;
     for(i = 0; i < net.n; ++i){
         if(net.layers[i].type == COST){
-            sum += net.layers[i].output[0];
-            ++count;
-        }
-        if(net.layers[i].type == DETECTION){
             sum += net.layers[i].cost[0];
             ++count;
         }
-        if(net.layers[i].type == REGION){
+        if(net.layers[i].type == DETECTION){
             sum += net.layers[i].cost[0];
             ++count;
         }
@@ -201,6 +236,7 @@
     float *original_input = state.input;
     float *original_delta = state.delta;
     for(i = net.n-1; i >= 0; --i){
+        state.index = i;
         if(i == 0){
             state.input = original_input;
             state.delta = original_delta;
@@ -214,6 +250,8 @@
             backward_convolutional_layer(l, state);
         } else if(l.type == DECONVOLUTIONAL){
             backward_deconvolutional_layer(l, state);
+        } else if(l.type == ACTIVE){
+            backward_activation_layer(l, state);
         } else if(l.type == NORMALIZATION){
             backward_normalization_layer(l, state);
         } else if(l.type == MAXPOOL){
@@ -224,16 +262,22 @@
             backward_dropout_layer(l, state);
         } else if(l.type == DETECTION){
             backward_detection_layer(l, state);
-        } else if(l.type == REGION){
-            backward_region_layer(l, state);
         } else if(l.type == SOFTMAX){
             if(i != 0) backward_softmax_layer(l, state);
         } else if(l.type == CONNECTED){
             backward_connected_layer(l, state);
+        } else if(l.type == RNN){
+            backward_rnn_layer(l, state);
+        } else if(l.type == CRNN){
+            backward_crnn_layer(l, state);
+        } else if(l.type == LOCAL){
+            backward_local_layer(l, state);
         } else if(l.type == COST){
             backward_cost_layer(l, state);
         } else if(l.type == ROUTE){
             backward_route_layer(l, net);
+        } else if(l.type == SHORTCUT){
+            backward_shortcut_layer(l, state);
         }
     }
 }
@@ -245,6 +289,8 @@
     if(gpu_index >= 0) return train_network_datum_gpu(net, x, y);
 #endif
     network_state state;
+    state.index = 0;
+    state.net = net;
     state.input = x;
     state.delta = 0;
     state.truth = y;
@@ -297,6 +343,8 @@
 {
     int i,j;
     network_state state;
+    state.index = 0;
+    state.net = net;
     state.train = 1;
     state.delta = 0;
     float sum = 0;
@@ -337,11 +385,12 @@
         layer l = net->layers[i];
         if(l.type == CONVOLUTIONAL){
             resize_convolutional_layer(&l, w, h);
+        }else if(l.type == CROP){
+            resize_crop_layer(&l, w, h);
         }else if(l.type == MAXPOOL){
             resize_maxpool_layer(&l, w, h);
         }else if(l.type == AVGPOOL){
             resize_avgpool_layer(&l, w, h);
-            break;
         }else if(l.type == NORMALIZATION){
             resize_normalization_layer(&l, w, h);
         }else if(l.type == COST){
@@ -353,6 +402,7 @@
         net->layers[i] = l;
         w = l.out_w;
         h = l.out_h;
+        if(l.type == AVGPOOL) break;
     }
     //fprintf(stderr, " Done!\n");
     return 0;
@@ -433,6 +483,8 @@
 #endif
 
     network_state state;
+    state.net = net;
+    state.index = 0;
     state.input = input;
     state.truth = 0;
     state.train = 0;
@@ -540,12 +592,12 @@
     return acc;
 }
 
-float *network_accuracies(network net, data d)
+float *network_accuracies(network net, data d, int n)
 {
     static float acc[2];
     matrix guess = network_predict_data(net, d);
-    acc[0] = matrix_topk_accuracy(d.y, guess,1);
-    acc[1] = matrix_topk_accuracy(d.y, guess,5);
+    acc[0] = matrix_topk_accuracy(d.y, guess, 1);
+    acc[1] = matrix_topk_accuracy(d.y, guess, n);
     free_matrix(guess);
     return acc;
 }

--
Gitblit v1.10.0