2013-11-04 23:11:01 +04:00
|
|
|
#include "connected_layer.h"
|
2016-05-07 02:25:16 +03:00
|
|
|
#include "batchnorm_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
|
|
|
|
2013-11-06 22:37:37 +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>
|
|
|
|
|
2016-01-28 23:30:38 +03:00
|
|
|
connected_layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
|
|
|
int i;
|
2015-05-11 23:46:49 +03:00
|
|
|
connected_layer l = {0};
|
|
|
|
l.type = CONNECTED;
|
2014-08-08 23:04:15 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
l.inputs = inputs;
|
|
|
|
l.outputs = outputs;
|
|
|
|
l.batch=batch;
|
2016-01-28 23:30:38 +03:00
|
|
|
l.batch_normalize = batch_normalize;
|
2016-05-07 02:25:16 +03:00
|
|
|
l.h = 1;
|
|
|
|
l.w = 1;
|
|
|
|
l.c = inputs;
|
|
|
|
l.out_h = 1;
|
|
|
|
l.out_w = 1;
|
|
|
|
l.out_c = outputs;
|
2013-11-04 23:11:01 +04:00
|
|
|
|
2016-01-28 23:30:38 +03:00
|
|
|
l.output = calloc(batch*outputs, sizeof(float));
|
|
|
|
l.delta = calloc(batch*outputs, sizeof(float));
|
2013-11-04 23:11:01 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
l.weight_updates = calloc(inputs*outputs, sizeof(float));
|
|
|
|
l.bias_updates = calloc(outputs, sizeof(float));
|
2014-12-23 01:35:37 +03:00
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
l.weights = calloc(outputs*inputs, sizeof(float));
|
2015-05-11 23:46:49 +03:00
|
|
|
l.biases = calloc(outputs, sizeof(float));
|
2014-12-23 01:35:37 +03:00
|
|
|
|
2015-05-20 20:06:42 +03:00
|
|
|
//float scale = 1./sqrt(inputs);
|
|
|
|
float scale = sqrt(2./inputs);
|
2015-12-14 22:57:10 +03:00
|
|
|
for(i = 0; i < outputs*inputs; ++i){
|
2016-01-19 02:40:14 +03:00
|
|
|
l.weights[i] = scale*rand_uniform(-1, 1);
|
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){
|
2016-05-07 02:25:16 +03:00
|
|
|
l.biases[i] = 0;
|
2014-10-17 02:17:23 +04:00
|
|
|
}
|
2013-11-04 23:11:01 +04:00
|
|
|
|
2016-01-28 23:30:38 +03:00
|
|
|
if(batch_normalize){
|
|
|
|
l.scales = calloc(outputs, sizeof(float));
|
|
|
|
l.scale_updates = calloc(outputs, sizeof(float));
|
|
|
|
for(i = 0; i < outputs; ++i){
|
|
|
|
l.scales[i] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
l.mean = calloc(outputs, sizeof(float));
|
|
|
|
l.mean_delta = calloc(outputs, sizeof(float));
|
|
|
|
l.variance = calloc(outputs, sizeof(float));
|
|
|
|
l.variance_delta = calloc(outputs, sizeof(float));
|
|
|
|
|
|
|
|
l.rolling_mean = calloc(outputs, sizeof(float));
|
|
|
|
l.rolling_variance = calloc(outputs, sizeof(float));
|
|
|
|
|
|
|
|
l.x = calloc(batch*outputs, sizeof(float));
|
|
|
|
l.x_norm = calloc(batch*outputs, sizeof(float));
|
|
|
|
}
|
|
|
|
|
2014-12-08 07:16:21 +03:00
|
|
|
#ifdef GPU
|
2015-12-14 22:57:10 +03:00
|
|
|
l.weights_gpu = cuda_make_array(l.weights, outputs*inputs);
|
2015-05-11 23:46:49 +03:00
|
|
|
l.biases_gpu = cuda_make_array(l.biases, outputs);
|
2014-10-17 02:17:23 +04:00
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
l.weight_updates_gpu = cuda_make_array(l.weight_updates, outputs*inputs);
|
2015-05-11 23:46:49 +03:00
|
|
|
l.bias_updates_gpu = cuda_make_array(l.bias_updates, outputs);
|
2014-10-17 02:17:23 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
l.output_gpu = cuda_make_array(l.output, outputs*batch);
|
|
|
|
l.delta_gpu = cuda_make_array(l.delta, outputs*batch);
|
2016-01-28 23:30:38 +03:00
|
|
|
if(batch_normalize){
|
|
|
|
l.scales_gpu = cuda_make_array(l.scales, outputs);
|
|
|
|
l.scale_updates_gpu = cuda_make_array(l.scale_updates, outputs);
|
|
|
|
|
|
|
|
l.mean_gpu = cuda_make_array(l.mean, outputs);
|
|
|
|
l.variance_gpu = cuda_make_array(l.variance, outputs);
|
|
|
|
|
|
|
|
l.rolling_mean_gpu = cuda_make_array(l.mean, outputs);
|
|
|
|
l.rolling_variance_gpu = cuda_make_array(l.variance, outputs);
|
|
|
|
|
|
|
|
l.mean_delta_gpu = cuda_make_array(l.mean, outputs);
|
|
|
|
l.variance_delta_gpu = cuda_make_array(l.variance, outputs);
|
|
|
|
|
|
|
|
l.x_gpu = cuda_make_array(l.output, l.batch*outputs);
|
|
|
|
l.x_norm_gpu = cuda_make_array(l.output, l.batch*outputs);
|
|
|
|
}
|
2014-12-08 07:16:21 +03:00
|
|
|
#endif
|
2015-05-11 23:46:49 +03:00
|
|
|
l.activation = activation;
|
2014-11-19 00:51:04 +03:00
|
|
|
fprintf(stderr, "Connected Layer: %d inputs, %d outputs\n", inputs, outputs);
|
2015-05-11 23:46:49 +03:00
|
|
|
return l;
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void update_connected_layer(connected_layer l, int batch, float learning_rate, float momentum, float decay)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
axpy_cpu(l.outputs, learning_rate/batch, l.bias_updates, 1, l.biases, 1);
|
|
|
|
scal_cpu(l.outputs, momentum, l.bias_updates, 1);
|
2014-10-14 09:31:48 +04:00
|
|
|
|
2016-01-28 23:30:38 +03:00
|
|
|
if(l.batch_normalize){
|
|
|
|
axpy_cpu(l.outputs, learning_rate/batch, l.scale_updates, 1, l.scales, 1);
|
|
|
|
scal_cpu(l.outputs, momentum, l.scale_updates, 1);
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
axpy_cpu(l.inputs*l.outputs, -decay*batch, l.weights, 1, l.weight_updates, 1);
|
|
|
|
axpy_cpu(l.inputs*l.outputs, learning_rate/batch, l.weight_updates, 1, l.weights, 1);
|
|
|
|
scal_cpu(l.inputs*l.outputs, momentum, l.weight_updates, 1);
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_connected_layer(connected_layer l, network_state state)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2014-07-14 09:07:51 +04:00
|
|
|
int i;
|
2016-01-28 23:30:38 +03:00
|
|
|
fill_cpu(l.outputs*l.batch, 0, l.output, 1);
|
2015-05-11 23:46:49 +03:00
|
|
|
int m = l.batch;
|
|
|
|
int k = l.inputs;
|
|
|
|
int n = l.outputs;
|
2015-03-12 08:20:15 +03:00
|
|
|
float *a = state.input;
|
2015-05-11 23:46:49 +03:00
|
|
|
float *b = l.weights;
|
|
|
|
float *c = l.output;
|
2015-12-14 22:57:10 +03:00
|
|
|
gemm(0,1,m,n,k,1,a,k,b,k,1,c,n);
|
2016-01-28 23:30:38 +03:00
|
|
|
if(l.batch_normalize){
|
|
|
|
if(state.train){
|
|
|
|
mean_cpu(l.output, l.batch, l.outputs, 1, l.mean);
|
|
|
|
variance_cpu(l.output, l.mean, l.batch, l.outputs, 1, l.variance);
|
|
|
|
|
|
|
|
scal_cpu(l.outputs, .95, l.rolling_mean, 1);
|
|
|
|
axpy_cpu(l.outputs, .05, l.mean, 1, l.rolling_mean, 1);
|
|
|
|
scal_cpu(l.outputs, .95, l.rolling_variance, 1);
|
|
|
|
axpy_cpu(l.outputs, .05, l.variance, 1, l.rolling_variance, 1);
|
|
|
|
|
|
|
|
copy_cpu(l.outputs*l.batch, l.output, 1, l.x, 1);
|
|
|
|
normalize_cpu(l.output, l.mean, l.variance, l.batch, l.outputs, 1);
|
|
|
|
copy_cpu(l.outputs*l.batch, l.output, 1, l.x_norm, 1);
|
|
|
|
} else {
|
|
|
|
normalize_cpu(l.output, l.rolling_mean, l.rolling_variance, l.batch, l.outputs, 1);
|
|
|
|
}
|
|
|
|
scale_bias(l.output, l.scales, l.batch, l.outputs, 1);
|
|
|
|
}
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
|
|
|
axpy_cpu(l.outputs, 1, l.biases, 1, l.output + i*l.outputs, 1);
|
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
activate_array(l.output, l.outputs*l.batch, l.activation);
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_connected_layer(connected_layer l, network_state state)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2014-01-25 02:49:02 +04:00
|
|
|
int i;
|
2015-05-11 23:46:49 +03:00
|
|
|
gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
|
|
|
axpy_cpu(l.outputs, 1, l.delta + i*l.outputs, 1, l.bias_updates, 1);
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
2016-01-28 23:30:38 +03:00
|
|
|
if(l.batch_normalize){
|
|
|
|
backward_scale_cpu(l.x_norm, l.delta, l.batch, l.outputs, 1, l.scale_updates);
|
|
|
|
|
|
|
|
scale_bias(l.delta, l.scales, l.batch, l.outputs, 1);
|
|
|
|
|
|
|
|
mean_delta_cpu(l.delta, l.variance, l.batch, l.outputs, 1, l.mean_delta);
|
|
|
|
variance_delta_cpu(l.x, l.delta, l.mean, l.variance, l.batch, l.outputs, 1, l.variance_delta);
|
|
|
|
normalize_delta_cpu(l.x, l.mean, l.variance, l.mean_delta, l.variance_delta, l.batch, l.outputs, 1, l.delta);
|
|
|
|
}
|
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
int m = l.outputs;
|
2015-05-11 23:46:49 +03:00
|
|
|
int k = l.batch;
|
2015-12-14 22:57:10 +03:00
|
|
|
int n = l.inputs;
|
|
|
|
float *a = l.delta;
|
|
|
|
float *b = state.input;
|
2015-05-11 23:46:49 +03:00
|
|
|
float *c = l.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
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
m = l.batch;
|
|
|
|
k = l.outputs;
|
|
|
|
n = l.inputs;
|
2014-01-25 02:49:02 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
a = l.delta;
|
|
|
|
b = l.weights;
|
2015-03-12 08:20:15 +03:00
|
|
|
c = state.delta;
|
2014-01-25 02:49:02 +04:00
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
if(c) gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
|
2016-05-07 02:25:16 +03:00
|
|
|
|
|
|
|
void denormalize_connected_layer(layer l)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
for(i = 0; i < l.outputs; ++i){
|
|
|
|
float scale = l.scales[i]/sqrt(l.rolling_variance[i] + .00001);
|
|
|
|
for(j = 0; j < l.inputs; ++j){
|
|
|
|
l.weights[i*l.inputs + j] *= scale;
|
|
|
|
}
|
|
|
|
l.biases[i] -= l.rolling_mean[i] * scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:17:23 +04:00
|
|
|
#ifdef GPU
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void pull_connected_layer(connected_layer l)
|
2014-10-22 01:49:18 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_pull_array(l.weights_gpu, l.weights, l.inputs*l.outputs);
|
|
|
|
cuda_pull_array(l.biases_gpu, l.biases, l.outputs);
|
|
|
|
cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.inputs*l.outputs);
|
|
|
|
cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.outputs);
|
2016-01-28 23:30:38 +03:00
|
|
|
if (l.batch_normalize){
|
|
|
|
cuda_pull_array(l.scales_gpu, l.scales, l.outputs);
|
|
|
|
cuda_pull_array(l.rolling_mean_gpu, l.rolling_mean, l.outputs);
|
|
|
|
cuda_pull_array(l.rolling_variance_gpu, l.rolling_variance, l.outputs);
|
|
|
|
}
|
2014-10-25 22:57:26 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void push_connected_layer(connected_layer l)
|
2014-10-25 22:57:26 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_push_array(l.weights_gpu, l.weights, l.inputs*l.outputs);
|
|
|
|
cuda_push_array(l.biases_gpu, l.biases, l.outputs);
|
|
|
|
cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.inputs*l.outputs);
|
|
|
|
cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.outputs);
|
2016-01-28 23:30:38 +03:00
|
|
|
if (l.batch_normalize){
|
|
|
|
cuda_push_array(l.scales_gpu, l.scales, l.outputs);
|
|
|
|
cuda_push_array(l.rolling_mean_gpu, l.rolling_mean, l.outputs);
|
|
|
|
cuda_push_array(l.rolling_variance_gpu, l.rolling_variance, l.outputs);
|
|
|
|
}
|
2014-10-22 01:49:18 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void update_connected_layer_gpu(connected_layer l, int batch, float learning_rate, float momentum, float decay)
|
2014-10-17 02:17:23 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
axpy_ongpu(l.outputs, learning_rate/batch, l.bias_updates_gpu, 1, l.biases_gpu, 1);
|
|
|
|
scal_ongpu(l.outputs, momentum, l.bias_updates_gpu, 1);
|
2014-10-17 02:17:23 +04:00
|
|
|
|
2016-01-28 23:30:38 +03:00
|
|
|
if(l.batch_normalize){
|
|
|
|
axpy_ongpu(l.outputs, learning_rate/batch, l.scale_updates_gpu, 1, l.scales_gpu, 1);
|
|
|
|
scal_ongpu(l.outputs, momentum, l.scale_updates_gpu, 1);
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
axpy_ongpu(l.inputs*l.outputs, -decay*batch, l.weights_gpu, 1, l.weight_updates_gpu, 1);
|
|
|
|
axpy_ongpu(l.inputs*l.outputs, learning_rate/batch, l.weight_updates_gpu, 1, l.weights_gpu, 1);
|
|
|
|
scal_ongpu(l.inputs*l.outputs, momentum, l.weight_updates_gpu, 1);
|
2014-10-17 02:17:23 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_connected_layer_gpu(connected_layer l, network_state state)
|
2014-10-17 02:17:23 +04:00
|
|
|
{
|
|
|
|
int i;
|
2016-01-28 23:30:38 +03:00
|
|
|
fill_ongpu(l.outputs*l.batch, 0, l.output_gpu, 1);
|
2016-05-07 02:25:16 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
int m = l.batch;
|
|
|
|
int k = l.inputs;
|
|
|
|
int n = l.outputs;
|
2015-03-12 08:20:15 +03:00
|
|
|
float * a = state.input;
|
2015-05-11 23:46:49 +03:00
|
|
|
float * b = l.weights_gpu;
|
|
|
|
float * c = l.output_gpu;
|
2015-12-14 22:57:10 +03:00
|
|
|
gemm_ongpu(0,1,m,n,k,1,a,k,b,k,1,c,n);
|
2016-01-28 23:30:38 +03:00
|
|
|
if(l.batch_normalize){
|
2016-05-07 02:25:16 +03:00
|
|
|
forward_batchnorm_layer_gpu(l, state);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
|
|
|
axpy_ongpu(l.outputs, 1, l.biases_gpu, 1, l.output_gpu + i*l.outputs, 1);
|
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
activate_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation);
|
2015-12-08 04:18:04 +03:00
|
|
|
|
2014-10-17 02:17:23 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_connected_layer_gpu(connected_layer l, network_state state)
|
2014-10-17 02:17:23 +04:00
|
|
|
{
|
|
|
|
int i;
|
2016-05-07 02:25:16 +03:00
|
|
|
constrain_ongpu(l.outputs*l.batch, 5, l.delta_gpu, 1);
|
2015-05-11 23:46:49 +03:00
|
|
|
gradient_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
2016-01-28 23:30:38 +03:00
|
|
|
axpy_ongpu(l.outputs, 1, l.delta_gpu + i*l.outputs, 1, l.bias_updates_gpu, 1);
|
2014-10-17 02:17:23 +04:00
|
|
|
}
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
if(l.batch_normalize){
|
2016-05-07 02:25:16 +03:00
|
|
|
backward_batchnorm_layer_gpu(l, state);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
int m = l.outputs;
|
2015-05-11 23:46:49 +03:00
|
|
|
int k = l.batch;
|
2015-12-14 22:57:10 +03:00
|
|
|
int n = l.inputs;
|
|
|
|
float * a = l.delta_gpu;
|
|
|
|
float * b = state.input;
|
2015-05-11 23:46:49 +03:00
|
|
|
float * c = l.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
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
m = l.batch;
|
|
|
|
k = l.outputs;
|
|
|
|
n = l.inputs;
|
2014-10-17 02:17:23 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
a = l.delta_gpu;
|
|
|
|
b = l.weights_gpu;
|
2015-03-12 08:20:15 +03:00
|
|
|
c = state.delta;
|
2014-10-17 02:17:23 +04:00
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
if(c) gemm_ongpu(0,0,m,n,k,1,a,k,b,n,1,c,n);
|
2014-10-17 02:17:23 +04:00
|
|
|
}
|
2014-12-08 07:16:21 +03:00
|
|
|
#endif
|