darknet/src/connected_layer.c

98 lines
3.2 KiB
C
Raw Normal View History

2013-11-04 23:11:01 +04:00
#include "connected_layer.h"
#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>
connected_layer *make_connected_layer(int inputs, int outputs, ACTIVATION activator)
2013-11-04 23:11:01 +04:00
{
2013-11-13 22:50:38 +04:00
printf("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-11-04 23:11:01 +04:00
for(i = 0; i < inputs*outputs; ++i)
2013-11-13 22:50:38 +04:00
layer->weights[i] = .01*(.5 - (double)rand()/RAND_MAX);
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-11-13 22:50:38 +04:00
layer->biases[i] = 1;
2013-11-04 23:11:01 +04:00
if(activator == SIGMOID){
layer->activation = sigmoid_activation;
layer->gradient = sigmoid_gradient;
}else if(activator == RELU){
layer->activation = relu_activation;
layer->gradient = relu_gradient;
}else if(activator == IDENTITY){
layer->activation = identity_activation;
layer->gradient = identity_gradient;
}
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
}
layer.output[i] = layer.activation(layer.output[i]);
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-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-11-13 22:50:38 +04:00
layer.bias_momentum[i] = step*(layer.bias_updates[i] - decay*layer.biases[i]) + momentum*layer.bias_momentum[i];
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){
double grad = layer.gradient(input[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
}
2013-11-13 22:50:38 +04:00
delta[j] *= grad;
2013-11-04 23:11:01 +04:00
}
}