74 lines
No EOL
2.1 KiB
C
74 lines
No EOL
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "randomness.h"
|
|
#include "neurons.h"
|
|
#include "network.h"
|
|
|
|
Neuron *generate_layer(int n_neurons, int n_neurons_prev_layer, char *activation_function)
|
|
{
|
|
Neuron *first=NULL, *last=NULL;
|
|
int i;
|
|
for(i=1 ; i<=n_neurons ; i++)
|
|
{
|
|
Neuron *n = init_neuron(n_neurons_prev_layer, activation_function);
|
|
if(i==1)
|
|
{
|
|
first = n;
|
|
last = n;
|
|
}else
|
|
{
|
|
last->same_layer_next_neuron = n;
|
|
last = n;
|
|
}
|
|
}
|
|
|
|
return first;
|
|
}
|
|
|
|
Network *init_network(int n_neurons_per_layer[], int n_layers, char *activation_function_per_layer[])
|
|
{
|
|
/* initialize the network based on array n_neurons_per_layer :
|
|
- n_layers is simply the size of array n_neurons_per_layer
|
|
- each ith number in array n_neurons_per_layer is the number of neurons in ith layer
|
|
- array activation_function_per_layer must be of same size as n_neurons_per_layer
|
|
*/
|
|
Network *network = (Network*)malloc(sizeof(Network));
|
|
network->n_layers = n_layers;
|
|
network->neurons_per_layer = (int*)malloc(n_layers * sizeof(int));
|
|
network->layers_first_neurons = (Neuron**)malloc(n_layers * sizeof(Neuron*));
|
|
int i;
|
|
for(i=0 ; i<n_layers ; i++)
|
|
{
|
|
network->neurons_per_layer[i] = n_neurons_per_layer[i];
|
|
if(i==0)
|
|
{
|
|
network->layers_first_neurons[i] = generate_layer(n_neurons_per_layer[i], 0, activation_function_per_layer[i]);
|
|
}else
|
|
{
|
|
network->layers_first_neurons[i] = generate_layer(n_neurons_per_layer[i], n_neurons_per_layer[i-1], activation_function_per_layer[i]);
|
|
}
|
|
}
|
|
|
|
return network;
|
|
}
|
|
|
|
void print_network(Network *network)
|
|
{
|
|
|
|
}
|
|
|
|
void destroy_network(Network *network)
|
|
{
|
|
int i;
|
|
Neuron *temp;
|
|
for(i=0 ; i<network->n_layers ; i++)
|
|
{
|
|
while(network->layers_first_neurons[i] != NULL)
|
|
{
|
|
temp = network->layers_first_neurons[i];
|
|
network->layers_first_neurons[i] = temp->same_layer_next_neuron;
|
|
destroy_neuron(temp);
|
|
}
|
|
}
|
|
} |