darknet/src/connected_layer.c

90 lines
3.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"
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>
2013-12-03 04:41:40 +04:00
connected_layer *make_connected_layer(int inputs, int outputs, ACTIVATION activation)
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));
layer->inputs = inputs;
layer->outputs = outputs;
2013-11-04 23:11:01 +04:00
layer->output = calloc(outputs, sizeof(double*));
2013-11-13 22:50:38 +04:00
layer->delta = calloc(outputs, sizeof(double*));
2013-11-04 23:11:01 +04:00
layer->weight_updates = calloc(inputs*outputs, sizeof(double));
2013-11-13 22:50:38 +04:00
layer->weight_momentum = calloc(inputs*outputs, sizeof(double));
layer->weights = calloc(inputs*outputs, sizeof(double));
2013-12-03 04:41:40 +04:00
double scale = 2./inputs;
2013-11-04 23:11:01 +04:00
for(i = 0; i < inputs*outputs; ++i)
2013-12-03 04:41:40 +04:00
layer->weights[i] = rand_normal()*scale;
2013-11-04 23:11:01 +04:00
layer->bias_updates = calloc(outputs, sizeof(double));
2013-11-13 22:50:38 +04:00
layer->bias_momentum = calloc(outputs, sizeof(double));
layer->biases = calloc(outputs, sizeof(double));
2013-11-04 23:11:01 +04:00
for(i = 0; i < outputs; ++i)
2013-12-03 04:41:40 +04:00
//layer->biases[i] = rand_normal()*scale + scale;
2013-12-06 01:17:16 +04:00
layer->biases[i] = 0;
2013-11-04 23:11:01 +04:00
2013-12-03 04:41:40 +04:00
layer->activation = activation;
2013-11-04 23:11:01 +04:00
return layer;
}
2013-11-13 22:50:38 +04:00
void forward_connected_layer(connected_layer layer, double *input)
2013-11-04 23:11:01 +04:00
{
int i, j;
for(i = 0; i < layer.outputs; ++i){
layer.output[i] = layer.biases[i];
for(j = 0; j < layer.inputs; ++j){
layer.output[i] += input[j]*layer.weights[i*layer.inputs + j];
2013-11-04 23:11:01 +04:00
}
2013-12-03 04:41:40 +04:00
layer.output[i] = activate(layer.output[i], layer.activation);
2013-11-04 23:11:01 +04:00
}
}
2013-11-13 22:50:38 +04:00
void learn_connected_layer(connected_layer layer, double *input)
2013-11-04 23:11:01 +04:00
{
2013-11-13 22:50:38 +04:00
int i, j;
2013-11-04 23:11:01 +04:00
for(i = 0; i < layer.outputs; ++i){
2013-12-03 04:41:40 +04:00
layer.delta[i] *= gradient(layer.output[i], layer.activation);
2013-11-13 22:50:38 +04:00
layer.bias_updates[i] += layer.delta[i];
2013-11-04 23:11:01 +04:00
for(j = 0; j < layer.inputs; ++j){
2013-11-13 22:50:38 +04:00
layer.weight_updates[i*layer.inputs + j] += layer.delta[i]*input[j];
2013-11-04 23:11:01 +04:00
}
}
}
2013-11-13 22:50:38 +04:00
void update_connected_layer(connected_layer layer, double step, double momentum, double decay)
2013-11-04 23:11:01 +04:00
{
2013-11-13 22:50:38 +04:00
int i,j;
2013-11-04 23:11:01 +04:00
for(i = 0; i < layer.outputs; ++i){
2013-12-03 04:41:40 +04:00
layer.bias_momentum[i] = step*(layer.bias_updates[i]) + momentum*layer.bias_momentum[i];
2013-11-13 22:50:38 +04:00
layer.biases[i] += layer.bias_momentum[i];
2013-11-04 23:11:01 +04:00
for(j = 0; j < layer.inputs; ++j){
2013-11-13 22:50:38 +04:00
int index = i*layer.inputs+j;
layer.weight_momentum[index] = step*(layer.weight_updates[index] - decay*layer.weights[index]) + momentum*layer.weight_momentum[index];
layer.weights[index] += layer.weight_momentum[index];
2013-11-04 23:11:01 +04:00
}
}
2013-11-13 22:50:38 +04:00
memset(layer.bias_updates, 0, layer.outputs*sizeof(double));
memset(layer.weight_updates, 0, layer.outputs*layer.inputs*sizeof(double));
2013-11-04 23:11:01 +04:00
}
2013-11-13 22:50:38 +04:00
void backward_connected_layer(connected_layer layer, double *input, double *delta)
2013-11-04 23:11:01 +04:00
{
int i, j;
for(j = 0; j < layer.inputs; ++j){
2013-11-13 22:50:38 +04:00
delta[j] = 0;
for(i = 0; i < layer.outputs; ++i){
2013-11-13 22:50:38 +04:00
delta[j] += layer.delta[i]*layer.weights[i*layer.inputs + j];
2013-11-04 23:11:01 +04:00
}
}
}