darknet/src/connected_layer.c

180 lines
6.0 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"
2015-01-23 03:38:24 +03:00
#include "cuda.h"
#include "blas.h"
#include "gemm.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>
2015-03-12 08:20:15 +03:00
connected_layer *make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation)
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->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-12-23 01:35:37 +03:00
layer->bias_updates = calloc(outputs, sizeof(float));
layer->weight_prev = calloc(inputs*outputs, sizeof(float));
layer->bias_prev = calloc(outputs, sizeof(float));
layer->weights = calloc(inputs*outputs, sizeof(float));
2014-12-23 01:35:37 +03:00
layer->biases = calloc(outputs, sizeof(float));
2014-12-12 00:15:26 +03:00
float scale = 1./sqrt(inputs);
2014-12-08 07:16:21 +03:00
for(i = 0; i < inputs*outputs; ++i){
2015-04-14 00:09:55 +03:00
layer->weights[i] = 2*scale*rand_uniform() - scale;
2014-12-08 07:16:21 +03:00
}
2013-11-04 23:11:01 +04:00
2014-10-13 11:29:01 +04:00
for(i = 0; i < outputs; ++i){
2014-12-12 00:15:26 +03:00
layer->biases[i] = scale;
2014-10-17 02:17:23 +04:00
}
2013-11-04 23:11:01 +04:00
2014-12-08 07:16:21 +03:00
#ifdef GPU
2015-01-23 03:38:24 +03:00
layer->weights_gpu = cuda_make_array(layer->weights, inputs*outputs);
layer->biases_gpu = cuda_make_array(layer->biases, outputs);
2014-10-17 02:17:23 +04:00
2015-01-23 03:38:24 +03:00
layer->weight_updates_gpu = cuda_make_array(layer->weight_updates, inputs*outputs);
layer->bias_updates_gpu = cuda_make_array(layer->bias_updates, outputs);
2014-10-17 02:17:23 +04:00
2015-01-23 03:38:24 +03:00
layer->output_gpu = cuda_make_array(layer->output, outputs*batch);
layer->delta_gpu = cuda_make_array(layer->delta, outputs*batch);
2014-12-08 07:16:21 +03:00
#endif
2013-12-03 04:41:40 +04:00
layer->activation = activation;
2014-11-19 00:51:04 +03:00
fprintf(stderr, "Connected Layer: %d inputs, %d outputs\n", inputs, outputs);
2013-11-04 23:11:01 +04:00
return layer;
}
2015-03-22 19:56:40 +03:00
void update_connected_layer(connected_layer layer, int batch, float learning_rate, float momentum, float decay)
2013-11-04 23:11:01 +04:00
{
2015-03-22 19:56:40 +03:00
axpy_cpu(layer.outputs, learning_rate/batch, layer.bias_updates, 1, layer.biases, 1);
2015-03-12 08:20:15 +03:00
scal_cpu(layer.outputs, momentum, layer.bias_updates, 1);
2014-10-14 09:31:48 +04:00
2015-03-22 19:56:40 +03:00
axpy_cpu(layer.inputs*layer.outputs, -decay*batch, layer.weights, 1, layer.weight_updates, 1);
axpy_cpu(layer.inputs*layer.outputs, learning_rate/batch, layer.weight_updates, 1, layer.weights, 1);
2015-03-12 08:20:15 +03:00
scal_cpu(layer.inputs*layer.outputs, momentum, layer.weight_updates, 1);
2013-11-04 23:11:01 +04:00
}
2015-03-12 08:20:15 +03:00
void forward_connected_layer(connected_layer layer, network_state state)
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;
2015-03-12 08:20:15 +03:00
float *a = state.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
}
2015-03-12 08:20:15 +03:00
void backward_connected_layer(connected_layer layer, network_state state)
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){
2015-03-22 19:56:40 +03:00
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;
2015-03-12 08:20:15 +03:00
float *a = state.input;
float *b = layer.delta;
float *c = layer.weight_updates;
2015-03-22 19:56:40 +03:00
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;
2015-03-12 08:20:15 +03:00
c = state.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
2014-10-22 01:49:18 +04:00
void pull_connected_layer(connected_layer layer)
{
2015-01-23 03:38:24 +03:00
cuda_pull_array(layer.weights_gpu, layer.weights, layer.inputs*layer.outputs);
cuda_pull_array(layer.biases_gpu, layer.biases, layer.outputs);
cuda_pull_array(layer.weight_updates_gpu, layer.weight_updates, layer.inputs*layer.outputs);
cuda_pull_array(layer.bias_updates_gpu, layer.bias_updates, layer.outputs);
2014-10-25 22:57:26 +04:00
}
void push_connected_layer(connected_layer layer)
{
2015-01-23 03:38:24 +03:00
cuda_push_array(layer.weights_gpu, layer.weights, layer.inputs*layer.outputs);
cuda_push_array(layer.biases_gpu, layer.biases, layer.outputs);
cuda_push_array(layer.weight_updates_gpu, layer.weight_updates, layer.inputs*layer.outputs);
cuda_push_array(layer.bias_updates_gpu, layer.bias_updates, layer.outputs);
2014-10-22 01:49:18 +04:00
}
2015-03-22 19:56:40 +03:00
void update_connected_layer_gpu(connected_layer layer, int batch, float learning_rate, float momentum, float decay)
2014-10-17 02:17:23 +04:00
{
2015-03-22 19:56:40 +03:00
axpy_ongpu(layer.outputs, learning_rate/batch, layer.bias_updates_gpu, 1, layer.biases_gpu, 1);
2015-03-12 08:20:15 +03:00
scal_ongpu(layer.outputs, momentum, layer.bias_updates_gpu, 1);
2014-10-17 02:17:23 +04:00
2015-03-22 19:56:40 +03:00
axpy_ongpu(layer.inputs*layer.outputs, -decay*batch, layer.weights_gpu, 1, layer.weight_updates_gpu, 1);
axpy_ongpu(layer.inputs*layer.outputs, learning_rate/batch, layer.weight_updates_gpu, 1, layer.weights_gpu, 1);
2015-03-12 08:20:15 +03:00
scal_ongpu(layer.inputs*layer.outputs, momentum, layer.weight_updates_gpu, 1);
2014-10-17 02:17:23 +04:00
}
2015-03-12 08:20:15 +03:00
void forward_connected_layer_gpu(connected_layer layer, network_state state)
2014-10-17 02:17:23 +04:00
{
int i;
for(i = 0; i < layer.batch; ++i){
2015-01-23 03:38:24 +03:00
copy_ongpu_offset(layer.outputs, layer.biases_gpu, 0, 1, layer.output_gpu, i*layer.outputs, 1);
2014-10-17 02:17:23 +04:00
}
int m = layer.batch;
int k = layer.inputs;
int n = layer.outputs;
2015-03-12 08:20:15 +03:00
float * a = state.input;
2015-01-23 03:38:24 +03:00
float * b = layer.weights_gpu;
float * c = layer.output_gpu;
2014-10-17 02:17:23 +04:00
gemm_ongpu(0,0,m,n,k,1,a,k,b,n,1,c,n);
2015-01-23 03:38:24 +03:00
activate_array_ongpu(layer.output_gpu, layer.outputs*layer.batch, layer.activation);
2014-10-17 02:17:23 +04:00
}
2015-03-12 08:20:15 +03:00
void backward_connected_layer_gpu(connected_layer layer, network_state state)
2014-10-17 02:17:23 +04:00
{
int i;
2015-01-23 03:38:24 +03:00
gradient_array_ongpu(layer.output_gpu, layer.outputs*layer.batch, layer.activation, layer.delta_gpu);
2014-10-17 02:17:23 +04:00
for(i = 0; i < layer.batch; ++i){
2015-03-22 19:56:40 +03:00
axpy_ongpu_offset(layer.outputs, 1, layer.delta_gpu, i*layer.outputs, 1, layer.bias_updates_gpu, 0, 1);
2014-10-17 02:17:23 +04:00
}
int m = layer.inputs;
int k = layer.batch;
int n = layer.outputs;
2015-03-12 08:20:15 +03:00
float * a = state.input;
2015-01-23 03:38:24 +03:00
float * b = layer.delta_gpu;
float * c = layer.weight_updates_gpu;
2015-03-22 19:56:40 +03:00
gemm_ongpu(1,0,m,n,k,1,a,m,b,n,1,c,n);
2014-10-17 02:17:23 +04:00
m = layer.batch;
k = layer.outputs;
n = layer.inputs;
2015-01-23 03:38:24 +03:00
a = layer.delta_gpu;
b = layer.weights_gpu;
2015-03-12 08:20:15 +03:00
c = state.delta;
2014-10-17 02:17:23 +04:00
if(c) gemm_ongpu(0,1,m,n,k,1,a,k,b,k,0,c,n);
}
2014-12-08 07:16:21 +03:00
#endif