From 79fffcce3ce495bd415dc1284224c915d7194d4c Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 11 Dec 2014 21:15:26 +0000
Subject: [PATCH] Better imagenet distributed training

---
 src/network.c |   96 ++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 76 insertions(+), 20 deletions(-)

diff --git a/src/network.c b/src/network.c
index d7af995..ae030ce 100644
--- a/src/network.c
+++ b/src/network.c
@@ -213,7 +213,7 @@
         }
         if(net.types[i] == CONVOLUTIONAL){
             convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-            backward_convolutional_layer(layer, prev_delta);
+            backward_convolutional_layer(layer, prev_input, prev_delta);
         }
         else if(net.types[i] == MAXPOOL){
             maxpool_layer layer = *(maxpool_layer *)net.layers[i];
@@ -323,6 +323,65 @@
     fprintf(stderr, "Accuracy: %f\n", (float)correct/d.X.rows);
 }
 
+void set_learning_network(network *net, float rate, float momentum, float decay)
+{
+    int i;
+    net->learning_rate=rate;
+    net->momentum = momentum;
+    net->decay = decay;
+    for(i = 0; i < net->n; ++i){
+        if(net->types[i] == CONVOLUTIONAL){
+            convolutional_layer *layer = (convolutional_layer *)net->layers[i];
+            layer->learning_rate=rate;
+            layer->momentum = momentum;
+            layer->decay = decay;
+        }
+        else if(net->types[i] == CONNECTED){
+            connected_layer *layer = (connected_layer *)net->layers[i];
+            layer->learning_rate=rate;
+            layer->momentum = momentum;
+            layer->decay = decay;
+        }
+    }
+}
+
+
+void set_batch_network(network *net, int b)
+{
+    net->batch = b;
+    int i;
+    for(i = 0; i < net->n; ++i){
+        if(net->types[i] == CONVOLUTIONAL){
+            convolutional_layer *layer = (convolutional_layer *)net->layers[i];
+            layer->batch = b;
+        }
+        else if(net->types[i] == MAXPOOL){
+            maxpool_layer *layer = (maxpool_layer *)net->layers[i];
+            layer->batch = b;
+        }
+        else if(net->types[i] == CONNECTED){
+            connected_layer *layer = (connected_layer *)net->layers[i];
+            layer->batch = b;
+        } else if(net->types[i] == DROPOUT){
+            dropout_layer *layer = (dropout_layer *) net->layers[i];
+            layer->batch = b;
+        }
+        else if(net->types[i] == FREEWEIGHT){
+            freeweight_layer *layer = (freeweight_layer *) net->layers[i];
+            layer->batch = b;
+        }
+        else if(net->types[i] == SOFTMAX){
+            softmax_layer *layer = (softmax_layer *)net->layers[i];
+            layer->batch = b;
+        }
+        else if(net->types[i] == COST){
+            cost_layer *layer = (cost_layer *)net->layers[i];
+            layer->batch = b;
+        }
+    }
+}
+
+
 int get_network_input_size_layer(network net, int i)
 {
     if(net.types[i] == CONVOLUTIONAL){
@@ -476,25 +535,11 @@
     } 
 }
 
-void top_predictions(network net, int n, int *index)
+void top_predictions(network net, int k, int *index)
 {
-    int i,j;
-    int k = get_network_output_size(net);
+    int size = get_network_output_size(net);
     float *out = get_network_output(net);
-    float thresh = FLT_MAX;
-    for(i = 0; i < n; ++i){
-        float max = -FLT_MAX;
-        int max_i = -1;
-        for(j = 0; j < k; ++j){
-            float val = out[j];
-            if(val > max &&  val < thresh){
-                max = val;
-                max_i = j;
-            }
-        }
-        index[i] = max_i;
-        thresh = max;
-    }
+    top_k(out, size, k, index);
 }
 
 
@@ -600,15 +645,26 @@
 float network_accuracy(network net, data d)
 {
     matrix guess = network_predict_data(net, d);
-    float acc = matrix_accuracy(d.y, guess);
+    float acc = matrix_topk_accuracy(d.y, guess,1);
     free_matrix(guess);
     return acc;
 }
 
+float *network_accuracies(network net, data d)
+{
+    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);
+    free_matrix(guess);
+    return acc;
+}
+
+
 float network_accuracy_multi(network net, data d, int n)
 {
     matrix guess = network_predict_data_multi(net, d, n);
-    float acc = matrix_accuracy(d.y, guess);
+    float acc = matrix_topk_accuracy(d.y, guess,1);
     free_matrix(guess);
     return acc;
 }

--
Gitblit v1.10.0