cbasedann/neurons.c

70 lines
No EOL
1.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "randomness.h"
#include "neurons.h"
#include "activations.h"
Weight *init_weight_list(int n_weights)
{
Weight *first, *last;
int i;
for(i=1 ; i<=n_weights ; i++)
{
Weight *current = (Weight*)malloc(sizeof(Weight));
current->value = random_float(0.0 , 1.0);
current->next = NULL;
if(i==1)
{
first = current;
last = current;
}else
{
last->next = current;
last = current;
}
}
return first;
}
Neuron *init_neuron(int n_weights, char *activation_function)
{
Neuron *neuron = (Neuron*)malloc(sizeof(Neuron));
if(n_weights == 0)
{
neuron->weights = NULL;
}else
{
neuron->weights = init_weight_list(n_weights);
}
neuron->bias = random_float(0.0 , 1.0);
neuron->output = 0.0;
if(strcmp(activation_function, "nothing") == 0)
{
//to be completed later with tanh, relu, etc : for now only sigmoid is supported and will be the default function
}
else
{
neuron->activation = sigmoid;
neuron->activation_derivative = sigmoid_derivative;
}
neuron->delta_error = 0.0;
neuron->same_layer_next_neuron = NULL;
return neuron;
}
void destroy_neuron(Neuron *neuron)
{
Weight *temp = NULL;
while(neuron->weights != NULL)
{
temp = neuron->weights;
neuron->weights = neuron->weights->next;
free(temp);
}
if(neuron != NULL)
{
free(neuron);
}
}