123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #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(const Network *network)
- {
- int i, n_params=0;
- Neuron *temp;
- printf("\n#>>==========================================<<#\n");
- printf("# NEURAL NETWORK #\n");
- printf("#>>==========================================<<#\n");
- printf(">> Number of layers : %d\n", network->n_layers);
- printf("------------------------------------------------\n");
- for(i=0 ; i<network->n_layers ; i++)
- {
- if(i==0)
- {
- printf(">> Input layer\n");
- printf("size : %d\n", network->neurons_per_layer[i]);
- printf("neurons' outputs : ");
- temp = network->layers_first_neurons[i];
- while(temp != NULL)
- {
- printf("%f ", temp->output);
- temp = temp->same_layer_next_neuron;
- }
- printf("\n");
- }else if(i==network->n_layers-1)
- {
- printf(">> Output layer\n");
- printf("size : %d\n", network->neurons_per_layer[i]);
- printf("neurons' outputs : ");
- temp = network->layers_first_neurons[i];
- while(temp != NULL)
- {
- printf("%f ", temp->output);
- temp = temp->same_layer_next_neuron;
- }
- printf("\n");
- }else
- {
- printf(">> Hidden layer %d\n", i);
- printf("size : %d\n", network->neurons_per_layer[i]);
- }
- printf("------------------------------------------------\n");
- }
- printf("Number of parameters : ");
- for(i=1 ; i<network->n_layers ; i++)
- {
- n_params += network->neurons_per_layer[i] * (network->neurons_per_layer[i-1] + 1);
- }
- printf("%d\n", n_params);
- printf("#>>==========================================<<#\n\n");
- }
-
- 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] = network->layers_first_neurons[i]->same_layer_next_neuron;
- destroy_neuron(temp);
- }
- }
- free(network);
- }
|