2013-11-04 23:11:01 +04:00
|
|
|
#include "connected_layer.h"
|
|
|
|
|
2013-11-06 22:37:37 +04:00
|
|
|
#include <math.h>
|
2013-11-04 23:11:01 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-06 22:37:37 +04:00
|
|
|
connected_layer make_connected_layer(int inputs, int outputs, ACTIVATOR_TYPE activator)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
connected_layer layer;
|
|
|
|
layer.inputs = inputs;
|
|
|
|
layer.outputs = outputs;
|
|
|
|
|
|
|
|
layer.output = calloc(outputs, sizeof(double*));
|
|
|
|
|
|
|
|
layer.weight_updates = calloc(inputs*outputs, sizeof(double));
|
|
|
|
layer.weights = calloc(inputs*outputs, sizeof(double));
|
|
|
|
for(i = 0; i < inputs*outputs; ++i)
|
|
|
|
layer.weights[i] = .5 - (double)rand()/RAND_MAX;
|
|
|
|
|
|
|
|
layer.bias_updates = calloc(outputs, sizeof(double));
|
|
|
|
layer.biases = calloc(outputs, sizeof(double));
|
|
|
|
for(i = 0; i < outputs; ++i)
|
|
|
|
layer.biases[i] = (double)rand()/RAND_MAX;
|
|
|
|
|
2013-11-06 22:37:37 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_connected_layer(double *input, connected_layer layer)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
for(i = 0; i < layer.outputs; ++i){
|
|
|
|
layer.output[i] = layer.biases[i];
|
|
|
|
for(j = 0; j < layer.inputs; ++j){
|
2013-11-06 22:37:37 +04:00
|
|
|
layer.output[i] += input[j]*layer.weights[i*layer.inputs + j];
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
2013-11-06 22:37:37 +04:00
|
|
|
layer.output[i] = layer.activation(layer.output[i]);
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:37:37 +04:00
|
|
|
void learn_connected_layer(double *input, connected_layer layer)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2013-11-06 22:37:37 +04:00
|
|
|
calculate_update_connected_layer(input, layer);
|
|
|
|
backpropagate_connected_layer(input, layer);
|
|
|
|
}
|
2013-11-04 23:11:01 +04:00
|
|
|
|
2013-11-06 22:37:37 +04:00
|
|
|
void update_connected_layer(connected_layer layer, double step)
|
|
|
|
{
|
|
|
|
int i,j;
|
2013-11-04 23:11:01 +04:00
|
|
|
for(i = 0; i < layer.outputs; ++i){
|
2013-11-06 22:37:37 +04:00
|
|
|
layer.biases[i] += step*layer.bias_updates[i];
|
2013-11-04 23:11:01 +04:00
|
|
|
for(j = 0; j < layer.inputs; ++j){
|
2013-11-06 22:37:37 +04:00
|
|
|
int index = i*layer.inputs+j;
|
|
|
|
layer.weights[index] += step*layer.weight_updates[index];
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 22:37:37 +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-06 22:37:37 +04:00
|
|
|
void calculate_update_connected_layer(double *input, connected_layer layer)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
for(i = 0; i < layer.outputs; ++i){
|
|
|
|
layer.bias_updates[i] += layer.output[i];
|
|
|
|
for(j = 0; j < layer.inputs; ++j){
|
2013-11-06 22:37:37 +04:00
|
|
|
layer.weight_updates[i*layer.inputs + j] += layer.output[i]*input[j];
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:37:37 +04:00
|
|
|
void backpropagate_connected_layer(double *input, connected_layer layer)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2013-11-06 22:37:37 +04:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for(j = 0; j < layer.inputs; ++j){
|
|
|
|
double grad = layer.gradient(input[j]);
|
|
|
|
input[j] = 0;
|
|
|
|
for(i = 0; i < layer.outputs; ++i){
|
|
|
|
input[j] += layer.output[i]*layer.weights[i*layer.inputs + j];
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
2013-11-06 22:37:37 +04:00
|
|
|
input[j] *= grad;
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|