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 |   42 +++++++++++++++++++-----------------------
 1 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index 6a172aa..2e25844 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -111,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;
@@ -151,19 +161,6 @@
     activate_array(layer.output, m*n*layer.batch, layer.activation);
 }
 
-void learn_bias_convolutional_layer(convolutional_layer layer)
-{
-    float alpha = 1./layer.batch;
-    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] += alpha * 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;
@@ -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));
 

--
Gitblit v1.10.0