From 979d02126b1a597361934f86f50eeda31ff083fe Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Mon, 09 Feb 2015 21:27:58 +0000
Subject: [PATCH] Generalizing conv layer so deconv is easier

---
 src/convolutional_layer.c |   46 +++++++++++++++++++++-------------------------
 1 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index 6848511..2e25844 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -66,10 +66,8 @@
     layer->biases = calloc(n, sizeof(float));
     layer->bias_updates = calloc(n, sizeof(float));
     float scale = 1./sqrt(size*size*c);
-    //scale = .05;
     for(i = 0; i < c*n*size*size; ++i) layer->filters[i] = scale*rand_normal();
     for(i = 0; i < n; ++i){
-        //layer->biases[i] = rand_normal()*scale + scale;
         layer->biases[i] = scale;
     }
     int out_h = convolutional_out_height(*layer);
@@ -113,27 +111,37 @@
                                 layer->batch*out_h * out_w * layer->n*sizeof(float));
 }
 
-void bias_output(const convolutional_layer layer)
+void bias_output(float *output, float *biases, int batch, int n, int size)
 {
     int i,j,b;
-    int out_h = convolutional_out_height(layer);
-    int out_w = convolutional_out_width(layer);
-    for(b = 0; b < layer.batch; ++b){
-        for(i = 0; i < layer.n; ++i){
-            for(j = 0; j < out_h*out_w; ++j){
-                layer.output[(b*layer.n + i)*out_h*out_w + j] = layer.biases[i];
+    for(b = 0; b < batch; ++b){
+        for(i = 0; i < n; ++i){
+            for(j = 0; j < size; ++j){
+                output[(b*n + i)*size + j] = biases[i];
             }
         }
     }
 }
 
+void backward_bias(float *bias_updates, float *delta, int batch, int n, int size)
+{
+    float alpha = 1./batch;
+    int i,b;
+    for(b = 0; b < batch; ++b){
+        for(i = 0; i < n; ++i){
+            bias_updates[i] += alpha * sum_array(delta+size*(i+b*n), size);
+        }
+    }
+}
+
+
 void forward_convolutional_layer(const convolutional_layer layer, float *in)
 {
     int out_h = convolutional_out_height(layer);
     int out_w = convolutional_out_width(layer);
     int i;
 
-    bias_output(layer);
+    bias_output(layer.output, layer.biases, layer.batch, layer.n, out_h*out_w);
 
     int m = layer.n;
     int k = layer.size*layer.size*layer.c;
@@ -153,20 +161,9 @@
     activate_array(layer.output, m*n*layer.batch, layer.activation);
 }
 
-void learn_bias_convolutional_layer(convolutional_layer layer)
-{
-    int i,b;
-    int size = convolutional_out_height(layer)
-        *convolutional_out_width(layer);
-    for(b = 0; b < layer.batch; ++b){
-        for(i = 0; i < layer.n; ++i){
-            layer.bias_updates[i] += sum_array(layer.delta+size*(i+b*layer.n), size);
-        }
-    }
-}
-
 void backward_convolutional_layer(convolutional_layer layer, float *in, float *delta)
 {
+    float alpha = 1./layer.batch;
     int i;
     int m = layer.n;
     int n = layer.size*layer.size*layer.c;
@@ -174,8 +171,7 @@
         convolutional_out_width(layer);
 
     gradient_array(layer.output, m*k*layer.batch, layer.activation, layer.delta);
-
-    learn_bias_convolutional_layer(layer);
+    backward_bias(layer.bias_updates, layer.delta, layer.batch, layer.n, k);
 
     if(delta) memset(delta, 0, layer.batch*layer.h*layer.w*layer.c*sizeof(float));
 
@@ -188,7 +184,7 @@
 
         im2col_cpu(im, layer.c, layer.h, layer.w, 
                 layer.size, layer.stride, layer.pad, b);
-        gemm(0,1,m,n,k,1,a,k,b,k,1,c,n);
+        gemm(0,1,m,n,k,alpha,a,k,b,k,1,c,n);
 
         if(delta){
             a = layer.filters;

--
Gitblit v1.10.0