darknet/src/connected_layer.c

167 lines
5.5 KiB
C
Raw Normal View History

2013-11-04 23:11:01 +04:00
#include "connected_layer.h"
2013-12-03 04:41:40 +04:00
#include "utils.h"
2014-01-25 02:49:02 +04:00
#include "mini_blas.h"
2013-11-04 23:11:01 +04:00
#include <math.h>
2013-11-13 22:50:38 +04:00
#include <stdio.h>
2013-11-04 23:11:01 +04:00
#include <stdlib.h>
#include <string.h>
2014-08-08 23:04:15 +04:00
connected_layer *make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, float learning_rate, float momentum, float decay)
2013-11-04 23:11:01 +04:00
{
2013-12-06 01:17:16 +04:00
fprintf(stderr, "Connected Layer: %d inputs, %d outputs\n", inputs, outputs);
2013-11-04 23:11:01 +04:00
int i;
connected_layer *layer = calloc(1, sizeof(connected_layer));
2014-08-08 23:04:15 +04:00
layer->learning_rate = learning_rate;
layer->momentum = momentum;
layer->decay = decay;
layer->inputs = inputs;
layer->outputs = outputs;
2014-03-13 08:57:34 +04:00
layer->batch=batch;
2013-11-04 23:11:01 +04:00
2014-03-13 08:57:34 +04:00
layer->output = calloc(batch*outputs, sizeof(float*));
layer->delta = calloc(batch*outputs, sizeof(float*));
2013-11-04 23:11:01 +04:00
layer->weight_updates = calloc(inputs*outputs, sizeof(float));
2014-10-13 11:29:01 +04:00
//layer->weight_adapt = calloc(inputs*outputs, sizeof(float));
layer->weights = calloc(inputs*outputs, sizeof(float));
2014-02-14 22:26:31 +04:00
float scale = 1./inputs;
2014-08-09 19:16:37 +04:00
scale = .05;
2013-11-04 23:11:01 +04:00
for(i = 0; i < inputs*outputs; ++i)
2014-08-09 19:16:37 +04:00
layer->weights[i] = scale*2*(rand_uniform()-.5);
2013-11-04 23:11:01 +04:00
layer->bias_updates = calloc(outputs, sizeof(float));
2014-10-13 11:29:01 +04:00
//layer->bias_adapt = calloc(outputs, sizeof(float));
layer->biases = calloc(outputs, sizeof(float));
2014-10-13 11:29:01 +04:00
for(i = 0; i < outputs; ++i){
2013-12-03 04:41:40 +04:00
//layer->biases[i] = rand_normal()*scale + scale;
2014-02-14 22:26:31 +04:00
layer->biases[i] = 1;
2014-10-17 02:17:23 +04:00
}
2013-11-04 23:11:01 +04:00
2014-10-13 11:29:01 +04:00
#ifdef GPU
2014-10-17 02:17:23 +04:00
layer->weights_cl = cl_make_array(layer->weights, inputs*outputs);
layer->biases_cl = cl_make_array(layer->biases, outputs);
layer->weight_updates_cl = cl_make_array(layer->weight_updates, inputs*outputs);
layer->bias_updates_cl = cl_make_array(layer->bias_updates, outputs);
layer->output_cl = cl_make_array(layer->output, outputs*batch);
layer->delta_cl = cl_make_array(layer->delta, outputs*batch);
2014-10-13 11:29:01 +04:00
#endif
2013-12-03 04:41:40 +04:00
layer->activation = activation;
2013-11-04 23:11:01 +04:00
return layer;
}
2014-08-08 23:04:15 +04:00
void update_connected_layer(connected_layer layer)
2013-11-04 23:11:01 +04:00
{
2014-10-14 09:31:48 +04:00
axpy_cpu(layer.outputs, layer.learning_rate, layer.bias_updates, 1, layer.biases, 1);
scal_cpu(layer.outputs, layer.momentum, layer.bias_updates, 1);
scal_cpu(layer.inputs*layer.outputs, 1.-layer.learning_rate*layer.decay, layer.weights, 1);
axpy_cpu(layer.inputs*layer.outputs, layer.learning_rate, layer.weight_updates, 1, layer.weights, 1);
scal_cpu(layer.inputs*layer.outputs, layer.momentum, layer.weight_updates, 1);
2013-11-04 23:11:01 +04:00
}
2014-08-08 23:04:15 +04:00
void forward_connected_layer(connected_layer layer, float *input)
2013-11-04 23:11:01 +04:00
{
2014-07-14 09:07:51 +04:00
int i;
for(i = 0; i < layer.batch; ++i){
2014-10-14 09:31:48 +04:00
copy_cpu(layer.outputs, layer.biases, 1, layer.output + i*layer.outputs, 1);
2014-07-14 09:07:51 +04:00
}
2014-03-13 08:57:34 +04:00
int m = layer.batch;
2014-01-25 02:49:02 +04:00
int k = layer.inputs;
int n = layer.outputs;
float *a = input;
float *b = layer.weights;
float *c = layer.output;
2014-01-25 02:49:02 +04:00
gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
2014-08-08 23:04:15 +04:00
activate_array(layer.output, layer.outputs*layer.batch, layer.activation);
2013-11-04 23:11:01 +04:00
}
2014-05-10 02:14:52 +04:00
void backward_connected_layer(connected_layer layer, float *input, float *delta)
2013-11-04 23:11:01 +04:00
{
2014-01-25 02:49:02 +04:00
int i;
2014-10-14 09:31:48 +04:00
gradient_array(layer.output, layer.outputs*layer.batch, layer.activation, layer.delta);
2014-10-17 02:17:23 +04:00
for(i = 0; i < layer.batch; ++i){
axpy_cpu(layer.outputs, 1, layer.delta + i*layer.outputs, 1, layer.bias_updates, 1);
2013-11-04 23:11:01 +04:00
}
2014-01-25 02:49:02 +04:00
int m = layer.inputs;
2014-03-13 08:57:34 +04:00
int k = layer.batch;
2014-01-25 02:49:02 +04:00
int n = layer.outputs;
float *a = input;
float *b = layer.delta;
float *c = layer.weight_updates;
gemm(1,0,m,n,k,1,a,m,b,n,1,c,n);
2013-11-04 23:11:01 +04:00
2014-07-14 09:07:51 +04:00
m = layer.batch;
2014-05-10 02:14:52 +04:00
k = layer.outputs;
2014-07-14 09:07:51 +04:00
n = layer.inputs;
2014-01-25 02:49:02 +04:00
2014-07-14 09:07:51 +04:00
a = layer.delta;
b = layer.weights;
2014-05-10 02:14:52 +04:00
c = delta;
2014-01-25 02:49:02 +04:00
2014-07-14 09:07:51 +04:00
if(c) gemm(0,1,m,n,k,1,a,k,b,k,0,c,n);
2013-11-04 23:11:01 +04:00
}
2014-10-17 02:17:23 +04:00
#ifdef GPU
void update_connected_layer_gpu(connected_layer layer)
{
axpy_ongpu(layer.outputs, layer.learning_rate, layer.bias_updates_cl, 1, layer.biases_cl, 1);
scal_ongpu(layer.outputs, layer.momentum, layer.bias_updates_cl, 1);
scal_ongpu(layer.inputs*layer.outputs, 1.-layer.learning_rate*layer.decay, layer.weights_cl, 1);
axpy_ongpu(layer.inputs*layer.outputs, layer.learning_rate, layer.weight_updates_cl, 1, layer.weights_cl, 1);
scal_ongpu(layer.inputs*layer.outputs, layer.momentum, layer.weight_updates_cl, 1);
}
void forward_connected_layer_gpu(connected_layer layer, cl_mem input)
{
int i;
for(i = 0; i < layer.batch; ++i){
cl_mem sub = cl_sub_array(layer.output_cl, i*layer.outputs, layer.outputs);
copy_ongpu(layer.outputs, layer.biases_cl, 1, sub, 1);
clReleaseMemObject(sub);
}
int m = layer.batch;
int k = layer.inputs;
int n = layer.outputs;
cl_mem a = input;
cl_mem b = layer.weights_cl;
cl_mem c = layer.output_cl;
gemm_ongpu(0,0,m,n,k,1,a,k,b,n,1,c,n);
activate_array_ongpu(layer.output_cl, layer.outputs*layer.batch, layer.activation);
}
void backward_connected_layer_gpu(connected_layer layer, cl_mem input, cl_mem delta)
{
int i;
gradient_array_ongpu(layer.output_cl, layer.outputs*layer.batch, layer.activation, layer.delta_cl);
for(i = 0; i < layer.batch; ++i){
cl_mem sub = cl_sub_array(layer.delta_cl, i*layer.outputs, layer.outputs);
axpy_ongpu(layer.outputs, 1, sub, 1, layer.bias_updates_cl, 1);
clReleaseMemObject(sub);
}
int m = layer.inputs;
int k = layer.batch;
int n = layer.outputs;
cl_mem a = input;
cl_mem b = layer.delta_cl;
cl_mem c = layer.weight_updates_cl;
gemm_ongpu(1,0,m,n,k,1,a,m,b,n,1,c,n);
m = layer.batch;
k = layer.outputs;
n = layer.inputs;
a = layer.delta_cl;
b = layer.weights_cl;
c = delta;
if(c) gemm_ongpu(0,1,m,n,k,1,a,k,b,k,0,c,n);
}
#endif