rnn_layer.c
Go to the documentation of this file.
00001 #include "rnn_layer.h"
00002 #include "connected_layer.h"
00003 #include "utils.h"
00004 #include "cuda.h"
00005 #include "blas.h"
00006 #include "gemm.h"
00007 
00008 #include <math.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 static void increment_layer(layer *l, int steps)
00014 {
00015     int num = l->outputs*l->batch*steps;
00016     l->output += num;
00017     l->delta += num;
00018     l->x += num;
00019     l->x_norm += num;
00020 
00021 #ifdef GPU
00022     l->output_gpu += num;
00023     l->delta_gpu += num;
00024     l->x_gpu += num;
00025     l->x_norm_gpu += num;
00026 #endif
00027 }
00028 
00029 layer make_rnn_layer(int batch, int inputs, int hidden, int outputs, int steps, ACTIVATION activation, int batch_normalize, int log)
00030 {
00031     fprintf(stderr, "RNN Layer: %d inputs, %d outputs\n", inputs, outputs);
00032     batch = batch / steps;
00033     layer l = {0};
00034     l.batch = batch;
00035     l.type = RNN;
00036     l.steps = steps;
00037     l.hidden = hidden;
00038     l.inputs = inputs;
00039 
00040     l.state = calloc(batch*hidden*(steps+1), sizeof(float));
00041 
00042     l.input_layer = malloc(sizeof(layer));
00043     fprintf(stderr, "\t\t");
00044     *(l.input_layer) = make_connected_layer(batch*steps, inputs, hidden, activation, batch_normalize);
00045     l.input_layer->batch = batch;
00046 
00047     l.self_layer = malloc(sizeof(layer));
00048     fprintf(stderr, "\t\t");
00049     *(l.self_layer) = make_connected_layer(batch*steps, hidden, hidden, (log==2)?LOGGY:(log==1?LOGISTIC:activation), batch_normalize);
00050     l.self_layer->batch = batch;
00051 
00052     l.output_layer = malloc(sizeof(layer));
00053     fprintf(stderr, "\t\t");
00054     *(l.output_layer) = make_connected_layer(batch*steps, hidden, outputs, activation, batch_normalize);
00055     l.output_layer->batch = batch;
00056 
00057     l.outputs = outputs;
00058     l.output = l.output_layer->output;
00059     l.delta = l.output_layer->delta;
00060 
00061     l.forward = forward_rnn_layer;
00062     l.backward = backward_rnn_layer;
00063     l.update = update_rnn_layer;
00064 #ifdef GPU
00065     l.forward_gpu = forward_rnn_layer_gpu;
00066     l.backward_gpu = backward_rnn_layer_gpu;
00067     l.update_gpu = update_rnn_layer_gpu;
00068     l.state_gpu = cuda_make_array(l.state, batch*hidden*(steps+1));
00069     l.output_gpu = l.output_layer->output_gpu;
00070     l.delta_gpu = l.output_layer->delta_gpu;
00071 #endif
00072 
00073     return l;
00074 }
00075 
00076 void update_rnn_layer(layer l, int batch, float learning_rate, float momentum, float decay)
00077 {
00078     update_connected_layer(*(l.input_layer), batch, learning_rate, momentum, decay);
00079     update_connected_layer(*(l.self_layer), batch, learning_rate, momentum, decay);
00080     update_connected_layer(*(l.output_layer), batch, learning_rate, momentum, decay);
00081 }
00082 
00083 void forward_rnn_layer(layer l, network_state state)
00084 {
00085     network_state s = {0};
00086     s.train = state.train;
00087     int i;
00088     layer input_layer = *(l.input_layer);
00089     layer self_layer = *(l.self_layer);
00090     layer output_layer = *(l.output_layer);
00091 
00092     fill_cpu(l.outputs * l.batch * l.steps, 0, output_layer.delta, 1);
00093     fill_cpu(l.hidden * l.batch * l.steps, 0, self_layer.delta, 1);
00094     fill_cpu(l.hidden * l.batch * l.steps, 0, input_layer.delta, 1);
00095     if(state.train) fill_cpu(l.hidden * l.batch, 0, l.state, 1);
00096 
00097     for (i = 0; i < l.steps; ++i) {
00098         s.input = state.input;
00099         forward_connected_layer(input_layer, s);
00100 
00101         s.input = l.state;
00102         forward_connected_layer(self_layer, s);
00103 
00104         float *old_state = l.state;
00105         if(state.train) l.state += l.hidden*l.batch;
00106         if(l.shortcut){
00107             copy_cpu(l.hidden * l.batch, old_state, 1, l.state, 1);
00108         }else{
00109             fill_cpu(l.hidden * l.batch, 0, l.state, 1);
00110         }
00111         axpy_cpu(l.hidden * l.batch, 1, input_layer.output, 1, l.state, 1);
00112         axpy_cpu(l.hidden * l.batch, 1, self_layer.output, 1, l.state, 1);
00113 
00114         s.input = l.state;
00115         forward_connected_layer(output_layer, s);
00116 
00117         state.input += l.inputs*l.batch;
00118         increment_layer(&input_layer, 1);
00119         increment_layer(&self_layer, 1);
00120         increment_layer(&output_layer, 1);
00121     }
00122 }
00123 
00124 void backward_rnn_layer(layer l, network_state state)
00125 {
00126     network_state s = {0};
00127     s.train = state.train;
00128     int i;
00129     layer input_layer = *(l.input_layer);
00130     layer self_layer = *(l.self_layer);
00131     layer output_layer = *(l.output_layer);
00132 
00133     increment_layer(&input_layer, l.steps-1);
00134     increment_layer(&self_layer, l.steps-1);
00135     increment_layer(&output_layer, l.steps-1);
00136 
00137     l.state += l.hidden*l.batch*l.steps;
00138     for (i = l.steps-1; i >= 0; --i) {
00139         copy_cpu(l.hidden * l.batch, input_layer.output, 1, l.state, 1);
00140         axpy_cpu(l.hidden * l.batch, 1, self_layer.output, 1, l.state, 1);
00141 
00142         s.input = l.state;
00143         s.delta = self_layer.delta;
00144         backward_connected_layer(output_layer, s);
00145 
00146         l.state -= l.hidden*l.batch;
00147         /*
00148            if(i > 0){
00149            copy_cpu(l.hidden * l.batch, input_layer.output - l.hidden*l.batch, 1, l.state, 1);
00150            axpy_cpu(l.hidden * l.batch, 1, self_layer.output - l.hidden*l.batch, 1, l.state, 1);
00151            }else{
00152            fill_cpu(l.hidden * l.batch, 0, l.state, 1);
00153            }
00154          */
00155 
00156         s.input = l.state;
00157         s.delta = self_layer.delta - l.hidden*l.batch;
00158         if (i == 0) s.delta = 0;
00159         backward_connected_layer(self_layer, s);
00160 
00161         copy_cpu(l.hidden*l.batch, self_layer.delta, 1, input_layer.delta, 1);
00162         if (i > 0 && l.shortcut) axpy_cpu(l.hidden*l.batch, 1, self_layer.delta, 1, self_layer.delta - l.hidden*l.batch, 1);
00163         s.input = state.input + i*l.inputs*l.batch;
00164         if(state.delta) s.delta = state.delta + i*l.inputs*l.batch;
00165         else s.delta = 0;
00166         backward_connected_layer(input_layer, s);
00167 
00168         increment_layer(&input_layer, -1);
00169         increment_layer(&self_layer, -1);
00170         increment_layer(&output_layer, -1);
00171     }
00172 }
00173 
00174 #ifdef GPU
00175 
00176 void pull_rnn_layer(layer l)
00177 {
00178     pull_connected_layer(*(l.input_layer));
00179     pull_connected_layer(*(l.self_layer));
00180     pull_connected_layer(*(l.output_layer));
00181 }
00182 
00183 void push_rnn_layer(layer l)
00184 {
00185     push_connected_layer(*(l.input_layer));
00186     push_connected_layer(*(l.self_layer));
00187     push_connected_layer(*(l.output_layer));
00188 }
00189 
00190 void update_rnn_layer_gpu(layer l, int batch, float learning_rate, float momentum, float decay)
00191 {
00192     update_connected_layer_gpu(*(l.input_layer), batch, learning_rate, momentum, decay);
00193     update_connected_layer_gpu(*(l.self_layer), batch, learning_rate, momentum, decay);
00194     update_connected_layer_gpu(*(l.output_layer), batch, learning_rate, momentum, decay);
00195 }
00196 
00197 void forward_rnn_layer_gpu(layer l, network_state state)
00198 {
00199     network_state s = {0};
00200     s.train = state.train;
00201     int i;
00202     layer input_layer = *(l.input_layer);
00203     layer self_layer = *(l.self_layer);
00204     layer output_layer = *(l.output_layer);
00205 
00206     fill_ongpu(l.outputs * l.batch * l.steps, 0, output_layer.delta_gpu, 1);
00207     fill_ongpu(l.hidden * l.batch * l.steps, 0, self_layer.delta_gpu, 1);
00208     fill_ongpu(l.hidden * l.batch * l.steps, 0, input_layer.delta_gpu, 1);
00209     if(state.train) fill_ongpu(l.hidden * l.batch, 0, l.state_gpu, 1);
00210 
00211     for (i = 0; i < l.steps; ++i) {
00212         s.input = state.input;
00213         forward_connected_layer_gpu(input_layer, s);
00214 
00215         s.input = l.state_gpu;
00216         forward_connected_layer_gpu(self_layer, s);
00217 
00218         float *old_state = l.state_gpu;
00219         if(state.train) l.state_gpu += l.hidden*l.batch;
00220         if(l.shortcut){
00221             copy_ongpu(l.hidden * l.batch, old_state, 1, l.state_gpu, 1);
00222         }else{
00223             fill_ongpu(l.hidden * l.batch, 0, l.state_gpu, 1);
00224         }
00225         axpy_ongpu(l.hidden * l.batch, 1, input_layer.output_gpu, 1, l.state_gpu, 1);
00226         axpy_ongpu(l.hidden * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
00227 
00228         s.input = l.state_gpu;
00229         forward_connected_layer_gpu(output_layer, s);
00230 
00231         state.input += l.inputs*l.batch;
00232         increment_layer(&input_layer, 1);
00233         increment_layer(&self_layer, 1);
00234         increment_layer(&output_layer, 1);
00235     }
00236 }
00237 
00238 void backward_rnn_layer_gpu(layer l, network_state state)
00239 {
00240     network_state s = {0};
00241     s.train = state.train;
00242     int i;
00243     layer input_layer = *(l.input_layer);
00244     layer self_layer = *(l.self_layer);
00245     layer output_layer = *(l.output_layer);
00246     increment_layer(&input_layer,  l.steps - 1);
00247     increment_layer(&self_layer,   l.steps - 1);
00248     increment_layer(&output_layer, l.steps - 1);
00249     l.state_gpu += l.hidden*l.batch*l.steps;
00250     for (i = l.steps-1; i >= 0; --i) {
00251 
00252         s.input = l.state_gpu;
00253         s.delta = self_layer.delta_gpu;
00254         backward_connected_layer_gpu(output_layer, s);
00255 
00256         l.state_gpu -= l.hidden*l.batch;
00257 
00258         copy_ongpu(l.hidden*l.batch, self_layer.delta_gpu, 1, input_layer.delta_gpu, 1);
00259 
00260         s.input = l.state_gpu;
00261         s.delta = self_layer.delta_gpu - l.hidden*l.batch;
00262         if (i == 0) s.delta = 0;
00263         backward_connected_layer_gpu(self_layer, s);
00264 
00265         //copy_ongpu(l.hidden*l.batch, self_layer.delta_gpu, 1, input_layer.delta_gpu, 1);
00266         if (i > 0 && l.shortcut) axpy_ongpu(l.hidden*l.batch, 1, self_layer.delta_gpu, 1, self_layer.delta_gpu - l.hidden*l.batch, 1);
00267         s.input = state.input + i*l.inputs*l.batch;
00268         if(state.delta) s.delta = state.delta + i*l.inputs*l.batch;
00269         else s.delta = 0;
00270         backward_connected_layer_gpu(input_layer, s);
00271 
00272         increment_layer(&input_layer,  -1);
00273         increment_layer(&self_layer,   -1);
00274         increment_layer(&output_layer, -1);
00275     }
00276 }
00277 #endif


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30